Skip to content
This repository
Browse code

- added onRetrieve user function

  • Loading branch information...
commit c066000fd4895751bec5d25eb5847378f06327a2 1 parent 14d5b28
Guillaume Potier authored
5 CHANGELOG.md
Source Rendered
... ... @@ -1,5 +1,10 @@
1 1 #Changelog
2 2
  3 +* **1.0.3** :
  4 +
  5 + - added onRetrieve function in config. Called each time Garlic retrieves a field
  6 + stored value. Overridable by configuration
  7 +
3 8 * **1.0.2** :
4 9
5 10 - fixed bug that did not allowed user to clear a field persisted by Garlic
3  README.md
Source Rendered
@@ -10,7 +10,7 @@ http://garlicjs.org/
10 10
11 11 #Version
12 12
13   -1.0.2
  13 +1.0.3
14 14
15 15 See CHANGELOG for more info.
16 16
@@ -40,6 +40,7 @@ They'll be created and dumped in the dist/ directory
40 40 * @johnrees
41 41 * @Marfa
42 42 * @leondewey
  43 +* @willdurand
43 44
44 45 #Contribute!
45 46
6 dist/garlic-standalone.min.js
@@ -36,11 +36,11 @@ g){return void 0===g?c.isPlainObject(f)?this.each(function(g,h){c.each(f,functio
36 36 !function(c){var j=function(){this.defined="undefined"!==typeof localStorage};j.prototype={constructor:j,get:function(c,g){return localStorage.getItem(c)?localStorage.getItem(c):"undefined"!==typeof g?g:null},has:function(c){return localStorage.getItem(c)?!0:!1},set:function(c,g,h){"string"===typeof g&&(""===g?this.destroy(c):localStorage.setItem(c,g));return"function"===typeof h?h():!0},destroy:function(c,g){localStorage.removeItem(c);return"function"===typeof g?g():!0},clean:function(c){for(var g=
37 37 localStorage.length-1;0<=g;g--)"undefined"===typeof Array.indexOf&&-1!==localStorage.key(g).indexOf("garlic:")&&localStorage.removeItem(localStorage.key(g));return"function"===typeof c?c():!0},clear:function(c){localStorage.clear();return"function"===typeof c?c():!0}};var u=function(c,g,h){this.init("garlic",c,g,h)};u.prototype={constructor:u,init:function(f,g,h,l){this.type=f;this.$element=c(g);this.options=this.getOptions(l);this.storage=h;this.path=this.getPath();this.parentForm=this.$element.closest("form");
38 38 this.retrieve();this.$element.on(this.options.events.join("."+this.type+" "),!1,c.proxy(this.persist,this));if(this.options.destroy)this.$element.closest("form").on("submit reset",!1,c.proxy(this.destroy,this));this.$element.addClass("garlic-auto-save")},getOptions:function(f){return f=c.extend({},c.fn[this.type].defaults,f,this.$element.data())},persist:function(){if(this.$element.is("input[type=checkbox]"))return this.storage.set(this.path,this.$element.attr("checked")?"checked":"unchecked");this.storage.set(this.path,
39   -this.$element.val())},retrieve:function(){if(this.storage.has(this.path)){var c=this.storage.get(this.path);if(this.options.conflictManager.enabled&&this.detectConflict())return this.conflictManager();if(this.$element.is("input[type=radio], input[type=checkbox]")){if("checked"===c||this.$element.val()===c)return this.$element.attr("checked",!0);"unchecked"===c&&this.$element.attr("checked",!1)}else this.$element.val(c),this.options.retrieveTrigger(this.$element,c)}},detectConflict:function(){var f=
40   -this;if(this.$element.is("input[type=checkbox], input[type=radio]"))return!1;if(this.$element.val()&&this.storage.get(this.path)!==this.$element.val()){if(this.$element.is("select")){var g=!1;this.$element.find("option").each(function(){0!==c(this).index()&&(c(this).attr("selected")&&c(this).val()!==f.storage.get(this.path))&&(g=!0)});return g}return!0}return!1},conflictManager:function(){if("function"===typeof this.options.conflictManager.onConflictDetected&&!this.options.conflictManager.onConflictDetected(this.$element,
  39 +this.$element.val())},retrieve:function(){if(this.storage.has(this.path)){var c=this.storage.get(this.path);if(this.options.conflictManager.enabled&&this.detectConflict())return this.conflictManager();if(this.$element.is("input[type=radio], input[type=checkbox]")){if("checked"===c||this.$element.val()===c)return this.$element.attr("checked",!0);"unchecked"===c&&this.$element.attr("checked",!1)}else this.$element.val(c),this.options.onRetrieve(this.$element,c)}},detectConflict:function(){var f=this;
  40 +if(this.$element.is("input[type=checkbox], input[type=radio]"))return!1;if(this.$element.val()&&this.storage.get(this.path)!==this.$element.val()){if(this.$element.is("select")){var g=!1;this.$element.find("option").each(function(){0!==c(this).index()&&(c(this).attr("selected")&&c(this).val()!==f.storage.get(this.path))&&(g=!0)});return g}return!0}return!1},conflictManager:function(){if("function"===typeof this.options.conflictManager.onConflictDetected&&!this.options.conflictManager.onConflictDetected(this.$element,
41 41 this.storage.get(this.path)))return!1;this.options.conflictManager.garlicPriority?(this.$element.data("swap-data",this.$element.val()),this.$element.data("swap-state","garlic"),this.$element.val(this.storage.get(this.path))):(this.$element.data("swap-data",this.storage.get(this.path)),this.$element.data("swap-state","default"));this.swapHandler();this.$element.addClass("garlic-conflict-detected");this.$element.closest("input[type=submit]").attr("disabled",!0)},swapHandler:function(){var f=c(this.options.conflictManager.template);
42 42 this.$element.after(f.text(this.options.conflictManager.message));f.on("click",!1,c.proxy(this.swap,this))},swap:function(){var f=this.$element.data("swap-data");this.$element.data("swap-state","garlic"===this.$element.data("swap-state")?"default":"garlic");this.$element.data("swap-data",this.$element.val());c(this.$element).val(f)},destroy:function(){this.storage.destroy(this.path)},remove:function(){this.remove();this.$element.is("input[type=radio], input[type=checkbox]")?c(this.$element).attr("checked",
43 43 !1):this.$element.val("")},getPath:function(){if(1!=this.$element.length)return!1;for(var f="",g=this.$element.is("input[type=checkbox]"),h=this.$element;h.length;){var l=h[0],i=l.nodeName;if(!i)break;var i=i.toLowerCase(),h=h.parent(),j=h.children(i);if(c(l).is("form, input, select, textarea")||g)if(i+=c(l).attr("name")?"."+c(l).attr("name"):"",1<j.length&&!c(l).is("input[type=radio]")&&(i+=":eq("+j.index(l)+")"),f=i+(f?">"+f:""),"form"==l.nodeName.toLowerCase())break}return"garlic:"+document.domain+
44 44 (this.options.domain?"*":window.location.pathname)+">"+f},getStorage:function(){return this.storage}};c.fn.garlic=function(f,g){function h(g){var h=c(g),j=h.data("garlic"),m=c.extend({},l,h.data());if("undefined"===typeof m.storage||m.storage)if(j||h.data("garlic",j=new u(g,i,m)),"string"===typeof f&&"function"===typeof j[f])return j[f]()}var l=c.extend(!0,{},c.fn.garlic.defaults,f,this.data()),i=new j,m=!1;if(!i.defined)return!1;this.each(function(){c(this).is("form")?c(this).find(l.inputs).each(function(){m=
45   -h(c(this))}):c(this).is(l.inputs)&&(m=h(c(this)))});return"function"===typeof g?g():m};c.fn.garlic.Constructor=u;c.fn.garlic.defaults={destroy:!0,inputs:"input, textarea, select",events:"DOMAttrModified textInput input change keypress paste focus".split(" "),domain:!1,conflictManager:{enabled:!0,garlicPriority:!0,template:'<span class="garlic-swap"></span>',message:"This is your saved data. Click here to see default one",onConflictDetected:function(){return!0}},retrieveTrigger:function(){}};c(window).on("load",
  45 +h(c(this))}):c(this).is(l.inputs)&&(m=h(c(this)))});return"function"===typeof g?g():m};c.fn.garlic.Constructor=u;c.fn.garlic.defaults={destroy:!0,inputs:"input, textarea, select",events:"DOMAttrModified textInput input change keypress paste focus".split(" "),domain:!1,conflictManager:{enabled:!0,garlicPriority:!0,template:'<span class="garlic-swap"></span>',message:"This is your saved data. Click here to see default one",onConflictDetected:function(){return!0}},onRetrieve:function(){}};c(window).on("load",
46 46 function(){c('[data-persist="garlic"]').each(function(){c(this).garlic()})})}(window.jQuery||window.Zepto);
6 dist/garlic.min.js
@@ -2,11 +2,11 @@
2 2 !function(a){var h=function(){this.defined="undefined"!==typeof localStorage};h.prototype={constructor:h,get:function(b,a){return localStorage.getItem(b)?localStorage.getItem(b):"undefined"!==typeof a?a:null},has:function(b){return localStorage.getItem(b)?!0:!1},set:function(b,a,c){"string"===typeof a&&(""===a?this.destroy(b):localStorage.setItem(b,a));return"function"===typeof c?c():!0},destroy:function(b,a){localStorage.removeItem(b);return"function"===typeof a?a():!0},clean:function(b){for(var a=
3 3 localStorage.length-1;0<=a;a--)"undefined"===typeof Array.indexOf&&-1!==localStorage.key(a).indexOf("garlic:")&&localStorage.removeItem(localStorage.key(a));return"function"===typeof b?b():!0},clear:function(a){localStorage.clear();return"function"===typeof a?a():!0}};var i=function(a,d,c){this.init("garlic",a,d,c)};i.prototype={constructor:i,init:function(b,d,c,e){this.type=b;this.$element=a(d);this.options=this.getOptions(e);this.storage=c;this.path=this.getPath();this.parentForm=this.$element.closest("form");
4 4 this.retrieve();this.$element.on(this.options.events.join("."+this.type+" "),!1,a.proxy(this.persist,this));if(this.options.destroy)this.$element.closest("form").on("submit reset",!1,a.proxy(this.destroy,this));this.$element.addClass("garlic-auto-save")},getOptions:function(b){return b=a.extend({},a.fn[this.type].defaults,b,this.$element.data())},persist:function(){if(this.$element.is("input[type=checkbox]"))return this.storage.set(this.path,this.$element.attr("checked")?"checked":"unchecked");this.storage.set(this.path,
5   -this.$element.val())},retrieve:function(){if(this.storage.has(this.path)){var a=this.storage.get(this.path);if(this.options.conflictManager.enabled&&this.detectConflict())return this.conflictManager();if(this.$element.is("input[type=radio], input[type=checkbox]")){if("checked"===a||this.$element.val()===a)return this.$element.attr("checked",!0);"unchecked"===a&&this.$element.attr("checked",!1)}else this.$element.val(a),this.options.retrieveTrigger(this.$element,a)}},detectConflict:function(){var b=
6   -this;if(this.$element.is("input[type=checkbox], input[type=radio]"))return!1;if(this.$element.val()&&this.storage.get(this.path)!==this.$element.val()){if(this.$element.is("select")){var d=!1;this.$element.find("option").each(function(){0!==a(this).index()&&(a(this).attr("selected")&&a(this).val()!==b.storage.get(this.path))&&(d=!0)});return d}return!0}return!1},conflictManager:function(){if("function"===typeof this.options.conflictManager.onConflictDetected&&!this.options.conflictManager.onConflictDetected(this.$element,
  5 +this.$element.val())},retrieve:function(){if(this.storage.has(this.path)){var a=this.storage.get(this.path);if(this.options.conflictManager.enabled&&this.detectConflict())return this.conflictManager();if(this.$element.is("input[type=radio], input[type=checkbox]")){if("checked"===a||this.$element.val()===a)return this.$element.attr("checked",!0);"unchecked"===a&&this.$element.attr("checked",!1)}else this.$element.val(a),this.options.onRetrieve(this.$element,a)}},detectConflict:function(){var b=this;
  6 +if(this.$element.is("input[type=checkbox], input[type=radio]"))return!1;if(this.$element.val()&&this.storage.get(this.path)!==this.$element.val()){if(this.$element.is("select")){var d=!1;this.$element.find("option").each(function(){0!==a(this).index()&&(a(this).attr("selected")&&a(this).val()!==b.storage.get(this.path))&&(d=!0)});return d}return!0}return!1},conflictManager:function(){if("function"===typeof this.options.conflictManager.onConflictDetected&&!this.options.conflictManager.onConflictDetected(this.$element,
7 7 this.storage.get(this.path)))return!1;this.options.conflictManager.garlicPriority?(this.$element.data("swap-data",this.$element.val()),this.$element.data("swap-state","garlic"),this.$element.val(this.storage.get(this.path))):(this.$element.data("swap-data",this.storage.get(this.path)),this.$element.data("swap-state","default"));this.swapHandler();this.$element.addClass("garlic-conflict-detected");this.$element.closest("input[type=submit]").attr("disabled",!0)},swapHandler:function(){var b=a(this.options.conflictManager.template);
8 8 this.$element.after(b.text(this.options.conflictManager.message));b.on("click",!1,a.proxy(this.swap,this))},swap:function(){var b=this.$element.data("swap-data");this.$element.data("swap-state","garlic"===this.$element.data("swap-state")?"default":"garlic");this.$element.data("swap-data",this.$element.val());a(this.$element).val(b)},destroy:function(){this.storage.destroy(this.path)},remove:function(){this.remove();this.$element.is("input[type=radio], input[type=checkbox]")?a(this.$element).attr("checked",
9 9 !1):this.$element.val("")},getPath:function(){if(1!=this.$element.length)return!1;for(var b="",d=this.$element.is("input[type=checkbox]"),c=this.$element;c.length;){var e=c[0],f=e.nodeName;if(!f)break;var f=f.toLowerCase(),c=c.parent(),g=c.children(f);if(a(e).is("form, input, select, textarea")||d)if(f+=a(e).attr("name")?"."+a(e).attr("name"):"",1<g.length&&!a(e).is("input[type=radio]")&&(f+=":eq("+g.index(e)+")"),b=f+(b?">"+b:""),"form"==e.nodeName.toLowerCase())break}return"garlic:"+document.domain+
10 10 (this.options.domain?"*":window.location.pathname)+">"+b},getStorage:function(){return this.storage}};a.fn.garlic=function(b,d){function c(c){var d=a(c),g=d.data("garlic"),h=a.extend({},e,d.data());if("undefined"===typeof h.storage||h.storage)if(g||d.data("garlic",g=new i(c,f,h)),"string"===typeof b&&"function"===typeof g[b])return g[b]()}var e=a.extend(!0,{},a.fn.garlic.defaults,b,this.data()),f=new h,g=!1;if(!f.defined)return!1;this.each(function(){a(this).is("form")?a(this).find(e.inputs).each(function(){g=
11   -c(a(this))}):a(this).is(e.inputs)&&(g=c(a(this)))});return"function"===typeof d?d():g};a.fn.garlic.Constructor=i;a.fn.garlic.defaults={destroy:!0,inputs:"input, textarea, select",events:"DOMAttrModified textInput input change keypress paste focus".split(" "),domain:!1,conflictManager:{enabled:!0,garlicPriority:!0,template:'<span class="garlic-swap"></span>',message:"This is your saved data. Click here to see default one",onConflictDetected:function(){return!0}},retrieveTrigger:function(){}};a(window).on("load",
  11 +c(a(this))}):a(this).is(e.inputs)&&(g=c(a(this)))});return"function"===typeof d?d():g};a.fn.garlic.Constructor=i;a.fn.garlic.defaults={destroy:!0,inputs:"input, textarea, select",events:"DOMAttrModified textInput input change keypress paste focus".split(" "),domain:!1,conflictManager:{enabled:!0,garlicPriority:!0,template:'<span class="garlic-swap"></span>',message:"This is your saved data. Click here to see default one",onConflictDetected:function(){return!0}},onRetrieve:function(){}};a(window).on("load",
12 12 function(){a('[data-persist="garlic"]').each(function(){a(this).garlic()})})}(window.jQuery||window.Zepto);
4 garlic.js
@@ -141,7 +141,7 @@
141 141 this.$element.val( storedValue );
142 142
143 143 // trigger custom user function when data is retrieved
144   - this.options.retrieveTrigger( this.$element, storedValue );
  144 + this.options.onRetrieve( this.$element, storedValue );
