Wrap menu in IIFE

This commit is contained in:
Surma 2015-08-05 11:49:30 +01:00
parent d45ab8000c
commit 2ec60aa32b

View File

@ -15,24 +15,28 @@
* limitations under the License.
*/
(function() {
'use strict';
/**
* Class constructor for dropdown 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 MaterialMenu(element) {
'use strict';
var MaterialMenu = function MaterialMenu(element) {
this.element_ = element;
// Initialize instance.
this.init();
}
};
window.MaterialMenu = MaterialMenu;
/**
* Store constants in one place so they can be updated easily.
* @enum {string | number}
*
* @enum {String | Number}
* @private
*/
MaterialMenu.prototype.Constant_ = {
@ -47,7 +51,8 @@ MaterialMenu.prototype.Constant_ = {
/**
* Keycodes, for code readability.
* @enum {number}
*
* @enum {Number}
* @private
*/
MaterialMenu.prototype.Keycodes_ = {
@ -62,7 +67,8 @@ MaterialMenu.prototype.Keycodes_ = {
* 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
*/
MaterialMenu.prototype.CssClasses_ = {
@ -89,8 +95,6 @@ MaterialMenu.prototype.CssClasses_ = {
* Initialize element.
*/
MaterialMenu.prototype.init = function() {
'use strict';
if (this.element_) {
// Create container for the menu.
var container = document.createElement('div');
@ -174,11 +178,11 @@ MaterialMenu.prototype.init = function() {
/**
* Handles a click on the "for" element, by positioning the menu and then
* toggling it.
*
* @param {Event} evt The event that fired.
* @private
*/
MaterialMenu.prototype.handleForClick_ = function(evt) {
'use strict';
if (this.element_ && this.forElement_) {
var rect = this.forElement_.getBoundingClientRect();
var forRect = this.forElement_.parentElement.getBoundingClientRect();
@ -213,11 +217,11 @@ MaterialMenu.prototype.handleForClick_ = function(evt) {
/**
* Handles a keyboard event on the "for" element.
*
* @param {Event} evt The event that fired.
* @private
*/
MaterialMenu.prototype.handleForKeyboardEvent_ = function(evt) {
'use strict';
if (this.element_ && this.container_ && this.forElement_) {
var items = this.element_.querySelectorAll('.' + this.CssClasses_.ITEM +
':not([disabled])');
@ -237,11 +241,11 @@ MaterialMenu.prototype.handleForKeyboardEvent_ = function(evt) {
/**
* Handles a keyboard event on an item.
*
* @param {Event} evt The event that fired.
* @private
*/
MaterialMenu.prototype.handleItemKeyboardEvent_ = function(evt) {
'use strict';
if (this.element_ && this.container_) {
var items = this.element_.querySelectorAll('.' + this.CssClasses_.ITEM +
':not([disabled])');
@ -284,11 +288,11 @@ MaterialMenu.prototype.handleItemKeyboardEvent_ = function(evt) {
/**
* Handles a click event on an item.
*
* @param {Event} evt The event that fired.
* @private
*/
MaterialMenu.prototype.handleItemClick_ = function(evt) {
'use strict';
if (evt.target.getAttribute('disabled') !== null) {
evt.stopPropagation();
} else {
@ -305,11 +309,12 @@ MaterialMenu.prototype.handleItemClick_ = function(evt) {
* Calculates the initial clip (for opening the menu) or final clip (for closing
* it), and applies it. This allows us to animate from or to the correct point,
* that is, the point it's aligned to in the "for" element.
*
* @param {Number} height Height of the clip rectangle
* @param {Number} width Width of the clip rectangle
* @private
*/
MaterialMenu.prototype.applyClip_ = function(height, width) {
'use strict';
if (this.element_.classList.contains(this.CssClasses_.UNALIGNED)) {
// Do not clip.
this.element_.style.clip = null;
@ -333,11 +338,10 @@ MaterialMenu.prototype.applyClip_ = function(height, width) {
/**
* Adds an event listener to clean up after the animation ends.
*
* @private
*/
MaterialMenu.prototype.addAnimationEndListener_ = function() {
'use strict';
var cleanup = function() {
this.element_.removeEventListener('transitionend', cleanup);
this.element_.removeEventListener('webkitTransitionEnd', cleanup);
@ -351,11 +355,10 @@ MaterialMenu.prototype.addAnimationEndListener_ = function() {
/**
* Displays the menu.
*
* @public
*/
MaterialMenu.prototype.show = function(evt) {
'use strict';
if (this.element_ && this.container_ && this.outline_) {
// Measure the inner element.
var height = this.element_.getBoundingClientRect().height;
@ -416,11 +419,10 @@ MaterialMenu.prototype.show = function(evt) {
/**
* Hides the menu.
*
* @public
*/
MaterialMenu.prototype.hide = function() {
'use strict';
if (this.element_ && this.container_ && this.outline_) {
var items = this.element_.querySelectorAll('.' + this.CssClasses_.ITEM);
@ -446,11 +448,10 @@ MaterialMenu.prototype.hide = function() {
/**
* Displays or hides the menu, depending on current state.
*
* @public
*/
MaterialMenu.prototype.toggle = function(evt) {
'use strict';
if (this.container_.classList.contains(this.CssClasses_.IS_VISIBLE)) {
this.hide();
} else {
@ -458,11 +459,12 @@ MaterialMenu.prototype.toggle = function(evt) {
}
};
/*
/**
* Downgrade the component.
*
* @private
*/
MaterialMenu.prototype.mdlDowngrade_ = function() {
'use strict';
var items = this.element_.querySelectorAll('.' + this.CssClasses_.ITEM);
for (var i = 0; i < items.length; i++) {
@ -479,3 +481,4 @@ componentHandler.register({
cssClass: 'mdl-js-menu',
widget: true
});
})();