UNPKG

9.56 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 function BaseInput(config, elementRef, renderer, name, _defaultValue, _form, _item, _ngControl) {
18 var _this = _super.call(this, config, elementRef, renderer, name) || this;
19 _this._defaultValue = _defaultValue;
20 _this._form = _form;
21 _this._item = _item;
22 _this._ngControl = _ngControl;
23 _this._isFocus = false;
24 _this._disabled = false;
25 _this._debouncer = new TimeoutDebouncer(0);
26 _this._init = false;
27 _this._initModel = false;
28 /**
29 * @output {Range} Emitted when the range selector drag starts.
30 */
31 _this.ionFocus = new EventEmitter();
32 /**
33 * @output {Range} Emitted when the range value changes.
34 */
35 _this.ionChange = new EventEmitter();
36 /**
37 * @output {Range} Emitted when the range selector drag ends.
38 */
39 _this.ionBlur = new EventEmitter();
40 _form && _form.register(_this);
41 _this._value = deepCopy(_this._defaultValue);
42 if (_item) {
43 (void 0) /* assert */;
44 _this.id = name + '-' + _item.registerInput(name);
45 _this._labelId = _item.labelId;
46 _this._item.setElementClass('item-' + name, true);
47 }
48 // If the user passed a ngControl we need to set the valueAccessor
49 if (_ngControl) {
50 _ngControl.valueAccessor = _this;
51 }
52 return _this;
53 }
54 Object.defineProperty(BaseInput.prototype, "disabled", {
55 /**
56 * @input {boolean} If true, the user cannot interact with this element.
57 */
58 get: function () {
59 return this._disabled;
60 },
61 set: function (val) {
62 this.setDisabledState(val);
63 },
64 enumerable: true,
65 configurable: true
66 });
67 Object.defineProperty(BaseInput.prototype, "value", {
68 get: function () {
69 return this._value;
70 },
71 set: function (val) {
72 if (this._writeValue(val)) {
73 this.onChange();
74 this._fireIonChange();
75 }
76 },
77 enumerable: true,
78 configurable: true
79 });
80 // 1. Updates the value
81 // 2. Calls _inputUpdated()
82 // 3. Dispatch onChange events
83 BaseInput.prototype.setValue = function (val) {
84 this.value = val;
85 };
86 /**
87 * @hidden
88 */
89 BaseInput.prototype.setDisabledState = function (isDisabled) {
90 this._disabled = isDisabled = isTrueProperty(isDisabled);
91 this._item && this._item.setElementClass("item-" + this._componentName + "-disabled", isDisabled);
92 };
93 /**
94 * @hidden
95 */
96 BaseInput.prototype.writeValue = function (val) {
97 if (this._writeValue(val)) {
98 if (this._initModel) {
99 this._fireIonChange();
100 }
101 else if (this._init) {
102 // ngModel fires the first time too late, we need to skip the first ngModel update
103 this._initModel = true;
104 }
105 }
106 };
107 /**
108 * @hidden
109 */
110 BaseInput.prototype._writeValue = function (val) {
111 (void 0) /* assert */;
112 if (isUndefined(val)) {
113 return false;
114 }
115 var normalized = (val === null)
116 ? deepCopy(this._defaultValue)
117 : this._inputNormalize(val);
118 var notUpdate = isUndefined(normalized) || !this._inputShouldChange(normalized);
119 if (notUpdate) {
120 return false;
121 }
122 (void 0) /* console.debug */;
123 this._value = normalized;
124 if (this._init) {
125 this._inputUpdated();
126 }
127 return true;
128 };
129 /**
130 * @hidden
131 */
132 BaseInput.prototype._fireIonChange = function () {
133 var _this = this;
134 if (this._init) {
135 this._debouncer.debounce(function () {
136 (void 0) /* assert */;
137 _this.ionChange.emit(_this._inputChangeEvent());
138 _this._initModel = true;
139 });
140 }
141 };
142 /**
143 * @hidden
144 */
145 BaseInput.prototype.registerOnChange = function (fn) {
146 this._onChanged = fn;
147 };
148 /**
149 * @hidden
150 */
151 BaseInput.prototype.registerOnTouched = function (fn) {
152 this._onTouched = fn;
153 };
154 /**
155 * @hidden
156 */
157 BaseInput.prototype._initialize = function () {
158 if (this._init) {
159 (void 0) /* assert */;
160 return;
161 }
162 this._init = true;
163 if (isPresent(this._value)) {
164 this._inputUpdated();
165 }
166 };
167 /**
168 * @hidden
169 */
170 BaseInput.prototype._fireFocus = function () {
171 if (this._isFocus) {
172 return;
173 }
174 (void 0) /* console.debug */;
175 this._form && this._form.setAsFocused(this);
176 this._setFocus(true);
177 this.ionFocus.emit(this);
178 };
179 /**
180 * @hidden
181 */
182 BaseInput.prototype._fireBlur = function () {
183 if (!this._isFocus) {
184 return;
185 }
186 (void 0) /* console.debug */;
187 this._form && this._form.unsetAsFocused(this);
188 this._setFocus(false);
189 this._fireTouched();
190 this.ionBlur.emit(this);
191 };
192 /**
193 * @hidden
194 */
195 BaseInput.prototype._fireTouched = function () {
196 this._onTouched && this._onTouched();
197 };
198 /**
199 * @hidden
200 */
201 BaseInput.prototype._setFocus = function (isFocused) {
202 (void 0) /* assert */;
203 (void 0) /* assert */;
204 (void 0) /* assert */;
205 this._isFocus = isFocused;
206 var item = this._item;
207 if (item) {
208 item.setElementClass('input-has-focus', isFocused);
209 item.setElementClass('item-input-has-focus', isFocused);
210 }
211 this._inputUpdated();
212 };
213 /**
214 * @hidden
215 */
216 BaseInput.prototype.onChange = function () {
217 this._onChanged && this._onChanged(this._inputNgModelEvent());
218 };
219 /**
220 * @hidden
221 */
222 BaseInput.prototype.isFocus = function () {
223 return this._isFocus;
224 };
225 /**
226 * @hidden
227 */
228 BaseInput.prototype.hasValue = function () {
229 var val = this._value;
230 if (!isPresent(val)) {
231 return false;
232 }
233 if (isArray(val) || isString(val)) {
234 return val.length > 0;
235 }
236 return true;
237 };
238 /**
239 * @hidden
240 */
241 BaseInput.prototype.focusNext = function () {
242 this._form && this._form.tabFocus(this);
243 };
244 /**
245 * @hidden
246 */
247 BaseInput.prototype.ngOnDestroy = function () {
248 (void 0) /* assert */;
249 var form = this._form;
250 form && form.deregister(this);
251 this._init = false;
252 };
253 /**
254 * @hidden
255 */
256 BaseInput.prototype.ngAfterContentInit = function () {
257 this._initialize();
258 };
259 /**
260 * @hidden
261 */
262 BaseInput.prototype.initFocus = function () {
263 var ele = this._elementRef.nativeElement.querySelector('button');
264 ele && ele.focus();
265 };
266 /**
267 * @hidden
268 */
269 BaseInput.prototype._inputNormalize = function (val) {
270 return val;
271 };
272 /**
273 * @hidden
274 */
275 BaseInput.prototype._inputShouldChange = function (val) {
276 return this._value !== val;
277 };
278 /**
279 * @hidden
280 */
281 BaseInput.prototype._inputChangeEvent = function () {
282 return this;
283 };
284 /**
285 * @hidden
286 */
287 BaseInput.prototype._inputNgModelEvent = function () {
288 return this._value;
289 };
290 /**
291 * @hidden
292 */
293 BaseInput.prototype._inputUpdated = function () {
294 (void 0) /* assert */;
295 var item = this._item;
296 if (item) {
297 setControlCss(item, this._ngControl);
298 // TODO remove all uses of input-has-value in v4
299 var hasValue = this.hasValue();
300 item.setElementClass('input-has-value', hasValue);
301 item.setElementClass('item-input-has-value', hasValue);
302 }
303 };
304 BaseInput.propDecorators = {
305 'ionFocus': [{ type: Output },],
306 'ionChange': [{ type: Output },],
307 'ionBlur': [{ type: Output },],
308 'disabled': [{ type: Input },],
309 };
310 return BaseInput;
311}(Ion));
312export { BaseInput };
313function setControlCss(element, control) {
314 if (!control) {
315 return;
316 }
317 element.setElementClass('ng-untouched', control.untouched);
318 element.setElementClass('ng-touched', control.touched);
319 element.setElementClass('ng-pristine', control.pristine);
320 element.setElementClass('ng-dirty', control.dirty);
321 element.setElementClass('ng-valid', control.valid);
322 element.setElementClass('ng-invalid', !control.valid);
323}
324//# sourceMappingURL=base-input.js.map
\No newline at end of file