145 145
146 146 return;
147 147 }
@@ -371,7 +371,7 @@
371 371 , message: 'This is your saved data. Click here to see default one' // Default message for swapping data / state
372 372 , onConflictDetected: function ( item, storedVal ) { return true; } // This function will be triggered if a conflict is detected on an item. Return true if you want Garlic behavior, return false if you want to override it
373 373 }
374   - , retrieveTrigger: function ( item, storedVal ) {} // This function will be triggered each time Garlic find an retrieve a local stored data for a field
  374 + , onRetrieve: function ( item, storedVal ) {} // This function will be triggered each time Garlic find an retrieve a local stored data for a field
375 375 }
376 376
377 377 /* GARLIC DATA-API
25 index.html
@@ -105,10 +105,10 @@ <h1 class="">
105 105
106 106 <div class="row well intro">
107 107 <div id="head-download" class="download">
108   - <a href="https://github.com/guillaumepotier/Garlic.js/archive/1.0.2.zip">
  108 + <a href="https://github.com/guillaumepotier/Garlic.js/archive/1.0.3.zip">
109 109 <img width="90" border="0" src="http://github.com/images/modules/download/zip.png" onclick="onclick=”var that=this;_gaq.push(['_trackEvent','Download','ZIP',this.href]);setTimeout(function(){location.href=that.href;},200);return false;”">
110 110 </a>
111   - <a href="https://github.com/guillaumepotier/Garlic.js/archive/1.0.2.tar.gz">
  111 + <a href="https://github.com/guillaumepotier/Garlic.js/archive/1.0.3.tar.gz">
112 112 <img width="90" border="0" src="http://github.com/images/modules/download/tar.png" onclick="onclick=”var that=this;_gaq.push(['_trackEvent','Download','TAR',this.href]);setTimeout(function(){location.href=that.href;},200);return false;”">
113 113 </a>
114 114 </div>
@@ -280,10 +280,10 @@ <h1 class="">
280 280 <h1>Download</h1>
281 281 </div>
282 282 <div id="download" class="download">
283   - <a href="https://github.com/guillaumepotier/Garlic.js/archive/1.0.2.zip" onclick="onclick=”var that=this;_gaq.push(['_trackEvent','Download','ZIP',this.href]);setTimeout(function(){location.href=that.href;},200);return false;”" >
  283 + <a href="https://github.com/guillaumepotier/Garlic.js/archive/1.0.3.zip" onclick="onclick=”var that=this;_gaq.push(['_trackEvent','Download','ZIP',this.href]);setTimeout(function(){location.href=that.href;},200);return false;”" >
284 284 <img width="90" border="0" src="http://github.com/images/modules/download/zip.png">
285 285 </a>
286   - <a href="https://github.com/guillaumepotier/Garlic.js/archive/1.0.2.tar.gz" onclick="onclick=”var that=this;_gaq.push(['_trackEvent','Download','TAR',this.href]);setTimeout(function(){location.href=that.href;},200);return false;”">
  286 + <a href="https://github.com/guillaumepotier/Garlic.js/archive/1.0.3.tar.gz" onclick="onclick=”var that=this;_gaq.push(['_trackEvent','Download','TAR',this.href]);setTimeout(function(){location.href=that.href;},200);return false;”">
287 287 <img width="90" border="0" src="http://github.com/images/modules/download/tar.png">
288 288 </a>
289 289 </div>
@@ -327,14 +327,25 @@ <h1 class="">
327 327 Manually call garlic in javascript<br/>
328 328 <pre>
329 329 &lt;script type="text/javascript">
330   - $('[rel=persist]').garlic();
  330 + $( '[rel=persist]' ).garlic();
331 331 &lt;/script></pre>
332 332 </li>
333 333 <li>
334 334 Destroy storage for an element <strong>(don't work on an entire form yet, only input by input)</strong><br/>
335 335 <pre>
336 336 &lt;script type="text/javascript">
337   - $('input.no_good').garlic('destroy');
  337 + $( 'input.no_good' ).garlic( 'destroy' );
  338 +&lt;/script></pre>
  339 + </li>
  340 + <li>
  341 + Be notified when Garlic retrieve a field val by a custom overridable <code>onRetrieve</code> callback:
  342 + <pre>
  343 +&lt;script type="text/javascript">
  344 + $( 'input.no_good' ).garlic( {
  345 + onRetrieve: function ( elem, retrievedValue ) {
  346 + console.log( 'The retrieved value for ' + elem.name() + ' is : ' + retrievedValue );
  347 + }
  348 + } );
338 349 &lt;/script></pre>
339 350 </li>
340 351 </ul>
@@ -374,7 +385,7 @@ <h1 class="">
374 385 <div class="page-header">
375 386 <h1>Version / Licence</h1>
376 387 </div>
377   - <a href="https://github.com/guillaumepotier/Garlic.js/blob/master/README.md">1.0.2 - MIT</a> - <a href="https://github.com/guillaumepotier/Garlic.js/blob/master/CHANGELOG.md">See changelog</a>
  388 + <a href="https://github.com/guillaumepotier/Garlic.js/blob/master/README.md">1.0.3 - MIT</a> - <a href="https://github.com/guillaumepotier/Garlic.js/blob/master/CHANGELOG.md">See changelog</a>
378 389 <a href="#top" class="pull-right">Top</a>
379 390 </section>
380 391 </div>
2  tests/tests.js
@@ -8,7 +8,7 @@ var testSuite = function () {
8 8 e.preventDefault();
9 9 } );
10 10 $( '#form1' ).garlic( { domain: true } );
11   - $( '#retrieve-trigger' ).garlic( { retrieveTrigger: function ( elem, retrieveVal ) {
  11 + $( '#retrieve-trigger' ).garlic( { onRetrieve: function ( elem, retrieveVal ) {
12 12 elem.attr( 'storedValue', retrieveVal );
13 13 } } );
14 14 var garlicStorage = $( '#form1' ).garlic( 'getStorage' );

0 comments on commit c066000

Please sign in to comment.
Something went wrong with that request. Please try again.