UNPKG

22.9 kBJavaScriptView Raw
1var __extends = (this && this.__extends) || function (d, b) {
2 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3 function __() { this.constructor = d; }
4 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
5};
6var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
7 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
8 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
9 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
10 return c > 3 && r && Object.defineProperty(target, key, r), r;
11};
12var __metadata = (this && this.__metadata) || function (k, v) {
13 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
14};
15import { forwardRef, Component, HostBinding, Input, Directive, ContentChild, ContentChildren, ViewChild, ElementRef, QueryList, EventEmitter, Output, NgModule } from '@angular/core';
16import { NG_VALUE_ACCESSOR, FormsModule } from '@angular/forms';
17import { CommonModule } from '@angular/common';
18import { BooleanFieldValue, MdError } from '@angular2-material/core';
19import { Observable } from 'rxjs/Observable';
20var noop = function () { };
21export var MD_INPUT_CONTROL_VALUE_ACCESSOR = {
22 provide: NG_VALUE_ACCESSOR,
23 useExisting: forwardRef(function () { return MdInput; }),
24 multi: true
25};
26// Invalid input type. Using one of these will throw an MdInputUnsupportedTypeError.
27var MD_INPUT_INVALID_INPUT_TYPE = [
28 'file',
29 'radio',
30 'checkbox',
31];
32var nextUniqueId = 0;
33export var MdInputPlaceholderConflictError = (function (_super) {
34 __extends(MdInputPlaceholderConflictError, _super);
35 function MdInputPlaceholderConflictError() {
36 _super.call(this, 'Placeholder attribute and child element were both specified.');
37 }
38 return MdInputPlaceholderConflictError;
39}(MdError));
40export var MdInputUnsupportedTypeError = (function (_super) {
41 __extends(MdInputUnsupportedTypeError, _super);
42 function MdInputUnsupportedTypeError(type) {
43 _super.call(this, "Input type \"" + type + "\" isn't supported by md-input.");
44 }
45 return MdInputUnsupportedTypeError;
46}(MdError));
47export var MdInputDuplicatedHintError = (function (_super) {
48 __extends(MdInputDuplicatedHintError, _super);
49 function MdInputDuplicatedHintError(align) {
50 _super.call(this, "A hint was already declared for 'align=\"" + align + "\"'.");
51 }
52 return MdInputDuplicatedHintError;
53}(MdError));
54/**
55 * The placeholder directive. The content can declare this to implement more
56 * complex placeholders.
57 */
58export var MdPlaceholder = (function () {
59 function MdPlaceholder() {
60 }
61 MdPlaceholder = __decorate([
62 Directive({
63 selector: 'md-placeholder'
64 }),
65 __metadata('design:paramtypes', [])
66 ], MdPlaceholder);
67 return MdPlaceholder;
68}());
69/** The hint directive, used to tag content as hint labels (going under the input). */
70export var MdHint = (function () {
71 function MdHint() {
72 // Whether to align the hint label at the start or end of the line.
73 this.align = 'start';
74 }
75 __decorate([
76 Input(),
77 __metadata('design:type', Object)
78 ], MdHint.prototype, "align", void 0);
79 MdHint = __decorate([
80 Directive({
81 selector: 'md-hint',
82 host: {
83 '[class.md-right]': 'align == "end"',
84 '[class.md-hint]': 'true'
85 }
86 }),
87 __metadata('design:paramtypes', [])
88 ], MdHint);
89 return MdHint;
90}());
91/**
92 * Component that represents a text input. It encapsulates the <input> HTMLElement and
93 * improve on its behaviour, along with styling it according to the Material Design.
94 */
95export var MdInput = (function () {
96 function MdInput() {
97 this._focused = false;
98 this._value = '';
99 /** Callback registered via registerOnTouched (ControlValueAccessor) */
100 this._onTouchedCallback = noop;
101 /** Callback registered via registerOnChange (ControlValueAccessor) */
102 this._onChangeCallback = noop;
103 /**
104 * Bindings.
105 */
106 this.align = 'start';
107 this.dividerColor = 'primary';
108 this.floatingPlaceholder = true;
109 this.hintLabel = '';
110 this.autofocus = false;
111 this.disabled = false;
112 this.id = "md-input-" + nextUniqueId++;
113 this.list = null;
114 this.max = null;
115 this.maxlength = null;
116 this.min = null;
117 this.minlength = null;
118 this.placeholder = null;
119 this.readonly = false;
120 this.required = false;
121 this.spellcheck = false;
122 this.step = null;
123 this.tabindex = null;
124 this.type = 'text';
125 this.name = null;
126 this._blurEmitter = new EventEmitter();
127 this._focusEmitter = new EventEmitter();
128 }
129 Object.defineProperty(MdInput.prototype, "focused", {
130 /** Readonly properties. */
131 get: function () { return this._focused; },
132 enumerable: true,
133 configurable: true
134 });
135 Object.defineProperty(MdInput.prototype, "empty", {
136 get: function () { return this._value == null || this._value === ''; },
137 enumerable: true,
138 configurable: true
139 });
140 Object.defineProperty(MdInput.prototype, "characterCount", {
141 get: function () {
142 return this.empty ? 0 : ('' + this._value).length;
143 },
144 enumerable: true,
145 configurable: true
146 });
147 Object.defineProperty(MdInput.prototype, "inputId", {
148 get: function () { return this.id + "-input"; },
149 enumerable: true,
150 configurable: true
151 });
152 Object.defineProperty(MdInput.prototype, "onBlur", {
153 get: function () {
154 return this._blurEmitter.asObservable();
155 },
156 enumerable: true,
157 configurable: true
158 });
159 Object.defineProperty(MdInput.prototype, "onFocus", {
160 get: function () {
161 return this._focusEmitter.asObservable();
162 },
163 enumerable: true,
164 configurable: true
165 });
166 Object.defineProperty(MdInput.prototype, "value", {
167 get: function () { return this._value; },
168 set: function (v) {
169 v = this._convertValueForInputType(v);
170 if (v !== this._value) {
171 this._value = v;
172 this._onChangeCallback(v);
173 }
174 },
175 enumerable: true,
176 configurable: true
177 });
178 ;
179 Object.defineProperty(MdInput.prototype, "_align", {
180 // This is to remove the `align` property of the `md-input` itself. Otherwise HTML5
181 // might place it as RTL when we don't want to. We still want to use `align` as an
182 // Input though, so we use HostBinding.
183 get: function () { return null; },
184 enumerable: true,
185 configurable: true
186 });
187 /** Set focus on input */
188 MdInput.prototype.focus = function () {
189 this._inputElement.nativeElement.focus();
190 };
191 MdInput.prototype._handleFocus = function (event) {
192 this._focused = true;
193 this._focusEmitter.emit(event);
194 };
195 MdInput.prototype._handleBlur = function (event) {
196 this._focused = false;
197 this._onTouchedCallback();
198 this._blurEmitter.emit(event);
199 };
200 MdInput.prototype._handleChange = function (event) {
201 this.value = event.target.value;
202 this._onTouchedCallback();
203 };
204 MdInput.prototype._hasPlaceholder = function () {
205 return !!this.placeholder || this._placeholderChild != null;
206 };
207 /**
208 * Implemented as part of ControlValueAccessor.
209 * TODO: internal
210 */
211 MdInput.prototype.writeValue = function (value) {
212 this._value = value;
213 };
214 /**
215 * Implemented as part of ControlValueAccessor.
216 * TODO: internal
217 */
218 MdInput.prototype.registerOnChange = function (fn) {
219 this._onChangeCallback = fn;
220 };
221 /**
222 * Implemented as part of ControlValueAccessor.
223 * TODO: internal
224 */
225 MdInput.prototype.registerOnTouched = function (fn) {
226 this._onTouchedCallback = fn;
227 };
228 /** TODO: internal */
229 MdInput.prototype.ngAfterContentInit = function () {
230 var _this = this;
231 this._validateConstraints();
232 // Trigger validation when the hint children change.
233 this._hintChildren.changes.subscribe(function () {
234 _this._validateConstraints();
235 });
236 };
237 /** TODO: internal */
238 MdInput.prototype.ngOnChanges = function (changes) {
239 this._validateConstraints();
240 };
241 /**
242 * Convert the value passed in to a value that is expected from the type of the md-input.
243 * This is normally performed by the *_VALUE_ACCESSOR in forms, but since the type is bound
244 * on our internal input it won't work locally.
245 * @private
246 */
247 MdInput.prototype._convertValueForInputType = function (v) {
248 switch (this.type) {
249 case 'number': return parseFloat(v);
250 default: return v;
251 }
252 };
253 /**
254 * Ensure that all constraints defined by the API are validated, or throw errors otherwise.
255 * Constraints for now:
256 * - placeholder attribute and <md-placeholder> are mutually exclusive.
257 * - type attribute is not one of the forbidden types (see constant at the top).
258 * - Maximum one of each `<md-hint>` alignment specified, with the attribute being
259 * considered as align="start".
260 * @private
261 */
262 MdInput.prototype._validateConstraints = function () {
263 var _this = this;
264 if (this.placeholder != '' && this.placeholder != null && this._placeholderChild != null) {
265 throw new MdInputPlaceholderConflictError();
266 }
267 if (MD_INPUT_INVALID_INPUT_TYPE.indexOf(this.type) != -1) {
268 throw new MdInputUnsupportedTypeError(this.type);
269 }
270 if (this._hintChildren) {
271 // Validate the hint labels.
272 var startHint_1 = null;
273 var endHint_1 = null;
274 this._hintChildren.forEach(function (hint) {
275 if (hint.align == 'start') {
276 if (startHint_1 || _this.hintLabel) {
277 throw new MdInputDuplicatedHintError('start');
278 }
279 startHint_1 = hint;
280 }
281 else if (hint.align == 'end') {
282 if (endHint_1) {
283 throw new MdInputDuplicatedHintError('end');
284 }
285 endHint_1 = hint;
286 }
287 });
288 }
289 };
290 __decorate([
291 Input('aria-label'),
292 __metadata('design:type', String)
293 ], MdInput.prototype, "ariaLabel", void 0);
294 __decorate([
295 Input('aria-labelledby'),
296 __metadata('design:type', String)
297 ], MdInput.prototype, "ariaLabelledBy", void 0);
298 __decorate([
299 Input('aria-disabled'),
300 BooleanFieldValue(),
301 __metadata('design:type', Boolean)
302 ], MdInput.prototype, "ariaDisabled", void 0);
303 __decorate([
304 Input('aria-required'),
305 BooleanFieldValue(),
306 __metadata('design:type', Boolean)
307 ], MdInput.prototype, "ariaRequired", void 0);
308 __decorate([
309 Input('aria-invalid'),
310 BooleanFieldValue(),
311 __metadata('design:type', Boolean)
312 ], MdInput.prototype, "ariaInvalid", void 0);
313 __decorate([
314 ContentChild(MdPlaceholder),
315 __metadata('design:type', MdPlaceholder)
316 ], MdInput.prototype, "_placeholderChild", void 0);
317 __decorate([
318 ContentChildren(MdHint),
319 __metadata('design:type', QueryList)
320 ], MdInput.prototype, "_hintChildren", void 0);
321 __decorate([
322 Input(),
323 __metadata('design:type', Object)
324 ], MdInput.prototype, "align", void 0);
325 __decorate([
326 Input(),
327 __metadata('design:type', Object)
328 ], MdInput.prototype, "dividerColor", void 0);
329 __decorate([
330 Input(),
331 BooleanFieldValue(),
332 __metadata('design:type', Boolean)
333 ], MdInput.prototype, "floatingPlaceholder", void 0);
334 __decorate([
335 Input(),
336 __metadata('design:type', String)
337 ], MdInput.prototype, "hintLabel", void 0);
338 __decorate([
339 Input(),
340 __metadata('design:type', String)
341 ], MdInput.prototype, "autocomplete", void 0);
342 __decorate([
343 Input(),
344 __metadata('design:type', String)
345 ], MdInput.prototype, "autocorrect", void 0);
346 __decorate([
347 Input(),
348 __metadata('design:type', String)
349 ], MdInput.prototype, "autocapitalize", void 0);
350 __decorate([
351 Input(),
352 BooleanFieldValue(),
353 __metadata('design:type', Boolean)
354 ], MdInput.prototype, "autofocus", void 0);
355 __decorate([
356 Input(),
357 BooleanFieldValue(),
358 __metadata('design:type', Boolean)
359 ], MdInput.prototype, "disabled", void 0);
360 __decorate([
361 Input(),
362 __metadata('design:type', String)
363 ], MdInput.prototype, "id", void 0);
364 __decorate([
365 Input(),
366 __metadata('design:type', String)
367 ], MdInput.prototype, "list", void 0);
368 __decorate([
369 Input(),
370 __metadata('design:type', Object)
371 ], MdInput.prototype, "max", void 0);
372 __decorate([
373 Input(),
374 __metadata('design:type', Number)
375 ], MdInput.prototype, "maxlength", void 0);
376 __decorate([
377 Input(),
378 __metadata('design:type', Object)
379 ], MdInput.prototype, "min", void 0);
380 __decorate([
381 Input(),
382 __metadata('design:type', Number)
383 ], MdInput.prototype, "minlength", void 0);
384 __decorate([
385 Input(),
386 __metadata('design:type', String)
387 ], MdInput.prototype, "placeholder", void 0);
388 __decorate([
389 Input(),
390 BooleanFieldValue(),
391 __metadata('design:type', Boolean)
392 ], MdInput.prototype, "readonly", void 0);
393 __decorate([
394 Input(),
395 BooleanFieldValue(),
396 __metadata('design:type', Boolean)
397 ], MdInput.prototype, "required", void 0);
398 __decorate([
399 Input(),
400 BooleanFieldValue(),
401 __metadata('design:type', Boolean)
402 ], MdInput.prototype, "spellcheck", void 0);
403 __decorate([
404 Input(),
405 __metadata('design:type', Number)
406 ], MdInput.prototype, "step", void 0);
407 __decorate([
408 Input(),
409 __metadata('design:type', Number)
410 ], MdInput.prototype, "tabindex", void 0);
411 __decorate([
412 Input(),
413 __metadata('design:type', String)
414 ], MdInput.prototype, "type", void 0);
415 __decorate([
416 Input(),
417 __metadata('design:type', String)
418 ], MdInput.prototype, "name", void 0);
419 __decorate([
420 Output('blur'),
421 __metadata('design:type', Observable)
422 ], MdInput.prototype, "onBlur", null);
423 __decorate([
424 Output('focus'),
425 __metadata('design:type', Observable)
426 ], MdInput.prototype, "onFocus", null);
427 __decorate([
428 Input(),
429 __metadata('design:type', Object)
430 ], MdInput.prototype, "value", null);
431 __decorate([
432 HostBinding('attr.align'),
433 __metadata('design:type', Object)
434 ], MdInput.prototype, "_align", null);
435 __decorate([
436 ViewChild('input'),
437 __metadata('design:type', ElementRef)
438 ], MdInput.prototype, "_inputElement", void 0);
439 MdInput = __decorate([
440 Component({selector: 'md-input',
441 template: "<div class=\"md-input-wrapper\"> <div class=\"md-input-table\"> <div class=\"md-input-prefix\"><ng-content select=\"[md-prefix]\"></ng-content></div> <div class=\"md-input-infix\"> <input #input aria-target class=\"md-input-element\" [class.md-end]=\"align == 'end'\" [attr.aria-label]=\"ariaLabel\" [attr.aria-labelledby]=\"ariaLabelledBy\" [attr.aria-disabled]=\"ariaDisabled\" [attr.aria-required]=\"ariaRequired\" [attr.aria-invalid]=\"ariaInvalid\" [attr.autocomplete]=\"autocomplete\" [attr.autocorrect]=\"autocorrect\" [attr.autocapitalize]=\"autocapitalize\" [autofocus]=\"autofocus\" [disabled]=\"disabled\" [id]=\"inputId\" [attr.list]=\"list\" [attr.max]=\"max\" [attr.maxlength]=\"maxlength\" [attr.min]=\"min\" [attr.minlength]=\"minlength\" [readonly]=\"readonly\" [required]=\"required\" [spellcheck]=\"spellcheck\" [attr.step]=\"step\" [attr.tabindex]=\"tabindex\" [type]=\"type\" [attr.name]=\"name\" (focus)=\"_handleFocus($event)\" (blur)=\"_handleBlur($event)\" [(ngModel)]=\"value\" (change)=\"_handleChange($event)\"> <label class=\"md-input-placeholder\" [attr.for]=\"inputId\" [class.md-empty]=\"empty\" [class.md-focused]=\"focused\" [class.md-float]=\"floatingPlaceholder\" [class.md-accent]=\"dividerColor == 'accent'\" [class.md-warn]=\"dividerColor == 'warn'\" *ngIf=\"_hasPlaceholder()\"> <ng-content select=\"md-placeholder\"></ng-content> {{placeholder}} <span class=\"md-placeholder-required\" *ngIf=\"required\">*</span> </label> </div> <div class=\"md-input-suffix\"><ng-content select=\"[md-suffix]\"></ng-content></div> </div> <div class=\"md-input-underline\" [class.md-disabled]=\"disabled\"> <span class=\"md-input-ripple\" [class.md-focused]=\"focused\" [class.md-accent]=\"dividerColor == 'accent'\" [class.md-warn]=\"dividerColor == 'warn'\"></span> </div> <div *ngIf=\"hintLabel != ''\" class=\"md-hint\">{{hintLabel}}</div> <ng-content select=\"md-hint\"></ng-content> </div> ",
442 styles: ["/** * Mixin that creates a new stacking context. * see https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context */ /** * This mixin hides an element visually. * That means it's still accessible for screen-readers but not visible in view. */ /** * Forces an element to grow to fit floated contents; used as as an alternative to * `overflow: hidden;` because it doesn't cut off contents. */ /** * A mixin, which generates temporary ink ripple on a given component. * When $bindToParent is set to true, it will check for the focused class on the same selector as you included * that mixin. * It is also possible to specify the color palette of the temporary ripple. By default it uses the * accent palette for its background. */ /** * Undo the red box-shadow glow added by Firefox on invalid inputs. * See https://developer.mozilla.org/en-US/docs/Web/CSS/:-moz-ui-invalid */ :-moz-ui-invalid { box-shadow: none; } /** * Applies a floating placeholder above the input itself. */ :host { display: inline-block; position: relative; font-family: Roboto, \"Helvetica Neue\", sans-serif; text-align: left; } :host .md-input-wrapper { margin: 16px 0; } :host .md-input-table { display: inline-table; flex-flow: column; vertical-align: bottom; width: 100%; } :host .md-input-table > * { display: table-cell; } :host .md-input-element { font: inherit; background: transparent; border: none; outline: none; padding: 0; width: 100%; } :host .md-input-element.md-end { text-align: right; } :host .md-input-infix { position: relative; } :host .md-input-placeholder { position: absolute; left: 0; top: 0; font-size: 100%; pointer-events: none; color: rgba(0, 0, 0, 0.38); z-index: 1; width: 100%; display: none; white-space: nowrap; text-overflow: ellipsis; overflow-x: hidden; transform: translateY(0); transform-origin: bottom left; transition: transform 400ms cubic-bezier(0.25, 0.8, 0.25, 1), scale 400ms cubic-bezier(0.25, 0.8, 0.25, 1), color 400ms cubic-bezier(0.25, 0.8, 0.25, 1), width 400ms cubic-bezier(0.25, 0.8, 0.25, 1); } :host .md-input-placeholder.md-empty { display: block; cursor: text; } :host .md-input-placeholder.md-float:not(.md-empty), :host .md-input-placeholder.md-float.md-focused { display: block; padding-bottom: 5px; transform: translateY(-100%) scale(0.75); width: 133.33333%; } :host .md-input-placeholder.md-float:not(.md-empty) .md-placeholder-required, :host .md-input-placeholder.md-float.md-focused .md-placeholder-required { color: #9c27b0; } :host .md-input-placeholder.md-focused { color: #009688; } :host .md-input-placeholder.md-focused.md-accent { color: #9c27b0; } :host .md-input-placeholder.md-focused.md-warn { color: #f44336; } :host input:-webkit-autofill + .md-input-placeholder { display: block; padding-bottom: 5px; transform: translateY(-100%) scale(0.75); width: 133.33333%; } :host input:-webkit-autofill + .md-input-placeholder .md-placeholder-required { color: #9c27b0; } :host .md-input-underline { position: absolute; height: 1px; width: 100%; margin-top: 4px; border-top: 1px solid rgba(0, 0, 0, 0.38); } :host .md-input-underline.md-disabled { border-top: 0; background-image: linear-gradient(to right, rgba(0, 0, 0, 0.26) 0%, rgba(0, 0, 0, 0.26) 33%, transparent 0%); background-position: 0; background-size: 4px 1px; background-repeat: repeat-x; } :host .md-input-underline .md-input-ripple { position: absolute; height: 2px; z-index: 1; background-color: #009688; top: -1px; width: 100%; transform-origin: top; opacity: 0; transform: scaleY(0); transition: transform 400ms cubic-bezier(0.25, 0.8, 0.25, 1), opacity 400ms cubic-bezier(0.25, 0.8, 0.25, 1); } :host .md-input-underline .md-input-ripple.md-accent { background-color: #9c27b0; } :host .md-input-underline .md-input-ripple.md-warn { background-color: #f44336; } :host .md-input-underline .md-input-ripple.md-focused { opacity: 1; transform: scaleY(1); } :host .md-hint { position: absolute; font-size: 75%; bottom: -0.5em; } :host .md-hint.md-right { right: 0; } :host-context([dir='rtl']) { text-align: right; } :host-context([dir='rtl']) .md-input-placeholder { transform-origin: bottom right; } :host-context([dir='rtl']) .md-input-element.md-end { text-align: left; } :host-context([dir='rtl']) .md-hint { right: 0; left: auto; } :host-context([dir='rtl']) .md-hint.md-right { right: auto; left: 0; } /*# sourceMappingURL=input.css.map */ "],
443 providers: [MD_INPUT_CONTROL_VALUE_ACCESSOR],
444 host: { '(click)': 'focus()' }
445 }),
446 __metadata('design:paramtypes', [])
447 ], MdInput);
448 return MdInput;
449}());
450export var MdInputModule = (function () {
451 function MdInputModule() {
452 }
453 MdInputModule.forRoot = function () {
454 return {
455 ngModule: MdInputModule,
456 providers: []
457 };
458 };
459 MdInputModule = __decorate([
460 NgModule({
461 declarations: [MdPlaceholder, MdInput, MdHint],
462 imports: [CommonModule, FormsModule],
463 exports: [MdPlaceholder, MdInput, MdHint],
464 }),
465 __metadata('design:paramtypes', [])
466 ], MdInputModule);
467 return MdInputModule;
468}());
469
470//# sourceMappingURL=input.js.map