vendor/assets/javascripts/material.js in material_design_lite-rails-1.1.1 vs vendor/assets/javascripts/material.js in material_design_lite-rails-1.1.2

- old
+ new

@@ -343,21 +343,23 @@ * Remove component from createdComponents list. * * @param {?componentHandler.Component} component */ function deconstructComponentInternal(component) { - var componentIndex = createdComponents_.indexOf(component); - createdComponents_.splice(componentIndex, 1); + if (component) { + var componentIndex = createdComponents_.indexOf(component); + createdComponents_.splice(componentIndex, 1); - var upgrades = component.element_.getAttribute('data-upgraded').split(','); - var componentPlace = upgrades.indexOf(component[componentConfigProperty_].classAsString); - upgrades.splice(componentPlace, 1); - component.element_.setAttribute('data-upgraded', upgrades.join(',')); + var upgrades = component.element_.getAttribute('data-upgraded').split(','); + var componentPlace = upgrades.indexOf(component[componentConfigProperty_].classAsString); + upgrades.splice(componentPlace, 1); + component.element_.setAttribute('data-upgraded', upgrades.join(',')); - var ev = document.createEvent('Events'); - ev.initEvent('mdl-componentdowngraded', true, true); - component.element_.dispatchEvent(ev); + var ev = document.createEvent('Events'); + ev.initEvent('mdl-componentdowngraded', true, true); + component.element_.dispatchEvent(ev); + } } /** * Downgrade either a given node, an array of nodes, or a NodeList. * @@ -2772,11 +2774,12 @@ INPUT: 'mdl-textfield__input', IS_DIRTY: 'is-dirty', IS_FOCUSED: 'is-focused', IS_DISABLED: 'is-disabled', IS_INVALID: 'is-invalid', - IS_UPGRADED: 'is-upgraded' + IS_UPGRADED: 'is-upgraded', + HAS_PLACEHOLDER: 'has-placeholder' }; /** * Handle input being entered. * * @param {Event} event The event that fired. @@ -2926,10 +2929,13 @@ this.maxRows = parseInt(this.input_.getAttribute(this.Constant_.MAX_ROWS_ATTRIBUTE), 10); if (isNaN(this.maxRows)) { this.maxRows = this.Constant_.NO_MAX_ROWS; } } + if (this.input_.hasAttribute('placeholder')) { + this.element_.classList.add(this.CssClasses_.HAS_PLACEHOLDER); + } this.boundUpdateClassesHandler = this.updateClasses_.bind(this); this.boundFocusHandler = this.onFocus_.bind(this); this.boundBlurHandler = this.onBlur_.bind(this); this.boundResetHandler = this.onReset_.bind(this); this.input_.addEventListener('input', this.boundUpdateClassesHandler); @@ -3131,10 +3137,11 @@ * @private */ MaterialLayout.prototype.Constant_ = { MAX_WIDTH: '(max-width: 1024px)', TAB_SCROLL_PIXELS: 100, + RESIZE_TIMEOUT: 100, MENU_ICON: '&#xE5D2;', CHEVRON_LEFT: 'chevron_left', CHEVRON_RIGHT: 'chevron_right' }; /** @@ -3233,11 +3240,12 @@ * * @param {Event} evt The event that fired. * @private */ MaterialLayout.prototype.keyboardEventHandler_ = function (evt) { - if (evt.keyCode === this.Keycodes_.ESCAPE) { + // Only react when the drawer is open. + if (evt.keyCode === this.Keycodes_.ESCAPE && this.drawer_.classList.contains(this.CssClasses_.IS_DRAWER_OPEN)) { this.toggleDrawer(); } }; /** * Handles changes in screen size. @@ -3337,13 +3345,17 @@ */ MaterialLayout.prototype.init = function () { if (this.element_) { var container = document.createElement('div'); container.classList.add(this.CssClasses_.CONTAINER); + var focusedElement = this.element_.querySelector(':focus'); this.element_.parentElement.insertBefore(container, this.element_); this.element_.parentElement.removeChild(this.element_); container.appendChild(this.element_); + if (focusedElement) { + focusedElement.focus(); + } var directChildren = this.element_.childNodes; var numChildren = directChildren.length; for (var c = 0; c < numChildren; c++) { var child = directChildren[c]; if (child.classList && child.classList.contains(this.CssClasses_.HEADER)) { @@ -3474,12 +3486,13 @@ this.tabBar_.scrollLeft += this.Constant_.TAB_SCROLL_PIXELS; }.bind(this)); tabContainer.appendChild(leftButton); tabContainer.appendChild(this.tabBar_); tabContainer.appendChild(rightButton); - // Add and remove buttons depending on scroll position. - var tabScrollHandler = function () { + // Add and remove tab buttons depending on scroll position and total + // window size. + var tabUpdateHandler = function () { if (this.tabBar_.scrollLeft > 0) { leftButton.classList.add(this.CssClasses_.IS_ACTIVE); } else { leftButton.classList.remove(this.CssClasses_.IS_ACTIVE); } @@ -3487,11 +3500,23 @@ rightButton.classList.add(this.CssClasses_.IS_ACTIVE); } else { rightButton.classList.remove(this.CssClasses_.IS_ACTIVE); } }.bind(this); - this.tabBar_.addEventListener('scroll', tabScrollHandler); - tabScrollHandler(); + this.tabBar_.addEventListener('scroll', tabUpdateHandler); + tabUpdateHandler(); + // Update tabs when the window resizes. + var windowResizeHandler = function () { + // Use timeouts to make sure it doesn't happen too often. + if (this.resizeTimeoutId_) { + clearTimeout(this.resizeTimeoutId_); + } + this.resizeTimeoutId_ = setTimeout(function () { + tabUpdateHandler(); + this.resizeTimeoutId_ = null; + }.bind(this), this.Constant_.RESIZE_TIMEOUT); + }.bind(this); + window.addEventListener('resize', windowResizeHandler); if (this.tabBar_.classList.contains(this.CssClasses_.JS_RIPPLE_EFFECT)) { this.tabBar_.classList.add(this.CssClasses_.RIPPLE_IGNORE_EVENTS); } // Select element tabs, document panels var tabs = this.tabBar_.querySelectorAll('.' + this.CssClasses_.TAB);