1 | !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/forms"),require("rxjs"),require("rxjs/operators"),require("@angular/common")):"function"==typeof define&&define.amd?define("@eclat/form",["exports","@angular/core","@angular/forms","rxjs","rxjs/operators","@angular/common"],e):e(((t=t||self).eclat=t.eclat||{},t.eclat.form={}),t.ng.core,t.ng.forms,t.rxjs,t.rxjs.operators,t.ng.common)}(this,function(t,e,r,o,n,i){"use strict";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 c(t,e){function r(){this.constructor=t}a(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}function s(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(c){n={error:c}}finally{try{o&&!o.done&&(r=i["return"])&&r.call(i)}finally{if(n)throw n.error}}return a}var l=function(){return function(){}}();!function(t){t.isEclatFormControlConfig=function(t){return!!Array.isArray(t)},t.isEclatFormGroupConfig=function(t){return!!t.controlsRules},t.isEclatFormArrayConfig=function(t){return!!t.controlConfig},t.eclatRecursiveTransformer=function(t){var e=function n(t,e){if(void 0===e&&(e=null),t instanceof l)return"";if(Array.isArray(t)){var r=null;return t.some(function(t,o){var i=n(t,e);return null!==i&&(r=i?o+"."+i:o.toString()),!!r}),r}if(t===Object(t)){var o=null;return Object.entries(t).some(function(t){var r=s(t,2),i=r[0],a=r[1],c=n(a,e);return null!==c&&(o=c?i+"."+c:i),!!o}),o}return null}(t);if(!e)throw Error();var r=e.split("."),o=r.pop();return r.reduce(function(t,e){return t[e]},t)[o]=t,t}}(t.EclatFormUtility||(t.EclatFormUtility={}));var u=function(){function t(t){var e=[],r=[],o={};t&&t.forEach(function(t){var n=t.validator,i=t.asyncValidator,a=t.key,c=t.message;o[a]=c,n&&e.push(n),i&&r.push(i)}),this.rules=o,this.validators=e,this.asyncValidators=r}return Object.defineProperty(t.prototype,"errorMessages",{get:function(){var t=this;return this.control.errors?Object.entries(this.control.errors).reduce(function(e,r){var o=s(r,2),n=o[0];if(o[1]){var i=t.rules[n],a="function"==typeof i?i(t):i;Array.isArray(a)?e.push.apply(e,function(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(s(arguments[e]));return t}(a)):e.push(a)}return e},[]):[]},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"value",{get:function(){return this.control.value},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"valid",{get:function(){return this.control.valid},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"valueChanges",{get:function(){return this.control.valueChanges},enumerable:!0,configurable:!0}),t.prototype.disable=function(t,e){void 0===t&&(t=!0),t?this.control.disable(e):this.control.enable(e)},t}();function p(e,r){return t.EclatFormUtility.isEclatFormControlConfig(e)?new f(e,r):t.EclatFormUtility.isEclatFormGroupConfig(e)?new h(e,r):t.EclatFormUtility.isEclatFormArrayConfig(e)?new m(e,r):null}var f=function(t){function e(e,o){var n=t.call(this,e)||this;return n.control=new r.FormControl(null,n.validators,n.asyncValidators),o&&n.resetState(o),n}return c(e,t),e.prototype.patchState=function(t,e){void 0===e&&(e=!0),this.control.patchValue(t,{emitEvent:e})},e.prototype.resetState=function(t,e){void 0===e&&(e=!0),this.control.reset(t,{emitEvent:e})},e}(u),h=function(t){function e(e,o){var n=t.call(this,e.rules)||this,i={},a={};return Object.keys(e.controlsRules).forEach(function(t){var r=p(e.controlsRules[t]);r&&(i[t]=r,a[t]=r.control)}),n.control=new r.FormGroup(a,n.validators,n.asyncValidators),n.children=i,o&&n.resetState(o),n}return c(e,t),e.prototype.patchState=function(t,e){var r=this;void 0===e&&(e=!0),t?(Object.entries(t).forEach(function(t){var e=s(t,2),o=e[0],n=e[1];r.children[o]&&r.children[o].patchState(n,!1)}),this.control.patchValue({},{emitEvent:e})):console.warn("Expect an object, but "+t+" is given")},e.prototype.resetState=function(t,e){var r=this;void 0===e&&(e=!0),t?(Object.entries(t).forEach(function(t){var e=s(t,2),o=e[0],n=e[1];r.children[o]&&r.children[o].resetState(n,!1)}),this.control.reset(undefined,{emitEvent:e})):console.warn("Expect an object, but "+t+" is given")},e.prototype.getChild=function(t){return this.children[t]},e.prototype.addChild=function(t,e,r){var o=p(e);o&&(o.resetState(r),this.children[t]=o,this.control.addControl(t,this.children[t].control))},e.prototype.removeChild=function(t){delete this.children[t],this.control.removeControl(t)},e}(u),m=function(t){function e(e,o){var n=t.call(this,e.rules)||this;return n.control=new r.FormArray([],n.validators,n.asyncValidators),n.childConfig=e.controlConfig,n.children=[],o&&n.resetState(o),n}return c(e,t),e.prototype.addChild=function(t){var e=p(this.childConfig);e&&(e.resetState(t),this.children.push(e),this.control.push(e.control))},e.prototype.removeChild=function(t){this.children.splice(t,1),this.control.removeAt(t)},e.prototype.patchState=function(t,e){var r=this;void 0===e&&(e=!0),t?(this.control.controls=[],this.children=[],t.forEach(function(t){var e=p(r.childConfig);e&&(e.patchState(t,!1),r.children.push(e),r.control.push(e.control))}),this.control.patchValue([],{emitEvent:e})):console.warn("Expect an array, but "+t+" is given")},e.prototype.resetState=function(t,e){var r=this;void 0===e&&(e=!0),t?(this.control.controls=[],this.children=[],t.forEach(function(t){var e=p(r.childConfig);e&&(e.resetState(t,!1),r.children.push(e),r.control.push(e.control))}),this.control.reset(undefined,{emitEvent:e})):console.warn("Expect an array, but "+t+" is given")},e.prototype.getChild=function(t){return this.children[t]},e}(u),y=function(){return function(t){this.$implicit=t}}(),d=function(){function t(t,e){this.templateRef=t,this.viewContainer=e}return Object.defineProperty(t.prototype,"eclatFormOf",{set:function(t){this.context&&this.latestState&&this.latestState.config===t.config?this.latestState.data!==t.data&&this.context.$implicit.resetState(t.data):this.setContext(p(t.config,t.data)),this.latestState=t},enumerable:!0,configurable:!0}),t.prototype.setContext=function(t){if(!t)throw new Error("control not found");this.context?this.context.$implicit=t:(this.context=new y(t),this.viewContainer.createEmbeddedView(this.templateRef,this.context))},t.decorators=[{type:e.Directive,args:[{selector:"[eclatForm][eclatFormOf]"}]}],t.ctorParameters=function(){return[{type:e.TemplateRef},{type:e.ViewContainerRef}]},t.propDecorators={eclatFormOf:[{type:e.Input}]},t}(),v=function(){function t(){this._formControl=new o.BehaviorSubject(undefined),this.onSubmit=new e.EventEmitter}return Object.defineProperty(t.prototype,"formControl",{get:function(){return this._formControl.getValue()},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"formControl$",{get:function(){return this._formControl.asObservable()},enumerable:!0,configurable:!0}),t.prototype.submit=function(){this.formControl&&this.onSubmit.emit(this.formControl.value)},t.propDecorators={onSubmit:[{type:e.Output}]},t}(),C=function(){function t(t){this.control=t}return Object.defineProperty(t.prototype,"$implicit",{get:function(){return this.control.errorMessages},enumerable:!0,configurable:!0}),t}(),g=function(){function t(t,e,r){this.templateRef=t,this.viewContainer=e,this.parent=r,this.onDestroy$=new o.Subject,this.errorMatcher=function(){return!0}}return Object.defineProperty(t.prototype,"eclatControl",{set:function(t){this.setContext(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"eclatControlName",{set:function(t){if(!this.parent.formControl)throw new Error("parent formControl not exist");var e=t?this.parent.formControl.getChild(t):this.parent.formControl;if(!e)throw new Error("control not found");this.latestControlName=t,this.setContext(e)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"eclatFormError",{set:function(t){t instanceof u?this.setContext(t):this.eclatControlName=t},enumerable:!0,configurable:!0}),t.prototype.ngOnInit=function(){var t=this;this.parent.formControl$.pipe(n.takeUntil(this.onDestroy$)).subscribe(function(e){e&&t.latestControlName&&(t.eclatFormError=e.getChild(t.latestControlName))})},t.prototype.ngOnDestroy=function(){this.onDestroy$.next()},t.prototype.setContext=function(t){var e=this;this.context?this.context.control!==t&&(this.context.control=t):(this.context=new C(t),this.viewContainer.createEmbeddedView(this.templateRef,this.context)),this.valueChangesSub&&this.valueChangesSub.unsubscribe(),this.checkValidity(t),this.valueChangesSub=t.valueChanges.pipe(n.takeUntil(this.onDestroy$)).subscribe(function(){e.checkValidity(t)})},t.prototype.checkValidity=function(t){var e=!!this.viewContainer.length;!t.valid&&this.errorMatcher(t)?e||this.viewContainer.createEmbeddedView(this.templateRef,this.context):e&&this.viewContainer.remove()},t.decorators=[{type:e.Directive,args:[{selector:"[eclatFormError]"}]}],t.ctorParameters=function(){return[{type:e.TemplateRef},{type:e.ViewContainerRef},{type:v}]},t.propDecorators={errorMatcher:[{type:e.Input}],eclatControl:[{type:e.Input}],eclatControlName:[{type:e.Input}],eclatFormError:[{type:e.Input}]},t}(),b=function(t){function r(){return t.call(this)||this}return c(r,t),Object.defineProperty(r.prototype,"eclatFormGroup",{set:function(t){this._formControl.next(t)},enumerable:!0,configurable:!0}),r.prototype.getChild=function(t){return this.formControl?this.formControl.getChild(t):undefined},r.decorators=[{type:e.Directive,args:[{selector:"[eclatFormGroup]",providers:[{provide:v,useExisting:e.forwardRef(function(){return r})}]}]}],r.ctorParameters=function(){return[]},r.propDecorators={eclatFormGroup:[{type:e.Input}]},r}(v),F=function(t){function r(e){var r=t.call(this)||this;return r.parent=e,r.onDestroy$=new o.Subject,r}return c(r,t),Object.defineProperty(r.prototype,"eclatFormGroupName",{set:function(t){this.latestName=t,this._formControl.next(this.parent.getChild(t))},enumerable:!0,configurable:!0}),r.prototype.ngOnDestroy=function(){this.onDestroy$.next()},r.prototype.ngOnInit=function(){var t=this;this.parent.formControl$.pipe(n.takeUntil(this.onDestroy$)).subscribe(function(){t.eclatFormGroupName=t.latestName})},r.prototype.getChild=function(t){return this.formControl?this.formControl.getChild(t):undefined},r.decorators=[{type:e.Directive,args:[{selector:"[eclatFormGroupName]",providers:[{provide:v,useExisting:e.forwardRef(function(){return r})}]}]}],r.ctorParameters=function(){return[{type:v,decorators:[{type:e.SkipSelf}]}]},r.propDecorators={eclatFormGroupName:[{type:e.Input}]},r}(v),x=function(t){function r(e){var r=t.call(this)||this;return r.parent=e,r.onDestroy$=new o.Subject,r}return c(r,t),Object.defineProperty(r.prototype,"eclatFormArrayName",{set:function(t){this.latestName=t,this._formControl.next(this.parent.getChild(t))},enumerable:!0,configurable:!0}),r.prototype.ngOnDestroy=function(){this.onDestroy$.next()},r.prototype.ngOnInit=function(){var t=this;this.parent.formControl$.pipe(n.takeUntil(this.onDestroy$)).subscribe(function(){t.eclatFormArrayName=t.latestName})},r.prototype.getChild=function(t){return this.formControl?this.formControl.getChild(t):undefined},r.decorators=[{type:e.Directive,args:[{selector:"[eclatFormArrayName]",providers:[{provide:v,useExisting:e.forwardRef(function(){return r})}]}]}],r.ctorParameters=function(){return[{type:v,decorators:[{type:e.SkipSelf}]}]},r.propDecorators={eclatFormArrayName:[{type:e.Input}]},r}(v),w=function(t){function r(){return t.call(this)||this}return c(r,t),Object.defineProperty(r.prototype,"eclatFormArray",{set:function(t){this._formControl.next(t)},enumerable:!0,configurable:!0}),r.prototype.getChild=function(t){return this.formControl?this.formControl.getChild(t):undefined},r.decorators=[{type:e.Directive,args:[{selector:"[eclatFormArray]",providers:[{provide:v,useExisting:e.forwardRef(function(){return r})}]}]}],r.ctorParameters=function(){return[]},r.propDecorators={eclatFormArray:[{type:e.Input}]},r}(v),E=function(){function t(t,e,r){this.templateRef=t,this.viewContainer=e,this.parent=r,this.onDestroy$=new o.Subject,this.latestChildren=[]}return t.prototype.ngOnDestroy=function(){this.onDestroy$.next()},t.prototype.ngOnInit=function(){var t=this;this.parent.formControl$.pipe(n.takeUntil(this.onDestroy$)).subscribe(function(e){e&&(t.valueChangeSubs&&t.valueChangeSubs.unsubscribe(),t.onChange(e),t.valueChangeSubs=e.valueChanges.pipe(n.takeUntil(t.onDestroy$)).subscribe(function(){t.onChange(e)}))})},t.prototype.onChange=function(t){var e=this,r=[],o=t.children.length;for(t.children.forEach(function(t,n){var i=e.latestChildren.findIndex(function(e){return e===t}),a=0===n,c=n===o-1;if(i<0)e.viewContainer.createEmbeddedView(e.templateRef,{$implicit:n,last:c,first:a},n);else if(i!==n){var s=e.viewContainer.get(i);s.context.$implicit=n,s.context.last=c,s.context.first=a,e.viewContainer.move(s,n)}r.push(t)});this.viewContainer.length>o;)this.viewContainer.remove();this.latestChildren=r},t.decorators=[{type:e.Directive,args:[{selector:"[eclatFormArrayChildren]"}]}],t.ctorParameters=function(){return[{type:e.TemplateRef},{type:e.ViewContainerRef},{type:v}]},t}(),D=function(){function t(t){this.parent=t}return t.prototype.onClick=function(){this.parent.submit()},t.decorators=[{type:e.Directive,args:[{selector:"[eclatFormSubmit]"}]}],t.ctorParameters=function(){return[{type:v}]},t.propDecorators={onClick:[{type:e.HostListener,args:["click",["$event"]]},{type:e.HostListener,args:["tap",["$event"]]}]},t}(),O=function(){function t(t){this.parent=t}return t.prototype.onClick=function(){var t=this.parent.formControl;t&&t.addChild(this.value)},t.decorators=[{type:e.Directive,args:[{selector:"[eclatFormAddChildren]"}]}],t.ctorParameters=function(){return[{type:v}]},t.propDecorators={value:[{type:e.Input,args:["eclatFormAddChildren"]}],onClick:[{type:e.HostListener,args:["click",["$event"]]}]},t}(),j=function(){function t(t){this.control=t}return Object.defineProperty(t.prototype,"$implicit",{get:function(){return this.control.value},enumerable:!0,configurable:!0}),t}(),S=function(){function t(t,e,r){this.templateRef=t,this.viewContainer=e,this.parent=r,this.onDestroy$=new o.Subject}return Object.defineProperty(t.prototype,"eclatControl",{set:function(t){this.setContext(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"eclatControlName",{set:function(t){if(!this.parent.formControl)throw new Error("parent formControl not exist");var e=this.parent.formControl.getChild(t);if(!e)throw new Error("control not found");this.latestControlName=t,this.setContext(e)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"eclatFormValue",{set:function(t){t instanceof u?this.setContext(t):(t||0===t)&&(this.eclatControlName=t)},enumerable:!0,configurable:!0}),t.prototype.ngOnInit=function(){var t=this;this.parent.formControl$.pipe(n.takeUntil(this.onDestroy$)).subscribe(function(e){e&&t.latestControlName&&(t.eclatFormValue=e.getChild(t.latestControlName))})},t.prototype.ngOnDestroy=function(){this.onDestroy$.next()},t.prototype.setContext=function(t){this.context?this.context.control!==t&&(this.context.control=t):(this.context=new j(t),this.viewContainer.createEmbeddedView(this.templateRef,this.context))},t.decorators=[{type:e.Directive,args:[{selector:"[eclatFormValue]"}]}],t.ctorParameters=function(){return[{type:e.TemplateRef},{type:e.ViewContainerRef},{type:v}]},t.propDecorators={eclatControl:[{type:e.Input}],eclatControlName:[{type:e.Input}],eclatFormValue:[{type:e.Input}]},t}(),P=function(t){function o(){return null!==t&&t.apply(this,arguments)||this}return c(o,t),Object.defineProperty(o.prototype,"eclatFormGroup",{set:function(t){this.form=t.control},enumerable:!0,configurable:!0}),o.prototype.ngOnChanges=function(r){var o=r.eclatFormGroup;if(o){var n=o.previousValue?o.previousValue.control:undefined,i=o.currentValue.control,a=o.firstChange;t.prototype.ngOnChanges.call(this,{form:new e.SimpleChange(n,i,a)})}},o.decorators=[{type:e.Directive,args:[{selector:"[eclatFormGroup]",providers:[{provide:r.ControlContainer,useExisting:e.forwardRef(function(){return o})}]}]}],o.propDecorators={eclatFormGroup:[{type:e.Input}]},o}(r.FormGroupDirective),$=function(t){function o(){return null!==t&&t.apply(this,arguments)||this}return c(o,t),Object.defineProperty(o.prototype,"eclatFormGroupName",{set:function(t){this.name=t},enumerable:!0,configurable:!0}),o.decorators=[{type:e.Directive,args:[{selector:"[eclatFormGroupName]",providers:[{provide:r.ControlContainer,useExisting:e.forwardRef(function(){return o})}]}]}],o.propDecorators={eclatFormGroupName:[{type:e.Input}]},o}(r.FormGroupName),N=function(t){function o(){return null!==t&&t.apply(this,arguments)||this}return c(o,t),Object.defineProperty(o.prototype,"eclatFormArrayName",{set:function(t){this.name=t},enumerable:!0,configurable:!0}),o.decorators=[{type:e.Directive,args:[{selector:"[eclatFormArrayName]",providers:[{provide:r.ControlContainer,useExisting:e.forwardRef(function(){return o})}]}]}],o.propDecorators={eclatFormArrayName:[{type:e.Input}]},o}(r.FormArrayName),R=function(){function t(t){this.parent=t}return t.prototype.onClick=function(){var t=this.parent.formControl;t&&t.removeChild(this.index)},t.decorators=[{type:e.Directive,args:[{selector:"[eclatFormRemoveChildren]"}]}],t.ctorParameters=function(){return[{type:v}]},t.propDecorators={index:[{type:e.Input,args:["eclatFormRemoveChildren"]}],onClick:[{type:e.HostListener,args:["click",["$event"]]}]},t}(),V=function(){function t(){}return t.decorators=[{type:e.NgModule,args:[{imports:[i.CommonModule,r.ReactiveFormsModule],declarations:[d,g,b,F,w,x,E,D,O,S,P,$,N,R],exports:[i.CommonModule,r.ReactiveFormsModule,d,g,b,F,w,x,E,D,O,S,P,$,N,R]}]}],t}();t.AbstractEclatFormControl=u,t.EclatFormArray=m,t.EclatFormContext=y,t.EclatFormControl=f,t.EclatFormErrorDirective=g,t.EclatFormGroup=h,t.EclatFormGroupDirective=b,t.EclatFormModule=V,t.EclatFormOfDirective=d,t.EclatRecursiveConfig=l,t.getEclatControl=p,t.ɵa=v,t.ɵb=F,t.ɵc=w,t.ɵd=x,t.ɵe=E,t.ɵf=D,t.ɵg=O,t.ɵh=S,t.ɵi=P,t.ɵj=$,t.ɵk=N,t.ɵl=R,Object.defineProperty(t,"__esModule",{value:!0})});
|