/
web-store.min.js
executable file
·1 lines (1 loc) · 4.22 KB
/
web-store.min.js
1
!function(){"use strict";var t=function(){function t(t,e,o){console.assert(t instanceof Storage,"storageObject should be Storage"),console.assert("string"==typeof e,"storageId should be string"),o&&console.assert("function"==typeof o,"dataChanged should be function"),this.storage=t,this.id=e,this.dataChanged=o}return t.prototype._setItem=function(t,e){console.assert("string"==typeof t,"key should be string"),this.storage.setItem(t,JSON.stringify(e))},t.prototype._getItem=function(t){console.assert("string"==typeof t,"key should be string");var e=this.storage.getItem(t);return e?JSON.parse(e):null},t.prototype._removeItem=function(t){console.assert("string"==typeof t,"key should be string"),this.storage.removeItem(t)},t.prototype._getRecordCounterKey=function(){return this.id+"-counter"},t.prototype._getRecordCounterValue=function(){var t=this._getItem(this._getRecordCounterKey());return t?parseInt(t,10):0},t.prototype._incRecordCounterValue=function(){var t=this._getRecordCounterValue()+1;return this._setItem(this._getRecordCounterKey(),t),t},t.prototype._getSizeKey=function(){return this.id+"-size"},t.prototype.getSize=function(){var t=this._getItem(this._getSizeKey());return t?parseInt(t,10):0},t.prototype._incSize=function(){var t=this.getSize()+1;return this._setItem(this._getSizeKey(),t),t},t.prototype._decSize=function(){var t=this.getSize()-1;return this._setItem(this._getSizeKey(),t),t},t.prototype.getAllRecordIds=function(){return this._getItem(this.id)||[]},t.prototype._addRecordId=function(t){console.assert("string"==typeof t,"recordId should be string");var e=this.getAllRecordIds();e.push(t),this._setItem(this.id,e)},t.prototype._removeRecordId=function(t){console.assert("string"==typeof t,"recordId should be string");var e=this.getAllRecordIds(),o=e.indexOf(t);o>-1&&(e.splice(o,1),this._setItem(this.id,e))},t.prototype._getRecordKeyById=function(t){return console.assert("string"==typeof t,"recordId should be string"),this.id+"-"+t},t.prototype.createRecord=function(t){console.assert("object"==typeof t,"record should be object");var e=JSON.parse(JSON.stringify(t));return e.id=this._incRecordCounterValue().toString(),this._setItem(this._getRecordKeyById(e.id),e),this._addRecordId(e.id),this._incSize(),this.dataChanged&&this.dataChanged(),e},t.prototype.updateRecord=function(t){this._setItem(this._getRecordKeyById(t.id),t),this.dataChanged&&this.dataChanged()},t.prototype.getRecordById=function(t){return console.assert("string"==typeof t,"recordId should be string"),this._getItem(this._getRecordKeyById(t))||!1},t.prototype.getRecordByIndex=function(t){console.assert("number"==typeof t,"recordIndex should be number");var e=this.getAllRecordIds()[t];return this._getItem(this._getRecordKeyById(e))||!1},t.prototype.forEachRecord=function(t,e){console.assert("function"==typeof t,"callback should be function");var o,r,s=this.getAllRecordIds();for(o=e=void 0!==e?e:0;o<s.length&&(r=this.getRecordById(s[o]),!t.bind(this)(r,o));o+=1);},t.prototype.getAllRecords=function(){var t=[];return this.forEachRecord(function(e){t.push(e)}),t},t.prototype.find=function(t,e,o){var r=-1;return this.forEachRecord(function(o,s){if(o.hasOwnProperty(t)&&JSON.stringify(o[t]).match(e))return r=s,!0},o),r},t.prototype.findBy=function(t,e){console.assert("function"==typeof t,"fn should be function");var o=-1;return this.forEachRecord(function(e,r){if(t(e,r))return o=r,!0},e),o},t.prototype.deleteRecordById=function(t){console.assert("string"==typeof t,"recordId should be string"),this._removeItem(this._getRecordKeyById(t)),this._removeRecordId(t),this._decSize(),this.dataChanged&&this.dataChanged()},t.prototype.deleteRecordByIndex=function(t){console.assert("number"==typeof t,"recordIndex should be number");var e=this.getRecordByIndex(t).id;this.deleteRecordById(e)},t.prototype.getDhtmlxGrid=function(t){var e,o=[];return this.forEachRecord(function(r){e=this.getDhtmlxGridRowByRecordId(r.id,t),o.push(e)}),{rows:o,total_count:o.length}},t.prototype.getDhtmlxGridRowByRecordId=function(t,e){console.assert("string"==typeof t,"recordId should be string");var o=this.getRecordById(t),r=[];return void 0===e&&(e=Object.keys(o)),e.forEach(function(t){o.hasOwnProperty(t)&&r.push(o[t])}),{id:o.id,data:r}},t}();"undefined"==typeof window?module.exports=t:window.WebStore=t}();