UNPKG

@datorama/akita-ng-forms-manager

Version:

The best way to manage your Angular form state in Akita

16 lines (14 loc) 10.2 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/forms"),require("@datorama/akita"),require("rxjs"),require("rxjs/operators")):"function"==typeof define&&define.amd?define("@datorama/akita-ng-forms-manager",["exports","@angular/core","@angular/forms","@datorama/akita","rxjs","rxjs/operators"],e):e(((t=t||self).datorama=t.datorama||{},t.datorama["akita-ng-forms-manager"]={}),t.ng.core,t.ng.forms,t.akita,t.rxjs,t.rxjs.operators)}(this,(function(t,e,r,o,n,i){"use strict"; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */var a=function(t,e){return(a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])})(t,e)};function s(t,e){function r(){this.constructor=t}a(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}var u=function(){return(u=Object.assign||function(t){for(var e,r=1,o=arguments.length;r<o;r++)for(var n in e=arguments[r])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,arguments)};function c(t,e,r,o){var n,i=arguments.length,a=i<3?e:null===o?o=Object.getOwnPropertyDescriptor(e,r):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,e,r,o);else for(var s=t.length-1;s>=0;s--)(n=t[s])&&(a=(i<3?n(a):i>3?n(e,r,a):n(e,r))||a);return i>3&&a&&Object.defineProperty(e,r,a),a}function l(t,e){return function(r,o){e(r,o,t)}}function f(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)}function p(t){var e="function"==typeof Symbol&&Symbol.iterator,r=e&&t[e],o=0;if(r)return r.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&o>=t.length&&(t=void 0),{value:t&&t[o++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function y(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var o,n,i=r.call(t),a=[];try{for(;(void 0===e||e-- >0)&&!(o=i.next()).done;)a.push(o.value)}catch(t){n={error:t}}finally{try{o&&!o.done&&(r=i.return)&&r.call(i)}finally{if(n)throw n.error}}return a}var h=function(t){function e(e){return t.call(this,e)||this}return s(e,t),e}(o.Query),d=function(t){function e(e){return t.call(this,e)||this}return s(e,t),e.ctorParameters=function(){return[{type:void 0}]},e=c([o.StoreConfig({name:"formsManager"}),f("design:paramtypes",[Object])],e)}(o.Store),m=new e.InjectionToken("FormsManagerOptions"),v={debounceTime:300,updateStoreOnUnsubscribe:!1},g=function(){function t(t){void 0===t&&(t={}),this.valueChanges={},this.ngForms={},this._options=Object.assign({},v,t),this._store=new d({}),this._query=new h(this.store)}return Object.defineProperty(t.prototype,"query",{get:function(){return this._query},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"store",{get:function(){return this._store},enumerable:!0,configurable:!0}),t.prototype.selectValid=function(t,e){return this.selectControl(t,e).pipe(i.map((function(t){return t.valid})))},t.prototype.selectDirty=function(t,e){return this.selectControl(t,e).pipe(i.map((function(t){return t.dirty})))},t.prototype.selectDisabled=function(t,e){return this.selectControl(t,e).pipe(i.map((function(t){return t.disabled})))},t.prototype.selectValue=function(t,e){return this.selectControl(t,e).pipe(i.map((function(t){return t.value})))},t.prototype.selectErrors=function(t,e){return this.selectControl(t,e).pipe(i.map((function(t){return t.errors})))},t.prototype.selectNgForm=function(t){var e=this;return this.selectForm(t,{filterNil:!0}).pipe(i.map((function(){return e.ngForms[t]})))},t.prototype.selectControl=function(t,e){var r=this;return e?this.query.select((function(e){return e[t]})).pipe(o.filterNil,i.map((function(t){return r.resolveControl(t,e)})),i.distinctUntilChanged((function(t,e){return JSON.stringify(t)===JSON.stringify(e)}))):this.selectForm(t)},t.prototype.getControl=function(t,e){if(!e)return this.getForm(t);if(this.hasForm(t)){var r=this.getForm(t);return this.resolveControl(r,e)}return null},t.prototype.selectForm=function(t,e){return void 0===e&&(e={filterNil:!0}),this.query.select((function(e){return e[t]})).pipe(e.filterNil?o.filterNil:function(t){return t})},t.prototype.getForm=function(t){return this.query.getValue()[t]},t.prototype.getNgForm=function(t){return this.ngForms[t]},t.prototype.hasForm=function(t){return!!this.getForm(t)},t.prototype.upsert=function(t,e,r){var o=this;void 0===r&&(r={});var a=u({debounceTime:this._options.debounceTime,emitEvent:!1},r);return!0===this.hasForm(t)?e.patchValue(this.resolveStoreToForm(t,e,a.arrControlFactory),{emitEvent:a.emitEvent}):(this.updateStore(t,e,!0),a.persistForm&&this.storeFormInstance(t,e)),this.valueChanges[t]=n.merge(e.valueChanges,e.statusChanges.pipe(i.distinctUntilChanged())).pipe(i.debounceTime(a.debounceTime)).subscribe((function(){return o.updateStore(t,e)})),this},t.prototype.remove=function(t){var e,r;if(t)this.removeFromStore(t);else{var o=Object.keys(this.query.getValue());try{for(var n=p(o),i=n.next();!i.done;i=n.next()){var a=i.value;this.removeFromStore(a)}}catch(t){e={error:t}}finally{try{i&&!i.done&&(r=n.return)&&r.call(n)}finally{if(e)throw e.error}}}this.unsubscribe(t)},t.prototype.unsubscribe=function(t,e){var r,o,n=this;void 0===e&&(e={});var i=u({removeNgForm:!0},{updateStore:this._options.updateStoreOnUnsubscribe},e),a=t,s=function(t){return i.removeNgForm?n.removeFormInstance(t):null};if(a)this.valueChanges[a]&&(this.valueChanges[a].unsubscribe(),delete this.valueChanges[a],e.updateStore&&this.ngForms[a]&&this.updateStore(a,this.getNgForm(a)),s(a));else{try{for(var c=p(Object.keys(this.valueChanges)),l=c.next();!l.done;l=c.next()){var f=l.value;this.valueChanges[f].unsubscribe(),e.updateStore&&this.ngForms[f]&&this.updateStore(f,this.getNgForm(f)),s(f)}}catch(t){r={error:t}}finally{try{l&&!l.done&&(o=c.return)&&o.call(c)}finally{if(r)throw r.error}}this.valueChanges={}}},t.prototype.removeFromStore=function(t){var e=this.query.getValue(),r=Object.keys(e).reduce((function(r,o){return t!==o&&(r[o]=e[o]),r}),{});o.logAction("Remove "+t),this.store._setState((function(){return r}))},t.prototype.resolveControl=function(t,e){var r=y(e.split(".")),o=r[0],n=r.slice(1);return 0===n.length?t.controls[o]:this.find(t.controls[o],n)},t.prototype.find=function(t,e){return e.reduce((function(t,e){return t.controls.hasOwnProperty(e)?t.controls[e]:null}),t)},t.prototype.resolveStoreToForm=function(t,e,r){var o=this.getForm(t),n=o.value;return o.controls?(this.handleFormArray(n,e,r),n):n},t.prototype.handleFormArray=function(t,e,o){var n=this;if(e instanceof r.FormArray){if(this.cleanArray(e),!o)throw new Error("Please provide arrControlFactory");t.forEach((function(t,r){return e.insert(r,o(t))}))}else Object.keys(t).forEach((function(i){var a=t[i];if(Array.isArray(a)&&e.get(i)instanceof r.FormArray==!0){if(!o||o&&i in o==!1)throw new Error("Please provide arrControlFactory for "+i);var s=e.get(i),u=o[i];n.cleanArray(s),a.forEach((function(t,e){return s.insert(e,u(t))}))}}))},t.prototype.cleanArray=function(t){for(;0!==t.length;)t.removeAt(0)},t.prototype.buildFormStoreState=function(t,e){var o,n,i;if(e instanceof r.FormControl&&(i=this.resolveFormToStore(e)),e instanceof r.FormGroup||e instanceof r.FormArray){i=u({},this.resolveFormToStore(e),{controls:{}});try{for(var a=p(Object.keys(e.controls)),s=a.next();!s.done;s=a.next()){var c=s.value,l=e.controls[c];l instanceof r.FormGroup||e instanceof r.FormArray?i.controls[c]=this.buildFormStoreState(t,l):i.controls[c]=this.resolveFormToStore(l)}}catch(t){o={error:t}}finally{try{s&&!s.done&&(n=a.return)&&n.call(a)}finally{if(o)throw o.error}}}return i},t.prototype.updateStore=function(t,e,r){var n;void 0===r&&(r=!1);var i=this.buildFormStoreState(t,e),a=(r?"Create":"Update")+" "+(t[0].toUpperCase()+t.slice(1))+" Form";o.logAction(a),this.store.update(((n={})[t]=i,n))},t.prototype.resolveFormToStore=function(t){return{value:this.cloneValue(t.value),rawValue:t.getRawValue?t.getRawValue():null,valid:t.valid,dirty:t.dirty,invalid:t.invalid,disabled:t.disabled,errors:t.errors,touched:t.touched,pristine:t.pristine,pending:t.pending}},t.prototype.cloneValue=function(t){return this.isObject(t)?u({},t):Array.isArray(t)?function(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(y(arguments[e]));return t}(t):t},t.prototype.isObject=function(t){return null!=t&&(!Array.isArray(t)&&("function"==typeof t||"object"==typeof t))},t.prototype.storeFormInstance=function(t,e){var r,o=u({},this.ngForms,((r={})[t]=e,r));this.ngForms=o},t.prototype.removeFormInstance=function(t){this.ngForms[t]&&delete this.ngForms[t]},t.ctorParameters=function(){return[{type:void 0,decorators:[{type:e.Optional},{type:e.Inject,args:[m]}]}]},t.ɵprov=e["ɵɵdefineInjectable"]({factory:function(){return new t(e["ɵɵinject"](m,8))},token:t,providedIn:"root"}),t=c([e.Injectable({providedIn:"root"}),l(0,e.Optional()),l(0,e.Inject(m)),f("design:paramtypes",[Object])],t)}();t.AkitaNgFormsManager=g,t.FORMS_MANAGER_OPTIONS=m,t.FormsQuery=h,t.FormsStore=d,t.setAsyncValidators=function(t,e){t.setValidators(o.coerceArray(e)),t.updateValueAndValidity()},t.setValidators=function(t,e){t.setValidators(o.coerceArray(e)),t.updateValueAndValidity()},Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=datorama-akita-ng-forms-manager.umd.min.js.map