3 * Copyright 2015 Google Inc. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 * Class constructor for Textfield MDL component.
23 * Implements MDL component design pattern defined at:
24 * https://github.com/jasonmayes/mdl-component-design-pattern
27 * @param {HTMLElement} element The element that will be upgraded.
29 var MaterialTextfield = function MaterialTextfield(element) {
30 this.element_ = element;
31 this.maxRows = this.Constant_.NO_MAX_ROWS;
32 // Initialize instance.
35 window['MaterialTextfield'] = MaterialTextfield;
38 * Store constants in one place so they can be updated easily.
40 * @enum {string | number}
43 MaterialTextfield.prototype.Constant_ = {
45 MAX_ROWS_ATTRIBUTE: 'maxrows'
49 * Store strings for class names defined by this component that are used in
50 * JavaScript. This allows us to simply change it in one place should we
51 * decide to modify at a later date.
56 MaterialTextfield.prototype.CssClasses_ = {
57 LABEL: 'mdl-textfield__label',
58 INPUT: 'mdl-textfield__input',
60 IS_FOCUSED: 'is-focused',
61 IS_DISABLED: 'is-disabled',
62 IS_INVALID: 'is-invalid',
63 IS_UPGRADED: 'is-upgraded',
64 HAS_PLACEHOLDER: 'has-placeholder'
68 * Handle input being entered.
70 * @param {Event} event The event that fired.
73 MaterialTextfield.prototype.onKeyDown_ = function(event) {
74 var currentRowCount = event.target.value.split('\n').length;
75 if (event.keyCode === 13) {
76 if (currentRowCount >= this.maxRows) {
77 event.preventDefault();
85 * @param {Event} event The event that fired.
88 MaterialTextfield.prototype.onFocus_ = function(event) {
89 this.element_.classList.add(this.CssClasses_.IS_FOCUSED);
95 * @param {Event} event The event that fired.
98 MaterialTextfield.prototype.onBlur_ = function(event) {
99 this.element_.classList.remove(this.CssClasses_.IS_FOCUSED);
103 * Handle reset event from out side.
105 * @param {Event} event The event that fired.
108 MaterialTextfield.prototype.onReset_ = function(event) {
109 this.updateClasses_();
113 * Handle class updates.
117 MaterialTextfield.prototype.updateClasses_ = function() {
118 this.checkDisabled();
119 this.checkValidity();
127 * Check the disabled state and update field accordingly.
131 MaterialTextfield.prototype.checkDisabled = function() {
132 if (this.input_.disabled) {
133 this.element_.classList.add(this.CssClasses_.IS_DISABLED);
135 this.element_.classList.remove(this.CssClasses_.IS_DISABLED);
138 MaterialTextfield.prototype['checkDisabled'] =
139 MaterialTextfield.prototype.checkDisabled;
142 * Check the focus state and update field accordingly.
146 MaterialTextfield.prototype.checkFocus = function() {
147 if (Boolean(this.element_.querySelector(':focus'))) {
148 this.element_.classList.add(this.CssClasses_.IS_FOCUSED);
150 this.element_.classList.remove(this.CssClasses_.IS_FOCUSED);
153 MaterialTextfield.prototype['checkFocus'] =
154 MaterialTextfield.prototype.checkFocus;
157 * Check the validity state and update field accordingly.
161 MaterialTextfield.prototype.checkValidity = function() {
162 if (this.input_.validity) {
163 if (this.input_.validity.valid) {
164 this.element_.classList.remove(this.CssClasses_.IS_INVALID);
166 this.element_.classList.add(this.CssClasses_.IS_INVALID);
170 MaterialTextfield.prototype['checkValidity'] =
171 MaterialTextfield.prototype.checkValidity;
174 * Check the dirty state and update field accordingly.
178 MaterialTextfield.prototype.checkDirty = function() {
179 if (this.input_.value && this.input_.value.length > 0) {
180 this.element_.classList.add(this.CssClasses_.IS_DIRTY);
182 this.element_.classList.remove(this.CssClasses_.IS_DIRTY);
185 MaterialTextfield.prototype['checkDirty'] =
186 MaterialTextfield.prototype.checkDirty;
189 * Disable text field.
193 MaterialTextfield.prototype.disable = function() {
194 this.input_.disabled = true;
195 this.updateClasses_();
197 MaterialTextfield.prototype['disable'] = MaterialTextfield.prototype.disable;
204 MaterialTextfield.prototype.enable = function() {
205 this.input_.disabled = false;
206 this.updateClasses_();
208 MaterialTextfield.prototype['enable'] = MaterialTextfield.prototype.enable;
211 * Update text field value.
213 * @param {string} value The value to which to set the control (optional).
216 MaterialTextfield.prototype.change = function(value) {
218 this.input_.value = value || '';
219 this.updateClasses_();
221 MaterialTextfield.prototype['change'] = MaterialTextfield.prototype.change;
224 * Initialize element.
226 MaterialTextfield.prototype.init = function() {
229 this.label_ = this.element_.querySelector('.' + this.CssClasses_.LABEL);
230 this.input_ = this.element_.querySelector('.' + this.CssClasses_.INPUT);
233 if (this.input_.hasAttribute(
234 /** @type {string} */ (this.Constant_.MAX_ROWS_ATTRIBUTE))) {
235 this.maxRows = parseInt(this.input_.getAttribute(
236 /** @type {string} */ (this.Constant_.MAX_ROWS_ATTRIBUTE)), 10);
237 if (isNaN(this.maxRows)) {
238 this.maxRows = this.Constant_.NO_MAX_ROWS;
242 if (this.input_.hasAttribute('placeholder')) {
243 this.element_.classList.add(this.CssClasses_.HAS_PLACEHOLDER);
246 this.boundUpdateClassesHandler = this.updateClasses_.bind(this);
247 this.boundFocusHandler = this.onFocus_.bind(this);
248 this.boundBlurHandler = this.onBlur_.bind(this);
249 this.boundResetHandler = this.onReset_.bind(this);
250 this.input_.addEventListener('input', this.boundUpdateClassesHandler);
251 this.input_.addEventListener('focus', this.boundFocusHandler);
252 this.input_.addEventListener('blur', this.boundBlurHandler);
253 this.input_.addEventListener('reset', this.boundResetHandler);
255 if (this.maxRows !== this.Constant_.NO_MAX_ROWS) {
256 // TODO: This should handle pasting multi line text.
257 // Currently doesn't.
258 this.boundKeyDownHandler = this.onKeyDown_.bind(this);
259 this.input_.addEventListener('keydown', this.boundKeyDownHandler);
261 var invalid = this.element_.classList
262 .contains(this.CssClasses_.IS_INVALID);
263 this.updateClasses_();
264 this.element_.classList.add(this.CssClasses_.IS_UPGRADED);
266 this.element_.classList.add(this.CssClasses_.IS_INVALID);
268 if (this.input_.hasAttribute('autofocus')) {
269 this.element_.focus();
276 // The component registers itself. It can assume componentHandler is available
277 // in the global scope.
278 componentHandler.register({
279 constructor: MaterialTextfield,
280 classAsString: 'MaterialTextfield',
281 cssClass: 'mdl-js-textfield',