|
1 | | -import { |
2 | | - Directive, |
3 | | - ViewContainerRef, |
4 | | - HostListener, |
5 | | - ViewChild, |
6 | | - Input, |
7 | | - AfterViewInit, |
8 | | - ElementRef, |
9 | | - ComponentFactoryResolver, |
10 | | - forwardRef, |
11 | | - Renderer, |
12 | | - OnChanges, |
13 | | - OnInit, |
14 | | - Output, |
15 | | - EventEmitter, |
16 | | - TemplateRef, |
17 | | - ComponentRef, |
18 | | - NgZone, |
19 | | - ChangeDetectorRef |
20 | | -} from '@angular/core'; |
21 | | -import { NgControl } from '@angular/forms'; |
22 | | - |
23 | | -@Directive({ |
24 | | - selector: '[fancyInput]', |
25 | | -}) |
26 | | -export class FancyInputDirective implements OnInit { |
27 | | - @Input() formControlName: any; |
28 | | - @Input() fancyInput: any; |
29 | | - public formValue: any; |
30 | | - public value: any; |
31 | | - public el: HTMLDivElement; |
32 | | - config = { |
33 | | - selector: null, |
34 | | - tplRef: null, |
35 | | - prefix: 'spu', |
36 | | - classValid: 'valid', |
37 | | - classInvalid: 'invalid', |
38 | | - classPristine: 'pristine', |
39 | | - classDirty: 'dirty', |
40 | | - classTouched: 'touched', |
41 | | - classUnTouched: 'untouched', |
42 | | - classHasErrors: 'haserrors', |
43 | | - classNoErrors: 'noerrors', |
44 | | - classDisabled: 'disabled', |
45 | | - classEnabled: 'enabled', |
46 | | - }; |
47 | | - static globalconfig: any = {}; |
48 | | - c: any = {}; |
49 | | - allClasses: any[] = []; |
50 | | - localconfig: any = {}; |
51 | | - |
52 | | - |
53 | | - |
54 | | - @HostListener('focus', []) |
55 | | - focus() { |
56 | | - this.log(); |
57 | | - } |
58 | | - @HostListener('blur', []) |
59 | | - blur() { |
60 | | - this.log(); |
61 | | - } |
62 | | - |
63 | | - constructor( |
64 | | - private vcRef: ViewContainerRef, |
65 | | - private eRef: ElementRef, |
66 | | - private renderer: Renderer, |
67 | | - private cdRef: ChangeDetectorRef, |
68 | | - private formControl: NgControl |
69 | | - ) { } |
70 | | - |
71 | | - // static withConfig(config) { |
72 | | - // FancyInputDirective.globalconfig = config; |
73 | | - // // return new FancyInputDirective(); |
74 | | - // } |
75 | | - |
76 | | - ngOnInit() { |
77 | | - this.localconfig = { ...this.config, ...FancyInputDirective.globalconfig, ...this.fancyInput }; |
78 | | - console.log('locccalconfig', this.localconfig); |
79 | | - if (this.localconfig.selector) { |
80 | | - this.el = this.match(this.eRef.nativeElement, this.localconfig.selector) |
81 | | - } else if (this.localconfig.tplRef) { |
82 | | - this.el = this.localconfig.tplRef; |
83 | | - } else { |
84 | | - throw new Error('no valid selctor found'); |
85 | | - } |
86 | | - this.allClasses = this.getClasses(); |
87 | | - this.log(); |
88 | | - |
89 | | - this.formControl.valueChanges.subscribe(value => { |
90 | | - this.log(); |
91 | | - }); |
92 | | - this.formControl.statusChanges.subscribe(status => { |
93 | | - if (!this.formControl.control.validator) { |
94 | | - return; |
95 | | - } |
96 | | - if (status === 'VALID') { |
97 | | - this.markAsValid(); |
98 | | - } else { |
99 | | - this.markAsInvalid(); |
100 | | - } |
101 | | - |
102 | | - this.log(); |
103 | | - }); |
104 | | - |
105 | | - } |
106 | | - |
107 | | - getClasses() { |
108 | | - this.c.valid = this.localconfig.prefix + '-' + this.localconfig.classValid; |
109 | | - this.c.invalid = this.localconfig.prefix + '-' + this.localconfig.classInvalid; |
110 | | - this.c.pristine = this.localconfig.prefix + '-' + this.localconfig.classPristine; |
111 | | - this.c.dirty = this.localconfig.prefix + '-' + this.localconfig.classDirty; |
112 | | - this.c.touched = this.localconfig.prefix + '-' + this.localconfig.classTouched; |
113 | | - this.c.untouched = this.localconfig.prefix + '-' + this.localconfig.classUnTouched; |
114 | | - this.c.haserrors = this.localconfig.prefix + '-' + this.localconfig.classHasErrors; |
115 | | - this.c.noerrors = this.localconfig.prefix + '-' + this.localconfig.classNoErrors; |
116 | | - this.c.disabled = this.localconfig.prefix + '-' + this.localconfig.classDisabled; |
117 | | - this.c.enabled = this.localconfig.prefix + '-' + this.localconfig.classEnabled; |
118 | | - return Object.keys(this.c).map(itm => this.c[itm]); |
119 | | - } |
120 | | - |
121 | | - private validate(value: any) { |
122 | | - if (value) { |
123 | | - this.markAsValid(); |
124 | | - } else { |
125 | | - this.markAsInvalid(); |
126 | | - } |
127 | | - } |
128 | | - |
129 | | - private markAsInvalid() { |
130 | | - this.el.classList.remove('success'); |
131 | | - this.el.classList.add('error'); |
132 | | - } |
133 | | - private markAsValid() { |
134 | | - this.el.classList.remove('error'); |
135 | | - this.el.classList.add('success'); |
136 | | - } |
137 | | - |
138 | | - private closest(matchesSelector, el, selector) { |
139 | | - return !el ? null : |
140 | | - matchesSelector.call(el, selector) ? el : this.closest(matchesSelector, el.parentElement, selector); |
141 | | - } |
142 | | - |
143 | | - private match(el, selector) { |
144 | | - const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector; |
145 | | - return !el ? null : |
146 | | - matchesSelector.call(el, selector) ? el : this.closest(matchesSelector, el.parentElement, selector); |
147 | | - } |
148 | | - |
149 | | - log() { |
150 | | - const control = this.formControl; |
151 | | - const all = this.allClasses; |
152 | | - const data = [ |
153 | | - control.valid ? this.c.valid : this.c.invalid, |
154 | | - control.touched ? this.c.touched : this.c.untouched, |
155 | | - control.pristine ? this.c.pristine : this.c.dirty, |
156 | | - control.disabled ? this.c.disabled : this.c.enabled, |
157 | | - control.errors ? this.c.haserrors : this.c.noerrors |
158 | | - ]; |
159 | | - this.el.classList.remove(...all); |
160 | | - this.el.classList.add(...data); |
161 | | - console.log('validate--->', this.el.tagName); |
162 | | - } |
163 | | -} |
| 1 | +import { |
| 2 | + Directive, |
| 3 | + ViewContainerRef, |
| 4 | + HostListener, |
| 5 | + ViewChild, |
| 6 | + Input, |
| 7 | + AfterViewInit, |
| 8 | + ElementRef, |
| 9 | + ComponentFactoryResolver, |
| 10 | + forwardRef, |
| 11 | + Renderer, |
| 12 | + OnChanges, |
| 13 | + OnInit, |
| 14 | + Output, |
| 15 | + EventEmitter, |
| 16 | + TemplateRef, |
| 17 | + ComponentRef, |
| 18 | + NgZone, |
| 19 | + ChangeDetectorRef |
| 20 | +} from '@angular/core'; |
| 21 | +import { NgControl } from '@angular/forms'; |
| 22 | + |
| 23 | +@Directive({ |
| 24 | + selector: '[fancyInput]', |
| 25 | +}) |
| 26 | +export class FancyInputDirective implements OnInit { |
| 27 | + @Input() formControlName: any; |
| 28 | + @Input() fancyInput: any; |
| 29 | + public formValue: any; |
| 30 | + public value: any; |
| 31 | + public el: HTMLDivElement; |
| 32 | + config = { |
| 33 | + selector: null, |
| 34 | + tplRef: null, |
| 35 | + prefix: 'spu', |
| 36 | + classValid: 'valid', |
| 37 | + classInvalid: 'invalid', |
| 38 | + classPristine: 'pristine', |
| 39 | + classDirty: 'dirty', |
| 40 | + classTouched: 'touched', |
| 41 | + classUnTouched: 'untouched', |
| 42 | + classHasErrors: 'haserrors', |
| 43 | + classNoErrors: 'noerrors', |
| 44 | + classDisabled: 'disabled', |
| 45 | + classEnabled: 'enabled', |
| 46 | + }; |
| 47 | + static globalconfig: any = {}; |
| 48 | + c: any = {}; |
| 49 | + allClasses: any[] = []; |
| 50 | + localconfig: any = {}; |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + @HostListener('focus', []) |
| 55 | + focus() { |
| 56 | + this.log(); |
| 57 | + } |
| 58 | + @HostListener('blur', []) |
| 59 | + blur() { |
| 60 | + this.log(); |
| 61 | + } |
| 62 | + |
| 63 | + constructor( |
| 64 | + private vcRef: ViewContainerRef, |
| 65 | + private eRef: ElementRef, |
| 66 | + private renderer: Renderer, |
| 67 | + private cdRef: ChangeDetectorRef, |
| 68 | + private formControl: NgControl |
| 69 | + ) { } |
| 70 | + |
| 71 | + // static withConfig(config) { |
| 72 | + // FancyInputDirective.globalconfig = config; |
| 73 | + // // return new FancyInputDirective(); |
| 74 | + // } |
| 75 | + |
| 76 | + ngOnInit() { |
| 77 | + this.localconfig = { ...this.config, ...FancyInputDirective.globalconfig, ...this.fancyInput }; |
| 78 | + if (this.localconfig.selector) { |
| 79 | + this.el = this.match(this.eRef.nativeElement, this.localconfig.selector) |
| 80 | + } else if (this.localconfig.tplRef) { |
| 81 | + this.el = this.localconfig.tplRef; |
| 82 | + } else { |
| 83 | + throw new Error('no valid selctor found'); |
| 84 | + } |
| 85 | + this.allClasses = this.getClasses(); |
| 86 | + this.log(); |
| 87 | + |
| 88 | + this.formControl.valueChanges.subscribe(value => { |
| 89 | + this.log(); |
| 90 | + }); |
| 91 | + this.formControl.statusChanges.subscribe(status => { |
| 92 | + if (!this.formControl.control.validator) { |
| 93 | + return; |
| 94 | + } |
| 95 | + if (status === 'VALID') { |
| 96 | + this.markAsValid(); |
| 97 | + } else { |
| 98 | + this.markAsInvalid(); |
| 99 | + } |
| 100 | + |
| 101 | + this.log(); |
| 102 | + }); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + getClasses() { |
| 107 | + this.c.valid = this.localconfig.prefix + '-' + this.localconfig.classValid; |
| 108 | + this.c.invalid = this.localconfig.prefix + '-' + this.localconfig.classInvalid; |
| 109 | + this.c.pristine = this.localconfig.prefix + '-' + this.localconfig.classPristine; |
| 110 | + this.c.dirty = this.localconfig.prefix + '-' + this.localconfig.classDirty; |
| 111 | + this.c.touched = this.localconfig.prefix + '-' + this.localconfig.classTouched; |
| 112 | + this.c.untouched = this.localconfig.prefix + '-' + this.localconfig.classUnTouched; |
| 113 | + this.c.haserrors = this.localconfig.prefix + '-' + this.localconfig.classHasErrors; |
| 114 | + this.c.noerrors = this.localconfig.prefix + '-' + this.localconfig.classNoErrors; |
| 115 | + this.c.disabled = this.localconfig.prefix + '-' + this.localconfig.classDisabled; |
| 116 | + this.c.enabled = this.localconfig.prefix + '-' + this.localconfig.classEnabled; |
| 117 | + return Object.keys(this.c).map(itm => this.c[itm]); |
| 118 | + } |
| 119 | + |
| 120 | + private validate(value: any) { |
| 121 | + if (value) { |
| 122 | + this.markAsValid(); |
| 123 | + } else { |
| 124 | + this.markAsInvalid(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private markAsInvalid() { |
| 129 | + this.el.classList.remove('success'); |
| 130 | + this.el.classList.add('error'); |
| 131 | + } |
| 132 | + private markAsValid() { |
| 133 | + this.el.classList.remove('error'); |
| 134 | + this.el.classList.add('success'); |
| 135 | + } |
| 136 | + |
| 137 | + private closest(matchesSelector, el, selector) { |
| 138 | + return !el ? null : |
| 139 | + matchesSelector.call(el, selector) ? el : this.closest(matchesSelector, el.parentElement, selector); |
| 140 | + } |
| 141 | + |
| 142 | + private match(el, selector) { |
| 143 | + const matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector; |
| 144 | + return !el ? null : |
| 145 | + matchesSelector.call(el, selector) ? el : this.closest(matchesSelector, el.parentElement, selector); |
| 146 | + } |
| 147 | + |
| 148 | + log() { |
| 149 | + const control = this.formControl; |
| 150 | + const all = this.allClasses; |
| 151 | + const data = [ |
| 152 | + control.valid ? this.c.valid : this.c.invalid, |
| 153 | + control.touched ? this.c.touched : this.c.untouched, |
| 154 | + control.pristine ? this.c.pristine : this.c.dirty, |
| 155 | + control.disabled ? this.c.disabled : this.c.enabled, |
| 156 | + control.errors ? this.c.haserrors : this.c.noerrors |
| 157 | + ]; |
| 158 | + this.el.classList.remove(...all); |
| 159 | + this.el.classList.add(...data); |
| 160 | + } |
| 161 | +} |
0 commit comments