diff --git a/bower.json b/bower.json index 48a96e4..5eca481 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "vue-localstorage", - "version": "0.4.2", + "version": "0.5.0", "description": "Vue.js localStorage plugin with types support", "main": "vue-localstorage.js", "authors": [ diff --git a/dist/vue-local-storage.es2015.js b/dist/vue-local-storage.es2015.js index c2445e4..db298c6 100644 --- a/dist/vue-local-storage.es2015.js +++ b/dist/vue-local-storage.es2015.js @@ -1,5 +1,5 @@ /** - * vue-local-storage v0.4.2 + * vue-local-storage v0.5.0 * (c) 2017 Alexander Avakov * @license MIT */ @@ -74,13 +74,16 @@ class VueLocalStorage { * @param {function} type * @param {*} defaultValue */ - addProperty (key, type, defaultValue) { + addProperty (key, type, defaultValue = undefined) { type = type || String; this._properties[key] = { type }; if (!window.localStorage[key] && defaultValue !== null) { - window.localStorage.setItem(key, [Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue); + window.localStorage.setItem( + key, + [Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue + ); } } @@ -148,14 +151,42 @@ var index = { } const name = options.name || 'localStorage'; + const bind = options.bind; Vue.mixin({ - created () { + beforeCreate () { if (this.$options[name]) { Object.keys(this.$options[name]).forEach((key) => { - const [type, defaultValue] = [this.$options[name][key].type, this.$options[name][key].default]; + const config = this.$options[name][key]; + const [type, defaultValue] = [config.type, config.default]; VueLocalStorage$1.addProperty(key, type, defaultValue); + + const existingProp = Object.getOwnPropertyDescriptor(VueLocalStorage$1, key); + + if (!existingProp) { + const prop = { + get: () => Vue.localStorage.get(key, defaultValue), + set: val => Vue.localStorage.set(key, val), + configurable: true + }; + + Object.defineProperty(VueLocalStorage$1, key, prop); + Vue.util.defineReactive(VueLocalStorage$1, key, defaultValue); + } else if (!Vue.config.silent) { + console.log(`${key}: is already defined and will be reused`); + } + + if ((bind || config.bind) && config.bind !== false) { + this.$options.computed = this.$options.computed || {}; + + if (!this.$options.computed[key]) { + this.$options.computed[key] = { + get: () => Vue.localStorage[key], + set: (val) => { Vue.localStorage[key] = val; } + }; + } + } }); } } diff --git a/dist/vue-local-storage.js b/dist/vue-local-storage.js index 23f8bff..17f7b87 100644 --- a/dist/vue-local-storage.js +++ b/dist/vue-local-storage.js @@ -1,5 +1,5 @@ /** - * vue-local-storage v0.4.2 + * vue-local-storage v0.5.0 * (c) 2017 Alexander Avakov * @license MIT */ @@ -82,12 +82,17 @@ VueLocalStorage.prototype.remove = function remove (lsKey) { * @param {*} defaultValue */ VueLocalStorage.prototype.addProperty = function addProperty (key, type, defaultValue) { + if ( defaultValue === void 0 ) defaultValue = undefined; + type = type || String; this._properties[key] = { type: type }; if (!window.localStorage[key] && defaultValue !== null) { - window.localStorage.setItem(key, [Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue); + window.localStorage.setItem( + key, + [Array, Object].includes(type) ? JSON.stringify(defaultValue) : defaultValue + ); } }; @@ -156,18 +161,46 @@ var index = { } var name = options.name || 'localStorage'; + var bind = options.bind; Vue.mixin({ - created: function created () { + beforeCreate: function beforeCreate () { var this$1 = this; if (this.$options[name]) { Object.keys(this.$options[name]).forEach(function (key) { - var ref = [this$1.$options[name][key].type, this$1.$options[name][key].default]; + var config = this$1.$options[name][key]; + var ref = [config.type, config.default]; var type = ref[0]; var defaultValue = ref[1]; VueLocalStorage$1.addProperty(key, type, defaultValue); + + var existingProp = Object.getOwnPropertyDescriptor(VueLocalStorage$1, key); + + if (!existingProp) { + var prop = { + get: function () { return Vue.localStorage.get(key, defaultValue); }, + set: function (val) { return Vue.localStorage.set(key, val); }, + configurable: true + }; + + Object.defineProperty(VueLocalStorage$1, key, prop); + Vue.util.defineReactive(VueLocalStorage$1, key, defaultValue); + } else if (!Vue.config.silent) { + console.log((key + ": is already defined and will be reused")); + } + + if ((bind || config.bind) && config.bind !== false) { + this$1.$options.computed = this$1.$options.computed || {}; + + if (!this$1.$options.computed[key]) { + this$1.$options.computed[key] = { + get: function () { return Vue.localStorage[key]; }, + set: function (val) { Vue.localStorage[key] = val; } + }; + } + } }); } } diff --git a/dist/vue-local-storage.min.js b/dist/vue-local-storage.min.js index 9287c31..f66f42a 100644 --- a/dist/vue-local-storage.min.js +++ b/dist/vue-local-storage.min.js @@ -1 +1 @@ -!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):e.VueLocalStorage=r()}(this,function(){"use strict";var e=function(){this._properties={}};e.prototype.get=function(e,r){var t=this;if(void 0===r&&(r=null),window.localStorage[e]){var o=String;for(var n in t._properties)if(n===e){o=t._properties[n].type;break}return this._process(o,window.localStorage[e])}return null!==r?r:null},e.prototype.set=function(e,r){var t=this;for(var o in t._properties){var n=t._properties[o].type;if(o===e&&[Array,Object].includes(n))return window.localStorage.setItem(e,JSON.stringify(r)),r}return window.localStorage.setItem(e,r),r},e.prototype.remove=function(e){return window.localStorage.removeItem(e)},e.prototype.addProperty=function(e,r,t){r=r||String,this._properties[e]={type:r},window.localStorage[e]||null===t||window.localStorage.setItem(e,[Array,Object].includes(r)?JSON.stringify(t):t)},e.prototype._process=function(e,r){switch(e){case Boolean:return"true"===r;case Number:return parseInt(r,10);case Array:try{var t=JSON.parse(r);return Array.isArray(t)?t:[]}catch(e){return[]}case Object:try{return JSON.parse(r)}catch(e){return{}}default:return r}};var r=new e;return{install:function(e,t){if(void 0===t&&(t={}),"undefined"==typeof process||!(process.server||process.SERVER_BUILD||process.env&&"server"===process.env.VUE_ENV)){try{var o="__vue-localstorage-test__";window.localStorage.setItem(o,o),window.localStorage.removeItem(o)}catch(e){console.error("Local storage is not supported")}var n=t.name||"localStorage";e.mixin({created:function(){var e=this;this.$options[n]&&Object.keys(this.$options[n]).forEach(function(t){var o=[e.$options[n][t].type,e.$options[n][t].default],i=o[0],a=o[1];r.addProperty(t,i,a)})}}),e[n]=r,e.prototype["$"+n]=r}}}}); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.VueLocalStorage=t()}(this,function(){"use strict";var e=function(){this._properties={}};e.prototype.get=function(e,t){var r=this;if(void 0===t&&(t=null),window.localStorage[e]){var o=String;for(var n in r._properties)if(n===e){o=r._properties[n].type;break}return this._process(o,window.localStorage[e])}return null!==t?t:null},e.prototype.set=function(e,t){var r=this;for(var o in r._properties){var n=r._properties[o].type;if(o===e&&[Array,Object].includes(n))return window.localStorage.setItem(e,JSON.stringify(t)),t}return window.localStorage.setItem(e,t),t},e.prototype.remove=function(e){return window.localStorage.removeItem(e)},e.prototype.addProperty=function(e,t,r){void 0===r&&(r=void 0),t=t||String,this._properties[e]={type:t},window.localStorage[e]||null===r||window.localStorage.setItem(e,[Array,Object].includes(t)?JSON.stringify(r):r)},e.prototype._process=function(e,t){switch(e){case Boolean:return"true"===t;case Number:return parseInt(t,10);case Array:try{var r=JSON.parse(t);return Array.isArray(r)?r:[]}catch(e){return[]}case Object:try{return JSON.parse(t)}catch(e){return{}}default:return t}};var t=new e;return{install:function(e,r){if(void 0===r&&(r={}),"undefined"==typeof process||!(process.server||process.SERVER_BUILD||process.env&&"server"===process.env.VUE_ENV)){try{var o="__vue-localstorage-test__";window.localStorage.setItem(o,o),window.localStorage.removeItem(o)}catch(e){console.error("Local storage is not supported")}var n=r.name||"localStorage",i=r.bind;e.mixin({beforeCreate:function(){var r=this;this.$options[n]&&Object.keys(this.$options[n]).forEach(function(o){var a=r.$options[n][o],s=[a.type,a.default],c=s[0],p=s[1];if(t.addProperty(o,c,p),Object.getOwnPropertyDescriptor(t,o))e.config.silent||console.log(o+": is already defined and will be reused");else{var l={get:function(){return e.localStorage.get(o,p)},set:function(t){return e.localStorage.set(o,t)},configurable:!0};Object.defineProperty(t,o,l),e.util.defineReactive(t,o,p)}(i||a.bind)&&!1!==a.bind&&(r.$options.computed=r.$options.computed||{},r.$options.computed[o]||(r.$options.computed[o]={get:function(){return e.localStorage[o]},set:function(t){e.localStorage[o]=t}}))})}}),e[n]=t,e.prototype["$"+n]=t}}}}); diff --git a/package.json b/package.json index 1a911b5..18767ee 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vue-localstorage", "description": "Vue.js localStorage plugin with types support", - "version": "0.4.2", + "version": "0.5.0", "author": { "name": "Alexander Avakov", "email": "yaavakov@gmail.com"