UNPKG

43 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, false);
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, false);
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.updateValueAndValidity({ 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.updateValueAndValidity({ 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, false);
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 /** @type {?} */
456 const controls = [];
457 this.children = [];
458 data.forEach((/**
459 * @param {?} d
460 * @return {?}
461 */
462 d => {
463 /** @type {?} */
464 const eclatControl = getEclatControl(this.childConfig, d);
465 if (eclatControl) {
466 this.children.push((/** @type {?} */ (eclatControl)));
467 controls.push(eclatControl.control);
468 }
469 }));
470 this.control.controls = controls;
471 this.control.updateValueAndValidity({ emitEvent: true }); // used by eclat form array children
472 }
473 /**
474 * @param {?} data
475 * @param {?=} emitEvent
476 * @return {?}
477 */
478 resetState(data, emitEvent = true) {
479 if (!data) {
480 console.warn(`Expect an array, but ${data} is given`);
481 return;
482 }
483 /** @type {?} */
484 const controls = [];
485 this.children = [];
486 data.forEach((/**
487 * @param {?} d
488 * @return {?}
489 */
490 d => {
491 /** @type {?} */
492 const eclatControl = getEclatControl(this.childConfig, d);
493 if (eclatControl) {
494 this.children.push((/** @type {?} */ (eclatControl)));
495 controls.push(eclatControl.control);
496 }
497 }));
498 this.control.controls = controls;
499 this.control.updateValueAndValidity({ emitEvent: true }); // used by eclat form array children
500 }
501 /**
502 * @param {?} index
503 * @return {?}
504 */
505 getChild(index) {
506 return this.children[index];
507 }
508}
509
510/**
511 * @fileoverview added by tsickle
512 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
513 */
514/**
515 * @template T
516 */
517class EclatFormContext {
518 /**
519 * @param {?} $implicit
520 */
521 constructor($implicit) {
522 this.$implicit = $implicit;
523 }
524}
525/**
526 * @template T
527 */
528class EclatFormOfDirective {
529 /**
530 * @param {?} templateRef
531 * @param {?} viewContainer
532 */
533 constructor(templateRef, viewContainer) {
534 this.templateRef = templateRef;
535 this.viewContainer = viewContainer;
536 }
537 /**
538 * @param {?} state
539 * @return {?}
540 */
541 set eclatFormOf(state) {
542 if (!this.context || !this.latestState || this.latestState.config !== state.config) {
543 this.setContext(getEclatControl(state.config, state.data));
544 }
545 else if (this.latestState.data !== state.data) {
546 this.context.$implicit.resetState(state.data);
547 }
548 this.latestState = state;
549 }
550 /**
551 * @private
552 * @param {?=} control
553 * @return {?}
554 */
555 setContext(control) {
556 if (!control) {
557 throw new Error('control not found');
558 }
559 if (!this.context) {
560 this.context = new EclatFormContext(control);
561 this.viewContainer.createEmbeddedView(this.templateRef, this.context);
562 }
563 else {
564 this.context.$implicit = control;
565 }
566 }
567}
568EclatFormOfDirective.decorators = [
569 { type: Directive, args: [{ selector: '[eclatForm][eclatFormOf]' },] }
570];
571/** @nocollapse */
572EclatFormOfDirective.ctorParameters = () => [
573 { type: TemplateRef },
574 { type: ViewContainerRef }
575];
576EclatFormOfDirective.propDecorators = {
577 eclatFormOf: [{ type: Input }]
578};
579
580/**
581 * @fileoverview added by tsickle
582 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
583 */
584/**
585 * @abstract
586 * @template T
587 */
588class EclatFormContainerDirective {
589 /**
590 * @protected
591 */
592 constructor() {
593 this._formControl = new BehaviorSubject(undefined);
594 this.onSubmit = new EventEmitter();
595 }
596 /**
597 * @return {?}
598 */
599 get formControl() {
600 return this._formControl.getValue();
601 }
602 /**
603 * @return {?}
604 */
605 get formControl$() {
606 return this._formControl.asObservable();
607 }
608 /**
609 * @return {?}
610 */
611 submit() {
612 if (this.formControl) {
613 this.onSubmit.emit(this.formControl.value);
614 }
615 }
616}
617EclatFormContainerDirective.propDecorators = {
618 onSubmit: [{ type: Output }]
619};
620
621/**
622 * @fileoverview added by tsickle
623 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
624 */
625/**
626 * @template T
627 */
628class EclatFormErrorContext {
629 /**
630 * @param {?} control
631 */
632 constructor(control) {
633 this.control = control;
634 }
635 /**
636 * @return {?}
637 */
638 get $implicit() {
639 return this.control.errorMessages;
640 }
641}
642/**
643 * @template T
644 */
645class EclatFormErrorDirective {
646 /**
647 * @param {?} templateRef
648 * @param {?} viewContainer
649 * @param {?} parent
650 */
651 constructor(templateRef, viewContainer, parent) {
652 this.templateRef = templateRef;
653 this.viewContainer = viewContainer;
654 this.parent = parent;
655 this.onDestroy$ = new Subject();
656 this.errorMatcher = (/**
657 * @return {?}
658 */
659 () => true);
660 }
661 /**
662 * @param {?} control
663 * @return {?}
664 */
665 set eclatControl(control) {
666 this.setContext(control);
667 }
668 /**
669 * @param {?} controlName
670 * @return {?}
671 */
672 set eclatControlName(controlName) {
673 if (!this.parent.formControl) {
674 throw new Error('parent formControl not exist');
675 }
676 /** @type {?} */
677 const instance = !controlName ? this.parent.formControl : this.parent.formControl.getChild(controlName);
678 if (!instance) {
679 throw new Error('control not found');
680 return;
681 }
682 this.latestControlName = controlName;
683 this.setContext(instance);
684 }
685 /**
686 * @param {?} control
687 * @return {?}
688 */
689 set eclatFormError(control) {
690 if (control instanceof AbstractEclatFormControl) {
691 this.setContext(control);
692 }
693 else {
694 this.eclatControlName = control;
695 }
696 }
697 /**
698 * @return {?}
699 */
700 ngOnInit() {
701 this.parent.formControl$.pipe(takeUntil(this.onDestroy$)).subscribe((/**
702 * @param {?} formControl
703 * @return {?}
704 */
705 (formControl) => {
706 if (formControl && this.latestControlName) {
707 this.eclatFormError = formControl.getChild(this.latestControlName);
708 }
709 }));
710 }
711 /**
712 * @return {?}
713 */
714 ngOnDestroy() {
715 this.onDestroy$.next();
716 }
717 /**
718 * @private
719 * @param {?} control
720 * @return {?}
721 */
722 setContext(control) {
723 if (!this.context) {
724 this.context = new EclatFormErrorContext(control);
725 this.viewContainer.createEmbeddedView(this.templateRef, this.context);
726 }
727 else if (this.context.control !== control) {
728 this.context.control = control;
729 }
730 if (this.valueChangesSub) {
731 this.valueChangesSub.unsubscribe();
732 }
733 this.checkValidity(control);
734 this.valueChangesSub = control.valueChanges.pipe(takeUntil(this.onDestroy$)).subscribe((/**
735 * @return {?}
736 */
737 () => {
738 this.checkValidity(control);
739 }));
740 }
741 /**
742 * @private
743 * @param {?} control
744 * @return {?}
745 */
746 checkValidity(control) {
747 /** @type {?} */
748 const isAttached = !!this.viewContainer.length;
749 if (!control.valid && this.errorMatcher(control)) {
750 if (!isAttached) {
751 this.viewContainer.createEmbeddedView(this.templateRef, this.context);
752 }
753 }
754 else {
755 if (isAttached) {
756 this.viewContainer.remove();
757 }
758 }
759 }
760}
761EclatFormErrorDirective.decorators = [
762 { type: Directive, args: [{ selector: '[eclatFormError]' },] }
763];
764/** @nocollapse */
765EclatFormErrorDirective.ctorParameters = () => [
766 { type: TemplateRef },
767 { type: ViewContainerRef },
768 { type: EclatFormContainerDirective }
769];
770EclatFormErrorDirective.propDecorators = {
771 errorMatcher: [{ type: Input }],
772 eclatControl: [{ type: Input }],
773 eclatControlName: [{ type: Input }],
774 eclatFormError: [{ type: Input }]
775};
776
777/**
778 * @fileoverview added by tsickle
779 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
780 */
781/**
782 * @template T
783 */
784class EclatFormGroupDirective extends EclatFormContainerDirective {
785 constructor() {
786 super();
787 }
788 /**
789 * @param {?} formGroup
790 * @return {?}
791 */
792 set eclatFormGroup(formGroup) {
793 this._formControl.next(formGroup);
794 }
795 /**
796 * @param {?} key
797 * @return {?}
798 */
799 getChild(key) {
800 if (this.formControl) {
801 return this.formControl.getChild(key);
802 }
803 return undefined;
804 }
805}
806EclatFormGroupDirective.decorators = [
807 { type: Directive, args: [{
808 selector: '[eclatFormGroup]',
809 providers: [{
810 provide: EclatFormContainerDirective,
811 useExisting: forwardRef((/**
812 * @return {?}
813 */
814 () => EclatFormGroupDirective)),
815 }],
816 },] }
817];
818/** @nocollapse */
819EclatFormGroupDirective.ctorParameters = () => [];
820EclatFormGroupDirective.propDecorators = {
821 eclatFormGroup: [{ type: Input }]
822};
823
824/**
825 * @fileoverview added by tsickle
826 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
827 */
828/**
829 * @template T
830 */
831class EclatFormGroupNameDirective extends EclatFormContainerDirective {
832 /**
833 * @param {?} parent
834 */
835 constructor(parent) {
836 super();
837 this.parent = parent;
838 this.onDestroy$ = new Subject();
839 }
840 /**
841 * @param {?} name
842 * @return {?}
843 */
844 set eclatFormGroupName(name) {
845 this.latestName = name;
846 this._formControl.next((/** @type {?} */ (this.parent.getChild(name))));
847 }
848 /**
849 * @return {?}
850 */
851 ngOnDestroy() {
852 this.onDestroy$.next();
853 }
854 /**
855 * @return {?}
856 */
857 ngOnInit() {
858 this.parent.formControl$.pipe(takeUntil(this.onDestroy$)).subscribe((/**
859 * @return {?}
860 */
861 () => {
862 this.eclatFormGroupName = this.latestName;
863 }));
864 }
865 /**
866 * @param {?} key
867 * @return {?}
868 */
869 getChild(key) {
870 if (this.formControl) {
871 return this.formControl.getChild(key);
872 }
873 return undefined;
874 }
875}
876EclatFormGroupNameDirective.decorators = [
877 { type: Directive, args: [{
878 selector: '[eclatFormGroupName]',
879 providers: [{
880 provide: EclatFormContainerDirective,
881 useExisting: forwardRef((/**
882 * @return {?}
883 */
884 () => EclatFormGroupNameDirective)),
885 }],
886 },] }
887];
888/** @nocollapse */
889EclatFormGroupNameDirective.ctorParameters = () => [
890 { type: EclatFormContainerDirective, decorators: [{ type: SkipSelf }] }
891];
892EclatFormGroupNameDirective.propDecorators = {
893 eclatFormGroupName: [{ type: Input }]
894};
895
896/**
897 * @fileoverview added by tsickle
898 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
899 */
900/**
901 * @template T
902 */
903class EclatFormArrayNameDirective extends EclatFormContainerDirective {
904 /**
905 * @param {?} parent
906 */
907 constructor(parent) {
908 super();
909 this.parent = parent;
910 this.onDestroy$ = new Subject();
911 }
912 /**
913 * @param {?} name
914 * @return {?}
915 */
916 set eclatFormArrayName(name) {
917 this.latestName = name;
918 this._formControl.next((/** @type {?} */ (this.parent.getChild(name))));
919 }
920 /**
921 * @return {?}
922 */
923 ngOnDestroy() {
924 this.onDestroy$.next();
925 }
926 /**
927 * @return {?}
928 */
929 ngOnInit() {
930 this.parent.formControl$.pipe(takeUntil(this.onDestroy$)).subscribe((/**
931 * @return {?}
932 */
933 () => {
934 this.eclatFormArrayName = this.latestName;
935 }));
936 }
937 /**
938 * @param {?} key
939 * @return {?}
940 */
941 getChild(key) {
942 if (this.formControl) {
943 return this.formControl.getChild(key);
944 }
945 return undefined;
946 }
947}
948EclatFormArrayNameDirective.decorators = [
949 { type: Directive, args: [{
950 selector: '[eclatFormArrayName]',
951 providers: [{
952 provide: EclatFormContainerDirective,
953 useExisting: forwardRef((/**
954 * @return {?}
955 */
956 () => EclatFormArrayNameDirective)),
957 }],
958 },] }
959];
960/** @nocollapse */
961EclatFormArrayNameDirective.ctorParameters = () => [
962 { type: EclatFormContainerDirective, decorators: [{ type: SkipSelf }] }
963];
964EclatFormArrayNameDirective.propDecorators = {
965 eclatFormArrayName: [{ type: Input }]
966};
967
968/**
969 * @fileoverview added by tsickle
970 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
971 */
972/**
973 * @template T
974 */
975class EclatFormArrayDirective extends EclatFormContainerDirective {
976 constructor() {
977 super();
978 }
979 /**
980 * @param {?} formArray
981 * @return {?}
982 */
983 set eclatFormArray(formArray) {
984 this._formControl.next(formArray);
985 }
986 /**
987 * @param {?} key
988 * @return {?}
989 */
990 getChild(key) {
991 if (this.formControl) {
992 return this.formControl.getChild(key);
993 }
994 return undefined;
995 }
996}
997EclatFormArrayDirective.decorators = [
998 { type: Directive, args: [{
999 selector: '[eclatFormArray]',
1000 providers: [{
1001 provide: EclatFormContainerDirective,
1002 useExisting: forwardRef((/**
1003 * @return {?}
1004 */
1005 () => EclatFormArrayDirective)),
1006 }],
1007 },] }
1008];
1009/** @nocollapse */
1010EclatFormArrayDirective.ctorParameters = () => [];
1011EclatFormArrayDirective.propDecorators = {
1012 eclatFormArray: [{ type: Input }]
1013};
1014
1015/**
1016 * @fileoverview added by tsickle
1017 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1018 */
1019/**
1020 * @template T
1021 */
1022class EclatFormArrayChildrenDirective {
1023 /**
1024 * @param {?} templateRef
1025 * @param {?} viewContainer
1026 * @param {?} parent
1027 */
1028 constructor(templateRef, viewContainer, parent) {
1029 this.templateRef = templateRef;
1030 this.viewContainer = viewContainer;
1031 this.parent = parent;
1032 this.onDestroy$ = new Subject();
1033 this.latestChildren = [];
1034 }
1035 /**
1036 * @return {?}
1037 */
1038 ngOnDestroy() {
1039 this.onDestroy$.next();
1040 }
1041 /**
1042 * @return {?}
1043 */
1044 ngOnInit() {
1045 this.parent.formControl$.pipe(takeUntil(this.onDestroy$)).subscribe((/**
1046 * @param {?} formControl
1047 * @return {?}
1048 */
1049 (formControl) => {
1050 if (formControl) {
1051 if (this.valueChangeSubs) {
1052 this.valueChangeSubs.unsubscribe();
1053 }
1054 this.onChange(formControl);
1055 this.valueChangeSubs = formControl.valueChanges.pipe(takeUntil(this.onDestroy$)).subscribe((/**
1056 * @return {?}
1057 */
1058 () => {
1059 this.onChange(formControl);
1060 }));
1061 }
1062 }));
1063 }
1064 /**
1065 * @private
1066 * @param {?} control
1067 * @return {?}
1068 */
1069 onChange(control) {
1070 /** @type {?} */
1071 const children = [];
1072 /** @type {?} */
1073 const controlChildrenLength = control.children.length;
1074 control.children.forEach((/**
1075 * @param {?} c
1076 * @param {?} index
1077 * @return {?}
1078 */
1079 (c, index) => {
1080 /** @type {?} */
1081 const lastIndex = this.latestChildren.findIndex((/**
1082 * @param {?} o
1083 * @return {?}
1084 */
1085 o => o === c));
1086 /** @type {?} */
1087 const first = index === 0;
1088 /** @type {?} */
1089 const last = index === (controlChildrenLength - 1);
1090 if (lastIndex < 0) {
1091 this.viewContainer.createEmbeddedView(this.templateRef, { $implicit: index, last, first }, index);
1092 }
1093 else if (lastIndex !== index) {
1094 /** @type {?} */
1095 const embeddedView = (/** @type {?} */ ((/** @type {?} */ (this.viewContainer.get(lastIndex)))));
1096 embeddedView.context.$implicit = index;
1097 embeddedView.context.last = last;
1098 embeddedView.context.first = first;
1099 this.viewContainer.move(embeddedView, index);
1100 }
1101 children.push(c);
1102 }));
1103 while (this.viewContainer.length > controlChildrenLength) {
1104 this.viewContainer.remove();
1105 }
1106 this.latestChildren = children;
1107 }
1108}
1109EclatFormArrayChildrenDirective.decorators = [
1110 { type: Directive, args: [{
1111 selector: '[eclatFormArrayChildren]',
1112 },] }
1113];
1114/** @nocollapse */
1115EclatFormArrayChildrenDirective.ctorParameters = () => [
1116 { type: TemplateRef },
1117 { type: ViewContainerRef },
1118 { type: EclatFormContainerDirective }
1119];
1120
1121/**
1122 * @fileoverview added by tsickle
1123 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1124 */
1125class EclatFormSubmitDirective {
1126 /**
1127 * @param {?} parent
1128 */
1129 constructor(parent) {
1130 this.parent = parent;
1131 }
1132 /**
1133 * @return {?}
1134 */
1135 onClick() {
1136 this.parent.submit();
1137 }
1138}
1139EclatFormSubmitDirective.decorators = [
1140 { type: Directive, args: [{ selector: '[eclatFormSubmit]' },] }
1141];
1142/** @nocollapse */
1143EclatFormSubmitDirective.ctorParameters = () => [
1144 { type: EclatFormContainerDirective }
1145];
1146EclatFormSubmitDirective.propDecorators = {
1147 onClick: [{ type: HostListener, args: ['click', ['$event'],] }, { type: HostListener, args: ['tap', ['$event'],] }]
1148};
1149
1150/**
1151 * @fileoverview added by tsickle
1152 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1153 */
1154/**
1155 * @template T
1156 */
1157class EclatFormArrayAddDirective {
1158 /**
1159 * @param {?} parent
1160 */
1161 constructor(parent) {
1162 this.parent = parent;
1163 }
1164 /**
1165 * @return {?}
1166 */
1167 onClick() {
1168 /** @type {?} */
1169 const control = this.parent.formControl;
1170 if (control) {
1171 control.addChild(this.value);
1172 }
1173 }
1174}
1175EclatFormArrayAddDirective.decorators = [
1176 { type: Directive, args: [{
1177 selector: '[eclatFormAddChildren]',
1178 },] }
1179];
1180/** @nocollapse */
1181EclatFormArrayAddDirective.ctorParameters = () => [
1182 { type: EclatFormContainerDirective }
1183];
1184EclatFormArrayAddDirective.propDecorators = {
1185 value: [{ type: Input, args: ['eclatFormAddChildren',] }],
1186 onClick: [{ type: HostListener, args: ['click', ['$event'],] }]
1187};
1188
1189/**
1190 * @fileoverview added by tsickle
1191 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1192 */
1193/**
1194 * @template T
1195 */
1196class EclatFormValueContext {
1197 /**
1198 * @param {?} control
1199 */
1200 constructor(control) {
1201 this.control = control;
1202 }
1203 /**
1204 * @return {?}
1205 */
1206 get $implicit() {
1207 return this.control.value;
1208 }
1209}
1210/**
1211 * @template T
1212 */
1213class EclatFormValueDirective {
1214 /**
1215 * @param {?} templateRef
1216 * @param {?} viewContainer
1217 * @param {?} parent
1218 */
1219 constructor(templateRef, viewContainer, parent) {
1220 this.templateRef = templateRef;
1221 this.viewContainer = viewContainer;
1222 this.parent = parent;
1223 this.onDestroy$ = new Subject();
1224 }
1225 /**
1226 * @param {?} control
1227 * @return {?}
1228 */
1229 set eclatControl(control) {
1230 this.setContext(control);
1231 }
1232 /**
1233 * @param {?} controlName
1234 * @return {?}
1235 */
1236 set eclatControlName(controlName) {
1237 if (!this.parent.formControl) {
1238 throw new Error('parent formControl not exist');
1239 }
1240 /** @type {?} */
1241 const instance = this.parent.formControl.getChild(controlName);
1242 if (!instance) {
1243 throw new Error('control not found');
1244 return;
1245 }
1246 this.latestControlName = controlName;
1247 this.setContext(instance);
1248 }
1249 /**
1250 * @param {?} control
1251 * @return {?}
1252 */
1253 set eclatFormValue(control) {
1254 if (control instanceof AbstractEclatFormControl) {
1255 this.setContext(control);
1256 }
1257 else if (control || control === 0) {
1258 this.eclatControlName = control;
1259 }
1260 }
1261 /**
1262 * @return {?}
1263 */
1264 ngOnInit() {
1265 this.parent.formControl$.pipe(takeUntil(this.onDestroy$)).subscribe((/**
1266 * @param {?} formControl
1267 * @return {?}
1268 */
1269 (formControl) => {
1270 if (formControl && this.latestControlName) {
1271 this.eclatFormValue = formControl.getChild(this.latestControlName);
1272 }
1273 }));
1274 }
1275 /**
1276 * @return {?}
1277 */
1278 ngOnDestroy() {
1279 this.onDestroy$.next();
1280 }
1281 /**
1282 * @private
1283 * @param {?} control
1284 * @return {?}
1285 */
1286 setContext(control) {
1287 if (!this.context) {
1288 this.context = new EclatFormValueContext(control);
1289 this.viewContainer.createEmbeddedView(this.templateRef, this.context);
1290 }
1291 else if (this.context.control !== control) {
1292 this.context.control = control;
1293 }
1294 }
1295}
1296EclatFormValueDirective.decorators = [
1297 { type: Directive, args: [{ selector: '[eclatFormValue]' },] }
1298];
1299/** @nocollapse */
1300EclatFormValueDirective.ctorParameters = () => [
1301 { type: TemplateRef },
1302 { type: ViewContainerRef },
1303 { type: EclatFormContainerDirective }
1304];
1305EclatFormValueDirective.propDecorators = {
1306 eclatControl: [{ type: Input }],
1307 eclatControlName: [{ type: Input }],
1308 eclatFormValue: [{ type: Input }]
1309};
1310
1311/**
1312 * @fileoverview added by tsickle
1313 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1314 */
1315/**
1316 * @template T
1317 */
1318class EclatFormGroupBinderDirective extends FormGroupDirective {
1319 /**
1320 * @param {?} formGroup
1321 * @return {?}
1322 */
1323 set eclatFormGroup(formGroup) {
1324 this.form = formGroup.control;
1325 }
1326 /**
1327 * @param {?} changes
1328 * @return {?}
1329 */
1330 ngOnChanges(changes) {
1331 /** @type {?} */
1332 const eclatFormGroupChanges = changes.eclatFormGroup;
1333 if (eclatFormGroupChanges) {
1334 /** @type {?} */
1335 const prevValue = eclatFormGroupChanges.previousValue ? eclatFormGroupChanges.previousValue.control : undefined;
1336 /** @type {?} */
1337 const currentValue = eclatFormGroupChanges.currentValue.control;
1338 /** @type {?} */
1339 const firstChange = eclatFormGroupChanges.firstChange;
1340 super.ngOnChanges({ form: new SimpleChange(prevValue, currentValue, firstChange) });
1341 }
1342 }
1343}
1344EclatFormGroupBinderDirective.decorators = [
1345 { type: Directive, args: [{
1346 selector: '[eclatFormGroup]',
1347 providers: [{
1348 provide: ControlContainer,
1349 useExisting: forwardRef((/**
1350 * @return {?}
1351 */
1352 () => EclatFormGroupBinderDirective)),
1353 }],
1354 },] }
1355];
1356EclatFormGroupBinderDirective.propDecorators = {
1357 eclatFormGroup: [{ type: Input }]
1358};
1359
1360/**
1361 * @fileoverview added by tsickle
1362 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1363 */
1364/**
1365 * @template T
1366 */
1367class EclatFormGroupNameBinderDirective extends FormGroupName {
1368 /**
1369 * @param {?} name
1370 * @return {?}
1371 */
1372 set eclatFormGroupName(name) {
1373 this.name = name;
1374 }
1375}
1376EclatFormGroupNameBinderDirective.decorators = [
1377 { type: Directive, args: [{
1378 selector: '[eclatFormGroupName]',
1379 providers: [{
1380 provide: ControlContainer,
1381 useExisting: forwardRef((/**
1382 * @return {?}
1383 */
1384 () => EclatFormGroupNameBinderDirective)),
1385 }],
1386 },] }
1387];
1388EclatFormGroupNameBinderDirective.propDecorators = {
1389 eclatFormGroupName: [{ type: Input }]
1390};
1391
1392/**
1393 * @fileoverview added by tsickle
1394 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1395 */
1396/**
1397 * @template T
1398 */
1399class EclatFormArrayNameBinderDirective extends FormArrayName {
1400 /**
1401 * @param {?} name
1402 * @return {?}
1403 */
1404 set eclatFormArrayName(name) {
1405 this.name = name;
1406 }
1407}
1408EclatFormArrayNameBinderDirective.decorators = [
1409 { type: Directive, args: [{
1410 selector: '[eclatFormArrayName]',
1411 providers: [{
1412 provide: ControlContainer,
1413 useExisting: forwardRef((/**
1414 * @return {?}
1415 */
1416 () => EclatFormArrayNameBinderDirective)),
1417 }],
1418 },] }
1419];
1420EclatFormArrayNameBinderDirective.propDecorators = {
1421 eclatFormArrayName: [{ type: Input }]
1422};
1423
1424/**
1425 * @fileoverview added by tsickle
1426 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1427 */
1428/**
1429 * @template T
1430 */
1431class EclatFormArrayRemoveDirective {
1432 /**
1433 * @param {?} parent
1434 */
1435 constructor(parent) {
1436 this.parent = parent;
1437 }
1438 /**
1439 * @return {?}
1440 */
1441 onClick() {
1442 /** @type {?} */
1443 const control = this.parent.formControl;
1444 if (control) {
1445 control.removeChild(this.index);
1446 }
1447 }
1448}
1449EclatFormArrayRemoveDirective.decorators = [
1450 { type: Directive, args: [{
1451 selector: '[eclatFormRemoveChildren]',
1452 },] }
1453];
1454/** @nocollapse */
1455EclatFormArrayRemoveDirective.ctorParameters = () => [
1456 { type: EclatFormContainerDirective }
1457];
1458EclatFormArrayRemoveDirective.propDecorators = {
1459 index: [{ type: Input, args: ['eclatFormRemoveChildren',] }],
1460 onClick: [{ type: HostListener, args: ['click', ['$event'],] }]
1461};
1462
1463/**
1464 * @fileoverview added by tsickle
1465 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1466 */
1467class EclatFormModule {
1468}
1469EclatFormModule.decorators = [
1470 { type: NgModule, args: [{
1471 imports: [CommonModule, ReactiveFormsModule],
1472 declarations: [EclatFormOfDirective, EclatFormErrorDirective, EclatFormGroupDirective, EclatFormGroupNameDirective, EclatFormArrayDirective, EclatFormArrayNameDirective, EclatFormArrayChildrenDirective, EclatFormSubmitDirective, EclatFormArrayAddDirective, EclatFormValueDirective, EclatFormGroupBinderDirective, EclatFormGroupNameBinderDirective, EclatFormArrayNameBinderDirective, EclatFormArrayRemoveDirective],
1473 exports: [CommonModule, ReactiveFormsModule, EclatFormOfDirective, EclatFormErrorDirective, EclatFormGroupDirective, EclatFormGroupNameDirective, EclatFormArrayDirective, EclatFormArrayNameDirective, EclatFormArrayChildrenDirective, EclatFormSubmitDirective, EclatFormArrayAddDirective, EclatFormValueDirective, EclatFormGroupBinderDirective, EclatFormGroupNameBinderDirective, EclatFormArrayNameBinderDirective, EclatFormArrayRemoveDirective],
1474 },] }
1475];
1476
1477/**
1478 * @fileoverview added by tsickle
1479 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1480 */
1481
1482/**
1483 * @fileoverview added by tsickle
1484 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1485 */
1486
1487export { 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 };
1488//# sourceMappingURL=eclat-form.js.map