UNPKG

42.9 kBJavaScriptView Raw
1import { Directive, TemplateRef, ViewContainerRef, Input, EventEmitter, Output, forwardRef, SkipSelf, HostListener, SimpleChange, NgModule } from '@angular/core';
2import { FormControl, FormGroup, FormArray, FormGroupDirective, ControlContainer, FormGroupName, FormArrayName, ReactiveFormsModule } from '@angular/forms';
3import { BehaviorSubject, Subject } from 'rxjs';
4import { takeUntil } from 'rxjs/operators';
5import { CommonModule } from '@angular/common';
6
7/**
8 * @fileoverview added by tsickle
9 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
10 */
11class EclatRecursiveConfig {
12}
13
14/**
15 * @fileoverview added by tsickle
16 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
17 */
18
19/**
20 * @fileoverview added by tsickle
21 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
22 */
23var EclatFormUtility;
24(function (EclatFormUtility) {
25 /**
26 * @template T
27 * @param {?} eclatForm
28 * @return {?}
29 */
30 function isEclatFormControlConfig(eclatForm) {
31 if (Array.isArray(eclatForm)) {
32 return true;
33 }
34 return false;
35 }
36 EclatFormUtility.isEclatFormControlConfig = isEclatFormControlConfig;
37 /**
38 * @template T
39 * @param {?} eclatForm
40 * @return {?}
41 */
42 function isEclatFormGroupConfig(eclatForm) {
43 if (((/** @type {?} */ (eclatForm))).controlsRules) {
44 return true;
45 }
46 return false;
47 }
48 EclatFormUtility.isEclatFormGroupConfig = isEclatFormGroupConfig;
49 /**
50 * @template T
51 * @param {?} eclatForm
52 * @return {?}
53 */
54 function isEclatFormArrayConfig(eclatForm) {
55 if (((/** @type {?} */ (eclatForm))).controlConfig) {
56 return true;
57 }
58 return false;
59 }
60 EclatFormUtility.isEclatFormArrayConfig = isEclatFormArrayConfig;
61 /**
62 * @template T
63 * @param {?} data
64 * @return {?}
65 */
66 function eclatRecursiveTransformer(data) {
67 /** @type {?} */
68 const path = recursivePathFinder(data);
69 if (!path) {
70 throw Error();
71 }
72 /** @type {?} */
73 const paths = path.split('.');
74 /** @type {?} */
75 const latestPath = (/** @type {?} */ (paths.pop()));
76 paths.reduce((/**
77 * @param {?} acc
78 * @param {?} cur
79 * @return {?}
80 */
81 (acc, cur) => {
82 return acc[cur];
83 }), (/** @type {?} */ (data)))[latestPath] = data;
84 return (/** @type {?} */ (data));
85 }
86 EclatFormUtility.eclatRecursiveTransformer = eclatRecursiveTransformer;
87})(EclatFormUtility || (EclatFormUtility = {}));
88/**
89 * @param {?} data
90 * @param {?=} path
91 * @return {?}
92 */
93function recursivePathFinder(data, path = null) {
94 if (data instanceof EclatRecursiveConfig) {
95 return '';
96 }
97 if (Array.isArray(data)) {
98 /** @type {?} */
99 let found = null;
100 data.some((/**
101 * @param {?} value
102 * @param {?} key
103 * @return {?}
104 */
105 (value, key) => {
106 /** @type {?} */
107 const temp = recursivePathFinder(value, path);
108 if (temp !== null) {
109 if (temp) {
110 found = key + '.' + temp;
111 }
112 else {
113 found = key.toString();
114 }
115 }
116 return !!found;
117 }));
118 return found;
119 }
120 if (data === Object(data)) {
121 /** @type {?} */
122 let found = null;
123 Object.entries(data).some((/**
124 * @param {?} __0
125 * @return {?}
126 */
127 ([key, value]) => {
128 /** @type {?} */
129 const temp = recursivePathFinder(value, path);
130 if (temp !== null) {
131 if (temp) {
132 found = key + '.' + temp;
133 }
134 else {
135 found = key;
136 }
137 }
138 return !!found;
139 }));
140 return found;
141 }
142 return null;
143}
144
145/**
146 * @fileoverview added by tsickle
147 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
148 */
149/**
150 * @abstract
151 * @template T, C
152 */
153class AbstractEclatFormControl {
154 /**
155 * @param {?=} rules
156 */
157 constructor(rules) {
158 /** @type {?} */
159 const validators = [];
160 /** @type {?} */
161 const asyncValidators = [];
162 /** @type {?} */
163 const errorMapping = {};
164 if (rules) {
165 rules.forEach((/**
166 * @param {?} __0
167 * @return {?}
168 */
169 ({ validator, asyncValidator, key, message }) => {
170 errorMapping[key] = message;
171 if (validator) {
172 validators.push(validator);
173 }
174 if (asyncValidator) {
175 asyncValidators.push(asyncValidator);
176 }
177 }));
178 }
179 this.rules = errorMapping;
180 this.validators = validators;
181 this.asyncValidators = asyncValidators;
182 }
183 /**
184 * @return {?}
185 */
186 get errorMessages() {
187 if (!this.control.errors) {
188 return [];
189 }
190 return Object.entries(this.control.errors).reduce((/**
191 * @param {?} acc
192 * @param {?} __1
193 * @return {?}
194 */
195 (acc, [key, value]) => {
196 if (value) {
197 /** @type {?} */
198 const temp = this.rules[key];
199 /** @type {?} */
200 const errors = typeof temp === 'function' ? temp(this) : temp;
201 if (Array.isArray(errors)) {
202 acc.push(...errors);
203 }
204 else {
205 acc.push(errors);
206 }
207 }
208 return acc;
209 }), (/** @type {?} */ ([])));
210 }
211 /**
212 * @return {?}
213 */
214 get value() {
215 return this.control.value;
216 }
217 /**
218 * @return {?}
219 */
220 get valid() {
221 return this.control.valid;
222 }
223 /**
224 * @return {?}
225 */
226 get valueChanges() {
227 return this.control.valueChanges;
228 }
229 /**
230 * @param {?=} isDisabled
231 * @param {?=} opts
232 * @return {?}
233 */
234 disable(isDisabled = true, opts) {
235 if (isDisabled) {
236 this.control.disable(opts);
237 }
238 else {
239 this.control.enable(opts);
240 }
241 }
242}
243
244/**
245 * @fileoverview added by tsickle
246 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
247 */
248/**
249 * @param {?} config
250 * @param {?=} data
251 * @return {?}
252 */
253function getEclatControl(config, data) {
254 if (EclatFormUtility.isEclatFormControlConfig(config)) {
255 return new EclatFormControl(config, data);
256 }
257 if (EclatFormUtility.isEclatFormGroupConfig(config)) {
258 return new EclatFormGroup(config, data);
259 }
260 if (EclatFormUtility.isEclatFormArrayConfig(config)) {
261 return new EclatFormArray(config, data);
262 }
263 return null;
264}
265/**
266 * @template T
267 */
268class EclatFormControl extends AbstractEclatFormControl {
269 /**
270 * @param {?} config
271 * @param {?=} initialData
272 */
273 constructor(config, initialData) {
274 super(config);
275 this.control = new FormControl(null, this.validators, this.asyncValidators);
276 if (initialData) {
277 this.resetState(initialData);
278 }
279 }
280 /**
281 * @param {?} data
282 * @param {?=} emitEvent
283 * @return {?}
284 */
285 patchState(data, emitEvent = true) {
286 this.control.patchValue(data, { emitEvent });
287 }
288 /**
289 * @param {?} data
290 * @param {?=} emitEvent
291 * @return {?}
292 */
293 resetState(data, emitEvent = true) {
294 this.control.reset(data, { emitEvent });
295 }
296}
297/**
298 * @template T
299 */
300class EclatFormGroup extends AbstractEclatFormControl {
301 /**
302 * @param {?} dfg
303 * @param {?=} initialData
304 */
305 constructor(dfg, initialData) {
306 super(dfg.rules);
307 /** @type {?} */
308 const children = {};
309 /** @type {?} */
310 const controls = {};
311 Object.keys(dfg.controlsRules).forEach((/**
312 * @param {?} key
313 * @return {?}
314 */
315 (key) => {
316 /** @type {?} */
317 const value = dfg.controlsRules[(/** @type {?} */ (key))];
318 /** @type {?} */
319 const eclatControl = getEclatControl(value);
320 if (eclatControl) {
321 children[key] = eclatControl;
322 controls[key] = eclatControl.control;
323 }
324 }));
325 this.control = new FormGroup(controls, this.validators, this.asyncValidators);
326 this.children = children;
327 if (initialData) {
328 this.resetState(initialData);
329 }
330 }
331 /**
332 * @param {?} data
333 * @param {?=} emitEvent
334 * @return {?}
335 */
336 patchState(data, emitEvent = true) {
337 if (!data) {
338 console.warn(`Expect an object, but ${data} is given`);
339 return;
340 }
341 Object.entries(data).forEach((/**
342 * @param {?} __0
343 * @return {?}
344 */
345 ([key, value]) => {
346 if (this.children[(/** @type {?} */ (key))]) {
347 this.children[(/** @type {?} */ (key))].patchState(value, false);
348 }
349 }));
350 this.control.patchValue({}, { emitEvent });
351 }
352 /**
353 * @param {?} data
354 * @param {?=} emitEvent
355 * @return {?}
356 */
357 resetState(data, emitEvent = true) {
358 if (!data) {
359 console.warn(`Expect an object, but ${data} is given`);
360 return;
361 }
362 Object.entries(data).forEach((/**
363 * @param {?} __0
364 * @return {?}
365 */
366 ([key, value]) => {
367 if (this.children[(/** @type {?} */ (key))]) {
368 this.children[(/** @type {?} */ (key))].resetState(value, false);
369 }
370 }));
371 this.control.reset(undefined, { emitEvent });
372 }
373 /**
374 * @template P
375 * @param {?} key
376 * @return {?}
377 */
378 getChild(key) {
379 return this.children[key];
380 }
381 /**
382 * @private
383 * @param {?} key
384 * @param {?} config
385 * @param {?} data
386 * @return {?}
387 */
388 addChild(key, config, data) {
389 /** @type {?} */
390 const eclatControl = getEclatControl(config);
391 if (eclatControl) {
392 eclatControl.resetState(data);
393 this.children[key] = (/** @type {?} */ (eclatControl));
394 this.control.addControl(key, this.children[key].control);
395 }
396 }
397 /**
398 * @private
399 * @param {?} key
400 * @return {?}
401 */
402 removeChild(key) {
403 delete this.children[key];
404 this.control.removeControl(key);
405 }
406}
407/**
408 * @template T
409 */
410class EclatFormArray extends AbstractEclatFormControl {
411 /**
412 * @param {?} dfa
413 * @param {?=} initialData
414 */
415 constructor(dfa, initialData) {
416 super(dfa.rules);
417 this.control = new FormArray([], this.validators, this.asyncValidators);
418 this.childConfig = dfa.controlConfig;
419 this.children = [];
420 if (initialData) {
421 this.resetState(initialData);
422 }
423 }
424 /**
425 * @param {?=} data
426 * @return {?}
427 */
428 addChild(data) {
429 /** @type {?} */
430 const eclatControl = getEclatControl(this.childConfig);
431 if (eclatControl) {
432 eclatControl.resetState(data);
433 this.children.push((/** @type {?} */ (eclatControl)));
434 this.control.push(eclatControl.control);
435 }
436 }
437 /**
438 * @param {?} index
439 * @return {?}
440 */
441 removeChild(index) {
442 this.children.splice(index, 1);
443 this.control.removeAt(index);
444 }
445 /**
446 * @param {?} data
447 * @param {?=} emitEvent
448 * @return {?}
449 */
450 patchState(data, emitEvent = true) {
451 if (!data) {
452 console.warn(`Expect an array, but ${data} is given`);
453 return;
454 }
455 this.control.controls = [];
456 this.children = [];
457 data.forEach((/**
458 * @param {?} d
459 * @return {?}
460 */
461 d => {
462 /** @type {?} */
463 const eclatControl = getEclatControl(this.childConfig);
464 if (eclatControl) {
465 eclatControl.patchState(d, false);
466 this.children.push((/** @type {?} */ (eclatControl)));
467 this.control.push(eclatControl.control);
468 }
469 }));
470 this.control.patchValue([], { emitEvent });
471 }
472 /**
473 * @param {?} data
474 * @param {?=} emitEvent
475 * @return {?}
476 */
477 resetState(data, emitEvent = true) {
478 if (!data) {
479 console.warn(`Expect an array, but ${data} is given`);
480 return;
481 }
482 this.control.controls = [];
483 this.children = [];
484 data.forEach((/**
485 * @param {?} d
486 * @return {?}
487 */
488 d => {
489 /** @type {?} */
490 const eclatControl = getEclatControl(this.childConfig);
491 if (eclatControl) {
492 eclatControl.resetState(d, false);
493 this.children.push((/** @type {?} */ (eclatControl)));
494 this.control.push(eclatControl.control);
495 }
496 }));
497 this.control.reset(undefined, { emitEvent });
498 }
499 /**
500 * @param {?} index
501 * @return {?}
502 */
503 getChild(index) {
504 return this.children[index];
505 }
506}
507
508/**
509 * @fileoverview added by tsickle
510 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
511 */
512/**
513 * @template T
514 */
515class EclatFormContext {
516 /**
517 * @param {?} $implicit
518 */
519 constructor($implicit) {
520 this.$implicit = $implicit;
521 }
522}
523/**
524 * @template T
525 */
526class EclatFormOfDirective {
527 /**
528 * @param {?} templateRef
529 * @param {?} viewContainer
530 */
531 constructor(templateRef, viewContainer) {
532 this.templateRef = templateRef;
533 this.viewContainer = viewContainer;
534 }
535 /**
536 * @param {?} state
537 * @return {?}
538 */
539 set eclatFormOf(state) {
540 if (!this.context || !this.latestState || this.latestState.config !== state.config) {
541 this.setContext(getEclatControl(state.config, state.data));
542 }
543 else if (this.latestState.data !== state.data) {
544 this.context.$implicit.resetState(state.data);
545 }
546 this.latestState = state;
547 }
548 /**
549 * @private
550 * @param {?=} control
551 * @return {?}
552 */
553 setContext(control) {
554 if (!control) {
555 throw new Error('control not found');
556 }
557 if (!this.context) {
558 this.context = new EclatFormContext(control);
559 this.viewContainer.createEmbeddedView(this.templateRef, this.context);
560 }
561 else {
562 this.context.$implicit = control;
563 }
564 }
565}
566EclatFormOfDirective.decorators = [
567 { type: Directive, args: [{ selector: '[eclatForm][eclatFormOf]' },] }
568];
569/** @nocollapse */
570EclatFormOfDirective.ctorParameters = () => [
571 { type: TemplateRef },
572 { type: ViewContainerRef }
573];
574EclatFormOfDirective.propDecorators = {
575 eclatFormOf: [{ type: Input }]
576};
577
578/**
579 * @fileoverview added by tsickle
580 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
581 */
582/**
583 * @abstract
584 * @template T
585 */
586class EclatFormContainerDirective {
587 /**
588 * @protected
589 */
590 constructor() {
591 this._formControl = new BehaviorSubject(undefined);
592 this.onSubmit = new EventEmitter();
593 }
594 /**
595 * @return {?}
596 */
597 get formControl() {
598 return this._formControl.getValue();
599 }
600 /**
601 * @return {?}
602 */
603 get formControl$() {
604 return this._formControl.asObservable();
605 }
606 /**
607 * @return {?}
608 */
609 submit() {
610 if (this.formControl) {
611 this.onSubmit.emit(this.formControl.value);
612 }
613 }
614}
615EclatFormContainerDirective.propDecorators = {
616 onSubmit: [{ type: Output }]
617};
618
619/**
620 * @fileoverview added by tsickle
621 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
622 */
623/**
624 * @template T
625 */
626class EclatFormErrorContext {
627 /**
628 * @param {?} control
629 */
630 constructor(control) {
631 this.control = control;
632 }
633 /**
634 * @return {?}
635 */
636 get $implicit() {
637 return this.control.errorMessages;
638 }
639}
640/**
641 * @template T
642 */
643class EclatFormErrorDirective {
644 /**
645 * @param {?} templateRef
646 * @param {?} viewContainer
647 * @param {?} parent
648 */
649 constructor(templateRef, viewContainer, parent) {
650 this.templateRef = templateRef;
651 this.viewContainer = viewContainer;
652 this.parent = parent;
653 this.onDestroy$ = new Subject();
654 this.errorMatcher = (/**
655 * @return {?}
656 */
657 () => true);
658 }
659 /**
660 * @param {?} control
661 * @return {?}
662 */
663 set eclatControl(control) {
664 this.setContext(control);
665 }
666 /**
667 * @param {?} controlName
668 * @return {?}
669 */
670 set eclatControlName(controlName) {
671 if (!this.parent.formControl) {
672 throw new Error('parent formControl not exist');
673 }
674 /** @type {?} */
675 const instance = !controlName ? this.parent.formControl : this.parent.formControl.getChild(controlName);
676 if (!instance) {
677 throw new Error('control not found');
678 return;
679 }
680 this.latestControlName = controlName;
681 this.setContext(instance);
682 }
683 /**
684 * @param {?} control
685 * @return {?}
686 */
687 set eclatFormError(control) {
688 if (control instanceof AbstractEclatFormControl) {
689 this.setContext(control);
690 }
691 else {
692 this.eclatControlName = control;
693 }
694 }
695 /**
696 * @return {?}
697 */
698 ngOnInit() {
699 this.parent.formControl$.pipe(takeUntil(this.onDestroy$)).subscribe((/**
700 * @param {?} formControl
701 * @return {?}
702 */
703 (formControl) => {
704 if (formControl && this.latestControlName) {
705 this.eclatFormError = formControl.getChild(this.latestControlName);
706 }
707 }));
708 }
709 /**
710 * @return {?}
711 */
712 ngOnDestroy() {
713 this.onDestroy$.next();
714 }
715 /**
716 * @private
717 * @param {?} control
718 * @return {?}
719 */
720 setContext(control) {
721 if (!this.context) {
722 this.context = new EclatFormErrorContext(control);
723 this.viewContainer.createEmbeddedView(this.templateRef, this.context);
724 }
725 else if (this.context.control !== control) {
726 this.context.control = control;
727 }
728 if (this.valueChangesSub) {
729 this.valueChangesSub.unsubscribe();
730 }
731 this.checkValidity(control);
732 this.valueChangesSub = control.valueChanges.pipe(takeUntil(this.onDestroy$)).subscribe((/**
733 * @return {?}
734 */
735 () => {
736 this.checkValidity(control);
737 }));
738 }
739 /**
740 * @private
741 * @param {?} control
742 * @return {?}
743 */
744 checkValidity(control) {
745 /** @type {?} */
746 const isAttached = !!this.viewContainer.length;
747 if (!control.valid && this.errorMatcher(control)) {
748 if (!isAttached) {
749 this.viewContainer.createEmbeddedView(this.templateRef, this.context);
750 }
751 }
752 else {
753 if (isAttached) {
754 this.viewContainer.remove();
755 }
756 }
757 }
758}
759EclatFormErrorDirective.decorators = [
760 { type: Directive, args: [{ selector: '[eclatFormError]' },] }
761];
762/** @nocollapse */
763EclatFormErrorDirective.ctorParameters = () => [
764 { type: TemplateRef },
765 { type: ViewContainerRef },
766 { type: EclatFormContainerDirective }
767];
768EclatFormErrorDirective.propDecorators = {
769 errorMatcher: [{ type: Input }],
770 eclatControl: [{ type: Input }],
771 eclatControlName: [{ type: Input }],
772 eclatFormError: [{ type: Input }]
773};
774
775/**
776 * @fileoverview added by tsickle
777 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
778 */
779/**
780 * @template T
781 */
782class EclatFormGroupDirective extends EclatFormContainerDirective {
783 constructor() {
784 super();
785 }
786 /**
787 * @param {?} formGroup
788 * @return {?}
789 */
790 set eclatFormGroup(formGroup) {
791 this._formControl.next(formGroup);
792 }
793 /**
794 * @param {?} key
795 * @return {?}
796 */
797 getChild(key) {
798 if (this.formControl) {
799 return this.formControl.getChild(key);
800 }
801 return undefined;
802 }
803}
804EclatFormGroupDirective.decorators = [
805 { type: Directive, args: [{
806 selector: '[eclatFormGroup]',
807 providers: [{
808 provide: EclatFormContainerDirective,
809 useExisting: forwardRef((/**
810 * @return {?}
811 */
812 () => EclatFormGroupDirective)),
813 }],
814 },] }
815];
816/** @nocollapse */
817EclatFormGroupDirective.ctorParameters = () => [];
818EclatFormGroupDirective.propDecorators = {
819 eclatFormGroup: [{ type: Input }]
820};
821
822/**
823 * @fileoverview added by tsickle
824 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
825 */
826/**
827 * @template T
828 */
829class EclatFormGroupNameDirective extends EclatFormContainerDirective {
830 /**
831 * @param {?} parent
832 */
833 constructor(parent) {
834 super();
835 this.parent = parent;
836 this.onDestroy$ = new Subject();
837 }
838 /**
839 * @param {?} name
840 * @return {?}
841 */
842 set eclatFormGroupName(name) {
843 this.latestName = name;
844 this._formControl.next((/** @type {?} */ (this.parent.getChild(name))));
845 }
846 /**
847 * @return {?}
848 */
849 ngOnDestroy() {
850 this.onDestroy$.next();
851 }
852 /**
853 * @return {?}
854 */
855 ngOnInit() {
856 this.parent.formControl$.pipe(takeUntil(this.onDestroy$)).subscribe((/**
857 * @return {?}
858 */
859 () => {
860 this.eclatFormGroupName = this.latestName;
861 }));
862 }
863 /**
864 * @param {?} key
865 * @return {?}
866 */
867 getChild(key) {
868 if (this.formControl) {
869 return this.formControl.getChild(key);
870 }
871 return undefined;
872 }
873}
874EclatFormGroupNameDirective.decorators = [
875 { type: Directive, args: [{
876 selector: '[eclatFormGroupName]',
877 providers: [{
878 provide: EclatFormContainerDirective,
879 useExisting: forwardRef((/**
880 * @return {?}
881 */
882 () => EclatFormGroupNameDirective)),
883 }],
884 },] }
885];
886/** @nocollapse */
887EclatFormGroupNameDirective.ctorParameters = () => [
888 { type: EclatFormContainerDirective, decorators: [{ type: SkipSelf }] }
889];
890EclatFormGroupNameDirective.propDecorators = {
891 eclatFormGroupName: [{ type: Input }]
892};
893
894/**
895 * @fileoverview added by tsickle
896 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
897 */
898/**
899 * @template T
900 */
901class EclatFormArrayNameDirective extends EclatFormContainerDirective {
902 /**
903 * @param {?} parent
904 */
905 constructor(parent) {
906 super();
907 this.parent = parent;
908 this.onDestroy$ = new Subject();
909 }
910 /**
911 * @param {?} name
912 * @return {?}
913 */
914 set eclatFormArrayName(name) {
915 this.latestName = name;
916 this._formControl.next((/** @type {?} */ (this.parent.getChild(name))));
917 }
918 /**
919 * @return {?}
920 */
921 ngOnDestroy() {
922 this.onDestroy$.next();
923 }
924 /**
925 * @return {?}
926 */
927 ngOnInit() {
928 this.parent.formControl$.pipe(takeUntil(this.onDestroy$)).subscribe((/**
929 * @return {?}
930 */
931 () => {
932 this.eclatFormArrayName = this.latestName;
933 }));
934 }
935 /**
936 * @param {?} key
937 * @return {?}
938 */
939 getChild(key) {
940 if (this.formControl) {
941 return this.formControl.getChild(key);
942 }
943 return undefined;
944 }
945}
946EclatFormArrayNameDirective.decorators = [
947 { type: Directive, args: [{
948 selector: '[eclatFormArrayName]',
949 providers: [{
950 provide: EclatFormContainerDirective,
951 useExisting: forwardRef((/**
952 * @return {?}
953 */
954 () => EclatFormArrayNameDirective)),
955 }],
956 },] }
957];
958/** @nocollapse */
959EclatFormArrayNameDirective.ctorParameters = () => [
960 { type: EclatFormContainerDirective, decorators: [{ type: SkipSelf }] }
961];
962EclatFormArrayNameDirective.propDecorators = {
963 eclatFormArrayName: [{ type: Input }]
964};
965
966/**
967 * @fileoverview added by tsickle
968 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
969 */
970/**
971 * @template T
972 */
973class EclatFormArrayDirective extends EclatFormContainerDirective {
974 constructor() {
975 super();
976 }
977 /**
978 * @param {?} formArray
979 * @return {?}
980 */
981 set eclatFormArray(formArray) {
982 this._formControl.next(formArray);
983 }
984 /**
985 * @param {?} key
986 * @return {?}
987 */
988 getChild(key) {
989 if (this.formControl) {
990 return this.formControl.getChild(key);
991 }
992 return undefined;
993 }
994}
995EclatFormArrayDirective.decorators = [
996 { type: Directive, args: [{
997 selector: '[eclatFormArray]',
998 providers: [{
999 provide: EclatFormContainerDirective,
1000 useExisting: forwardRef((/**
1001 * @return {?}
1002 */
1003 () => EclatFormArrayDirective)),
1004 }],
1005 },] }
1006];
1007/** @nocollapse */
1008EclatFormArrayDirective.ctorParameters = () => [];
1009EclatFormArrayDirective.propDecorators = {
1010 eclatFormArray: [{ type: Input }]
1011};
1012
1013/**
1014 * @fileoverview added by tsickle
1015 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1016 */
1017/**
1018 * @template T
1019 */
1020class EclatFormArrayChildrenDirective {
1021 /**
1022 * @param {?} templateRef
1023 * @param {?} viewContainer
1024 * @param {?} parent
1025 */
1026 constructor(templateRef, viewContainer, parent) {
1027 this.templateRef = templateRef;
1028 this.viewContainer = viewContainer;
1029 this.parent = parent;
1030 this.onDestroy$ = new Subject();
1031 this.latestChildren = [];
1032 }
1033 /**
1034 * @return {?}
1035 */
1036 ngOnDestroy() {
1037 this.onDestroy$.next();
1038 }
1039 /**
1040 * @return {?}
1041 */
1042 ngOnInit() {
1043 this.parent.formControl$.pipe(takeUntil(this.onDestroy$)).subscribe((/**
1044 * @param {?} formControl
1045 * @return {?}
1046 */
1047 (formControl) => {
1048 if (formControl) {
1049 if (this.valueChangeSubs) {
1050 this.valueChangeSubs.unsubscribe();
1051 }
1052 this.onChange(formControl);
1053 this.valueChangeSubs = formControl.valueChanges.pipe(takeUntil(this.onDestroy$)).subscribe((/**
1054 * @return {?}
1055 */
1056 () => {
1057 this.onChange(formControl);
1058 }));
1059 }
1060 }));
1061 }
1062 /**
1063 * @private
1064 * @param {?} control
1065 * @return {?}
1066 */
1067 onChange(control) {
1068 /** @type {?} */
1069 const children = [];
1070 /** @type {?} */
1071 const controlChildrenLength = control.children.length;
1072 control.children.forEach((/**
1073 * @param {?} c
1074 * @param {?} index
1075 * @return {?}
1076 */
1077 (c, index) => {
1078 /** @type {?} */
1079 const lastIndex = this.latestChildren.findIndex((/**
1080 * @param {?} o
1081 * @return {?}
1082 */
1083 o => o === c));
1084 /** @type {?} */
1085 const first = index === 0;
1086 /** @type {?} */
1087 const last = index === (controlChildrenLength - 1);
1088 if (lastIndex < 0) {
1089 this.viewContainer.createEmbeddedView(this.templateRef, { $implicit: index, last, first }, index);
1090 }
1091 else if (lastIndex !== index) {
1092 /** @type {?} */
1093 const embeddedView = (/** @type {?} */ ((/** @type {?} */ (this.viewContainer.get(lastIndex)))));
1094 embeddedView.context.$implicit = index;
1095 embeddedView.context.last = last;
1096 embeddedView.context.first = first;
1097 this.viewContainer.move(embeddedView, index);
1098 }
1099 children.push(c);
1100 }));
1101 while (this.viewContainer.length > controlChildrenLength) {
1102 this.viewContainer.remove();
1103 }
1104 this.latestChildren = children;
1105 }
1106}
1107EclatFormArrayChildrenDirective.decorators = [
1108 { type: Directive, args: [{
1109 selector: '[eclatFormArrayChildren]',
1110 },] }
1111];
1112/** @nocollapse */
1113EclatFormArrayChildrenDirective.ctorParameters = () => [
1114 { type: TemplateRef },
1115 { type: ViewContainerRef },
1116 { type: EclatFormContainerDirective }
1117];
1118
1119/**
1120 * @fileoverview added by tsickle
1121 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1122 */
1123class EclatFormSubmitDirective {
1124 /**
1125 * @param {?} parent
1126 */
1127 constructor(parent) {
1128 this.parent = parent;
1129 }
1130 /**
1131 * @return {?}
1132 */
1133 onClick() {
1134 this.parent.submit();
1135 }
1136}
1137EclatFormSubmitDirective.decorators = [
1138 { type: Directive, args: [{ selector: '[eclatFormSubmit]' },] }
1139];
1140/** @nocollapse */
1141EclatFormSubmitDirective.ctorParameters = () => [
1142 { type: EclatFormContainerDirective }
1143];
1144EclatFormSubmitDirective.propDecorators = {
1145 onClick: [{ type: HostListener, args: ['click', ['$event'],] }, { type: HostListener, args: ['tap', ['$event'],] }]
1146};
1147
1148/**
1149 * @fileoverview added by tsickle
1150 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1151 */
1152/**
1153 * @template T
1154 */
1155class EclatFormArrayAddDirective {
1156 /**
1157 * @param {?} parent
1158 */
1159 constructor(parent) {
1160 this.parent = parent;
1161 }
1162 /**
1163 * @return {?}
1164 */
1165 onClick() {
1166 /** @type {?} */
1167 const control = this.parent.formControl;
1168 if (control) {
1169 control.addChild(this.value);
1170 }
1171 }
1172}
1173EclatFormArrayAddDirective.decorators = [
1174 { type: Directive, args: [{
1175 selector: '[eclatFormAddChildren]',
1176 },] }
1177];
1178/** @nocollapse */
1179EclatFormArrayAddDirective.ctorParameters = () => [
1180 { type: EclatFormContainerDirective }
1181];
1182EclatFormArrayAddDirective.propDecorators = {
1183 value: [{ type: Input, args: ['eclatFormAddChildren',] }],
1184 onClick: [{ type: HostListener, args: ['click', ['$event'],] }]
1185};
1186
1187/**
1188 * @fileoverview added by tsickle
1189 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1190 */
1191/**
1192 * @template T
1193 */
1194class EclatFormValueContext {
1195 /**
1196 * @param {?} control
1197 */
1198 constructor(control) {
1199 this.control = control;
1200 }
1201 /**
1202 * @return {?}
1203 */
1204 get $implicit() {
1205 return this.control.value;
1206 }
1207}
1208/**
1209 * @template T
1210 */
1211class EclatFormValueDirective {
1212 /**
1213 * @param {?} templateRef
1214 * @param {?} viewContainer
1215 * @param {?} parent
1216 */
1217 constructor(templateRef, viewContainer, parent) {
1218 this.templateRef = templateRef;
1219 this.viewContainer = viewContainer;
1220 this.parent = parent;
1221 this.onDestroy$ = new Subject();
1222 }
1223 /**
1224 * @param {?} control
1225 * @return {?}
1226 */
1227 set eclatControl(control) {
1228 this.setContext(control);
1229 }
1230 /**
1231 * @param {?} controlName
1232 * @return {?}
1233 */
1234 set eclatControlName(controlName) {
1235 if (!this.parent.formControl) {
1236 throw new Error('parent formControl not exist');
1237 }
1238 /** @type {?} */
1239 const instance = this.parent.formControl.getChild(controlName);
1240 if (!instance) {
1241 throw new Error('control not found');
1242 return;
1243 }
1244 this.latestControlName = controlName;
1245 this.setContext(instance);
1246 }
1247 /**
1248 * @param {?} control
1249 * @return {?}
1250 */
1251 set eclatFormValue(control) {
1252 if (control instanceof AbstractEclatFormControl) {
1253 this.setContext(control);
1254 }
1255 else if (control || control === 0) {
1256 this.eclatControlName = control;
1257 }
1258 }
1259 /**
1260 * @return {?}
1261 */
1262 ngOnInit() {
1263 this.parent.formControl$.pipe(takeUntil(this.onDestroy$)).subscribe((/**
1264 * @param {?} formControl
1265 * @return {?}
1266 */
1267 (formControl) => {
1268 if (formControl && this.latestControlName) {
1269 this.eclatFormValue = formControl.getChild(this.latestControlName);
1270 }
1271 }));
1272 }
1273 /**
1274 * @return {?}
1275 */
1276 ngOnDestroy() {
1277 this.onDestroy$.next();
1278 }
1279 /**
1280 * @private
1281 * @param {?} control
1282 * @return {?}
1283 */
1284 setContext(control) {
1285 if (!this.context) {
1286 this.context = new EclatFormValueContext(control);
1287 this.viewContainer.createEmbeddedView(this.templateRef, this.context);
1288 }
1289 else if (this.context.control !== control) {
1290 this.context.control = control;
1291 }
1292 }
1293}
1294EclatFormValueDirective.decorators = [
1295 { type: Directive, args: [{ selector: '[eclatFormValue]' },] }
1296];
1297/** @nocollapse */
1298EclatFormValueDirective.ctorParameters = () => [
1299 { type: TemplateRef },
1300 { type: ViewContainerRef },
1301 { type: EclatFormContainerDirective }
1302];
1303EclatFormValueDirective.propDecorators = {
1304 eclatControl: [{ type: Input }],
1305 eclatControlName: [{ type: Input }],
1306 eclatFormValue: [{ type: Input }]
1307};
1308
1309/**
1310 * @fileoverview added by tsickle
1311 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1312 */
1313/**
1314 * @template T
1315 */
1316class EclatFormGroupBinderDirective extends FormGroupDirective {
1317 /**
1318 * @param {?} formGroup
1319 * @return {?}
1320 */
1321 set eclatFormGroup(formGroup) {
1322 this.form = formGroup.control;
1323 }
1324 /**
1325 * @param {?} changes
1326 * @return {?}
1327 */
1328 ngOnChanges(changes) {
1329 /** @type {?} */
1330 const eclatFormGroupChanges = changes.eclatFormGroup;
1331 if (eclatFormGroupChanges) {
1332 /** @type {?} */
1333 const prevValue = eclatFormGroupChanges.previousValue ? eclatFormGroupChanges.previousValue.control : undefined;
1334 /** @type {?} */
1335 const currentValue = eclatFormGroupChanges.currentValue.control;
1336 /** @type {?} */
1337 const firstChange = eclatFormGroupChanges.firstChange;
1338 super.ngOnChanges({ form: new SimpleChange(prevValue, currentValue, firstChange) });
1339 }
1340 }
1341}
1342EclatFormGroupBinderDirective.decorators = [
1343 { type: Directive, args: [{
1344 selector: '[eclatFormGroup]',
1345 providers: [{
1346 provide: ControlContainer,
1347 useExisting: forwardRef((/**
1348 * @return {?}
1349 */
1350 () => EclatFormGroupBinderDirective)),
1351 }],
1352 },] }
1353];
1354EclatFormGroupBinderDirective.propDecorators = {
1355 eclatFormGroup: [{ type: Input }]
1356};
1357
1358/**
1359 * @fileoverview added by tsickle
1360 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1361 */
1362/**
1363 * @template T
1364 */
1365class EclatFormGroupNameBinderDirective extends FormGroupName {
1366 /**
1367 * @param {?} name
1368 * @return {?}
1369 */
1370 set eclatFormGroupName(name) {
1371 this.name = name;
1372 }
1373}
1374EclatFormGroupNameBinderDirective.decorators = [
1375 { type: Directive, args: [{
1376 selector: '[eclatFormGroupName]',
1377 providers: [{
1378 provide: ControlContainer,
1379 useExisting: forwardRef((/**
1380 * @return {?}
1381 */
1382 () => EclatFormGroupNameBinderDirective)),
1383 }],
1384 },] }
1385];
1386EclatFormGroupNameBinderDirective.propDecorators = {
1387 eclatFormGroupName: [{ type: Input }]
1388};
1389
1390/**
1391 * @fileoverview added by tsickle
1392 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1393 */
1394/**
1395 * @template T
1396 */
1397class EclatFormArrayNameBinderDirective extends FormArrayName {
1398 /**
1399 * @param {?} name
1400 * @return {?}
1401 */
1402 set eclatFormArrayName(name) {
1403 this.name = name;
1404 }
1405}
1406EclatFormArrayNameBinderDirective.decorators = [
1407 { type: Directive, args: [{
1408 selector: '[eclatFormArrayName]',
1409 providers: [{
1410 provide: ControlContainer,
1411 useExisting: forwardRef((/**
1412 * @return {?}
1413 */
1414 () => EclatFormArrayNameBinderDirective)),
1415 }],
1416 },] }
1417];
1418EclatFormArrayNameBinderDirective.propDecorators = {
1419 eclatFormArrayName: [{ type: Input }]
1420};
1421
1422/**
1423 * @fileoverview added by tsickle
1424 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1425 */
1426/**
1427 * @template T
1428 */
1429class EclatFormArrayRemoveDirective {
1430 /**
1431 * @param {?} parent
1432 */
1433 constructor(parent) {
1434 this.parent = parent;
1435 }
1436 /**
1437 * @return {?}
1438 */
1439 onClick() {
1440 /** @type {?} */
1441 const control = this.parent.formControl;
1442 if (control) {
1443 control.removeChild(this.index);
1444 }
1445 }
1446}
1447EclatFormArrayRemoveDirective.decorators = [
1448 { type: Directive, args: [{
1449 selector: '[eclatFormRemoveChildren]',
1450 },] }
1451];
1452/** @nocollapse */
1453EclatFormArrayRemoveDirective.ctorParameters = () => [
1454 { type: EclatFormContainerDirective }
1455];
1456EclatFormArrayRemoveDirective.propDecorators = {
1457 index: [{ type: Input, args: ['eclatFormRemoveChildren',] }],
1458 onClick: [{ type: HostListener, args: ['click', ['$event'],] }]
1459};
1460
1461/**
1462 * @fileoverview added by tsickle
1463 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1464 */
1465class EclatFormModule {
1466}
1467EclatFormModule.decorators = [
1468 { type: NgModule, args: [{
1469 imports: [CommonModule, ReactiveFormsModule],
1470 declarations: [EclatFormOfDirective, EclatFormErrorDirective, EclatFormGroupDirective, EclatFormGroupNameDirective, EclatFormArrayDirective, EclatFormArrayNameDirective, EclatFormArrayChildrenDirective, EclatFormSubmitDirective, EclatFormArrayAddDirective, EclatFormValueDirective, EclatFormGroupBinderDirective, EclatFormGroupNameBinderDirective, EclatFormArrayNameBinderDirective, EclatFormArrayRemoveDirective],
1471 exports: [CommonModule, ReactiveFormsModule, EclatFormOfDirective, EclatFormErrorDirective, EclatFormGroupDirective, EclatFormGroupNameDirective, EclatFormArrayDirective, EclatFormArrayNameDirective, EclatFormArrayChildrenDirective, EclatFormSubmitDirective, EclatFormArrayAddDirective, EclatFormValueDirective, EclatFormGroupBinderDirective, EclatFormGroupNameBinderDirective, EclatFormArrayNameBinderDirective, EclatFormArrayRemoveDirective],
1472 },] }
1473];
1474
1475/**
1476 * @fileoverview added by tsickle
1477 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1478 */
1479
1480/**
1481 * @fileoverview added by tsickle
1482 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1483 */
1484
1485export { AbstractEclatFormControl, EclatFormArray, EclatFormContext, EclatFormControl, EclatFormErrorDirective, EclatFormGroup, EclatFormGroupDirective, EclatFormModule, EclatFormOfDirective, EclatFormUtility, EclatRecursiveConfig, getEclatControl, EclatFormContainerDirective as ɵa, EclatFormGroupNameDirective as ɵb, EclatFormArrayDirective as ɵc, EclatFormArrayNameDirective as ɵd, EclatFormArrayChildrenDirective as ɵe, EclatFormSubmitDirective as ɵf, EclatFormArrayAddDirective as ɵg, EclatFormValueDirective as ɵh, EclatFormGroupBinderDirective as ɵi, EclatFormGroupNameBinderDirective as ɵj, EclatFormArrayNameBinderDirective as ɵk, EclatFormArrayRemoveDirective as ɵl };
1486//# sourceMappingURL=eclat-form.js.map