Merge pull request #2599 from tractorcow/pulls/3.1-uploadfield-formchanged

BUG / API - Change tracking for UploadField
This commit is contained in:
Ingo Schommer 2013-10-31 15:28:34 -07:00
commit c547e426dd
2 changed files with 27 additions and 10 deletions

View File

@ -55,6 +55,9 @@
// optional metadata plugin support // optional metadata plugin support
if ($.meta) options = $.extend({}, options, this.data()); if ($.meta) options = $.extend({}, options, this.data());
// Flag indicating this form was dirtied by an external component
var dirty = false;
var onchange = function(e) { var onchange = function(e) {
var $field = $(e.target); var $field = $(e.target);
var origVal = $field.data('changetracker.origVal'), newVal; var origVal = $field.data('changetracker.origVal'), newVal;
@ -76,8 +79,8 @@
if($field.is(':radio')) { if($field.is(':radio')) {
self.find(':radio[name=' + $field.attr('name') + ']').removeClass(options.changedCssClass); self.find(':radio[name=' + $field.attr('name') + ']').removeClass(options.changedCssClass);
} }
// Only unset form state if no other fields are changed as well // Only unset form state if no other fields are changed as well and the form isn't explicitly dirty
if(!self.getFields().filter('.' + options.changedCssClass).length) { if(!dirty && !self.getFields().filter('.' + options.changedCssClass).length) {
self.removeClass(options.changedCssClass); self.removeClass(options.changedCssClass);
} }
} }
@ -95,6 +98,11 @@
} }
$(this).data('changetracker.origVal', origVal); $(this).data('changetracker.origVal', origVal);
}); });
self.bind('dirty.changetracker', function() {
dirty = true;
self.addClass(options.changedCssClass);
});
this.data('changetracker', true); this.data('changetracker', true);
}; };
@ -104,7 +112,8 @@
.unbind('.changetracker') .unbind('.changetracker')
.removeClass(options.changedCssClass) .removeClass(options.changedCssClass)
.removeData('changetracker.origVal'); .removeData('changetracker.origVal');
this.removeData('changetracker'); this.unbind('.changetracker')
.removeData('changetracker');
}; };
/** /**
@ -137,13 +146,13 @@
// Support invoking "public" methods as string arguments // Support invoking "public" methods as string arguments
if (typeof arguments[0] === 'string') { if (typeof arguments[0] === 'string') {
var property = arguments[1]; var property = arguments[1];
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
args.splice(0, 1); args.splice(0, 1);
return this[arguments[0]].apply(this, args); return this[arguments[0]].apply(this, args);
} else { } else {
return this.initialize(); return this.initialize();
} }
}; };
}(jQuery)); }(jQuery));

View File

@ -48,6 +48,11 @@
return result; return result;
}, },
_onDone: function (result, textStatus, jqXHR, options) {
// Mark form as dirty on completion of successful upload
this.element.closest('form').trigger('dirty');
$.blueimpUI.fileupload.prototype._onDone.call(this, result, textStatus, jqXHR, options);
},
_onSend: function (e, data) { _onSend: function (e, data) {
//check the array of existing files to see if we are trying to upload a file that already exists //check the array of existing files to see if we are trying to upload a file that already exists
var that = this; var that = this;
@ -98,6 +103,7 @@
this._adjustMaxNumberOfFiles(0); this._adjustMaxNumberOfFiles(0);
}, },
attach: function(data) { attach: function(data) {
this.element.closest('form').trigger('dirty');
// Handles attachment of already uploaded files, similar to add // Handles attachment of already uploaded files, similar to add
var self = this, var self = this,
@ -337,6 +343,7 @@
if(this.is('.ss-uploadfield-item-delete')) { if(this.is('.ss-uploadfield-item-delete')) {
if(confirm(ss.i18n._t('UploadField.ConfirmDelete'))) { if(confirm(ss.i18n._t('UploadField.ConfirmDelete'))) {
this.closest('form').trigger('dirty');
fileupload._trigger('destroy', e, { fileupload._trigger('destroy', e, {
context: item, context: item,
url: this.data('href'), url: this.data('href'),
@ -346,6 +353,7 @@
} }
} else { } else {
// Removed files will be applied to object on save // Removed files will be applied to object on save
this.closest('form').trigger('dirty');
fileupload._trigger('destroy', e, {context: item}); fileupload._trigger('destroy', e, {context: item});
} }