From e05b4f7e0de8cc28e5b142ec8763844f2cbad6ed Mon Sep 17 00:00:00 2001 From: Patryk Majewski Date: Thu, 19 Jul 2012 15:02:01 +0200 Subject: [PATCH 1/2] Add edit-edit class when editing for backwards compatibility --- jquery.jeditable.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/jquery.jeditable.js b/jquery.jeditable.js index eb8a911..05d54bd 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, '') == @@ -315,6 +317,7 @@ /* TODO: this is not dry */ if (!$.trim($(self).html())) { $(self).html(settings.placeholder); + $(self).removeClass('edit-edit'); } } else { /* Add edited content and id of edited element to POST. */ @@ -351,6 +354,7 @@ if (!$.trim($(self).html())) { $(self).html(settings.placeholder); } + $(self).removeClass('edit-edit'); }, error : function(xhr, status, error) { onerror.apply(form, [settings, self, xhr]); @@ -387,6 +391,7 @@ if (settings.tooltip) { $(self).attr('title', settings.tooltip); } + $(self).removeClass('edit-edit'); } } }; From e5e2beb05ceb1671d9c67e5aaf977a57543e47fc Mon Sep 17 00:00:00 2001 From: Patryk Majewski Date: Thu, 19 Jul 2012 15:09:05 +0200 Subject: [PATCH 2/2] Add current input value to on submit and submit callbacks, allow aborting from target function, add finish api --- jquery.jeditable.js | 50 +++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/jquery.jeditable.js b/jquery.jeditable.js index 05d54bd..5923a41 100644 --- a/jquery.jeditable.js +++ b/jquery.jeditable.js @@ -300,24 +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); - $(self).removeClass('edit-edit'); + 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. */ @@ -347,14 +347,10 @@ 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) } - $(self).removeClass('edit-edit'); }, error : function(xhr, status, error) { onerror.apply(form, [settings, self, xhr]); @@ -394,7 +390,25 @@ $(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'); + }; }); };