/** * angular-localforage - Angular service & directive for https://github.com/mozilla/localForage (Offline storage, improved.) * @version v1.3.8 * @link https://github.com/ocombe/angular-localForage * @license MIT * @author Olivier Combe */ !function(e,r){"use strict";var t=e&&e.angular||window&&window.angular;if("function"==typeof define&&define.amd)define(["localforage"],function(e){return r(t,e)});else{if("object"!=typeof exports&&"object"!=typeof global)return r(t,e.localforage);"undefined"==typeof module?global.module.exports=r(t,require("localforage")):module.exports=r(t,require("localforage"))}}(this,function(e,r,t){"use strict";var n=e.module("LocalForageModule",["ng"]);return n.provider("$localForage",function(){var t={},n={name:"lf"},o={setItem:!1,removeItem:!1},i={};this.setNotify=function(e,r){o={setItem:e,removeItem:r}},this.config=function(r){if(!e.isObject(r))throw new Error("The config parameter should be an object");e.extend(n,r)},this.$get=["$rootScope","$q","$parse",function(a,f,s){function u(r,t){return function(n){if((e.isObject(n)&&n.name?"InvalidStateError"===n.name:e.isString(n)&&"InvalidStateError"===n)&&"asyncStorage"===this.driver()||e.isObject(n)&&n.code&&5===n.code){var o=this;return o.setDriver("localStorageWrapper").then(function(){return t.apply(o,r)})}return f.reject(n)}}var c=function(t){e.isDefined(t)?this._localforage=r.createInstance(t):(this._localforage=r,r.config(n))};c.prototype.createInstance=function(r){if(e.isObject(r)){if(r=e.extend({},n,r),e.isDefined(t[r.name]))throw new Error("A localForage instance with the name "+r.name+" is already defined.");return t[r.name]=new c(r),t[r.name]}throw new Error("The parameter should be a config object.")},c.prototype.instance=function(r){if(e.isUndefined(r))return t[n.name];if(e.isString(r)){if(e.isDefined(t[r]))return t[r];throw new Error("No localForage instance of that name exists.")}throw new Error("The parameter should be a string.")},c.prototype.setDriver=function(e){return this._localforage.setDriver(e)},c.prototype.driver=function(){return this._localforage.driver()},c.prototype.defineDriver=function(e){return this._localforage.defineDriver(e)},c.prototype.setItem=function(r,t){function n(r){var t;return e.isArray(r)?r.map(n):e.isObject(r)&&r.constructor===Object?(t=e.extend({},r),e.isDefined(t.$promise)&&delete t.$promise,e.isDefined(t.$$hashKey)&&delete t.$$hashKey,Object.keys(t).reduce(function(e,r){return e[r]=n(t[r]),e},{})):r}if(e.isUndefined(r))throw new Error("You must define a key to set");var i,s=this,c=arguments;if(e.isArray(r)){if(!e.isArray(t))throw new Error("If you set an array of keys, the values should be an array too");return f.all(r.map(function(e,r){return s.setItem(e,t[r])}))}return i=n(t),s._localforage.setItem(s.prefix()+r,i).then(function(){return o.setItem&&a.$broadcast("LocalForageModule.setItem",{key:r,newvalue:i,driver:s.driver()}),i})["catch"](u(c,s.setItem))},c.prototype.getItem=function(r,t){if(e.isUndefined(r))throw new Error("You must define a key to get");var n,o=f.defer(),i=arguments,a=this;if(e.isArray(r)){var s=[],u=0;n=a._localforage.iterate(function(e,t){var n=r.indexOf(a.prefix()+t);return n>-1&&(s[n]=e,u++),u===r.length?s:void 0}).then(function(){for(var n=!0,i=0;ii;i++)o.push(r[i].substr(t.prefix().length,r[i].length));r=o}e.resolve(r)},function(n){t.onError(n,r,t.keys,e)}),e.promise};return c.prototype.keys=l,c.prototype.getKeys=l,c.prototype.length=function(){var e=f.defer(),r=arguments,t=this;return t._localforage.length().then(function(r){e.resolve(r)},function(n){t.onError(n,r,length,e)}),e.promise},c.prototype.bind=function(r,o){if(e.isString(o))o={key:o};else if(!e.isObject(o)||e.isUndefined(o.key))throw new Error("You must define a key to bind");var a={defaultValue:"",name:n.name};o=e.extend({},a,o);var f=t[o.name];if(e.isUndefined(f))throw new Error("You must use the name of an existing instance");var u=o.scopeKey||o.key,c=s(u);return f.getItem(o.key,!0).then(function(e){return c.assign(r,e),e})["catch"](function(){return c.assign(r,o.defaultValue),f.setItem(o.key,o.defaultValue)}).then(function(t){return e.isDefined(i[o.key])&&i[o.key](),i[o.key]=r.$watch(u,function(r){e.isDefined(r)&&f.setItem(o.key,r)},!0),t})},c.prototype.unbind=function(r,o){if(e.isString(o))o={key:o};else if(!e.isObject(o)||e.isUndefined(o.key))throw new Error("You must define a key to unbind");var a={scopeKey:o.key,name:n.name};o=e.extend({},a,o);var f=t[o.name];if(e.isUndefined(f))throw new Error("You must use the name of an existing instance");return s(o.scopeKey).assign(r,null),e.isDefined(i[o.key])&&(i[o.key](),delete i[o.key]),f.removeItem(o.key)},c.prototype.prefix=function(){return"localStorageWrapper"===this.driver()&&n.oldPrefix?this._localforage.config().name+".":""},c.prototype.onError=function(r,t,n,o){if((e.isObject(r)&&r.name?"InvalidStateError"===r.name:e.isString(r)&&"InvalidStateError"===r)&&"asyncStorage"===this.driver()||e.isObject(r)&&r.code&&5===r.code){var i=this;i.setDriver("localStorageWrapper").then(function(){n.apply(i,t).then(function(e){o.resolve(e)},function(e){o.reject(e)})},function(){o.reject(r)})}else o.reject(r)},t[n.name]=new c,t[n.name]}]}),n.directive("localForage",["$localForage",function(r){return{restrict:"A",link:function(t,n,o){var i=t.$eval(o.localForage);e.isObject(i)&&e.isDefined(i.key)?r.bind(t,i):r.bind(t,o.localForage)}}}]),n.name});