Wrap switch in IIFE
parent
257aa342f9
commit
05ada74ec8
|
@ -15,24 +15,28 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Class constructor for Checkbox MDL component.
|
||||
* Implements MDL component design pattern defined at:
|
||||
* https://github.com/jasonmayes/mdl-component-design-pattern
|
||||
*
|
||||
* @param {HTMLElement} element The element that will be upgraded.
|
||||
*/
|
||||
function MaterialSwitch(element) {
|
||||
'use strict';
|
||||
|
||||
var MaterialSwitch = function MaterialSwitch(element) {
|
||||
this.element_ = element;
|
||||
|
||||
// Initialize instance.
|
||||
this.init();
|
||||
}
|
||||
};
|
||||
window.MaterialSwitch = MaterialSwitch;
|
||||
|
||||
/**
|
||||
* Store constants in one place so they can be updated easily.
|
||||
* @enum {string | number}
|
||||
*
|
||||
* @enum {String | Number}
|
||||
* @private
|
||||
*/
|
||||
MaterialSwitch.prototype.Constant_ = {
|
||||
|
@ -43,7 +47,8 @@ MaterialSwitch.prototype.Constant_ = {
|
|||
* 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}
|
||||
*
|
||||
* @enum {String}
|
||||
* @private
|
||||
*/
|
||||
MaterialSwitch.prototype.CssClasses_ = {
|
||||
|
@ -63,65 +68,60 @@ MaterialSwitch.prototype.CssClasses_ = {
|
|||
|
||||
/**
|
||||
* Handle change of state.
|
||||
*
|
||||
* @param {Event} event The event that fired.
|
||||
* @private
|
||||
*/
|
||||
MaterialSwitch.prototype.onChange_ = function(event) {
|
||||
'use strict';
|
||||
|
||||
this.updateClasses_();
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle focus of element.
|
||||
*
|
||||
* @param {Event} event The event that fired.
|
||||
* @private
|
||||
*/
|
||||
MaterialSwitch.prototype.onFocus_ = function(event) {
|
||||
'use strict';
|
||||
|
||||
this.element_.classList.add(this.CssClasses_.IS_FOCUSED);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle lost focus of element.
|
||||
*
|
||||
* @param {Event} event The event that fired.
|
||||
* @private
|
||||
*/
|
||||
MaterialSwitch.prototype.onBlur_ = function(event) {
|
||||
'use strict';
|
||||
|
||||
this.element_.classList.remove(this.CssClasses_.IS_FOCUSED);
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle mouseup.
|
||||
*
|
||||
* @param {Event} event The event that fired.
|
||||
* @private
|
||||
*/
|
||||
MaterialSwitch.prototype.onMouseUp_ = function(event) {
|
||||
'use strict';
|
||||
|
||||
this.blur_();
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle class updates.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
MaterialSwitch.prototype.updateClasses_ = function() {
|
||||
'use strict';
|
||||
this.checkDisabled();
|
||||
this.checkToggleState();
|
||||
};
|
||||
|
||||
/**
|
||||
* Add blur.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
MaterialSwitch.prototype.blur_ = function(event) {
|
||||
'use strict';
|
||||
|
||||
// TODO: figure out why there's a focus event being fired after our blur,
|
||||
// so that we can avoid this hack.
|
||||
window.setTimeout(function() {
|
||||
|
@ -133,10 +133,10 @@ MaterialSwitch.prototype.blur_ = function(event) {
|
|||
|
||||
/**
|
||||
* Check the components disabled state.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
MaterialSwitch.prototype.checkDisabled = function() {
|
||||
'use strict';
|
||||
if (this.inputElement_.disabled) {
|
||||
this.element_.classList.add(this.CssClasses_.IS_DISABLED);
|
||||
} else {
|
||||
|
@ -146,10 +146,10 @@ MaterialSwitch.prototype.checkDisabled = function() {
|
|||
|
||||
/**
|
||||
* Check the components toggled state.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
MaterialSwitch.prototype.checkToggleState = function() {
|
||||
'use strict';
|
||||
if (this.inputElement_.checked) {
|
||||
this.element_.classList.add(this.CssClasses_.IS_CHECKED);
|
||||
} else {
|
||||
|
@ -159,44 +159,40 @@ MaterialSwitch.prototype.checkToggleState = function() {
|
|||
|
||||
/**
|
||||
* Disable switch.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
MaterialSwitch.prototype.disable = function() {
|
||||
'use strict';
|
||||
|
||||
this.inputElement_.disabled = true;
|
||||
this.updateClasses_();
|
||||
};
|
||||
|
||||
/**
|
||||
* Enable switch.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
MaterialSwitch.prototype.enable = function() {
|
||||
'use strict';
|
||||
|
||||
this.inputElement_.disabled = false;
|
||||
this.updateClasses_();
|
||||
};
|
||||
|
||||
/**
|
||||
* Activate switch.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
MaterialSwitch.prototype.on = function() {
|
||||
'use strict';
|
||||
|
||||
this.inputElement_.checked = true;
|
||||
this.updateClasses_();
|
||||
};
|
||||
|
||||
/**
|
||||
* Deactivate switch.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
MaterialSwitch.prototype.off = function() {
|
||||
'use strict';
|
||||
|
||||
this.inputElement_.checked = false;
|
||||
this.updateClasses_();
|
||||
};
|
||||
|
@ -205,8 +201,6 @@ MaterialSwitch.prototype.off = function() {
|
|||
* Initialize element.
|
||||
*/
|
||||
MaterialSwitch.prototype.init = function() {
|
||||
'use strict';
|
||||
|
||||
if (this.element_) {
|
||||
this.inputElement_ = this.element_.querySelector('.' +
|
||||
this.CssClasses_.INPUT);
|
||||
|
@ -259,11 +253,12 @@ MaterialSwitch.prototype.init = function() {
|
|||
}
|
||||
};
|
||||
|
||||
/*
|
||||
/**
|
||||
* Downgrade the component.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
MaterialSwitch.prototype.mdlDowngrade_ = function() {
|
||||
'use strict';
|
||||
if (this.rippleContainerElement_) {
|
||||
this.rippleContainerElement_.removeEventListener('mouseup', this.boundMouseUpHandler);
|
||||
}
|
||||
|
@ -281,3 +276,4 @@ componentHandler.register({
|
|||
cssClass: 'mdl-js-switch',
|
||||
widget: true
|
||||
});
|
||||
})();
|
||||
|
|
Loading…
Reference in New Issue