@webdev-tools/ng-nested-reactive-forms
Version:
Implement Nested FormControl for Angular Reactive Forms.
2 lines • 10.3 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("@angular/forms"),require("rxjs/internal/ReplaySubject"),require("rxjs/operators"),require("@angular/common")):"function"==typeof define&&define.amd?define("@webdev-tools/ng-nested-reactive-forms",["exports","@angular/core","@angular/forms","rxjs/internal/ReplaySubject","rxjs/operators","@angular/common"],e):e((t["webdev-tools"]=t["webdev-tools"]||{},t["webdev-tools"]["ng-nested-reactive-forms"]={}),t.ng.core,t.ng.forms,t.rxjs["internal/ReplaySubject"],t.rxjs.operators,t.ng.common)}(this,function(t,e,s,a,r,o){"use strict";var n=function N(t){this.nrfForm=t,this.$implicit=t,this.formGroup=t.formGroup},i=function S(t,e){this.nrfForm=t,this.event=e,this.formData=t.formData,this.entity=t.nrfEntity,this.formGroup=t.formGroup};function u(r){return r&&"object"==typeof r?r instanceof Date?new Date(r):Array.isArray(r)?r.map(u):Object.keys(r).reduce(function(t,e){return t[e]=u(r[e]),t},{}):r}var p=function(){function t(){this.formGroup=new s.FormGroup({}),this.submit$=new e.EventEmitter}return Object.defineProperty(t.prototype,"entity",{get:function(){return this.privateEntity},set:function(t){this.privateEntity=t,this.formData=u(t)},enumerable:!0,configurable:!0}),t.decorators=[{type:e.Injectable}],t}();function l(){return new p}var f={provide:p,useFactory:l},c=function(){function t(t,e,r,o){this.templateRef=t,this.viewContainerRef=e,this.formService=r,this.renderer=o,r||(this.formService=new p)}return Object.defineProperty(t.prototype,"nrfEntity",{get:function(){return this.formService.entity},set:function(t){this.formService.entity=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"formData",{get:function(){return this.formService.formData},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"formGroup",{get:function(){return this.formService.formGroup},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"nrfSubmit",{get:function(){return this.formService.submit$},enumerable:!0,configurable:!0}),t.prototype.ngOnInit=function(){this.nrfEntity||(this.nrfEntity={}),this.renderView()},t.prototype.ngOnDestroy=function(){this.nrfSubmit.complete()},t.prototype.renderView=function(){var e=this;if(this.templateRef&&this.viewContainerRef){var t=this.viewContainerRef.createEmbeddedView(this.templateRef,new n(this)).rootNodes[0];this.renderer.listen(t,"submit",function(t){return e.formSubmitWrapper(t)})}},t.prototype.formSubmitWrapper=function(t){t.preventDefault(),this.formGroup.valid&&this.nrfSubmit.emit(new i(this,t))},t.decorators=[{type:e.Directive,args:[{selector:"[nrfForm]",exportAs:"nrfForm"}]}],t.ctorParameters=function(){return[{type:e.TemplateRef,decorators:[{type:e.Optional}]},{type:e.ViewContainerRef,decorators:[{type:e.Optional}]},{type:p,decorators:[{type:e.Optional}]},{type:e.Renderer2}]},t.propDecorators={nrfEntity:[{type:e.Input}],nrfSubmit:[{type:e.Output}],formSubmitWrapper:[{type:e.HostListener,args:["submit",["$event"]]}]},t}(),m=function(){function t(){}return t.decorators=[{type:e.NgModule,args:[{imports:[s.FormsModule,s.ReactiveFormsModule],exports:[s.FormsModule,s.ReactiveFormsModule,c],declarations:[c]}]}],t}(),h=function(){function t(){this.disabled=null,this.min=null,this.max=null,this.required=null,this.email=null,this.minLength=null,this.maxLength=null,this.pattern=null,this.updateOn=null}return t.prototype.ngOnInit=function(){this.controlOptions=this.generateControlOptions()},t.prototype.generateControlOptions=function(){var e=this;return{validators:Object.keys(s.Validators).filter(function(t){return e[t]}).map(function(t){return s.Validators[t]}),updateOn:this.updateOn,disabled:this.disabled}},t.propDecorators={disabled:[{type:e.Input,args:["disabled"]}],min:[{type:e.Input,args:["min"]}],max:[{type:e.Input,args:["max"]}],required:[{type:e.Input,args:["required"]}],email:[{type:e.Input,args:["email"]}],minLength:[{type:e.Input,args:["minLength"]}],maxLength:[{type:e.Input,args:["maxLength"]}],pattern:[{type:e.Input,args:["pattern"]}],updateOn:[{type:e.Input,args:["updateOn"]}]},t}(),d=function P(t,e,r){this.formControl=t,this.formGroup=e,this.nrfNestedControl=r,this.$implicit=t},y=function(){function t(){}return t.prototype.getNestedControl=function(t,e){var r=t.get(e);r||(r=(Array.isArray(e)?e:e.split(".")).reduce(this.createFormGroupHierarchy,t));return r instanceof s.FormControl&&(r=r.parent),r},t.prototype.createFormGroupHierarchy=function(t,e,r,o){if(r===o.length-1)return t;var n=t.get(e);if(!n){var i=o[r+1]||e;n=i&&!isNaN(i)?new s.FormArray([]):new s.FormGroup({})}return t instanceof s.FormGroup?t.addControl(e,n):t instanceof s.FormArray&&t.insert(parseInt(e,10)||0,n),n},t.decorators=[{type:e.Injectable}],t}(),g=function(){return(g=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 v(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var o,n,i=r.call(t),s=[];try{for(;(void 0===e||0<e--)&&!(o=i.next()).done;)s.push(o.value)}catch(a){n={error:a}}finally{try{o&&!o.done&&(r=i["return"])&&r.call(i)}finally{if(n)throw n.error}}return s}var b=function(){function t(){var p=this;this.getTargetPropToSet=function(t,e,r,o){var n=o[r+1],i=null==n;n=n||o.finalKey;var s=p.isArrayKey(n),a=t[e];return a?i&&(a=s?function u(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(v(arguments[e]));return t}(a):g({},a),t[e]=a):(a=s?[]:{},t[e]=a),a}}return t.prototype.getValue=function(t,e,r){if(void 0===r&&(r="."),null==t||!e)return null;try{return this.generatePathPieces(t,r).reduce(this.piecesReducer,e)||null}catch(o){return null}},t.prototype.piecesReducer=function(t,e){return""===e?t.slice(-1)[0]:t[e]},t.prototype.setValue=function(t,e,r,o){if(void 0===o&&(o="."),!t||!r)return null;try{var n=this.generatePathPieces(t,o),i=n.pop();n.finalKey=i;var s=n.reduce(this.getTargetPropToSet,r)||null;""===i?s.push(e):s[i]=e}catch(a){console.error("NestedProps setValue error: ",a)}},t.prototype.isArrayKey=function(t){return""===t||!isNaN(parseInt(t,10))},t.prototype.generatePathPieces=function(t,e){return t.replace("[",e).replace("]","").split(e)},t.decorators=[{type:e.Injectable}],t}(),F=function(){function t(t,e,r,o,n,i){this.modelSetter=t,this.templateRef=e,this.viewContainerRef=r,this.formHierarchy=i,this.isDestroyed=!1,this.ready$=new a.ReplaySubject(1),this.formOrService=n||o}return t.prototype.ngOnInit=function(){this.modelPath=this.getModelPathWithoutFirstPart(),this.modelPieces=this.modelPath&&this.modelPath.split("."),this.controlName=this.getControlName(),this.formControl=this.getFormControl(),this.registerToFormGroup(),this.subscribeToUpdateEntityValue(),this.showViewContent(),this.emitReadyState()},t.prototype.ngOnDestroy=function(){this.isDestroyed=!0,this.ready$.complete(),this.parentFormGroup&&this.removeFromParentFormGroup()},t.prototype.removeFromParentFormGroup=function(){var e=this;if(this.parentFormGroup instanceof s.FormGroup)this.parentFormGroup.removeControl(this.nrfModelName);else{var t=this.parentFormGroup.controls.findIndex(function(t){return e.formControl===t});this.parentFormGroup.removeAt(t)}},t.prototype.getModelPathWithoutFirstPart=function(){var t=this.nrfModelName;return t&&t.substr(t.indexOf(".")+1)},t.prototype.getControlName=function(){var t=this.modelPieces;return t&&t[t.length-1]},t.prototype.getNewFormControl=function(){var t=this.controlOptions&&this.controlOptions.disabled,e=this.getInitialValue();return new s.FormControl({value:e,disabled:t},this.controlOptions)},t.prototype.getInitialValue=function(){return this.formOrService&&this.modelPath?this.modelSetter.getValue(this.modelPath,this.formOrService.formData):null},t.prototype.showViewContent=function(){var t=new d(this.formControl,this.formOrService&&this.formOrService.formGroup,this);this.viewContainerRef.createEmbeddedView(this.templateRef,t)},t.prototype.registerToFormGroup=function(){if(!this.parentFormGroup&&(this.parentFormGroup=this.getParentFormGroup(),this.parentFormGroup))if(this.parentFormGroup instanceof s.FormGroup)this.parentFormGroup.addControl(this.modelPath,this.formControl);else{var t=this.controlName;this.parentFormGroup.insert(parseInt(t,10),this.formControl)}},t.prototype.getParentFormGroup=function(){var t=this.formOrService&&this.formOrService.formGroup;if(!t||!this.modelPath)return null;var e=Array.from(this.modelPieces);return this.formHierarchy.getNestedControl(t,e)},t.prototype.getFormControl=function(){var t=this.getParentFormGroup(),e=t&&this.nrfModelName&&t.get(this.nrfModelName);return e||(e=this.getNewFormControl()),e},t.prototype.subscribeToUpdateEntityValue=function(){var e=this;this.formControl.valueChanges.pipe(r.takeWhile(function(){return!e.isDestroyed})).subscribe(function(t){return e.setModelValue(t)})},t.prototype.setModelValue=function(t){if(this.formOrService&&this.modelPath)return this.modelSetter.setValue(this.modelPath,t||"",this.formOrService.formData)},t.prototype.emitReadyState=function(){this.ready$.next(this)},t.decorators=[{type:e.Directive,args:[{selector:"[nrfNestedControl]",exportAs:"nrfNestedControl"}]}],t.ctorParameters=function(){return[{type:b},{type:e.TemplateRef},{type:e.ViewContainerRef},{type:c,decorators:[{type:e.Optional}]},{type:p,decorators:[{type:e.Optional}]},{type:y}]},t.propDecorators={nrfModelName:[{type:e.Input,args:["nrfNestedControl"]}],controlOptions:[{type:e.Input,args:["nrfNestedControlControlOptions"]}],ready$:[{type:e.Output}]},t}(),O=function(){function t(){}return t.decorators=[{type:e.NgModule,args:[{imports:[o.CommonModule],providers:[b,y],declarations:[F],exports:[F]}]}],t}(),C=function(){function t(){}return t.decorators=[{type:e.NgModule,args:[{exports:[m,O]}]}],t}();t.NrFormsModule=C,t.cloneDeep=u,t.NrfFormDirective=c,t.NrfFormContext=n,t.NrfSubmitData=i,t.NrfFormService=p,t.NRF_FORM_SERVICE_PROVIDER=f,t.NrfFormModule=m,t.NrfNestedControlDirective=F,t.NrfNestedControlContext=d,t.NrfModelSetterService=b,t.NrfControlOptionsComponent=h,t.NrfFormHierarchyService=y,t.NrfModelModule=O,t.ɵa=l,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=webdev-tools-ng-nested-reactive-forms.umd.min.js.map