rx-form-mapper
Version:
Proper decorator-based transformation / serialization / deserialization of plain javascript classes to angular reactive forms
16 lines (14 loc) • 11.3 kB
JavaScript
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("reflect-metadata"),require("lodash"),require("@angular/core"),require("@angular/forms")):"function"==typeof define&&define.amd?define("rx-form-mapper",["exports","reflect-metadata","lodash","@angular/core","@angular/forms"],r):r((t="undefined"!=typeof globalThis?globalThis:t||self)["rx-form-mapper"]={},null,t.lodash,t.ng.core,t.ng.forms)}(this,(function(t,r,o,e,n){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */var a=function(t,r){return(a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,r){t.__proto__=r}||function(t,r){for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o])})(t,r)};function i(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function o(){this.constructor=t}a(t,r),t.prototype=null===r?Object.create(r):(o.prototype=r.prototype,new o)}Object.create;function s(t){var r="function"==typeof Symbol&&Symbol.iterator,o=r&&t[r],e=0;if(o)return o.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&e>=t.length&&(t=void 0),{value:t&&t[e++],done:!t}}};throw new TypeError(r?"Object is not iterable.":"Symbol.iterator is not defined.")}function c(t,r){var o="function"==typeof Symbol&&t[Symbol.iterator];if(!o)return t;var e,n,a=o.call(t),i=[];try{for(;(void 0===r||r-- >0)&&!(e=a.next()).done;)i.push(e.value)}catch(t){n={error:t}}finally{try{e&&!e.done&&(o=a.return)&&o.call(a)}finally{if(n)throw n.error}}return i}Object.create;var u=o.negate(o.isNil);function l(t){return o.castArray(t).filter(u)}function p(t){return o.isFunction(t)}var f,d=function(){function t(){this._validators=[],this._asyncValidators=[]}return t.prototype.setValidators=function(t){this._validators=l(null==t?void 0:t.validators),this._asyncValidators=l(null==t?void 0:t.asyncValidators),this._updateOn=null==t?void 0:t.updateOn},Object.defineProperty(t.prototype,"validators",{get:function(){return this._validators},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"asyncValidators",{get:function(){return this._asyncValidators},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"updateOn",{get:function(){return this._updateOn},enumerable:!1,configurable:!0}),t}(),y=function(t){function r(r){var o=t.call(this)||this;return o.mapper=r,o}return i(r,t),r.prototype.accept=function(t){return t.visitCustomControlMetadata(this)},r}(d),m=function(t){function r(r){var o=t.call(this)||this;return o.itemForm=r,o}return i(r,t),r.prototype.accept=function(t){return t.visitFormArrayMetadata(this)},r}(d),h=function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return i(r,t),r.prototype.accept=function(t){return t.visitFormControlMetadata(this)},r}(d),v=function(){function t(t){this.form=t}return t.prototype.accept=function(t){return t.visitFormGroupMetadata(this)},t}(),M=function(t){function r(r){var o=t.call(this)||this;return o.type=r,o._controls={},o}return i(r,t),r.prototype.accept=function(t){return t.visitFormMetadata(this)},Object.defineProperty(r.prototype,"controls",{get:function(){return this._controls},enumerable:!1,configurable:!0}),r.prototype.setFormControl=function(t,r){var o=new h;o.setValidators(r),this.controls[t]=o},r.prototype.setFormGroup=function(t,r){var o=new v(r);this.controls[t]=o},r.prototype.setCustomControl=function(t,r,o){var e=new y(r);e.setValidators(o),this.controls[t]=e},r.prototype.setFormArray=function(t,r,o){var e=new m(r);e.setValidators(o),this.controls[t]=e},r}(d),b=function(){function r(){this.metadataKey="rx-form-mapper-metadata"}return r.prototype.getMetadata=function(t){return Reflect.hasMetadata(this.metadataKey,t)||Reflect.defineMetadata(this.metadataKey,new M(t),t),Reflect.getMetadata(this.metadataKey,t)},r.prototype.bindForm=function(t,r){this.getMetadata(t).setValidators(r)},r.prototype.bindCustomControl=function(t,r,o){this.getMetadata(t.constructor).setCustomControl(r,o.mapper,o)},r.prototype.bindFormControl=function(t,r,o){this.getMetadata(t.constructor).setFormControl(r,o)},r.prototype.bindFormGroup=function(r,o,e){var n=null!=e?e:Reflect.getMetadata(t.MetadataDesignTypes.TYPE,r,o),a=this.getMetadata(n);this.getMetadata(r.constructor).setFormGroup(o,a)},r.prototype.bindFormArray=function(t,r,o){var e=this.getMetadata(o.type);this.getMetadata(t.constructor).setFormArray(r,e,o)},r}();b.instance=new b,(f=t.MetadataDesignTypes||(t.MetadataDesignTypes={})).TYPE="design:type",f.RETURN_TYPE="design:returntype",f.PARAM_TYPE="design:paramtypes";var F=function(){function t(t){this.injector=t}return t.prototype.resolve=function(t){var r;return null!==(r=this.injector.get(t,null,e.InjectFlags.Optional))&&void 0!==r?r:new t},t}();F.decorators=[{type:e.Injectable}],F.ctorParameters=function(){return[{type:e.Injector}]};var g=function(){function t(t,r){this.control=t,this.customMapperResolver=r}return t.prototype.visitCustomControlMetadata=function(t){return this.customMapperResolver.resolve(t.mapper).readForm(this.control)},t.prototype.visitFormArrayMetadata=function(t){var r=this;if(this.control)return this.checkControlType(this.control,n.FormArray),this.control.controls.map((function(o){var e=r.copyPrototype(o);return t.itemForm.accept(e)}))},t.prototype.visitFormControlMetadata=function(t){if(this.control)return this.checkControlType(this.control,n.FormControl),this.control.value},t.prototype.visitFormGroupMetadata=function(t){return this.visitFormMetadata(t.form)},t.prototype.visitFormMetadata=function(t){var r,o;if(this.control){this.checkControlType(this.control,n.FormGroup);var e=new t.type,a=this.control;try{for(var i=s(Object.entries(t.controls)),u=i.next();!u.done;u=i.next()){var l=c(u.value,2),p=l[0],f=l[1],d=a.controls[p],y=this.copyPrototype(d);e[p]=f.accept(y)}}catch(t){r={error:t}}finally{try{u&&!u.done&&(o=i.return)&&o.call(i)}finally{if(r)throw r.error}}return e}},t.prototype.copyPrototype=function(r){return new t(r,this.customMapperResolver)},t.prototype.checkControlType=function(t,r){if(!(t instanceof r))throw new Error("control is not "+r.name+" instance")},t}(),w=function(){function t(t,r,o){this.value=t,this.customMapperResolver=r,this.validatorResolver=o}return t.prototype.visitCustomControlMetadata=function(t){return this.customMapperResolver.resolve(t.mapper).writeForm(this.value,this.buildAbstractControlOptions(t))},t.prototype.visitFormArrayMetadata=function(t){var r,o,e,a=null!==(e=this.value)&&void 0!==e?e:[],i=[];try{for(var c=s(a),u=c.next();!u.done;u=c.next()){var l=u.value,p=this.copyPrototype(l),f=t.itemForm.accept(p);i.push(f)}}catch(t){r={error:t}}finally{try{u&&!u.done&&(o=c.return)&&o.call(c)}finally{if(r)throw r.error}}return new n.FormArray(i,this.buildAbstractControlOptions(t))},t.prototype.visitFormControlMetadata=function(t){return new n.FormControl(this.value,this.buildAbstractControlOptions(t))},t.prototype.visitFormGroupMetadata=function(t){return this.visitFormMetadata(t.form)},t.prototype.visitFormMetadata=function(t){var r,o,e,a={};try{for(var i=s(Object.entries(t.controls)),u=i.next();!u.done;u=i.next()){var l=c(u.value,2),p=l[0],f=l[1],d=null===(e=this.value)||void 0===e?void 0:e[p],y=this.copyPrototype(d);a[p]=f.accept(y)}}catch(t){r={error:t}}finally{try{u&&!u.done&&(o=i.return)&&o.call(i)}finally{if(r)throw r.error}}return new n.FormGroup(a,this.buildAbstractControlOptions(t))},t.prototype.buildAbstractControlOptions=function(t){var r=this;return{validators:t.validators.map((function(t){return r.validatorResolver.resolve(t)})),asyncValidators:t.asyncValidators.map((function(t){return r.validatorResolver.resolve(t)})),updateOn:t.updateOn}},t.prototype.copyPrototype=function(r){return new t(r,this.customMapperResolver,this.validatorResolver)},t}(),C=function(){function t(t){this.injector=t}return t.prototype.resolve=function(t){if(this.isValidatorFn(t))return t;var r=null;return this.isValidatorInstance(t)?r=t:null!=(r=this.injector.get(t,null,e.InjectFlags.Optional))||(r=new t),function(t){return r.validate(t)}},t.prototype.isValidatorFn=function(t){return o.isFunction(t)&&!t.prototype.validate},t.prototype.isValidatorInstance=function(t){return"validate"in t},t}();C.decorators=[{type:e.Injectable}],C.ctorParameters=function(){return[{type:e.Injector}]};var O=function(){function t(t,r){this.customMapperResolver=t,this.validatorResolver=r}return t.prototype.fromType=function(t){if(o.isNil(t))throw new Error("type cannot be inferred implicitly");var r=new w(void 0,this.customMapperResolver,this.validatorResolver);return b.instance.getMetadata(t).accept(r)},t.prototype.writeForm=function(t,r){if(o.isNil(t)&&o.isNil(r))throw new Error("type cannot be inferred implicitly");var e=null!=r?r:Object.getPrototypeOf(t).constructor,n=new w(t,this.customMapperResolver,this.validatorResolver);return b.instance.getMetadata(e).accept(n)},t.prototype.readForm=function(t,r){if(o.isNil(r))throw new Error("type cannot be inferred implicitly");var e=new g(t,this.customMapperResolver);return b.instance.getMetadata(r).accept(e)},t}();O.decorators=[{type:e.Injectable}],O.ctorParameters=function(){return[{type:F},{type:C}]};var R=function(){function t(t){if(t)throw new Error("RxFormMapperModule is already loaded. Import it in the AppModule only")}return t.forRoot=function(){return{ngModule:t,providers:[O,F,C]}},t}();R.decorators=[{type:e.NgModule}],R.ctorParameters=function(){return[{type:R,decorators:[{type:e.Optional},{type:e.SkipSelf}]}]},t.CustomControl=function(t){return function(r,e){if(o.isNil(t))throw new Error("unexpected CustomControl configuration: "+t);"object"!=typeof t&&(t={mapper:t}),b.instance.bindCustomControl(r,e,t)}},t.CustomControlMetadata=y,t.CustomMapperResolver=F,t.Form=function(t){return function(r){b.instance.bindForm(r,t)}},t.FormArray=function(t){return function(r,e){if(o.isNil(t))throw new Error("unexpected FormArray configuration: "+t);p(t)&&(t={type:t}),b.instance.bindFormArray(r,e,t)}},t.FormArrayMetadata=m,t.FormControl=function(t){return function(r,o){b.instance.bindFormControl(r,o,t)}},t.FormControlMetadata=h,t.FormGroup=function(t){return function(r,o){b.instance.bindFormGroup(r,o,t)}},t.FormGroupMetadata=v,t.FormMetadata=M,t.FormReader=g,t.FormWriter=w,t.ModelBinder=b,t.RxFormMapper=O,t.RxFormMapperModule=R,t.ValidableMetadata=d,t.ValidatorResolver=C,t.coerceArray=l,t.isType=p,t.ɵa=O,t.ɵb=F,t.ɵc=C,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=rx-form-mapper.umd.min.js.map