Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
add before option. Fix #113. Thanks to @BP323 for contribution!
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasCARPi committed Feb 6, 2018
1 parent 69d581e commit 031e5a8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
1 change: 1 addition & 0 deletions demos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ <h4 class='card-title'>Character counter</h4>
indicator : "<img src='img/indicator.gif' />",
type : "text",
label : "This is a label",
before : function(){console.log('Before function triggered')},
submit : 'OK',
tooltip : "Click to edit..."
});
Expand Down
85 changes: 46 additions & 39 deletions src/jquery.jeditable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,55 @@
* © 2017 Nicolas CARPi
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* http://www.appelsiini.net/projects/jeditable
* https://github.com/NicolasCARPi/jquery_jeditable
*
* Based on editable by Dylan Verheul <dylan_at_dyve.net>:
* http://www.dyve.net/jquery/?editable
* http://www.dyve.net/jquery/?editable
*
*/

/**
* ** means there is basic unit tests for this parameter.
*
* @name Jeditable
* @type jQuery
* @param String target (POST) URL or function to send edited content to **
* @param Hash options additional options
* @param String options[method] method to use to send edited content (POST or PUT) **
* @param Function options[callback] Function to run after submitting edited content **
* @param String options[name] POST parameter name of edited content
* @param String options[id] POST parameter name of edited div id
* @param Hash options[submitdata] Extra parameters to send when submitting edited content.
* @param String options[type] text, textarea or select (or any 3rd party input type) **
* @param Integer options[rows] number of rows if using textarea **
* @param Integer options[cols] number of columns if using textarea **
* @param Mixed options[height] 'auto', 'none' or height in pixels **
* @param Mixed options[width] 'auto', 'none' or width in pixels **
* @param String options[loadurl] URL to fetch input content before editing **
* @param String options[loadtype] Request type for load url. Should be GET or POST.
* @param String options[loadtext] Text to display while loading external content.
* @param Mixed options[loaddata] Extra parameters to pass when fetching content before editing.
* @param Mixed options[data] Or content given as paramameter. String or function.**
* @param String options[indicator] indicator html to show when saving
* @param String options[tooltip] optional tooltip text via title attribute **
* @param String options[event] jQuery event such as 'click' of 'dblclick' **
* @param String options[submit] submit button value, empty means no button **
* @param String options[cancel] cancel button value, empty means no button **
* @param String options[cssclass] CSS class to apply to input form. 'inherit' to copy from parent. **
* @param String options[style] Style to apply to input form 'inherit' to copy from parent. **
* @param String options[select] true or false, when true text is highlighted ??
* @param String options[placeholder] Placeholder text or html to insert when element is empty. **
* @param String options[onblur] 'cancel', 'submit', 'ignore' or function ??
* @param String options[size] the size of the text field
* @param String options[maxlength] the maximum number of character in the text field
* @param String options[label] optional label
*
* @param Function options[onsubmit] function(settings, original) { ... } called before submit
* @param Function options[onreset] function(settings, original) { ... } called before reset
* @param Function options[onerror] function(settings, original, xhr) { ... } called on error
* @param Hash options[ajaxoptions] jQuery Ajax options. See docs.jquery.com.
* @param String target (POST) URL or function to send edited content to
* @param Hash options additional options
* @param Hash options[ajaxoptions] jQuery Ajax options. See https://api.jquery.com/jQuery.ajax/
* @param String options[before] function to be executed before going into edit mode
* @param Function options[callback] function to run after submitting edited content
* @param String options[cancel] cancel button value, empty means no button
* @param Integer options[cols] number of columns if using textarea
* @param String options[cssclass] CSS class to apply to input form. 'inherit' to copy from parent
* @param Mixed options[data] content given as parameter. String or function
* @param String options[event] jQuery event such as 'click' of 'dblclick'
* @param Mixed options[height] 'auto', 'none' or height in pixels
* @param String options[id] POST parameter name of edited div id
* @param String options[indicator] indicator html to show when saving
* @param String options[label] optional label
* @param Mixed options[loaddata] Extra parameters to pass when fetching content before editing
* @param String options[loadtext] Text to display while loading external content
* @param String options[loadtype] Request type for load url (GET or POST)
* @param String options[loadurl] URL to fetch input content before editing
* @param String options[maxlength] the maximum number of character in the text field
* @param String options[method] method to use to send edited content (POST or PUT)
* @param String options[name] POST parameter name of edited content
* @param String options[onblur] 'cancel', 'submit', 'ignore' or function
* @param Function options[onerror] function(settings, original, xhr) { ... } called on error
* @param Function options[onreset] function(settings, original) { ... } called before reset
* @param Function options[onsubmit] function(settings, original) { ... } called before submit
* @param String options[placeholder] Placeholder text or html to insert when element is empty
* @param Integer options[rows] number of rows if using textarea
* @param String options[select] true or false, when true text is highlighted
* @param String options[size] the size of the text field
* @param String options[style] Style to apply to input form 'inherit' to copy from parent
* @param String options[submit] submit button value, empty means no button
* @param Hash options[submitdata] Extra parameters to send when submitting edited content
* @param String options[tooltip] optional tooltip text via title attribute
* @param String options[type] text, textarea or select (or any 3rd party input type)
* @param Mixed options[width] 'auto', 'none' or width in pixels
*/

(function($) {
Expand Down Expand Up @@ -90,6 +89,7 @@
var onsubmit = settings.onsubmit || function() { };
var onreset = settings.onreset || function() { };
var onerror = settings.onerror || reset;
var before = settings.before || false;

/* Show tooltip. */
if (settings.tooltip) {
Expand Down Expand Up @@ -138,6 +138,13 @@
return;
}

/* execute the before function if any was specified */
if (settings.before && jQuery.isFunction(settings.before)) {
settings.before();
} else if (settings.before && !jQuery.isFunction(settings.before)) {
throw "The 'before' option needs to be provided as a function!";
}

/* Prevent default action and bubbling. */
e.preventDefault();
e.stopPropagation();
Expand Down

0 comments on commit 031e5a8

Please sign in to comment.