diff --git a/jquery.jeditable.js b/jquery.jeditable.js index eb8a911..5923a41 100644 --- a/jquery.jeditable.js +++ b/jquery.jeditable.js @@ -163,6 +163,8 @@ settings.autoheight ? $(self).height() : settings.height; } } + + $(self).addClass('edit-edit'); /* Remove placeholder text, replace is here because of IE. */ if ($(this).html().toLowerCase().replace(/(;|"|\/)/g, '') == @@ -298,23 +300,24 @@ /* Do no submit. */ e.preventDefault(); + + var value = input.val(); /* Call before submit hook. */ /* If it returns false abort submitting. */ - if (false !== onsubmit.apply(form, [settings, self])) { + if (false !== onsubmit.apply(form, [settings, self, value])) { /* Custom inputs call before submit hook. */ /* If it returns false abort submitting. */ - if (false !== submit.apply(form, [settings, self])) { + if (false !== submit.apply(form, [settings, self, value])) { /* Check if given target is function */ if ($.isFunction(settings.target)) { - var str = settings.target.apply(self, [input.val(), settings]); - $(self).html(str); - self.editing = false; - callback.apply(self, [self.innerHTML, settings]); - /* TODO: this is not dry */ - if (!$.trim($(self).html())) { - $(self).html(settings.placeholder); + var str = settings.target.apply(self, [input.val(), settings, value]); + if (str !== false) { + if ($.type(str) !== 'string') { + str = input.val(); + } + self.finish(str, true); } } else { /* Add edited content and id of edited element to POST. */ @@ -344,12 +347,9 @@ url : settings.target, success : function(result, status) { if (ajaxoptions.dataType == 'html') { - $(self).html(result); - } - self.editing = false; - callback.apply(self, [result, settings]); - if (!$.trim($(self).html())) { - $(self).html(settings.placeholder); + $(self).html(result, true); + } else { + self.finish(null, true) } }, error : function(xhr, status, error) { @@ -387,9 +387,28 @@ if (settings.tooltip) { $(self).attr('title', settings.tooltip); } + $(self).removeClass('edit-edit'); } } - }; + }; + + this.finish = function(str, fireCallback) { + if (typeof str !== "undefiend" && str !== null) { + $(self).html(str); + } + self.editing = false; + + if (fireCallback === true) { + callback.apply(self, [self.innerHTML, settings]); + } + + /* TODO: this is not dry */ + if (!$.trim($(self).html())) { + $(self).html(settings.placeholder); + } + + $(self).removeClass('edit-edit'); + }; }); };