-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
base-input.ts
328 lines (281 loc) · 6.57 KB
/
base-input.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { AfterContentInit, ElementRef, EventEmitter, Input, NgZone, Output, Renderer } from '@angular/core';
import { ControlValueAccessor } from '@angular/forms';
import { NgControl } from '@angular/forms';
import { isPresent, isUndefined, isArray, isTrueProperty, deepCopy, assert } from './util';
import { Ion } from '../components/ion';
import { Config } from '../config/config';
import { Item } from '../components/item/item';
import { Form } from './form';
import { TimeoutDebouncer } from './debouncer';
export interface CommonInput<T> extends ControlValueAccessor, AfterContentInit {
id: string;
disabled: boolean;
value: T;
ionFocus: EventEmitter<CommonInput<T>>;
ionChange: EventEmitter<BaseInput<T>>;
ionBlur: EventEmitter<BaseInput<T>>;
initFocus(): void;
isFocus(): boolean;
_inputNormalize(val: any): T;
_inputShouldChange(val: T): boolean;
_inputUpdated(): void;
}
export class BaseInput<T> extends Ion implements CommonInput<T> {
_value: T;
_onChanged: Function;
_onTouched: Function;
_isFocus: boolean = false;
_labelId: string;
_disabled: boolean = false;
_debouncer: TimeoutDebouncer = new TimeoutDebouncer(0);
_init: boolean = false;
_initModel: boolean = false;
id: string;
/**
* @output {Range} Emitted when the range selector drag starts.
*/
@Output() ionFocus: EventEmitter<BaseInput<T>> = new EventEmitter<BaseInput<T>>();
/**
* @output {Range} Emitted when the range value changes.
*/
@Output() ionChange: EventEmitter<BaseInput<T>> = new EventEmitter<BaseInput<T>>();
/**
* @output {Range} Emitted when the range selector drag ends.
*/
@Output() ionBlur: EventEmitter<BaseInput<T>> = new EventEmitter<BaseInput<T>>();
/**
* @input {boolean} If true, the user cannot interact with this element.
*/
@Input()
get disabled(): boolean {
return this._disabled;
}
set disabled(val: boolean) {
this.setDisabledState(val);
}
constructor(
config: Config,
elementRef: ElementRef,
renderer: Renderer,
name: string,
private _defaultValue: T,
public _form: Form,
public _item: Item,
ngControl: NgControl
) {
super(config, elementRef, renderer, name);
_form && _form.register(this);
this._value = deepCopy(this._defaultValue);
if (_item) {
this.id = name + '-' + _item.registerInput(name);
this._labelId = 'lbl-' + _item.id;
this._item.setElementClass('item-' + name, true);
}
// If the user passed a ngControl we need to set the valueAccessor
if (ngControl) {
ngControl.valueAccessor = this;
}
}
get value(): T {
return this._value;
}
set value(val: T) {
if (this._writeValue(val)) {
this.onChange();
this._fireIonChange();
}
}
// 1. Updates the value
// 2. Calls _inputUpdated()
// 3. Dispatch onChange events
setValue(val: any) {
this.value = val;
}
/**
* @hidden
*/
setDisabledState(isDisabled: boolean) {
this._disabled = isDisabled = isTrueProperty(isDisabled);
this._item && this._item.setElementClass(`item-${this._componentName}-disabled`, isDisabled);
}
/**
* @hidden
*/
writeValue(val: any) {
if (this._writeValue(val)) {
if (this._initModel) {
this._fireIonChange();
} else if (this._init) {
// ngModel fires the first time too late, we need to skip the first ngModel update
this._initModel = true;
}
}
}
/**
* @hidden
*/
_writeValue(val: any): boolean {
assert(NgZone.isInAngularZone(), 'callback should be zoned');
if (isUndefined(val)) {
return false;
}
let normalized;
if (val === null) {
normalized = deepCopy(this._defaultValue);
} else {
normalized = this._inputNormalize(val);
}
const notUpdate = isUndefined(normalized) || !this._inputShouldChange(normalized);
if (notUpdate) {
return false;
}
console.debug('BaseInput: value changed:', normalized, this);
this._value = normalized;
this._inputCheckHasValue(normalized);
if (this._init) {
this._inputUpdated();
}
return true;
}
/**
* @hidden
*/
_fireIonChange() {
if (this._init) {
this._debouncer.debounce(() => {
assert(NgZone.isInAngularZone(), 'IonChange: should be zoned');
this.ionChange.emit(this._inputChangeEvent());
this._initModel = true;
});
}
}
/**
* @hidden
*/
registerOnChange(fn: Function) {
this._onChanged = fn;
}
/**
* @hidden
*/
registerOnTouched(fn: any) {
this._onTouched = fn;
}
/**
* @hidden
*/
_initialize() {
if (this._init) {
assert(false, 'input was already initilized');
return;
}
this._init = true;
if (isPresent(this._value)) {
this._inputUpdated();
}
}
/**
* @hidden
*/
_fireFocus() {
if (this._isFocus) {
return;
}
assert(this._init, 'component was not initialized');
assert(NgZone.isInAngularZone(), '_fireFocus: should be zoned');
this._isFocus = true;
this.ionFocus.emit(this);
this._inputUpdated();
}
/**
* @hidden
*/
_fireBlur() {
if (!this._isFocus) {
return;
}
assert(this._init, 'component was not initialized');
assert(NgZone.isInAngularZone(), '_fireBlur: should be zoned');
this._isFocus = false;
this.ionBlur.emit(this);
this._inputUpdated();
}
/**
* @hidden
*/
private onChange() {
this._onChanged && this._onChanged(this._inputNgModelEvent());
this._onTouched && this._onTouched();
}
/**
* @hidden
*/
isFocus(): boolean {
return this._isFocus;
}
/**
* @hidden
*/
hasValue(): boolean {
const val = this._value;
return isArray(val)
? val.length > 0
: isPresent(val);
}
/**
* @hidden
*/
ngOnDestroy() {
this._form && this._form.deregister(this);
this._init = false;
}
/**
* @hidden
*/
ngAfterContentInit() {
this._initialize();
}
/**
* @hidden
*/
_inputCheckHasValue(val: T) {
if (!this._item) {
return;
}
this._item.setElementClass('input-has-value', this.hasValue());
}
/**
* @hidden
*/
initFocus() { }
/**
* @hidden
*/
_inputNormalize(val: any): T {
return val;
}
/**
* @hidden
*/
_inputShouldChange(val: T): boolean {
return this._value !== val;
}
/**
* @hidden
*/
_inputChangeEvent(): any {
return this;
}
/**
* @hidden
*/
_inputNgModelEvent(): any {
return this._value;
}
/**
* @hidden
*/
_inputUpdated() {
assert(this._init, 'component should be initialized');
}
}