-
Notifications
You must be signed in to change notification settings - Fork 11
/
smartjax.min.js
1 lines (1 loc) · 7.02 KB
/
smartjax.min.js
1
var Smartjax=function(){var a={timer:null,expirationWindowInMilliseconds:null,groupBasedClean:!1,idBasedClean:!0,setExpirationWindow:function(a,b,c,d,e,f,g,h){null!==this.timer&&clearInterval(this.timer);var i=a+1e3*(b+60*(c+60*(d+24*e)));this.expirationWindowInMilliseconds=i,h===!1&&(this.idBasedClean=!1),g===!0&&(this.groupBasedClean=!0),this.clearSelective(),f===!0?this.timer=setInterval(function(){Smartjax.cleanAll()}.bind(this),i):this.timer=setInterval(function(){this.clearSelective()}.bind(this),i)},clearSelective:function(){var a=["page","tab","forever"],b=Date.now(),c=[],d=[];a.forEach(function(a){var e=h.getFullStore(a);if(e){var f=e.storeIds,g=e.groups;if(this.idBasedClean&&f)for(var i in f)if(f.hasOwnProperty(i)){var j=f[i];j&&j.noAutoClean===!0&&j.firstSavedOn&&b-j.firstSavedOn>this.expirationWindowInMilliseconds&&c.push(i)}if(this.groupBasedClean&&g)for(var k in g)if(g.hasOwnProperty(k)){var l=g[k];l&&l.firstSavedOn&&b-l.firstSavedOn>this.expirationWindowInMilliseconds&&d.push(k)}}}.bind(this)),g.cleanStore({ids:c,groups:d})}},b={registerGroup:function(a,b){var d=a.group;if(!d)return null;b||(b=c.buildRequestStoreId(a));var e=h.getFullStore(a.store);e||(e={}),e.groups||(e.groups={});var f=e.groups[a.group];return f||(f={group:a.group,storeIds:[],firstSavedOn:Date.now()},e.groups[a.group]=f),f.storeIds.indexOf(b)==-1&&f.storeIds.push(b),h.setFullStore(e,a.store),!0},clearGroupData:function(a,b){var c=h.getFullStore(b),d=c&&c.groups&&c.groups[a];if(!d||!c)return!1;var e=d.storeIds;e&&e.forEach(function(a){h.remove(a,b),h.clearStoreId(a,b)}),c=h.getFullStore(b),c&&c.groups&&c.groups[a]&&delete c.groups[a],h.setFullStore(c,b),console.log("group "+a+" cleared from Smartjax store")},clearGroups:function(a,b){a&&a.length&&a.forEach($.proxy(function(a,c){this.clearGroupData(a,b)},this))}},c={buildRequestStoreId:function(a){if(a.id)return a.id;var b="",c=a.url;return c&&("/"!==c[0]&&(b+=c[0]),b+=c.substr(1,c.length-2),"/"!==c[c.length-1]&&(b+=c[c.length-1])),a.data&&(b+=JSON.stringify(a.data)),b},isForce:function(a){return"boolean"==typeof a.force?a.force:g.defaults.alwaysForce},shouldStore:function(a){return"boolean"==typeof a.store?a.store:g.defaults.alwaysStore},getStorageObj:function(a){var b=g.defaults.store,c=null;if("string"==typeof a?b=a:a&&a.store===!1&&(b=null),!Storage)return e;if(b)switch(b.toLowerCase()){case"tab":c=sessionStorage;break;case"page":c=e;break;case"forever":c=localStorage}return c},returnWithAddedStore:function(a){var c=new $.Deferred,d=this.getOriginalRequestObject(a.requestObj),e=a.storeId,g=f.getPromiseFor(e);if(!g){var i=$.ajax(d);i.done(function(d){h.save({key:e,value:d,storeName:a.requestObj.store,noAutoClean:a.requestObj.noAutoClean}),b.registerGroup(a.requestObj,e),c.resolve(d)}),i.fail(function(a){c.reject(a)}),g=f.setAndRefinePromise(e,c.promise())}return g},getOriginalRequestObject:function(a){var b=$.extend({},a);return delete b.store,delete b.force,b},returnFromStore:function(a,b,c){var d=new $.Deferred,e=h.fetch({key:a,storeName:b});return c&&"function"==typeof c&&c(),d.resolve(e),d.promise()},findBy:function(a,b,c){if(!(a&&a.length&&b&&c))return null;var d=$.grep(a,function(a){return a[b]==c});return d&&d.length&&d[0]?d[0]:null},clearAll:function(a){var b=h.getFullStore(a),c=b&&b.storeIds;c&&c.length&&c.forEach(function(b){h.clearStoreId(b,a)}),h.setFullStore({},a),console.log("All Smartjax store data cleared")}},d={replaceURL:function(a){if(window.history&&window.history.pushState){var b={title:"Smartjax Url"};history.pushState(b,"Smartjax Url",a)}},addQueryString:function(a,b){var c=a||" ",e=c.split("#"),f={url:e[0]},g={url:e[1]},h=g.url&&g.url.indexOf("?")!=-1?g:f;"/"===h.url[h.url.length-1]&&(h.url=h.url.slice(h.url.length-1,h.url.length)),b=$.extend({},d.existingQueryParams(h.url),b),h.url=h.url.split("?")[0];for(var i in b)if(b.hasOwnProperty(i)){var j=b[i];h.url+=h.url.indexOf("?")===-1?"?":"&",h.url+=i+"="+j}var k=f.url;return g.url&&(k+="#"+g.url),k},existingQueryParams:function(a){var b={},c=a.split("?")[1];if(c){var d=c.split("&");d.forEach(function(a){a=a.split("=");var c=a[0],d=a[1];b[c]=d})}return b}},e={SmartjaxStore:{},getItem:function(a){return this[a]},setItem:function(a,b){this[a]=b},removeItem:function(a){this.setItem(a,null)}},f={promiseStore:{},getPromiseFor:function(a){return this.promiseStore[a]&&this.promiseStore[a].promise},setAndRefinePromise:function(a,b){this.promiseStore[a]={promise:b};var c=new $.Deferred;return b.then(function(){delete f.promiseStore[a],c.resolve.apply(this,arguments)},function(){delete f.promiseStore[a],c.reject.apply(this,arguments)}),c.promise()}},g={defaults:{defaultMethod:"get",alwaysForce:!1,alwaysStore:!0,defaultStorageName:"SmartjaxStore",store:"tab"},setDefaults:function(a){$.extend(this.defaults,a)},ajax:function(a){var b=c.buildRequestStoreId(a);return!c.isForce(a)&&h.isInStore(b,a.store)?c.returnFromStore(b,a.store,a.success):c.shouldStore(a)?c.returnWithAddedStore({storeId:b,requestObj:a}):$.ajax(c.getOriginalRequestObject(a))},cleanAll:function(a){this.cleanStore({clearAll:!0},a)},cleanStore:function(a,d){var e=a.clearAll;if(e===!0){if(!d)return c.clearAll("page"),c.clearAll("tab"),c.clearAll("forever"),!0;c.clearAll(d)}var f=a.ids;f&&(d?h.remove(f,d):(h.remove(f,"page"),h.remove(f,"tab"),h.remove(f,"forever")));var g=a.groups;g&&(d?b.clearGroups(g,d):(b.clearGroups(g,"page"),b.clearGroups(g,"tab"),b.clearGroups(g,"forever")))},setExpirationWindow:function(b){var c=b.milliseconds||0,d=b.seconds||0,e=b.minutes||0,f=b.hours||0,g=b.days||0,h=b.cleanAll===!0||!1;a.setExpirationWindow(c,d,e,f,g,h,b.groupBasedClean,b.idBasedClean)},changeUrl:function(a){var b=a.url||window.location.href,c=a.params;c&&(b=d.addQueryString(b,c)),d.replaceURL(b)}},h={getFullStore:function(a){a=a||g.defaults.store;var b=c.getStorageObj(a).getItem("SmartjaxStore");return a&&"page"!=a.toLowerCase()&&(b=JSON.parse(b)),b},setFullStore:function(a,b){b=b||g.defaults.store;var d=c.getStorageObj(b);b&&"page"==b.toLowerCase()?d.setItem("SmartjaxStore",a):d.setItem("SmartjaxStore",JSON.stringify(a))},save:function(a){var b=a.storeName||g.defaults.store,d=c.getStorageObj(b);this.isInStore(a.key,a.storeName)||this.registerNewKey(a.key,a.storeName,{status:"success"});var e=a.value;"page"!=b&&(e=JSON.stringify(e)),d.setItem(a.key,e)},fetch:function(a){var b=c.getStorageObj(a.storeName),d=b.getItem(a.key);return a.storeName&&"page"==a.storeName.toLowerCase()?d:JSON.parse(d)},isInStore:function(a,b){b=b||g.defaults.store;var c=this.getFullStore(b)&&this.getFullStore(b).storeIds;return!(!c||void 0===c[a]||null===c[a])},registerNewKey:function(a,b,c){b=b||g.defaults.store;var d=this.getFullStore(b);d||(d={}),d.storeIds||(d.storeIds={}),d.storeIds[a]={firstSavedOn:Date.now()},this.setFullStore(d,b)},clearStoreId:function(a,b){b=b||g.defaults.store;var d=c.getStorageObj(b);d.removeItem(a)},remove:function(a,b){"string"==typeof a&&(a=[a]);var c=h.getFullStore(b);c&&(a.forEach(function(a){c.storeIds&&delete c.storeIds[a],this.clearStoreId(a,b)}.bind(this)),h.setFullStore(c,b))}};return g}();"undefined"!=typeof module?module.exports=Smartjax:"undefined"!=typeof window&&(window.Smartjax=Smartjax);