/** * Class constructor for Button WSK component. * Implements WSK component design pattern defined at: * https://github.com/jasonmayes/wsk-component-design-pattern * @param {HTMLElement} element The element that will be upgraded. */ function MaterialButton(element) { 'use strict'; this.element_ = element; // Initialize instance. this.init(); } /** * Store constants in one place so they can be updated easily. * @enum {string | number} * @private */ MaterialButton.prototype.Constant_ = { // None for now. }; /** * Store strings for class names defined by this component that are used in * JavaScript. This allows us to simply change it in one place should we * decide to modify at a later date. * @enum {string} * @private */ MaterialButton.prototype.CssClasses_ = { RIPPLE_EFFECT: 'wsk-js-ripple-effect', RIPPLE_CONTAINER: 'wsk-button__ripple-container', RIPPLE: 'wsk-ripple' }; /** * Handle blur of element. * @param {HTMLElement} element The instance of a button we want to blur. * @private */ MaterialButton.prototype.blurHandler = function(event) { 'use strict'; if (event) { this.element_.blur(); } }; // Public methods. /** * Disable button. * @public */ MaterialButton.prototype.disable = function() { 'use strict'; this.element_.disabled = true; }; /** * Enable button. * @public */ MaterialButton.prototype.enable = function() { 'use strict'; this.element_.disabled = false; }; /** * Initialize element. */ MaterialButton.prototype.init = function() { 'use strict'; if (this.element_) { if (this.element_.classList.contains(this.CssClasses_.RIPPLE_EFFECT)) { var rippleContainer = document.createElement('span'); rippleContainer.classList.add(this.CssClasses_.RIPPLE_CONTAINER); var ripple = document.createElement('span'); ripple.classList.add(this.CssClasses_.RIPPLE); rippleContainer.appendChild(ripple); ripple.addEventListener('mouseup', this.blurHandler.bind(this)); this.element_.appendChild(rippleContainer); } this.element_.addEventListener('mouseup', this.blurHandler.bind(this)); this.element_.addEventListener('mouseleave', this.blurHandler.bind(this)); } }; // The component registers itself. It can assume componentHandler is available // in the global scope. componentHandler.register({ constructor: MaterialButton, classAsString: 'MaterialButton', cssClass: 'wsk-js-button' });