UNPKG

memoria-js

Version:

Extensible form storage for memorising user inputs with localStorage.

2 lines 5.52 kB
/*! memoria-js by Adam Timberlake created on 2014-01-07 */ !function(a,b){"use strict";a.Memoria=function(){"undefined"!=typeof b&&(b.getItem("memoria")||b.setItem("memoria",JSON.stringify({})),this._initialiseForms())},a.Memoria.Observer={},a.Memoria.prototype={_supportedNodes:["input","select","textarea","*[data-memoria-input]"],clear:function(a){var c=JSON.parse(b.memoria);return"undefined"==typeof c[a]?!1:(delete c[a],b.memoria=JSON.stringify(c),!0)},_initialiseForms:function(){for(var b=a.document.querySelectorAll("form[name]"),c=0,d=b.length;d>c;c++)for(var e=b[c],f=this._fetchInputs(e),g=0,h=f.length;h>g;g++){var i=f[g];if("password"!==i.getAttribute("type")&&!i.hasAttribute("data-memoria-ignore")){var j=i.hasAttribute("data-memoria-input"),k=j?"Element":i.nodeName.charAt(0).toUpperCase()+i.nodeName.slice(1).toLowerCase(),l=j?a.Memoria.Element:a.Memoria[k],m={formName:e.getAttribute("name"),nodeName:i.getAttribute("data-memoria-name")||i.getAttribute("name")};if("undefined"==typeof l)throw"MEMORIA: Unable to instantiate `Memoria."+k+"` delegator object.";var n=new l;n.form=e,n.node=i,n.key=m,j&&n.attachObserver(a.Memoria.Observer[i.getAttribute("data-memoria-input")]),n.initialise(),n.transferValue()}}},_fetchInputs:function(a){for(var b=[],c=0,d=this._supportedNodes.length;d>c;c++)for(var e=a.querySelectorAll(this._supportedNodes[c]),f=0,g=e.length;g>f;f++)b.push(e[f]);return b}},a.document.addEventListener("DOMContentLoaded",function(){a.memoria=new a.Memoria},!1)}(window,window.localStorage),function(a){"use strict";a.Memoria.Observer.MultipleSelect={eventFired:function(a,b){for(var c=[],d=0,e=b.options.length;e>d;d++){var f=b.options[d];f.selected&&c.push(d)}return c},setupElement:function(a,b){for(var c=0,d=a.options.length;d>c;c++){var e=a.options[c];-1!==b.indexOf(c)&&e.setAttribute("selected")}}}}(window),function(a,b){"use strict";a.Memoria.Element=function(){},a.Memoria.Element.prototype={node:null,form:null,key:null,observer:null,_attribute:"value",initialise:function(){var a=this.node.getAttribute("data-memoria-event"),b=["click","dblclick","mousedown","mousemove","mouseover","mouseleave","mouseout","mouseup","keydown","keyup","keypress","blur","change","focus","reset","select","submit"],c=[];if(!a)throw"MEMORIA: Unable to automatically find the event name.";c=a.split(/,/);for(var d=0,e=c.length;e>d;d++){var f=c[d].trim();"on"!==f.substr(0,2)&&-1!==b.indexOf(f)&&(f="on"+f),this._attachEvent(f)}},attachObserver:function(a){this.observer=a},_attachEvent:function(a){Array.isArray(a)||(a=[a]);for(var b=0,c=a.length;c>b;b++){var d=a[b];if("undefined"==typeof this.node[d])throw"MEMORIA: Unable to find the `"+d+"` event on `"+this.node.nodeName.toLowerCase()+"` node.";this.node[d]=this._save.bind(this)}},transferValue:function(){var a=this._getStorage(),b=this.key,c=a[b.formName][b.nodeName];c&&(this.observer&&"function"==typeof this.observer.setupElement?this.observer.setupElement(this.node,c):this.node[this._attribute]=c)},_save:function(a){if(!this.key.nodeName)throw'Memoria: Attempting to save value "'+this.node[this._attribute]+'" but node does not have a `name`.';var c=this._getStorage(),d=this.key;if(this.observer&&"function"==typeof this.observer.eventFired){var e=this.observer.eventFired(a,this.node);if("undefined"==typeof e)throw"MEMORIA: You must return a value from the `eventFired` method.";c[d.formName][d.nodeName]=e}else c[d.formName][d.nodeName]=this.node[this._attribute];b.setItem("memoria",JSON.stringify(c)),this.node.hasAttribute("data-memoria-name")&&this._resetOthersWithSameName()},_getStorage:function(){var a=JSON.parse(b.getItem("memoria"));return"undefined"==typeof a[this.key.formName]&&(a[this.key.formName]={}),a},_getEventName:function(a){return this.node.getAttribute("data-memoria-event")||a},_setAttribute:function(a){this._attribute=a},_resetOthersWithSameName:function(){for(var a=this._getStorage(),c=this.node.getAttribute("name"),d=this.form.querySelectorAll('*[name="'+c+'"]'),e=0,f=d.length;f>e;e++){var g=d[e];if(g!==this.node&&g.hasAttribute("data-memoria-name")){var h=g.getAttribute("data-memoria-name");a[this.key.formName][h]&&(delete a[this.key.formName][h],b.setItem("memoria",JSON.stringify(a)))}}}}}(window,window.localStorage),function(a){"use strict";a.Memoria.Input=function(){},a.Memoria.Input.prototype=new a.Memoria.Element,a.Memoria.Input.prototype.initialise=function(){var a=function(a){var b=["number","search","date","range","month","time","week","datetime","datetime-local","color"];return-1!==b.indexOf(a)};if(a(this.node.getAttribute("type")))return this._attachEvent(this._getEventName(["onchange","onkeyup","onclick"])),void 0;switch(this.node.getAttribute("type")){case"radio":this._setAttribute("checked"),this._attachEvent(this._getEventName("onclick"));break;case"checkbox":this._setAttribute("checked"),this._attachEvent(this._getEventName("onchange"));break;default:this._attachEvent(this._getEventName(["onkeyup","onchange"]))}}}(window),function(a){"use strict";a.Memoria.Select=function(){},a.Memoria.Select.prototype=new a.Memoria.Element,a.Memoria.Select.prototype.initialise=function(){this.node.hasAttribute("multiple")&&this.attachObserver(a.Memoria.Observer.MultipleSelect),this._setAttribute("selectedIndex"),this._attachEvent(this._getEventName("onchange"))}}(window),function(a){"use strict";a.Memoria.Textarea=function(){},a.Memoria.Textarea.prototype=new a.Memoria.Element,a.Memoria.Textarea.prototype.initialise=function(){this._attachEvent("onkeyup")}}(window);