forked from gwendall/way.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathway.min.js
1 lines (1 loc) · 6.8 KB
/
way.min.js
1
window.way={},function(){"use strict";var a="way",b=function(){this._watchers={},this._watchersAll={}};b.prototype.constructor=b,b.prototype.watchAll=function(a){this._watchersAll=this._watchersAll||[],_.contains(this._watchersAll,a)||this._watchersAll.push(a)},b.prototype.watch=function(a,b){this._watchers||(this._watchers={}),this._watchers[a]=this._watchers[a]||[],this._watchers[a].push(b)},b.prototype.findWatcherDeps=function(a){var b=[],c=_.keys(this._watchers);return c.forEach(function(c){d(a,c)&&b.push(c)}),b},b.prototype.emitChange=function(a){this._watchers||(this._watchers={});var b=this,c=b.findWatcherDeps(a);c.forEach(function(a){this._watchers[a]&&this._watchers[a].forEach(function(c){c.apply(b,[b.get(a)])})}),b._watchersAll&&_.isArray(b._watchersAll)&&b._watchersAll.forEach(function(c){_.isFunction(c)&&c.apply(b,[a,b.get(a)])})};var c=function(){this.data={},this._bindings={},this.options={persistent:!0,timeoutInput:50,timeoutDOM:500}};c.prototype=Object.create(b.prototype),c.constructor=c,c.prototype.dom=function(a){return this._element=$(a),this},c.prototype.toStorage=function(a,b){var c=this,b=b||c._element,a=a||c.dom(b).getOptions(),d=c.dom(b).toJSON(a);return a.readonly?!1:void c.set(a.data,d,a)},c.prototype.toJSON=function(a,b){var c=this,b=b||c._element,d=c.dom(b).getValue(),a=a||c.dom(b).getOptions();return _.isArray(a.pick)&&(d=h(d,a.pick,!0)),_.isArray(a.omit)&&(d=h(d,a.omit,!1)),d},c.prototype.fromStorage=function(a,b){var c=this,b=b||c._element,a=a||c.dom(b).getOptions();if(a.writeonly)return!1;var d=c.get(a.data);c.dom(b).fromJSON(d,a)},c.prototype.fromJSON=function(a,b,c){var d=this,c=c||d._element,b=b||d.dom(c).getOptions();if(b.writeonly)return!1;if(_.isObject(a)){_.isArray(b.pick)&&(a=h(a,b.pick,!0)),_.isArray(b.omit)&&(a=h(a,b.omit,!1));var e=_.isObject(d.dom(c).toJSON())?d.dom(c).toJSON():{};a=_.extend(e,a)}b.json&&(a=_json.isStringified(a)?a:_json.prettyprint(a)),d.dom(c).setValue(a,b)},c.prototype.getValue=function(a){var b=this,a=a||b._element,c={FORM:function(){return form2js($(a).get(0))},INPUT:function(){return $(a).val()},TEXTAREA:function(){return $(a).val()}},d=function(){$(a).html()},e=$(a).get(0).tagName,f=c[e]||d;return f()},c.prototype.setValue=function(a,b,c){var d=this,c=c||d._element,b=b||d.dom(c).getOptions(),e={FORM:function(a){js2form($(c).get(0),a)},INPUT:function(a){_.isString(a)||(a=JSON.stringify(a)),$(c).val(a||"")},TEXTAREA:function(a){_.isString(a)||(a=JSON.stringify(a)),$(c).val(a||"")},PRE:function(a){b.html?$(c).html(a):$(c).text(a)},IMG:function(a){if(!a)return a=b.default||"",$(c).attr("src",a),!1;var d=function(a,b){$(c).addClass("way-loading"),$("<img>",{src:a,error:function(){b(!1)},load:function(){b(!0)}})};d(a,function(d){$(c).removeClass("way-loading"),d?$(c).removeClass("way-error").addClass("way-success"):(a?$(c).addClass("way-error"):$(c).removeClass("way-error").removeClass("way-success"),a=b.default||""),$(c).attr("src",a)})}},f=function(a){b.html?$(c).html(a):$(c).text(a)},g=$(c).get(0).tagName,h=e[g]||f;h(a)},c.prototype.setDefault=function(a,b,c){var d=this,c=c||d._element,a=a||!1,b=b?_.extend(d.dom(c).getOptions(),b):d.dom(c).getOptions();return b.default?a?a?d.set(b.data,b.default,b):void 0:d.dom(c).setValue(b.default,b):!1},c.prototype.setDefaults=function(){var b=this,c="["+a+"-default]";$(c).each(function(){b.dom(this).setDefault()})},c.prototype.registerBindings=function(){var b=this,c="["+a+"-data]";b._bindings={},$(c).each(function(){var a=this,c=b.dom(a).getOptions();return c.data?(b._bindings[c.data]=b._bindings[c.data]||[],void(e(b._bindings[c.data],a)||b._bindings[c.data].push($(a)))):!1})},c.prototype.updateBindings=function(a){var b=this;b._bindings=b._bindings||{};var c=[];if(a)for(var e in b._bindings)d(e,a)&&(c=_.union(c,b._bindings[e]));else for(var e in b._bindings)c=_.union(c,b._bindings[e]);c.forEach(function(a){var c="FORM"==$(a).get(0).tagName&&$(a).get(0)==$(":focus").parents("form").get(0)?!0:!1;c||b.dom(a).fromStorage()}),b._bindings.__all__.forEach(function(a){b.dom(a).fromJSON(b.data)})},c.prototype.getOptions=function(b){var c=this,b=b||c._element,d={data:null,html:!1,readonly:!1,writeonly:!1,persistent:!1};return _.extend(d,c.dom(b).getAttrs(a))},c.prototype.getAttrs=function(a,b){var c=this,b=b||c._element,e=function(a,b){var c={pick:"array",omit:"array",readonly:"boolean",writeonly:"boolean",json:"boolean",html:"boolean",persistent:"boolean"},d={array:function(a){return a.split(",")},"boolean":function(a){return"true"==a?!0:"false"==a?!1:!1}},e=function(){return b},f=c[a]||null,g=d[f]||e;return g(b)},f={};return $(b).length&&$.each($(b).get(0).attributes,function(b,c){var g=a&&d(c.name,a+"-")?!0:!1;if(g){var h=a?c.name.slice(a.length+1,c.name.length):c.name,i=e(h,c.value);f[h]=i}}),f},c.prototype.get=function(a){var b=this;return void 0==a||_.isString(a)?b.data?a?_json.get(b.data,a):b.data:{}:!1},c.prototype.set=function(a,b,c){var d=this;return c=c||{},a&&!_.isString(a)?!1:(d.data=d.data||{},d.data=a?_json.set(d.data,a,b):{},d.updateBindings(a),d.emitChange(a,b),void(c.persistent&&d.backup(a)))},c.prototype.remove=function(a,b){var c=this;b=b||{},c.data=a?_json.remove(c.data,a):{},c.updateBindings(a),c.emitChange(a,null),b.persistent&&c.backup(a)},c.prototype.clear=function(){this.remove(null,{persistent:!0})},c.prototype.backup=function(){var b=this;try{var c=b.data||{};localStorage.setItem(a,JSON.stringify(c))}catch(d){console.log("Your browser does not support localStorage.")}},c.prototype.restore=function(){var b=this;try{var c=localStorage.getItem(a);try{c=JSON.parse(c);for(var d in c)b.set(d,c[d])}catch(e){}}catch(e){console.log("Your browser does not support localStorage.")}};var d=function(a,b){return""===b?!0:null==a||null==b?!1:(a=String(a),b=String(b),a.length>=b.length&&a.slice(0,b.length)===b)},e=function(a,b){var c=!1;return a.every(function(a){return $(a).get(0)===$(b).get(0)?(c=!0,!1):!0}),c},f=function(a){return _.pick(a,_.compact(_.keys(a)))},g=function(a,b,c){var e=_.keys(a);return e.forEach(function(e){c?d(e,b)||delete a[e]:d(e,b)&&delete a[e]}),a},h=function(a,b,c){var d=_json.flatten(a);for(var e in b)d=g(d,b[e],c);var h=_json.unflatten(d);return f(h)};way=new c;var i=null;$(document).ready(function(){way.registerBindings(),way.setDefaults(),way.options.persistent&&way.restore(),$("body").bind("DOMSubtreeModified",function(){i&&clearTimeout(i),i=setTimeout(function(){way.registerBindings()},way.options.timeoutDOM)})});var j=null;$(document).on("keyup change","form["+a+"-data] :input",function(a){j&&clearTimeout(j),j=setTimeout(function(){var b=$(a.target).parents("form");way.dom(b).toStorage()},way.options.timeoutInput)}),$(document).on("keyup change",":input["+a+"-data]",function(a){j&&clearTimeout(j),j=setTimeout(function(){var b=$(a.target);way.dom(b).toStorage()},way.options.timeout)}),$(document).on("click","["+a+"-clear]",function(){var a=way.dom(this).getOptions();way.remove(a.data,a)})}.call(this);