UNPKG

12.1 kBJavaScriptView Raw
1var __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})();
11import { EventEmitter, Input, Output } from '@angular/core';
12import { deepCopy, isArray, isPresent, isString, isTrueProperty, isUndefined } from './util';
13import { Ion } from '../components/ion';
14import { TimeoutDebouncer } from './debouncer';
15var BaseInput = (function (_super) {
16 __extends(BaseInput, _super);
17 /**
18 * @param {?} config
19 * @param {?} elementRef
20 * @param {?} renderer
21 * @param {?} name
22 * @param {?} _defaultValue
23 * @param {?} _form
24 * @param {?} _item
25 * @param {?} _ngControl
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 * \@output {Range} Emitted when the range selector drag starts.
40 */
41 _this.ionFocus = new EventEmitter();
42 /**
43 * \@output {Range} Emitted when the range value changes.
44 */
45 _this.ionChange = new EventEmitter();
46 /**
47 * \@output {Range} Emitted when the range selector drag ends.
48 */
49 _this.ionBlur = new EventEmitter();
50 _form && _form.register(_this);
51 _this._value = deepCopy(_this._defaultValue);
52 if (_item) {
53 (void 0) /* assert */;
54 _this.id = name + '-' + _item.registerInput(name);
55 _this._labelId = _item.labelId;
56 _this._item.setElementClass('item-' + name, true);
57 }
58 // If the user passed a ngControl we need to set the valueAccessor
59 if (_ngControl) {
60 _ngControl.valueAccessor = _this;
61 }
62 return _this;
63 }
64 Object.defineProperty(BaseInput.prototype, "disabled", {
65 /**
66 * \@input {boolean} If true, the user cannot interact with this element.
67 * @return {?}
68 */
69 get: function () {
70 return this._disabled;
71 },
72 /**
73 * @param {?} val
74 * @return {?}
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 * @return {?}
85 */
86 get: function () {
87 return this._value;
88 },
89 /**
90 * @param {?} val
91 * @return {?}
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 * @param {?} val
104 * @return {?}
105 */
106 BaseInput.prototype.setValue = function (val) {
107 this.value = val;
108 };
109 /**
110 * @hidden
111 * @param {?} isDisabled
112 * @return {?}
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 * @hidden
120 * @param {?} val
121 * @return {?}
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 // ngModel fires the first time too late, we need to skip the first ngModel update
130 this._initModel = true;
131 }
132 }
133 };
134 /**
135 * @hidden
136 * @param {?} val
137 * @return {?}
138 */
139 BaseInput.prototype._writeValue = function (val) {
140 (void 0) /* assert */;
141 if (isUndefined(val)) {
142 return false;
143 }
144 var /** @type {?} */ normalized = (val === null)
145 ? deepCopy(this._defaultValue)
146 : this._inputNormalize(val);
147 var /** @type {?} */ notUpdate = isUndefined(normalized) || !this._inputShouldChange(normalized);
148 if (notUpdate) {
149 return false;
150 }
151 (void 0) /* console.debug */;
152 this._value = normalized;
153 if (this._init) {
154 this._inputUpdated();
155 }
156 return true;
157 };
158 /**
159 * @hidden
160 * @return {?}
161 */
162 BaseInput.prototype._fireIonChange = function () {
163 var _this = this;
164 if (this._init) {
165 this._debouncer.debounce(function () {
166 (void 0) /* assert */;
167 _this.ionChange.emit(_this._inputChangeEvent());
168 _this._initModel = true;
169 });
170 }
171 };
172 /**
173 * @hidden
174 * @param {?} fn
175 * @return {?}
176 */
177 BaseInput.prototype.registerOnChange = function (fn) {
178 this._onChanged = fn;
179 };
180 /**
181 * @hidden
182 * @param {?} fn
183 * @return {?}
184 */
185 BaseInput.prototype.registerOnTouched = function (fn) {
186 this._onTouched = fn;
187 };
188 /**
189 * @hidden
190 * @return {?}
191 */
192 BaseInput.prototype._initialize = function () {
193 if (this._init) {
194 (void 0) /* assert */;
195 return;
196 }
197 this._init = true;
198 if (isPresent(this._value)) {
199 this._inputUpdated();
200 }
201 };
202 /**
203 * @hidden
204 * @return {?}
205 */
206 BaseInput.prototype._fireFocus = function () {
207 if (this._isFocus) {
208 return;
209 }
210 (void 0) /* console.debug */;
211 this._form && this._form.setAsFocused(this);
212 this._setFocus(true);
213 this.ionFocus.emit(this);
214 };
215 /**
216 * @hidden
217 * @return {?}
218 */
219 BaseInput.prototype._fireBlur = function () {
220 if (!this._isFocus) {
221 return;
222 }
223 (void 0) /* console.debug */;
224 this._form && this._form.unsetAsFocused(this);
225 this._setFocus(false);
226 this._fireTouched();
227 this.ionBlur.emit(this);
228 };
229 /**
230 * @hidden
231 * @return {?}
232 */
233 BaseInput.prototype._fireTouched = function () {
234 this._onTouched && this._onTouched();
235 };
236 /**
237 * @hidden
238 * @param {?} isFocused
239 * @return {?}
240 */
241 BaseInput.prototype._setFocus = function (isFocused) {
242 (void 0) /* assert */;
243 (void 0) /* assert */;
244 (void 0) /* assert */;
245 this._isFocus = isFocused;
246 var /** @type {?} */ 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 * @hidden
255 * @return {?}
256 */
257 BaseInput.prototype.onChange = function () {
258 this._onChanged && this._onChanged(this._inputNgModelEvent());
259 };
260 /**
261 * @hidden
262 * @return {?}
263 */
264 BaseInput.prototype.isFocus = function () {
265 return this._isFocus;
266 };
267 /**
268 * @hidden
269 * @return {?}
270 */
271 BaseInput.prototype.hasValue = function () {
272 var /** @type {?} */ 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 * @hidden
283 * @return {?}
284 */
285 BaseInput.prototype.focusNext = function () {
286 this._form && this._form.tabFocus(this);
287 };
288 /**
289 * @hidden
290 * @return {?}
291 */
292 BaseInput.prototype.ngOnDestroy = function () {
293 (void 0) /* assert */;
294 var /** @type {?} */ form = this._form;
295 form && form.deregister(this);
296 this._init = false;
297 };
298 /**
299 * @hidden
300 * @return {?}
301 */
302 BaseInput.prototype.ngAfterContentInit = function () {
303 this._initialize();
304 };
305 /**
306 * @hidden
307 * @return {?}
308 */
309 BaseInput.prototype.initFocus = function () {
310 var /** @type {?} */ ele = this._elementRef.nativeElement.querySelector('button');
311 ele && ele.focus();
312 };
313 /**
314 * @hidden
315 * @param {?} val
316 * @return {?}
317 */
318 BaseInput.prototype._inputNormalize = function (val) {
319 return val;
320 };
321 /**
322 * @hidden
323 * @param {?} val
324 * @return {?}
325 */
326 BaseInput.prototype._inputShouldChange = function (val) {
327 return this._value !== val;
328 };
329 /**
330 * @hidden
331 * @return {?}
332 */
333 BaseInput.prototype._inputChangeEvent = function () {
334 return this;
335 };
336 /**
337 * @hidden
338 * @return {?}
339 */
340 BaseInput.prototype._inputNgModelEvent = function () {
341 return this._value;
342 };
343 /**
344 * @hidden
345 * @return {?}
346 */
347 BaseInput.prototype._inputUpdated = function () {
348 (void 0) /* assert */;
349 var /** @type {?} */ item = this._item;
350 if (item) {
351 setControlCss(item, this._ngControl);
352 // TODO remove all uses of input-has-value in v4
353 var /** @type {?} */ 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));
360export { BaseInput };
361BaseInput.propDecorators = {
362 'ionFocus': [{ type: Output },],
363 'ionChange': [{ type: Output },],
364 'ionBlur': [{ type: Output },],
365 'disabled': [{ type: Input },],
366};
367function BaseInput_tsickle_Closure_declarations() {
368 /** @type {?} */
369 BaseInput.propDecorators;
370 /** @type {?} */
371 BaseInput.prototype._value;
372 /** @type {?} */
373 BaseInput.prototype._onChanged;
374 /** @type {?} */
375 BaseInput.prototype._onTouched;
376 /** @type {?} */
377 BaseInput.prototype._isFocus;
378 /** @type {?} */
379 BaseInput.prototype._labelId;
380 /** @type {?} */
381 BaseInput.prototype._disabled;
382 /** @type {?} */
383 BaseInput.prototype._debouncer;
384 /** @type {?} */
385 BaseInput.prototype._init;
386 /** @type {?} */
387 BaseInput.prototype._initModel;
388 /** @type {?} */
389 BaseInput.prototype.id;
390 /**
391 * \@output {Range} Emitted when the range selector drag starts.
392 * @type {?}
393 */
394 BaseInput.prototype.ionFocus;
395 /**
396 * \@output {Range} Emitted when the range value changes.
397 * @type {?}
398 */
399 BaseInput.prototype.ionChange;
400 /**
401 * \@output {Range} Emitted when the range selector drag ends.
402 * @type {?}
403 */
404 BaseInput.prototype.ionBlur;
405 /** @type {?} */
406 BaseInput.prototype._defaultValue;
407 /** @type {?} */
408 BaseInput.prototype._form;
409 /** @type {?} */
410 BaseInput.prototype._item;
411 /** @type {?} */
412 BaseInput.prototype._ngControl;
413}
414/**
415 * @param {?} element
416 * @param {?} control
417 * @return {?}
418 */
419function 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//# sourceMappingURL=base-input.js.map
\No newline at end of file