Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- added onRetrieve user function

  • Loading branch information...
commit c066000fd4895751bec5d25eb5847378f06327a2 1 parent 14d5b28
@guillaumepotier authored
View
5 CHANGELOG.md
@@ -1,5 +1,10 @@
#Changelog
+* **1.0.3** :
+
+ - added onRetrieve function in config. Called each time Garlic retrieves a field
+ stored value. Overridable by configuration
+
* **1.0.2** :
- fixed bug that did not allowed user to clear a field persisted by Garlic
View
3  README.md
@@ -10,7 +10,7 @@ http://garlicjs.org/
#Version
-1.0.2
+1.0.3
See CHANGELOG for more info.
@@ -40,6 +40,7 @@ They'll be created and dumped in the dist/ directory
* @johnrees
* @Marfa
* @leondewey
+* @willdurand
#Contribute!
View
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
!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=
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");
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,
-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=
-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,
+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;
+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,
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);
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",
!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+
(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=
-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",
+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",
function(){c('[data-persist="garlic"]').each(function(){c(this).garlic()})})}(window.jQuery||window.Zepto);
View
6 dist/garlic.min.js
@@ -2,11 +2,11 @@
!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=
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");
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,
-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=
-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,
+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;
+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,
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);
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",
!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+
(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=
-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",
+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",
function(){a('[data-persist="garlic"]').each(function(){a(this).garlic()})})}(window.jQuery||window.Zepto);
View
4 garlic.js
@@ -141,7 +141,7 @@
this.$element.val( storedValue );
// trigger custom user function when data is retrieved
- this.options.retrieveTrigger( this.$element, storedValue );
+ this.options.onRetrieve( this.$element, storedValue );
return;
}
@@ -371,7 +371,7 @@
, message: 'This is your saved data. Click here to see default one' // Default message for swapping data / state
, 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
}
- , retrieveTrigger: function ( item, storedVal ) {} // This function will be triggered each time Garlic find an retrieve a local stored data for a field
+ , onRetrieve: function ( item, storedVal ) {} // This function will be triggered each time Garlic find an retrieve a local stored data for a field
}
/* GARLIC DATA-API
View
25 index.html
@@ -105,10 +105,10 @@ <h1 class="">
<div class="row well intro">
<div id="head-download" class="download">
- <a href="https://github.com/guillaumepotier/Garlic.js/archive/1.0.2.zip">
+ <a href="https://github.com/guillaumepotier/Garlic.js/archive/1.0.3.zip">
<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;”">
</a>
- <a href="https://github.com/guillaumepotier/Garlic.js/archive/1.0.2.tar.gz">
+ <a href="https://github.com/guillaumepotier/Garlic.js/archive/1.0.3.tar.gz">
<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;”">
</a>
</div>
@@ -280,10 +280,10 @@ <h1 class="">
<h1>Download</h1>
</div>
<div id="download" class="download">
- <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;”" >
+ <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;”" >
<img width="90" border="0" src="http://github.com/images/modules/download/zip.png">
</a>
- <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;”">
+ <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;”">
<img width="90" border="0" src="http://github.com/images/modules/download/tar.png">
</a>
</div>
@@ -327,14 +327,25 @@ <h1 class="">
Manually call garlic in javascript<br/>
<pre>
&lt;script type="text/javascript">
- $('[rel=persist]').garlic();
+ $( '[rel=persist]' ).garlic();
&lt;/script></pre>
</li>
<li>
Destroy storage for an element <strong>(don't work on an entire form yet, only input by input)</strong><br/>
<pre>
&lt;script type="text/javascript">
- $('input.no_good').garlic('destroy');
+ $( 'input.no_good' ).garlic( 'destroy' );
+&lt;/script></pre>
+ </li>
+ <li>
+ Be notified when Garlic retrieve a field val by a custom overridable <code>onRetrieve</code> callback:
+ <pre>
+&lt;script type="text/javascript">
+ $( 'input.no_good' ).garlic( {
+ onRetrieve: function ( elem, retrievedValue ) {
+ console.log( 'The retrieved value for ' + elem.name() + ' is : ' + retrievedValue );
+ }
+ } );
&lt;/script></pre>
</li>
</ul>
@@ -374,7 +385,7 @@ <h1 class="">
<div class="page-header">
<h1>Version / Licence</h1>
</div>
- <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>
+ <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>
<a href="#top" class="pull-right">Top</a>
</section>
</div>
View
2  tests/tests.js
@@ -8,7 +8,7 @@ var testSuite = function () {
e.preventDefault();
} );
$( '#form1' ).garlic( { domain: true } );
- $( '#retrieve-trigger' ).garlic( { retrieveTrigger: function ( elem, retrieveVal ) {
+ $( '#retrieve-trigger' ).garlic( { onRetrieve: function ( elem, retrieveVal ) {
elem.attr( 'storedValue', retrieveVal );
} } );
var garlicStorage = $( '#form1' ).garlic( 'getStorage' );

0 comments on commit c066000

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