@dwightbcoder/action-ui
Advanced tools
Comparing version 1.2.4 to 1.2.5
@@ -40,5 +40,5 @@ "use strict";function _get(){return _get="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(a,b,c){var d=_superPropBase(a,b);if(d){var e=Object.getOwnPropertyDescriptor(d,b);return e.get?e.get.call(3>arguments.length?a:c):e.value}},_get.apply(this,arguments)}function _superPropBase(a,b){for(;!Object.prototype.hasOwnProperty.call(a,b)&&(a=_getPrototypeOf(a),null!==a););return a}function asyncGeneratorStep(a,b,c,d,e,f,g){try{var h=a[f](g),i=h.value}catch(a){return void c(a)}h.done?b(i):Promise.resolve(i).then(d,e)}function _asyncToGenerator(a){return function(){var b=this,c=arguments;return new Promise(function(d,e){function f(a){asyncGeneratorStep(h,d,e,f,g,"next",a)}function g(a){asyncGeneratorStep(h,d,e,f,g,"throw",a)}var h=a.apply(b,c);f(void 0)})}}function _typeof(a){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(a){return typeof a}:function(a){return a&&"function"==typeof Symbol&&a.constructor===Symbol&&a!==Symbol.prototype?"symbol":typeof a},_typeof(a)}function _createForOfIteratorHelper(a,b){var c="undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(!c){if(Array.isArray(a)||(c=_unsupportedIterableToArray(a))||b&&a&&"number"==typeof a.length){c&&(a=c);var d=0,e=function(){};return{s:e,n:function n(){return d>=a.length?{done:!0}:{done:!1,value:a[d++]}},e:function e(a){throw a},f:e}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var f,g=!0,h=!1;return{s:function s(){c=c.call(a)},n:function n(){var a=c.next();return g=a.done,a},e:function e(a){h=!0,f=a},f:function f(){try{g||null==c.return||c.return()}finally{if(h)throw f}}}}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}function _inherits(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function");a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,writable:!0,configurable:!0}}),b&&_setPrototypeOf(a,b)}function _createSuper(a){var b=_isNativeReflectConstruct();return function(){var c,d=_getPrototypeOf(a);if(b){var e=_getPrototypeOf(this).constructor;c=Reflect.construct(d,arguments,e)}else c=d.apply(this,arguments);return _possibleConstructorReturn(this,c)}}function _possibleConstructorReturn(a,b){if(b&&("object"===_typeof(b)||"function"==typeof b))return b;if(void 0!==b)throw new TypeError("Derived constructors may only return object or undefined");return _assertThisInitialized(a)}function _assertThisInitialized(a){if(void 0===a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return a}function _wrapNativeSuper(a){var b="function"==typeof Map?new Map:void 0;return _wrapNativeSuper=function(a){function c(){return _construct(a,arguments,_getPrototypeOf(this).constructor)}if(null===a||!_isNativeFunction(a))return a;if("function"!=typeof a)throw new TypeError("Super expression must either be null or a function");if("undefined"!=typeof b){if(b.has(a))return b.get(a);b.set(a,c)}return c.prototype=Object.create(a.prototype,{constructor:{value:c,enumerable:!1,writable:!0,configurable:!0}}),_setPrototypeOf(c,a)},_wrapNativeSuper(a)}function _construct(){return _construct=_isNativeReflectConstruct()?Reflect.construct:function(b,c,d){var e=[null];e.push.apply(e,c);var a=Function.bind.apply(b,e),f=new a;return d&&_setPrototypeOf(f,d.prototype),f},_construct.apply(null,arguments)}function _isNativeReflectConstruct(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(a){return!1}}function _isNativeFunction(a){return-1!==Function.toString.call(a).indexOf("[native code]")}function _setPrototypeOf(a,b){return _setPrototypeOf=Object.setPrototypeOf||function(a,b){return a.__proto__=b,a},_setPrototypeOf(a,b)}function _getPrototypeOf(a){return _getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(a){return a.__proto__||Object.getPrototypeOf(a)},_getPrototypeOf(a)}function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}var ActionUI=function(a){'use strict';/** | ||
if(_classCallCheck(this,a),this.options={baseUrl:null,types:null,keys:{data:null,type:"type",id:"id"},keysExcludeFromCache:[],per_page:0,query:{"page[number]":"page[number]","page[size]":"page[size]"},fetch:{method:"GET",headers:{"Content-Type":"application/json"},mode:"no-cors"},searchDepth:1,triggerChangesOnError:!0,// Allows views to update contents on failed requests, especially useful for Fetch All requests which return no results or 404 | ||
verbose:!1,viewClass:null,viewMap:{},actionVerb:{get:"fetch",post:"create",patch:"update",delete:"delete"},actionHandler:{get:this.actionHandler,post:this.actionHandler,patch:this.actionHandler,delete:this.actionHandler},eventBefore:new CustomEvent("store.before",{bubbles:!0,cancelable:!0,detail:{type:"before",name:null,fetch:null,data:null,model:null,view:null}}),eventAfter:new CustomEvent("store.after",{bubbles:!0,detail:{type:"after",name:null,fetch:null,data:null,model:null,view:null,success:null,response:null}})},b(this.options,c||{}),this._model=new k,this._urlCache={},this._model._paging={},this.options.types instanceof Array){var d={};// Turn them into seperate stores | ||
for(var e in this.options.types)d[this.options.types[e]]=this.options.types[e];this.options.types=d}if(this.options.types instanceof Object)for(var f in this.options.types)this.modelCreate(this.options.types[f]);a.cache(this)}return _createClass(a,[{key:"body",value:function body(a,b){return JSON.stringify(b)}},{key:"model",value:function model(a,b){return b?this._model[a][b]:this._model[a]}},{key:"modelCreate",value:function modelCreate(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null;if("string"!=typeof a)throw new Error("Store: Cannot create model without `type`");this._model[a]||(this._model[a]=new k({_type:a,_loading:!1},{model:this._model,property:a}),this.actionCreate(a,"get"),this.actionCreate(a,"post"),this.actionCreate(a,"patch"),this.actionCreate(a,"delete"),this.options.viewClass&&this.options.viewMap.hasOwnProperty(a)&&new this.options.viewClass(this.options.viewMap[a],this._model[a])),null==b||"string"!=typeof b&&"number"!=typeof b||this._model[a][b]||(this._model[a][b]=new k({_type:a},{model:this._model[a],property:b}))}},{key:"actionCreate",value:function actionCreate(a,b){b=b.toLowerCase();var c=a+" "+this.options.actionVerb[b],d=this.options.actionHandler[b].bind(this,a,b),e=new l(c,d);return l.cache(e),e}},{key:"actionHandler",value:function actionHandler(a,b,c,d,e){var f=null;return"get"===b?f=this.fetch(a,e.id):"post"===b?f=this.post(a,e):"patch"===b?f=this.patch(a,e):"delete"===b?f=this.delete(a,e.id):void 0,f.then(function(a){return c(a)}).catch(function(a){return d(a)})}},{key:"data",value:function data(a){return this.options.keys.data?a[this.options.keys.data]:a}},{key:"type",value:function(a){var b=a[this.options.keys.type];return this.options.types[b]&&(b=this.options.types[b]),b}},{key:"id",value:function id(a){return a[this.options.keys.id]}},{key:"sync",value:function sync(a,b){var c=!!(2<arguments.length&&void 0!==arguments[2])&&arguments[2],d=this.data(a);if(!d)throw new Error("Store: No data to sync");if(d instanceof Array){var g={};for(var h in d){var i=null;this.options.keys.data?(i={},i[this.options.keys.data]=d[h]):i=d[h],g[this.id(d[h])]=this.sync(i,b)}return c||this.syncPaging(a,b),g}var e=this.type(d),f=this.id(d);if(this._model[e]||this.modelCreate(e),this._model[e][f]||this.modelCreate(e,f),!this._model[e][f])throw new Error("Store: No model for type: "+e+", id: "+f);return this._model[e][f].sync(d),b&&(!this._model[e][f].hasOwnProperty("_store")&&(this._model[e][f]._store={url:[]}),-1==this._model[e][f]._store.url.indexOf(b)&&this._model[e][f]._store.url.push(b),b=this.url({type:e,id:f}),-1==this._model[e][f]._store.url.indexOf(b)&&this._model[e][f]._store.url.push(b)),this._model[e][f]}},{key:"syncPaging",value:function syncPaging(a,b){var d=this.pageData(b);// Paging | ||
if(d.type&&d.pageSize&&d.pageNumber){var j=this.data(a);if(Array.isArray(j)){var e,f=this.model(d.type),g=[],h=c({},d),i=_createForOfIteratorHelper(j);try{for(i.s();!(e=i.n()).done;){var l=e.value;g.push({id:l[this.options.keys.id],type:l[this.options.keys.type]})}}catch(a){i.e(a)}finally{i.f()}h.data=g,h.url=[b],f._paging||(f._paging=new k({current:null,type:d.type,pageNumber:d.pageNumber,pageSize:d.pageSize},{model:f,property:"_paging"})),f._paging[d.pageSize]||(f._paging[d.pageSize]=new k({},{model:f._paging,property:d.pageSize})),f._paging[d.pageSize][d.pageNumber]||(f._paging[d.pageSize][d.pageNumber]=new k({},{model:f._paging[d.pageSize],property:d.pageNumber})),f._paging[d.pageSize][d.pageNumber].sync(h)}return this.pageChange(d.type,d.pageNumber,d.pageSize)}}},{key:"url",value:function(a){var b=this.options.types[a.type]||a.type,c=this.options.baseUrl+"/"+b+"/"+(a.id?a.id+"/":""),d=new URLSearchParams;for(var f in a)f in this.options.query?d.set(this.options.query[f],a[f]):"type"!=f&&"id"!=f&&d.set(f,a[f]);d.sort();var e=d.toString();return e&&(c+="?"+e),c}},{key:"urlParse",value:function urlParse(a){if(!a)return{url:null,type:null,pageNumber:!1,pageSize:!1};-1==a.indexOf("://")&&(0<a.indexOf("/")&&(a="/"+a),a=location.origin+a);var b=new URL(a),c=b.pathname.replace(this.options.baseUrl,"").split("/"),d=c[0]||c[1];return b.searchParams.sort(),{url:b,type:d,pageNumber:parseInt(b.searchParams.get(this.options.query["page[number]"])),pageSize:parseInt(b.searchParams.get(this.options.query["page[size]"]))}}},{key:"search",value:function search(a){var b=new k;for(var e in this._model)if(!(0==e.indexOf("_")||a.type&&this.type({type:a.type})!=e))for(var f in this._model[e]){var c=!1,d=this._model[e][f];for(var g in a)c=this.propertyValueExists(d,g,a[g],this.options.searchDepth);c&&(b[f]=d)}return b}},{key:"propertyValueExists",value:function propertyValueExists(a,b,c){var d=3<arguments.length&&void 0!==arguments[3]?arguments[3]:1,e=Object.keys(a);if(-1<e.indexOf(b))return a[b]==c;if(1>=d)return!1;for(var f,g=0,h=e;g<h.length;g++)if(f=h[g],"object"==_typeof(a[f]))return this.propertyValueExists(a[f],b,c,d-1)}},{key:"fetch",value:function(){var a=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function c(a,b){var d,e,f=arguments;return regeneratorRuntime.wrap(function(c){for(;;)switch(c.prev=c.next){case 0:return d=2<f.length&&void 0!==f[2]?f[2]:{},a=this.type({type:a}),d=d||{},b=b||void 0,"object"==_typeof(b)&&(d=b,b=d.id),e=this.url(Object.assign({},d,{type:a,id:b})),this.options.verbose&&console.info("Store.fetch()",a,b,{type:a,id:b,query:d,store:this,url:e,options:this.options.fetch}),c.abrupt("return",this.fetchUrl(e,a,d));case 8:case"end":return c.stop();}},c,this)}));return function fetch(){return a.apply(this,arguments)}}()},{key:"fetchUrl",value:function(){var a=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function c(a,b){var d,e,f,g,h,i,j,k=arguments;return regeneratorRuntime.wrap(function(c){for(;;)switch(c.prev=c.next){case 0:if(d=2<k.length&&void 0!==k[2]?k[2]:{},a){c.next=3;break}return c.abrupt("return",Promise.reject());case 3:if(c.prev=3,d=d||{},d.type=b,e=this.urlParse(a).url.toString(),f=this.urlCache(e),b=d.type||(f?f.type:null)||e.type||null,!f){c.next=12;break}return this.pageChange(f.type,f.pageNumber,f.pageSize),c.abrupt("return",Promise.resolve(f.model));case 12:return this.before(b,this.options.fetch,d),c.next=15,fetch(a,this.options.fetch);case 15:return g=c.sent,this.after(b,this.options.fetch,d,!!g&&g.ok,g),c.next=19,g.json();case 19:return h=c.sent,i=g.ok?h:Promise.reject(h),c.prev=21,j=this.sync(i,a),f=this.urlCache(e,j,i),c.abrupt("return",j);case 27:return c.prev=27,c.t0=c["catch"](21),c.abrupt("return",Promise.reject(h));case 30:c.next=38;break;case 32:return c.prev=32,c.t1=c["catch"](3),b&&this.options.triggerChangesOnError&&this._model[b]&&this.model(b).triggerChanges(),c.next=37,Promise.reject(c.t1);case 37:return c.abrupt("return",c.sent);case 38:case"end":return c.stop();}},c,this,[[3,32],[21,27]])}));return function fetchUrl(){return a.apply(this,arguments)}}()},{key:"urlCache",value:function urlCache(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,c=2<arguments.length&&void 0!==arguments[2]?arguments[2]:null;if(a=decodeURIComponent(a),!b&&!c)return this._urlCache[a];var d=this.pageData(a),e=b[this.options.keys.type]||b._type||(d?d.type:null);if(c){var f,g=_createForOfIteratorHelper(this.options.keysExcludeFromCache);try{for(g.s();!(f=g.n()).done;){var h=f.value;delete c[h]}}catch(a){g.e(a)}finally{g.f()}}return this._urlCache[a]={url:a,type:e,model:b,json:c,pageNumber:!!d&&d.pageNumber,pageSize:!!d&&d.pageSize}}},{key:"pageData",value:function pageData(a){if(!a)return{type:null,pageNumber:!1,pageSize:!1};var b=this.urlParse(a),c=this._urlCache[a];return{type:b.type||(c?c.type:null),pageNumber:b.pageNumber||!!c&&c.pageNumber,pageSize:b.pageSize||!!c&&c.pageSize}}},{key:"paging",value:function paging(a){return this.model(a)._paging||{current:!1,pageNumber:!1,pageSize:!1}}},{key:"page",value:function(){var a=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function b(a){var c,d,e,f=arguments;return regeneratorRuntime.wrap(function(b){for(;;)switch(b.prev=b.next){case 0:return c=1<f.length&&void 0!==f[1]?f[1]:1,d=2<f.length&&void 0!==f[2]?f[2]:0,e=3<f.length&&void 0!==f[3]?f[3]:{},e=e||{},d=parseInt(d)||this.options.per_page,c=parseInt(c)||1,e.type=a,e[this.options.query["page[number]"]]=c,e[this.options.query["page[size]"]]=d,b.next=11,this.fetch(a,0,e);case 11:return b.abrupt("return",b.sent);case 12:case"end":return b.stop();}},b,this)}));return function page(){return a.apply(this,arguments)}}()},{key:"pageChange",value:function pageChange(a,b,c){return!!(a&&this._model[a]._paging&&this._model[a]._paging[c][b])&&(this._model[a]._paging.pageNumber=b,this._model[a]._paging.pageSize=c,delete this._model[a]._paging.current,this._model[a]._paging.current=this._model[a]._paging[c][b],this._model[a]._paging)}},{key:"post",value:function post(a,b){var c=this;a=a||this.type(b);var d=this.url({type:a,id:this.id(b)}),e=Object.create(this.options.fetch);return e.method="POST",e.body=this.body(a,b),this.options.verbose&&console.info("Store.post()",a,{type:a,data:b,store:this,url:d,options:e}),fetch(d,e).then(function(a){return a.json().then(function(b){return a.ok?b:Promise.reject(b)})}).then(function(a){return c.sync(a,d)}).catch(function(b){return c.options.triggerChangesOnError&&c.model(a).triggerChanges(),Promise.reject(b)})}},{key:"patch",value:function patch(a,b){var d=this;a=a||this.type(b);var e=this.url({type:a,id:this.id(b)}),f={};return c(f,this.options.fetch),f.method="PATCH",f.body=this.body(a,b),this.options.verbose&&console.info("Store.patch()",a,{type:a,data:b,store:this,url:e,options:f}),fetch(e,f).then(function(a){return a.json().then(function(b){return a.ok?b:Promise.reject(b)})}).then(function(a){return d.sync(a,e)}).catch(function(b){return d.options.triggerChangesOnError&&d.model(a).triggerChanges(),Promise.reject(b)})}},{key:"delete",value:function _delete(a,b){var c=this,d=Object.create(this.options.fetch);d.method="DELETE";var e=this.url({type:a,id:b});return this.options.verbose&&console.info("Store.delete()",a,b,{type:a,id:b,store:this,url:e,options:d}),fetch(e,d).then(function(a){return a.json().then(function(b){return a.ok?b:Promise.reject(b)})}).then(function(d){return delete c._model[a][b],d}).catch(function(b){return c.options.triggerChangesOnError&&c.model(a).triggerChanges(),Promise.reject(b)})}},{key:"loading",value:function loading(a,b){return 1==arguments.length?this.model(a)._loading:void(this.model(a)._loading=!!b)}},{key:"before",value:function before(a,b,c){var d=this,e=this.options.baseUrl+"/"+a,f=null;this.loading(a,!0),this.options.viewClass&&this.options.viewMap.hasOwnProperty(a)&&(f=this.options.viewClass.cache(this.options.viewMap[a])),this.options.verbose&&console.info("Store.before()",e,{store:this,type:a,fetch:b,data:c,view:f,model:this.model(a)}),f&&document.querySelectorAll("[ui-view=\""+f.name+"\"]").forEach(function(a){d.options.viewClass.setCssClass(a,d.options.viewClass.options.cssClass.loading)});var g=new CustomEvent(this.options.eventBefore.type,this.options.eventBefore);return Object.assign(g.detail,{name:e,fetch:b,store:this,data:c,model:this.model(a),view:f}),document.dispatchEvent(g)}},{key:"after",value:function after(a,b,c,d,e){var f=this,g=this.options.baseUrl+"/"+a,h=null;this.loading(a,!1),this.options.viewClass&&this.options.viewMap.hasOwnProperty(a)&&(h=this.options.viewClass.cache(this.options.viewMap[a])),this.options.verbose&&console.info("Store.after()",g,{store:this,type:a,fetch:b,data:c,view:h,model:this.model(a),success:d,response:e}),h&&document.querySelectorAll("[ui-view=\""+h.name+"\"]").forEach(function(a){f.options.viewClass.setCssClass(a,d?f.options.viewClass.options.cssClass.success:f.options.viewClass.options.cssClass.fail)});var i=new CustomEvent(this.options.eventAfter.type,this.options.eventAfter);return Object.assign(i.detail,{name:g,success:d,fetch:b,store:this,data:c,model:this.model(a),view:h,response:e}),document.dispatchEvent(i)}}],[{key:"cache",value:function cache(a){if(null==a)return s;if("string"==typeof a){if(a=a.toLowerCase(),null!=s[a])return s[a];// Find a store that starts with the requested url to match ('/api/v1/entity' with a '/api/v1' store) | ||
triggerChangesOnEmpty:!0,verbose:!1,viewClass:null,viewMap:{},actionVerb:{get:"fetch",post:"create",patch:"update",delete:"delete"},actionHandler:{get:this.actionHandler,post:this.actionHandler,patch:this.actionHandler,delete:this.actionHandler},eventBefore:new CustomEvent("store.before",{bubbles:!0,cancelable:!0,detail:{type:"before",name:null,fetch:null,data:null,model:null,view:null}}),eventAfter:new CustomEvent("store.after",{bubbles:!0,detail:{type:"after",name:null,fetch:null,data:null,model:null,view:null,success:null,response:null}})},b(this.options,c||{}),this._model=new k,this._urlCache={},this._model._paging={},this.options.types instanceof Array){var d={};// Turn them into seperate stores | ||
for(var e in this.options.types)d[this.options.types[e]]=this.options.types[e];this.options.types=d}if(this.options.types instanceof Object)for(var f in this.options.types)this.modelCreate(this.options.types[f]);a.cache(this)}return _createClass(a,[{key:"body",value:function body(a,b){return JSON.stringify(b)}},{key:"model",value:function model(a,b){return b?this._model[a][b]:this._model[a]}},{key:"modelCreate",value:function modelCreate(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null;if("string"!=typeof a)throw new Error("Store: Cannot create model without `type`");this._model[a]||(this._model[a]=new k({_type:a,_loading:!1},{model:this._model,property:a}),this.actionCreate(a,"get"),this.actionCreate(a,"post"),this.actionCreate(a,"patch"),this.actionCreate(a,"delete"),this.options.viewClass&&this.options.viewMap.hasOwnProperty(a)&&new this.options.viewClass(this.options.viewMap[a],this._model[a])),null==b||"string"!=typeof b&&"number"!=typeof b||this._model[a][b]||(this._model[a][b]=new k({_type:a},{model:this._model[a],property:b}))}},{key:"actionCreate",value:function actionCreate(a,b){b=b.toLowerCase();var c=a+" "+this.options.actionVerb[b],d=this.options.actionHandler[b].bind(this,a,b),e=new l(c,d);return l.cache(e),e}},{key:"actionHandler",value:function actionHandler(a,b,c,d,e){var f=null;return"get"===b?f=this.fetch(a,e.id):"post"===b?f=this.post(a,e):"patch"===b?f=this.patch(a,e):"delete"===b?f=this.delete(a,e.id):void 0,f.then(function(a){return c(a)}).catch(function(a){return d(a)})}},{key:"data",value:function data(a){return this.options.keys.data?a[this.options.keys.data]:a}},{key:"type",value:function(a){var b=a[this.options.keys.type];return this.options.types[b]&&(b=this.options.types[b]),b}},{key:"id",value:function id(a){return a[this.options.keys.id]}},{key:"sync",value:function sync(a,b){var c=!!(2<arguments.length&&void 0!==arguments[2])&&arguments[2],d=this.data(a);if(!d||0==Object.keys(d).length)throw new Error("Store: No data to sync");if(d instanceof Array){var g={};for(var h in d){var i=null;this.options.keys.data?(i={},i[this.options.keys.data]=d[h]):i=d[h],g[this.id(d[h])]=this.sync(i,b)}return c||this.syncPaging(a,b),g}var e=this.type(d),f=this.id(d);if(this._model[e]||this.modelCreate(e),this._model[e][f]||this.modelCreate(e,f),!this._model[e][f])throw new Error("Store: No model for type: "+e+", id: "+f);return this._model[e][f].sync(d),b&&(!this._model[e][f].hasOwnProperty("_store")&&(this._model[e][f]._store={url:[]}),-1==this._model[e][f]._store.url.indexOf(b)&&this._model[e][f]._store.url.push(b),b=this.url({type:e,id:f}),-1==this._model[e][f]._store.url.indexOf(b)&&this._model[e][f]._store.url.push(b)),this._model[e][f]}},{key:"syncPaging",value:function syncPaging(a,b){var d=this.pageData(b);// Paging | ||
if(d.type&&d.pageSize&&d.pageNumber){var j=this.data(a);if(Array.isArray(j)){var e,f=this.model(d.type),g=[],h=c({},d),i=_createForOfIteratorHelper(j);try{for(i.s();!(e=i.n()).done;){var l=e.value;g.push({id:l[this.options.keys.id],type:l[this.options.keys.type]})}}catch(a){i.e(a)}finally{i.f()}h.data=g,h.url=[b],f._paging||(f._paging=new k({current:null,type:d.type,pageNumber:d.pageNumber,pageSize:d.pageSize},{model:f,property:"_paging"})),f._paging[d.pageSize]||(f._paging[d.pageSize]=new k({},{model:f._paging,property:d.pageSize})),f._paging[d.pageSize][d.pageNumber]||(f._paging[d.pageSize][d.pageNumber]=new k({},{model:f._paging[d.pageSize],property:d.pageNumber})),f._paging[d.pageSize][d.pageNumber].sync(h)}return this.pageChange(d.type,d.pageNumber,d.pageSize)}}},{key:"url",value:function(a){var b=this.options.types[a.type]||a.type,c=this.options.baseUrl+"/"+b+"/"+(a.id?a.id+"/":""),d=new URLSearchParams;for(var f in a)f in this.options.query?d.set(this.options.query[f],a[f]):"type"!=f&&"id"!=f&&d.set(f,a[f]);d.sort();var e=d.toString();return e&&(c+="?"+e),c}},{key:"urlParse",value:function urlParse(a){if(!a)return{url:null,type:null,pageNumber:!1,pageSize:!1};-1==a.indexOf("://")&&(0<a.indexOf("/")&&(a="/"+a),a=location.origin+a);var b=new URL(a),c=b.pathname.replace(this.options.baseUrl,"").split("/"),d=c[0]||c[1];return b.searchParams.sort(),{url:b,type:d,pageNumber:parseInt(b.searchParams.get(this.options.query["page[number]"])),pageSize:parseInt(b.searchParams.get(this.options.query["page[size]"]))}}},{key:"search",value:function search(a){var b=new k;for(var e in this._model)if(!(0==e.indexOf("_")||a.type&&this.type({type:a.type})!=e))for(var f in this._model[e]){var c=!1,d=this._model[e][f];for(var g in a)c=this.propertyValueExists(d,g,a[g],this.options.searchDepth);c&&(b[f]=d)}return b}},{key:"propertyValueExists",value:function propertyValueExists(a,b,c){var d=3<arguments.length&&void 0!==arguments[3]?arguments[3]:1,e=Object.keys(a);if(-1<e.indexOf(b))return a[b]==c;if(1>=d)return!1;for(var f,g=0,h=e;g<h.length;g++)if(f=h[g],"object"==_typeof(a[f]))return this.propertyValueExists(a[f],b,c,d-1)}},{key:"fetch",value:function(){var a=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function c(a,b){var d,e,f=arguments;return regeneratorRuntime.wrap(function(c){for(;;)switch(c.prev=c.next){case 0:return d=2<f.length&&void 0!==f[2]?f[2]:{},a=this.type({type:a}),d=d||{},b=b||void 0,"object"==_typeof(b)&&(d=b,b=d.id),e=this.url(Object.assign({},d,{type:a,id:b})),this.options.verbose&&console.info("Store.fetch()",a,b,{type:a,id:b,query:d,store:this,url:e,options:this.options.fetch}),c.abrupt("return",this.fetchUrl(e,a,d));case 8:case"end":return c.stop();}},c,this)}));return function fetch(){return a.apply(this,arguments)}}()},{key:"fetchUrl",value:function(){var a=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function c(a,b){var d,e,f,g,h,i,j=arguments;return regeneratorRuntime.wrap(function(c){for(;;)switch(c.prev=c.next){case 0:if(d=2<j.length&&void 0!==j[2]?j[2]:{},a){c.next=3;break}return c.abrupt("return",Promise.reject());case 3:if(c.prev=3,d=d||{},d.type=b,e=this.urlParse(a).url.toString(),f=this.urlCache(e),b=d.type||(f?f.type:null)||e.type||null,!f){c.next=12;break}return this.pageChange(f.type,f.pageNumber,f.pageSize),c.abrupt("return",Promise.resolve(f.model));case 12:return this.before(b,this.options.fetch,d),c.next=15,fetch(a,this.options.fetch);case 15:return g=c.sent,this.after(b,this.options.fetch,d,!!g&&g.ok,g),c.next=19,g.json();case 19:if(h=c.sent,g.ok){c.next=22;break}return c.abrupt("return",Promise.reject(h));case 22:return c.prev=22,i=this.sync(h,a),this.urlCache(e,i,h),c.abrupt("return",i);case 28:return c.prev=28,c.t0=c["catch"](22),b&&this.options.triggerChangesOnError&&this._model[b]&&this.model(b).triggerChanges(),c.abrupt("return",Promise.reject(h));case 32:c.next=40;break;case 34:return c.prev=34,c.t1=c["catch"](3),b&&this.options.triggerChangesOnError&&this._model[b]&&this.model(b).triggerChanges(),c.next=39,Promise.reject(c.t1);case 39:return c.abrupt("return",c.sent);case 40:case"end":return c.stop();}},c,this,[[3,34],[22,28]])}));return function fetchUrl(){return a.apply(this,arguments)}}()},{key:"urlCache",value:function urlCache(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,c=2<arguments.length&&void 0!==arguments[2]?arguments[2]:null;if(a=decodeURIComponent(a),!b&&!c)return this._urlCache[a];var d=this.pageData(a),e=b[this.options.keys.type]||b._type||(d?d.type:null);if(c){var f,g=_createForOfIteratorHelper(this.options.keysExcludeFromCache);try{for(g.s();!(f=g.n()).done;){var h=f.value;delete c[h]}}catch(a){g.e(a)}finally{g.f()}}return this._urlCache[a]={url:a,type:e,model:b,json:c,pageNumber:!!d&&d.pageNumber,pageSize:!!d&&d.pageSize}}},{key:"pageData",value:function pageData(a){if(!a)return{type:null,pageNumber:!1,pageSize:!1};var b=this.urlParse(a),c=this._urlCache[a];return{type:b.type||(c?c.type:null),pageNumber:b.pageNumber||!!c&&c.pageNumber,pageSize:b.pageSize||!!c&&c.pageSize}}},{key:"paging",value:function paging(a){return this.model(a)._paging||{current:!1,pageNumber:!1,pageSize:!1}}},{key:"page",value:function(){var a=_asyncToGenerator(/*#__PURE__*/regeneratorRuntime.mark(function b(a){var c,d,e,f=arguments;return regeneratorRuntime.wrap(function(b){for(;;)switch(b.prev=b.next){case 0:return c=1<f.length&&void 0!==f[1]?f[1]:1,d=2<f.length&&void 0!==f[2]?f[2]:0,e=3<f.length&&void 0!==f[3]?f[3]:{},e=e||{},d=parseInt(d)||this.options.per_page,c=parseInt(c)||1,e.type=a,e[this.options.query["page[number]"]]=c,e[this.options.query["page[size]"]]=d,b.next=11,this.fetch(a,0,e);case 11:return b.abrupt("return",b.sent);case 12:case"end":return b.stop();}},b,this)}));return function page(){return a.apply(this,arguments)}}()},{key:"pageChange",value:function pageChange(a,b,c){return!!(a&&this._model[a]._paging&&this._model[a]._paging[c][b])&&(this._model[a]._paging.pageNumber=b,this._model[a]._paging.pageSize=c,delete this._model[a]._paging.current,this._model[a]._paging.current=this._model[a]._paging[c][b],this._model[a]._paging)}},{key:"post",value:function post(a,b){var c=this;a=a||this.type(b);var d=this.url({type:a,id:this.id(b)}),e=Object.create(this.options.fetch);return e.method="POST",e.body=this.body(a,b),this.options.verbose&&console.info("Store.post()",a,{type:a,data:b,store:this,url:d,options:e}),fetch(d,e).then(function(a){return a.json().then(function(b){return a.ok?b:Promise.reject(b)})}).then(function(a){return c.sync(a,d)}).catch(function(b){return c.options.triggerChangesOnError&&c.model(a).triggerChanges(),Promise.reject(b)})}},{key:"patch",value:function patch(a,b){var d=this;a=a||this.type(b);var e=this.url({type:a,id:this.id(b)}),f={};return c(f,this.options.fetch),f.method="PATCH",f.body=this.body(a,b),this.options.verbose&&console.info("Store.patch()",a,{type:a,data:b,store:this,url:e,options:f}),fetch(e,f).then(function(a){return a.json().then(function(b){return a.ok?b:Promise.reject(b)})}).then(function(a){return d.sync(a,e)}).catch(function(b){return d.options.triggerChangesOnError&&d.model(a).triggerChanges(),Promise.reject(b)})}},{key:"delete",value:function _delete(a,b){var c=this,d=Object.create(this.options.fetch);d.method="DELETE";var e=this.url({type:a,id:b});return this.options.verbose&&console.info("Store.delete()",a,b,{type:a,id:b,store:this,url:e,options:d}),fetch(e,d).then(function(a){return a.json().then(function(b){return a.ok?b:Promise.reject(b)})}).then(function(d){return delete c._model[a][b],d}).catch(function(b){return c.options.triggerChangesOnError&&c.model(a).triggerChanges(),Promise.reject(b)})}},{key:"loading",value:function loading(a,b){this._model[a]||this.modelCreate(a);var c=this.model(a);return 1==arguments.length?c._loading:void(c._loading=!!b)}},{key:"before",value:function before(a,b,c){var d=this,e=this.options.baseUrl+"/"+a,f=null;this.loading(a,!0),this.options.viewClass&&this.options.viewMap.hasOwnProperty(a)&&(f=this.options.viewClass.cache(this.options.viewMap[a])),this.options.verbose&&console.info("Store.before()",e,{store:this,type:a,fetch:b,data:c,view:f,model:this.model(a)}),f&&document.querySelectorAll("[ui-view=\""+f.name+"\"]").forEach(function(a){d.options.viewClass.setCssClass(a,d.options.viewClass.options.cssClass.loading)});var g=new CustomEvent(this.options.eventBefore.type,this.options.eventBefore);return Object.assign(g.detail,{name:e,fetch:b,store:this,data:c,model:this.model(a),view:f}),document.dispatchEvent(g)}},{key:"after",value:function after(a,b,c,d,e){var f=this,g=this.options.baseUrl+"/"+a,h=null;this.loading(a,!1),this.options.viewClass&&this.options.viewMap.hasOwnProperty(a)&&(h=this.options.viewClass.cache(this.options.viewMap[a])),this.options.verbose&&console.info("Store.after()",g,{store:this,type:a,fetch:b,data:c,view:h,model:this.model(a),success:d,response:e}),h&&document.querySelectorAll("[ui-view=\""+h.name+"\"]").forEach(function(a){f.options.viewClass.setCssClass(a,d?f.options.viewClass.options.cssClass.success:f.options.viewClass.options.cssClass.fail)});var i=new CustomEvent(this.options.eventAfter.type,this.options.eventAfter);return Object.assign(i.detail,{name:g,success:d,fetch:b,store:this,data:c,model:this.model(a),view:h,response:e}),document.dispatchEvent(i)}}],[{key:"cache",value:function cache(a){if(null==a)return s;if("string"==typeof a){if(a=a.toLowerCase(),null!=s[a])return s[a];// Find a store that starts with the requested url to match ('/api/v1/entity' with a '/api/v1' store) | ||
for(var b in s)if(b.startsWith(a))return s[b];return}var c=a.options.baseUrl.toLowerCase();return c in s?this:(s[c]=a,this)}}]),a}(),s={},t=/*#__PURE__*/function(a){function c(a){_classCallCheck(this,c);var e={keys:{data:"data",type:"type",id:"id",links:"links",meta:"meta",included:"included"},keysExcludeFromCache:["data","included"],query:{"page[number]":"page[number]","page[size]":"page[size]"},per_page:0,fetch:{headers:{"Content-Type":"application/vnd.api+json"}},searchDepth:2};return b(e,a||{}),d.call(this,e)}_inherits(c,a);var d=_createSuper(c);return _createClass(c,[{key:"sync",value:function sync(a,b){var d=!!(2<arguments.length&&void 0!==arguments[2])&&arguments[2];if(a[this.options.keys.included]){var e=this.options.keys.data;this.options.keys.data=this.options.keys.included,_get(_getPrototypeOf(c.prototype),"sync",this).call(this,a,b,!0),this.options.keys.data=e}return _get(_getPrototypeOf(c.prototype),"sync",this).call(this,a,b,d)}},{key:"syncPaging",value:function syncPaging(a,b){var d=_get(_getPrototypeOf(c.prototype),"syncPaging",this).call(this,a,b);return d&&d.current&&(d.current[this.options.keys.links]=a[this.options.keys.links],d.current[this.options.keys.meta]=a[this.options.keys.meta]),d}},{key:"body",value:function body(a,d){var e=d.id?d.id:this.id(d),f={};return f[this.options.keys.data]={},f[this.options.keys.data][this.options.keys.type]=a,f[this.options.keys.data].attributes={},e&&(f[this.options.keys.data][this.options.keys.id]=e),d[this.options.keys.data]?b(f[this.options.keys.data],d[this.options.keys.data]):d.attributes?b(f[this.options.keys.data],d):b(f[this.options.keys.data].attributes,d),f[this.options.keys.data].attributes[this.options.keys.type]&&delete f[this.options.keys.data].attributes[this.options.keys.type],f[this.options.keys.data].attributes[this.options.keys.id]&&delete f[this.options.keys.data].attributes[this.options.keys.id],_get(_getPrototypeOf(c.prototype),"body",this).call(this,a,f)}}]),c}(r),u=/*#__PURE__*/function(a){function c(a){_classCallCheck(this,c);var e={baseUrl:"/wp-json/wp/v2",types:{page:"pages",post:"posts",category:"categories",tag:"tags",comment:"comments"},keys:{data:null,type:"type",id:"id"},query:{"page[number]":"page","page[size]":"per_page"},per_page:10};return b(e,a||{}),d.call(this,e)}_inherits(c,a);var d=_createSuper(c);return _createClass(c,[{key:"fetch",value:function fetch(a,b){var d=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};return"string"==typeof b&&(d.slug=b,b=void 0),_get(_getPrototypeOf(c.prototype),"fetch",this).call(this,a,b,d)}}]),c}(r),v=/*#__PURE__*/function(){function a(b,c,d){var e=this;_classCallCheck(this,a),d instanceof k||(d=new k(d||{controller:null,data:{},dev:!1,error:null,method:null,path:null,view:null})),this._name=b,this._html=c,this._model=d,d.watch(function(a){return e.update(a)}),this.constructor.options.autoCache&&a.cache(this)}return _createClass(a,[{key:"clear",value:function clear(){return document.querySelectorAll("[ui-view=\""+this._name+"\"]").forEach(function(a){a.innerHTML=""}),this}// When the model updates | ||
@@ -45,0 +45,0 @@ },{key:"update",value:function update(a){this.constructor.options.verbose&&console.info(this.constructor.name+".update()",this.name,{view:this,changes:a}),this.render()}// Render the model data into the view template for all elements with [ui-view=this.name] |
@@ -797,3 +797,3 @@ var ActionUI = (function (exports) { | ||
* Store | ||
* @version 20220221.1215 | ||
* @version 20220310 | ||
* @description Remote data store | ||
@@ -827,2 +827,3 @@ * @tutorial let store = new Store({baseUrl:'http://localhost:8080/api', types:['category', 'product']}) | ||
'triggerChangesOnError': true, // Allows views to update contents on failed requests, especially useful for Fetch All requests which return no results or 404 | ||
'triggerChangesOnEmpty': true, | ||
'verbose': false, | ||
@@ -965,3 +966,3 @@ 'viewClass': null, | ||
if (!data) | ||
if (!data || Object.keys(data).length == 0) | ||
{ | ||
@@ -1208,8 +1209,10 @@ throw new Error('Store: No data to sync') | ||
const json = await response.json(); | ||
const json_2 = response.ok ? json : Promise.reject(json); | ||
if(!response.ok) | ||
return Promise.reject(json) | ||
try | ||
{ | ||
let model = this.sync(json_2, url); | ||
cached = this.urlCache(parsedUrl, model, json_2); | ||
let model = this.sync(json, url); | ||
this.urlCache(parsedUrl, model, json); | ||
return model | ||
@@ -1219,2 +1222,4 @@ } | ||
{ | ||
if (type && this.options.triggerChangesOnError && this._model[type] ) | ||
this.model(type).triggerChanges(); | ||
return Promise.reject(json) | ||
@@ -1368,8 +1373,15 @@ } | ||
{ | ||
if (!this._model[type]) | ||
{ | ||
this.modelCreate(type); | ||
} | ||
const model = this.model(type); | ||
if (arguments.length == 1) | ||
{ | ||
return this.model(type)._loading | ||
return model._loading | ||
} | ||
this.model(type)._loading = !!isLoading; | ||
model._loading = !!isLoading; | ||
} | ||
@@ -1376,0 +1388,0 @@ |
@@ -7,3 +7,3 @@ import * as Util from './util.js' | ||
* Store | ||
* @version 20220221.1215 | ||
* @version 20220310 | ||
* @description Remote data store | ||
@@ -37,2 +37,3 @@ * @tutorial let store = new Store({baseUrl:'http://localhost:8080/api', types:['category', 'product']}) | ||
'triggerChangesOnError': true, // Allows views to update contents on failed requests, especially useful for Fetch All requests which return no results or 404 | ||
'triggerChangesOnEmpty': true, | ||
'verbose': false, | ||
@@ -175,3 +176,3 @@ 'viewClass': null, | ||
if (!data) | ||
if (!data || Object.keys(data).length == 0) | ||
{ | ||
@@ -418,8 +419,10 @@ throw new Error('Store: No data to sync') | ||
const json = await response.json() | ||
const json_2 = response.ok ? json : Promise.reject(json) | ||
if(!response.ok) | ||
return Promise.reject(json) | ||
try | ||
{ | ||
let model = this.sync(json_2, url) | ||
cached = this.urlCache(parsedUrl, model, json_2) | ||
let model = this.sync(json, url) | ||
this.urlCache(parsedUrl, model, json) | ||
return model | ||
@@ -429,2 +432,4 @@ } | ||
{ | ||
if (type && this.options.triggerChangesOnError && this._model[type] ) | ||
this.model(type).triggerChanges() | ||
return Promise.reject(json) | ||
@@ -578,8 +583,15 @@ } | ||
{ | ||
if (!this._model[type]) | ||
{ | ||
this.modelCreate(type) | ||
} | ||
const model = this.model(type) | ||
if (arguments.length == 1) | ||
{ | ||
return this.model(type)._loading | ||
return model._loading | ||
} | ||
this.model(type)._loading = !!isLoading | ||
model._loading = !!isLoading | ||
} | ||
@@ -586,0 +598,0 @@ |
{ | ||
"name": "@dwightbcoder/action-ui", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"description": "Action UI is a Javascript framework to simplify connecting actions to user interface elements", | ||
@@ -5,0 +5,0 @@ "main": "dist/action-ui.min.js", |
@@ -797,3 +797,3 @@ var ActionUI = (function (exports) { | ||
* Store | ||
* @version 20220221.1215 | ||
* @version 20220310 | ||
* @description Remote data store | ||
@@ -827,2 +827,3 @@ * @tutorial let store = new Store({baseUrl:'http://localhost:8080/api', types:['category', 'product']}) | ||
'triggerChangesOnError': true, // Allows views to update contents on failed requests, especially useful for Fetch All requests which return no results or 404 | ||
'triggerChangesOnEmpty': true, | ||
'verbose': false, | ||
@@ -965,3 +966,3 @@ 'viewClass': null, | ||
if (!data) | ||
if (!data || Object.keys(data).length == 0) | ||
{ | ||
@@ -1208,8 +1209,10 @@ throw new Error('Store: No data to sync') | ||
const json = await response.json(); | ||
const json_2 = response.ok ? json : Promise.reject(json); | ||
if(!response.ok) | ||
return Promise.reject(json) | ||
try | ||
{ | ||
let model = this.sync(json_2, url); | ||
cached = this.urlCache(parsedUrl, model, json_2); | ||
let model = this.sync(json, url); | ||
this.urlCache(parsedUrl, model, json); | ||
return model | ||
@@ -1219,2 +1222,4 @@ } | ||
{ | ||
if (type && this.options.triggerChangesOnError && this._model[type] ) | ||
this.model(type).triggerChanges(); | ||
return Promise.reject(json) | ||
@@ -1368,8 +1373,15 @@ } | ||
{ | ||
if (!this._model[type]) | ||
{ | ||
this.modelCreate(type); | ||
} | ||
const model = this.model(type); | ||
if (arguments.length == 1) | ||
{ | ||
return this.model(type)._loading | ||
return model._loading | ||
} | ||
this.model(type)._loading = !!isLoading; | ||
model._loading = !!isLoading; | ||
} | ||
@@ -1376,0 +1388,0 @@ |
@@ -7,3 +7,3 @@ import * as Util from './util.js' | ||
* Store | ||
* @version 20220221.1215 | ||
* @version 20220310 | ||
* @description Remote data store | ||
@@ -37,2 +37,3 @@ * @tutorial let store = new Store({baseUrl:'http://localhost:8080/api', types:['category', 'product']}) | ||
'triggerChangesOnError': true, // Allows views to update contents on failed requests, especially useful for Fetch All requests which return no results or 404 | ||
'triggerChangesOnEmpty': true, | ||
'verbose': false, | ||
@@ -175,3 +176,3 @@ 'viewClass': null, | ||
if (!data) | ||
if (!data || Object.keys(data).length == 0) | ||
{ | ||
@@ -418,8 +419,10 @@ throw new Error('Store: No data to sync') | ||
const json = await response.json() | ||
const json_2 = response.ok ? json : Promise.reject(json) | ||
if(!response.ok) | ||
return Promise.reject(json) | ||
try | ||
{ | ||
let model = this.sync(json_2, url) | ||
cached = this.urlCache(parsedUrl, model, json_2) | ||
let model = this.sync(json, url) | ||
this.urlCache(parsedUrl, model, json) | ||
return model | ||
@@ -429,2 +432,4 @@ } | ||
{ | ||
if (type && this.options.triggerChangesOnError && this._model[type] ) | ||
this.model(type).triggerChanges() | ||
return Promise.reject(json) | ||
@@ -578,8 +583,15 @@ } | ||
{ | ||
if (!this._model[type]) | ||
{ | ||
this.modelCreate(type) | ||
} | ||
const model = this.model(type) | ||
if (arguments.length == 1) | ||
{ | ||
return this.model(type)._loading | ||
return model._loading | ||
} | ||
this.model(type)._loading = !!isLoading | ||
model._loading = !!isLoading | ||
} | ||
@@ -586,0 +598,0 @@ |
Sorry, the diff of this file is too big to display
420072
10571