Comparing version 2.2.21 to 2.3.0
@@ -132,27 +132,65 @@ (function (global, factory) { | ||
var eventListeners = {}; | ||
var listeners = {}; | ||
/** | ||
* Event callback | ||
* | ||
* @param {Object} event | ||
*/ | ||
function change(event) { | ||
var e = event || window.event; | ||
var _class$2 = function () { | ||
function _class() { | ||
classCallCheck(this, _class); | ||
} | ||
var emit = function emit(listener) { | ||
listener(e.newValue ? JSON.parse(e.newValue).value : e.newValue, e.oldValue ? JSON.parse(e.oldValue).value : e.oldValue, e.url || e.uri); | ||
}; | ||
createClass(_class, null, [{ | ||
key: 'on', | ||
value: function on(name, callback) { | ||
if (typeof listeners[name] === 'undefined') { | ||
listeners[name] = []; | ||
} | ||
if (typeof e === 'undefined' || typeof e.key === 'undefined') { | ||
return; | ||
} | ||
listeners[name].push(callback); | ||
} | ||
}, { | ||
key: 'off', | ||
value: function off(name, callback) { | ||
if (listeners[name].length) { | ||
listeners[name].splice(listeners[name].indexOf(callback), 1); | ||
} else { | ||
listeners[name] = []; | ||
} | ||
} | ||
}, { | ||
key: 'emit', | ||
value: function emit(event) { | ||
var e = event || window.event; | ||
var all = eventListeners[e.key]; | ||
var fire = function fire(listener) { | ||
var newValue = void 0; | ||
var oldValue = void 0; | ||
if (typeof all !== 'undefined') { | ||
all.forEach(emit); | ||
} | ||
} | ||
try { | ||
newValue = JSON.parse(e.newValue).value; | ||
} catch (err) { | ||
newValue = e.newValue; | ||
} | ||
try { | ||
oldValue = JSON.parse(e.oldValue).value; | ||
} catch (err) { | ||
oldValue = e.oldValue; | ||
} | ||
listener(newValue, oldValue, e.url || e.uri); | ||
}; | ||
if (typeof e === 'undefined' || typeof e.key === 'undefined') { | ||
return; | ||
} | ||
var all = listeners[e.key]; | ||
if (typeof all !== 'undefined') { | ||
all.forEach(fire); | ||
} | ||
} | ||
}]); | ||
return _class; | ||
}(); | ||
/** | ||
@@ -162,8 +200,8 @@ * Storage Bridge | ||
var Storage = function () { | ||
var _class$1 = function () { | ||
/** | ||
* @param {Object} storage | ||
*/ | ||
function Storage(storage) { | ||
classCallCheck(this, Storage); | ||
function _class(storage) { | ||
classCallCheck(this, _class); | ||
@@ -190,7 +228,7 @@ this.storage = storage; | ||
if (window.addEventListener) { | ||
window.addEventListener(this.options.events[i], change, false); | ||
window.addEventListener(this.options.events[i], _class$2.emit, false); | ||
} else if (window.attachEvent) { | ||
window.attachEvent('on' + this.options.events[i], change); | ||
window.attachEvent('on' + this.options.events[i], _class$2.emit); | ||
} else { | ||
window['on' + this.options.events[i]] = change; | ||
window['on' + this.options.events[i]] = _class$2.emit; | ||
} | ||
@@ -208,3 +246,3 @@ } | ||
createClass(Storage, [{ | ||
createClass(_class, [{ | ||
key: 'setOptions', | ||
@@ -334,7 +372,3 @@ value: function setOptions() { | ||
value: function on(name, callback) { | ||
if (eventListeners[this.options.namespace + name]) { | ||
eventListeners[this.options.namespace + name].push(callback); | ||
} else { | ||
eventListeners[this.options.namespace + name] = [callback]; | ||
} | ||
_class$2.on(this.options.namespace + name, callback); | ||
} | ||
@@ -352,16 +386,10 @@ | ||
value: function off(name, callback) { | ||
var ns = eventListeners[this.options.namespace + name]; | ||
if (ns.length > 1) { | ||
ns.splice(ns.indexOf(callback), 1); | ||
} else { | ||
eventListeners[this.options.namespace + name] = []; | ||
} | ||
_class$2.off(this.options.namespace + name, callback); | ||
} | ||
}]); | ||
return Storage; | ||
return _class; | ||
}(); | ||
var store = typeof window !== 'undefined' && 'localStorage' in window ? window.localStorage : memoryStorage; | ||
var storageObject = new Storage(store); | ||
var ls = new _class$1(store); | ||
@@ -377,7 +405,7 @@ var VueLocalStorage = { | ||
install: function install(Vue, options) { | ||
storageObject.setOptions(_extends(storageObject.options, { | ||
ls.setOptions(_extends(ls.options, { | ||
namespace: '' | ||
}, options || {})); | ||
Vue.ls = storageObject; // eslint-disable-line | ||
Vue.ls = ls; // eslint-disable-line | ||
Object.defineProperty(Vue.prototype, '$ls', { | ||
@@ -390,3 +418,3 @@ /** | ||
get: function get$$1() { | ||
return storageObject; | ||
return ls; | ||
} | ||
@@ -393,0 +421,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e["vue-ls"]=t()}(this,function(){"use strict";function e(e){var t=e||window.event,n=function(e){e(t.newValue?JSON.parse(t.newValue).value:t.newValue,t.oldValue?JSON.parse(t.oldValue).value:t.oldValue,t.url||t.uri)};if(void 0!==t&&void 0!==t.key){var o=a[t.key];void 0!==o&&o.forEach(n)}}var t={},n={getItem:function(e){return e in t?t[e]:null},setItem:function(e,n){return t[e]=n,!0},removeItem:function(e){return!!(e in t)&&delete t[e]},clear:function(){return t={},!0},key:function(e){var n=Object.keys(t);return void 0!==n[e]?n[e]:null}};Object.defineProperty(n,"length",{get:function(){return Object.keys(t).length}});var o=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},i=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,n,o){return n&&e(t.prototype,n),o&&e(t,o),t}}(),r=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},a={},s=new(function(){function t(n){if(o(this,t),this.storage=n,this.options={namespace:"",events:["storage"]},Object.defineProperty(this,"length",{get:function(){return this.storage.length}}),"undefined"!=typeof window)for(var i in this.options.events)window.addEventListener?window.addEventListener(this.options.events[i],e,!1):window.attachEvent?window.attachEvent("on"+this.options.events[i],e):window["on"+this.options.events[i]]=e}return i(t,[{key:"setOptions",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.options=r(this.options,e)}},{key:"set",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;this.storage.setItem(this.options.namespace+e,JSON.stringify({value:t,expire:null!==n?(new Date).getTime()+n:null}))}},{key:"get",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=this.storage.getItem(this.options.namespace+e);if(null!==n)try{var o=JSON.parse(n);if(null===o.expire)return o.value;if(o.expire>=(new Date).getTime())return o.value;this.remove(e)}catch(e){return t}return t}},{key:"key",value:function(e){return this.storage.key(e)}},{key:"remove",value:function(e){return this.storage.removeItem(this.options.namespace+e)}},{key:"clear",value:function(){if(0!==this.length){for(var e=[],t=0;t<this.length;t++){var n=this.storage.key(t);!1!==new RegExp("^"+this.options.namespace+".+","i").test(n)&&e.push(n)}for(var o in e)this.storage.removeItem(e[o])}}},{key:"on",value:function(e,t){a[this.options.namespace+e]?a[this.options.namespace+e].push(t):a[this.options.namespace+e]=[t]}},{key:"off",value:function(e,t){var n=a[this.options.namespace+e];n.length>1?n.splice(n.indexOf(t),1):a[this.options.namespace+e]=[]}}]),t}())("undefined"!=typeof window&&"localStorage"in window?window.localStorage:n),u={install:function(e,t){s.setOptions(r(s.options,{namespace:""},t||{})),e.ls=s,Object.defineProperty(e.prototype,"$ls",{get:function(){return s}})}};return"undefined"!=typeof window&&(window.VueLocalStorage=u),u}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e["vue-ls"]=t()}(this,function(){"use strict";var e={},t={getItem:function(t){return t in e?e[t]:null},setItem:function(t,n){return e[t]=n,!0},removeItem:function(t){return!!(t in e)&&delete e[t]},clear:function(){return e={},!0},key:function(t){var n=Object.keys(e);return void 0!==n[t]?n[t]:null}};Object.defineProperty(t,"length",{get:function(){return Object.keys(e).length}});var n=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},o=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,n,o){return n&&e(t.prototype,n),o&&e(t,o),t}}(),i=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(e[o]=n[o])}return e},r={},a=function(){function e(){n(this,e)}return o(e,null,[{key:"on",value:function(e,t){void 0===r[e]&&(r[e]=[]),r[e].push(t)}},{key:"off",value:function(e,t){r[e].length?r[e].splice(r[e].indexOf(t),1):r[e]=[]}},{key:"emit",value:function(e){var t=e||window.event,n=function(e){var n=void 0,o=void 0;try{n=JSON.parse(t.newValue).value}catch(e){n=t.newValue}try{o=JSON.parse(t.oldValue).value}catch(e){o=t.oldValue}e(n,o,t.url||t.uri)};if(void 0!==t&&void 0!==t.key){var o=r[t.key];void 0!==o&&o.forEach(n)}}}]),e}(),u=new(function(){function e(t){if(n(this,e),this.storage=t,this.options={namespace:"",events:["storage"]},Object.defineProperty(this,"length",{get:function(){return this.storage.length}}),"undefined"!=typeof window)for(var o in this.options.events)window.addEventListener?window.addEventListener(this.options.events[o],a.emit,!1):window.attachEvent?window.attachEvent("on"+this.options.events[o],a.emit):window["on"+this.options.events[o]]=a.emit}return o(e,[{key:"setOptions",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};this.options=i(this.options,e)}},{key:"set",value:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;this.storage.setItem(this.options.namespace+e,JSON.stringify({value:t,expire:null!==n?(new Date).getTime()+n:null}))}},{key:"get",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=this.storage.getItem(this.options.namespace+e);if(null!==n)try{var o=JSON.parse(n);if(null===o.expire)return o.value;if(o.expire>=(new Date).getTime())return o.value;this.remove(e)}catch(e){return t}return t}},{key:"key",value:function(e){return this.storage.key(e)}},{key:"remove",value:function(e){return this.storage.removeItem(this.options.namespace+e)}},{key:"clear",value:function(){if(0!==this.length){for(var e=[],t=0;t<this.length;t++){var n=this.storage.key(t);!1!==new RegExp("^"+this.options.namespace+".+","i").test(n)&&e.push(n)}for(var o in e)this.storage.removeItem(e[o])}}},{key:"on",value:function(e,t){a.on(this.options.namespace+e,t)}},{key:"off",value:function(e,t){a.off(this.options.namespace+e,t)}}]),e}())("undefined"!=typeof window&&"localStorage"in window?window.localStorage:t),s={install:function(e,t){u.setOptions(i(u.options,{namespace:""},t||{})),e.ls=u,Object.defineProperty(e.prototype,"$ls",{get:function(){return u}})}};return"undefined"!=typeof window&&(window.VueLocalStorage=s),s}); | ||
//# sourceMappingURL=vue-ls.min.js.map |
{ | ||
"name": "vue-ls", | ||
"version": "2.2.21", | ||
"version": "2.3.0", | ||
"description": "Vue plugin for work with LocalStorage from Vue context", | ||
@@ -5,0 +5,0 @@ "main": "dist/vue-ls.js", |
@@ -1,3 +0,10 @@ | ||
import ls from './localStorage'; | ||
import Memory from './memory'; | ||
import Storage from './storage'; | ||
const store = typeof window !== 'undefined' && 'localStorage' in window | ||
? window.localStorage | ||
: Memory | ||
; | ||
const ls = new Storage(store); | ||
const VueLocalStorage = { | ||
@@ -4,0 +11,0 @@ /** |
@@ -1,37 +0,7 @@ | ||
const eventListeners = {}; | ||
import StorageEvent from './event'; | ||
/** | ||
* Event callback | ||
* | ||
* @param {Object} event | ||
*/ | ||
function change(event) { | ||
const e = event || window.event; | ||
const emit = (listener) => { | ||
listener( | ||
e.newValue | ||
? JSON.parse(e.newValue).value | ||
: e.newValue, | ||
e.oldValue | ||
? JSON.parse(e.oldValue).value | ||
: e.oldValue | ||
, e.url || e.uri); | ||
}; | ||
if (typeof e === 'undefined' || typeof e.key === 'undefined') { | ||
return; | ||
} | ||
const all = eventListeners[e.key]; | ||
if (typeof all !== 'undefined') { | ||
all.forEach(emit); | ||
} | ||
} | ||
/** | ||
* Storage Bridge | ||
*/ | ||
class Storage { | ||
export default class { | ||
/** | ||
@@ -61,7 +31,7 @@ * @param {Object} storage | ||
if (window.addEventListener) { | ||
window.addEventListener(this.options.events[i], change, false); | ||
window.addEventListener(this.options.events[i], StorageEvent.emit, false); | ||
} else if (window.attachEvent) { | ||
window.attachEvent(`on${this.options.events[i]}`, change); | ||
window.attachEvent(`on${this.options.events[i]}`, StorageEvent.emit); | ||
} else { | ||
window[`on${this.options.events[i]}`] = change; | ||
window[`on${this.options.events[i]}`] = StorageEvent.emit; | ||
} | ||
@@ -179,7 +149,3 @@ } | ||
on(name, callback) { | ||
if (eventListeners[this.options.namespace + name]) { | ||
eventListeners[this.options.namespace + name].push(callback); | ||
} else { | ||
eventListeners[this.options.namespace + name] = [callback]; | ||
} | ||
StorageEvent.on(this.options.namespace + name, callback); | ||
} | ||
@@ -194,15 +160,4 @@ | ||
off(name, callback) { | ||
const ns = eventListeners[this.options.namespace + name]; | ||
if (ns.length > 1) { | ||
ns.splice(ns.indexOf(callback), 1); | ||
} else { | ||
eventListeners[this.options.namespace + name] = []; | ||
} | ||
StorageEvent.off(this.options.namespace + name, callback); | ||
} | ||
} | ||
export { | ||
Storage, | ||
change, | ||
}; |
30984
637