1 | var __extends = (this && this.__extends) || (function () {
|
2 | var extendStatics = Object.setPrototypeOf ||
|
3 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
4 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
5 | return function (d, b) {
|
6 | extendStatics(d, b);
|
7 | function __() { this.constructor = d; }
|
8 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
9 | };
|
10 | })();
|
11 | import { EventEmitter, Input, Output } from '@angular/core';
|
12 | import { deepCopy, isArray, isPresent, isString, isTrueProperty, isUndefined } from './util';
|
13 | import { Ion } from '../components/ion';
|
14 | import { TimeoutDebouncer } from './debouncer';
|
15 | var BaseInput = (function (_super) {
|
16 | __extends(BaseInput, _super);
|
17 | |
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function BaseInput(config, elementRef, renderer, name, _defaultValue, _form, _item, _ngControl) {
|
28 | var _this = _super.call(this, config, elementRef, renderer, name) || this;
|
29 | _this._defaultValue = _defaultValue;
|
30 | _this._form = _form;
|
31 | _this._item = _item;
|
32 | _this._ngControl = _ngControl;
|
33 | _this._isFocus = false;
|
34 | _this._disabled = false;
|
35 | _this._debouncer = new TimeoutDebouncer(0);
|
36 | _this._init = false;
|
37 | _this._initModel = false;
|
38 | |
39 |
|
40 |
|
41 | _this.ionFocus = new EventEmitter();
|
42 | |
43 |
|
44 |
|
45 | _this.ionChange = new EventEmitter();
|
46 | |
47 |
|
48 |
|
49 | _this.ionBlur = new EventEmitter();
|
50 | _form && _form.register(_this);
|
51 | _this._value = deepCopy(_this._defaultValue);
|
52 | if (_item) {
|
53 | (void 0) ;
|
54 | _this.id = name + '-' + _item.registerInput(name);
|
55 | _this._labelId = _item.labelId;
|
56 | _this._item.setElementClass('item-' + name, true);
|
57 | }
|
58 |
|
59 | if (_ngControl) {
|
60 | _ngControl.valueAccessor = _this;
|
61 | }
|
62 | return _this;
|
63 | }
|
64 | Object.defineProperty(BaseInput.prototype, "disabled", {
|
65 | |
66 |
|
67 |
|
68 |
|
69 | get: function () {
|
70 | return this._disabled;
|
71 | },
|
72 | |
73 |
|
74 |
|
75 |
|
76 | set: function (val) {
|
77 | this.setDisabledState(val);
|
78 | },
|
79 | enumerable: true,
|
80 | configurable: true
|
81 | });
|
82 | Object.defineProperty(BaseInput.prototype, "value", {
|
83 | |
84 |
|
85 |
|
86 | get: function () {
|
87 | return this._value;
|
88 | },
|
89 | |
90 |
|
91 |
|
92 |
|
93 | set: function (val) {
|
94 | if (this._writeValue(val)) {
|
95 | this.onChange();
|
96 | this._fireIonChange();
|
97 | }
|
98 | },
|
99 | enumerable: true,
|
100 | configurable: true
|
101 | });
|
102 | |
103 |
|
104 |
|
105 |
|
106 | BaseInput.prototype.setValue = function (val) {
|
107 | this.value = val;
|
108 | };
|
109 | |
110 |
|
111 |
|
112 |
|
113 |
|
114 | BaseInput.prototype.setDisabledState = function (isDisabled) {
|
115 | this._disabled = isDisabled = isTrueProperty(isDisabled);
|
116 | this._item && this._item.setElementClass("item-" + this._componentName + "-disabled", isDisabled);
|
117 | };
|
118 | |
119 |
|
120 |
|
121 |
|
122 |
|
123 | BaseInput.prototype.writeValue = function (val) {
|
124 | if (this._writeValue(val)) {
|
125 | if (this._initModel) {
|
126 | this._fireIonChange();
|
127 | }
|
128 | else if (this._init) {
|
129 |
|
130 | this._initModel = true;
|
131 | }
|
132 | }
|
133 | };
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 | BaseInput.prototype._writeValue = function (val) {
|
140 | (void 0) ;
|
141 | if (isUndefined(val)) {
|
142 | return false;
|
143 | }
|
144 | var normalized = (val === null)
|
145 | ? deepCopy(this._defaultValue)
|
146 | : this._inputNormalize(val);
|
147 | var notUpdate = isUndefined(normalized) || !this._inputShouldChange(normalized);
|
148 | if (notUpdate) {
|
149 | return false;
|
150 | }
|
151 | (void 0) ;
|
152 | this._value = normalized;
|
153 | if (this._init) {
|
154 | this._inputUpdated();
|
155 | }
|
156 | return true;
|
157 | };
|
158 | |
159 |
|
160 |
|
161 |
|
162 | BaseInput.prototype._fireIonChange = function () {
|
163 | var _this = this;
|
164 | if (this._init) {
|
165 | this._debouncer.debounce(function () {
|
166 | (void 0) ;
|
167 | _this.ionChange.emit(_this._inputChangeEvent());
|
168 | _this._initModel = true;
|
169 | });
|
170 | }
|
171 | };
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 | BaseInput.prototype.registerOnChange = function (fn) {
|
178 | this._onChanged = fn;
|
179 | };
|
180 | |
181 |
|
182 |
|
183 |
|
184 |
|
185 | BaseInput.prototype.registerOnTouched = function (fn) {
|
186 | this._onTouched = fn;
|
187 | };
|
188 | |
189 |
|
190 |
|
191 |
|
192 | BaseInput.prototype._initialize = function () {
|
193 | if (this._init) {
|
194 | (void 0) ;
|
195 | return;
|
196 | }
|
197 | this._init = true;
|
198 | if (isPresent(this._value)) {
|
199 | this._inputUpdated();
|
200 | }
|
201 | };
|
202 | |
203 |
|
204 |
|
205 |
|
206 | BaseInput.prototype._fireFocus = function () {
|
207 | if (this._isFocus) {
|
208 | return;
|
209 | }
|
210 | (void 0) ;
|
211 | this._form && this._form.setAsFocused(this);
|
212 | this._setFocus(true);
|
213 | this.ionFocus.emit(this);
|
214 | };
|
215 | |
216 |
|
217 |
|
218 |
|
219 | BaseInput.prototype._fireBlur = function () {
|
220 | if (!this._isFocus) {
|
221 | return;
|
222 | }
|
223 | (void 0) ;
|
224 | this._form && this._form.unsetAsFocused(this);
|
225 | this._setFocus(false);
|
226 | this._fireTouched();
|
227 | this.ionBlur.emit(this);
|
228 | };
|
229 | |
230 |
|
231 |
|
232 |
|
233 | BaseInput.prototype._fireTouched = function () {
|
234 | this._onTouched && this._onTouched();
|
235 | };
|
236 | |
237 |
|
238 |
|
239 |
|
240 |
|
241 | BaseInput.prototype._setFocus = function (isFocused) {
|
242 | (void 0) ;
|
243 | (void 0) ;
|
244 | (void 0) ;
|
245 | this._isFocus = isFocused;
|
246 | var item = this._item;
|
247 | if (item) {
|
248 | item.setElementClass('input-has-focus', isFocused);
|
249 | item.setElementClass('item-input-has-focus', isFocused);
|
250 | }
|
251 | this._inputUpdated();
|
252 | };
|
253 | |
254 |
|
255 |
|
256 |
|
257 | BaseInput.prototype.onChange = function () {
|
258 | this._onChanged && this._onChanged(this._inputNgModelEvent());
|
259 | };
|
260 | |
261 |
|
262 |
|
263 |
|
264 | BaseInput.prototype.isFocus = function () {
|
265 | return this._isFocus;
|
266 | };
|
267 | |
268 |
|
269 |
|
270 |
|
271 | BaseInput.prototype.hasValue = function () {
|
272 | var val = this._value;
|
273 | if (!isPresent(val)) {
|
274 | return false;
|
275 | }
|
276 | if (isArray(val) || isString(val)) {
|
277 | return val.length > 0;
|
278 | }
|
279 | return true;
|
280 | };
|
281 | |
282 |
|
283 |
|
284 |
|
285 | BaseInput.prototype.focusNext = function () {
|
286 | this._form && this._form.tabFocus(this);
|
287 | };
|
288 | |
289 |
|
290 |
|
291 |
|
292 | BaseInput.prototype.ngOnDestroy = function () {
|
293 | (void 0) ;
|
294 | var form = this._form;
|
295 | form && form.deregister(this);
|
296 | this._init = false;
|
297 | };
|
298 | |
299 |
|
300 |
|
301 |
|
302 | BaseInput.prototype.ngAfterContentInit = function () {
|
303 | this._initialize();
|
304 | };
|
305 | |
306 |
|
307 |
|
308 |
|
309 | BaseInput.prototype.initFocus = function () {
|
310 | var ele = this._elementRef.nativeElement.querySelector('button');
|
311 | ele && ele.focus();
|
312 | };
|
313 | |
314 |
|
315 |
|
316 |
|
317 |
|
318 | BaseInput.prototype._inputNormalize = function (val) {
|
319 | return val;
|
320 | };
|
321 | |
322 |
|
323 |
|
324 |
|
325 |
|
326 | BaseInput.prototype._inputShouldChange = function (val) {
|
327 | return this._value !== val;
|
328 | };
|
329 | |
330 |
|
331 |
|
332 |
|
333 | BaseInput.prototype._inputChangeEvent = function () {
|
334 | return this;
|
335 | };
|
336 | |
337 |
|
338 |
|
339 |
|
340 | BaseInput.prototype._inputNgModelEvent = function () {
|
341 | return this._value;
|
342 | };
|
343 | |
344 |
|
345 |
|
346 |
|
347 | BaseInput.prototype._inputUpdated = function () {
|
348 | (void 0) ;
|
349 | var item = this._item;
|
350 | if (item) {
|
351 | setControlCss(item, this._ngControl);
|
352 |
|
353 | var hasValue = this.hasValue();
|
354 | item.setElementClass('input-has-value', hasValue);
|
355 | item.setElementClass('item-input-has-value', hasValue);
|
356 | }
|
357 | };
|
358 | return BaseInput;
|
359 | }(Ion));
|
360 | export { BaseInput };
|
361 | BaseInput.propDecorators = {
|
362 | 'ionFocus': [{ type: Output },],
|
363 | 'ionChange': [{ type: Output },],
|
364 | 'ionBlur': [{ type: Output },],
|
365 | 'disabled': [{ type: Input },],
|
366 | };
|
367 | function BaseInput_tsickle_Closure_declarations() {
|
368 |
|
369 | BaseInput.propDecorators;
|
370 |
|
371 | BaseInput.prototype._value;
|
372 |
|
373 | BaseInput.prototype._onChanged;
|
374 |
|
375 | BaseInput.prototype._onTouched;
|
376 |
|
377 | BaseInput.prototype._isFocus;
|
378 |
|
379 | BaseInput.prototype._labelId;
|
380 |
|
381 | BaseInput.prototype._disabled;
|
382 |
|
383 | BaseInput.prototype._debouncer;
|
384 |
|
385 | BaseInput.prototype._init;
|
386 |
|
387 | BaseInput.prototype._initModel;
|
388 |
|
389 | BaseInput.prototype.id;
|
390 | |
391 |
|
392 |
|
393 |
|
394 | BaseInput.prototype.ionFocus;
|
395 | |
396 |
|
397 |
|
398 |
|
399 | BaseInput.prototype.ionChange;
|
400 | |
401 |
|
402 |
|
403 |
|
404 | BaseInput.prototype.ionBlur;
|
405 |
|
406 | BaseInput.prototype._defaultValue;
|
407 |
|
408 | BaseInput.prototype._form;
|
409 |
|
410 | BaseInput.prototype._item;
|
411 |
|
412 | BaseInput.prototype._ngControl;
|
413 | }
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 | function setControlCss(element, control) {
|
420 | if (!control) {
|
421 | return;
|
422 | }
|
423 | element.setElementClass('ng-untouched', control.untouched);
|
424 | element.setElementClass('ng-touched', control.touched);
|
425 | element.setElementClass('ng-pristine', control.pristine);
|
426 | element.setElementClass('ng-dirty', control.dirty);
|
427 | element.setElementClass('ng-valid', control.valid);
|
428 | element.setElementClass('ng-invalid', !control.valid);
|
429 | }
|
430 |
|
\ | No newline at end of file |