-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.js
121 lines (107 loc) · 2.78 KB
/
menu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
(function(window) {
'use strict';
/**
* Extend Object helper function.
*/
function extend(a, b) {
for(var key in b) {
if(b.hasOwnProperty(key)) {
a[key] = b[key];
}
}
return a;
}
/**
* Each helper function.
*/
function each(collection, callback) {
for (var i = 0; i < collection.length; i++) {
var item = collection[i];
callback(item);
}
}
/**
* Menu Constructor.
*/
function Menu(options) {
this.options = extend({}, this.options);
extend(this.options, options);
this._init();
}
/**
* Menu Options.
*/
Menu.prototype.options = {
wrapper: '#o-wrapper', // The content wrapper
type: 'slide-left', // The menu type
menuOpenerClass: '.c-button', // The menu opener class names (i.e. the buttons)
maskId: '#c-mask' // The ID of the mask
};
/**
* Initialise Menu.
*/
Menu.prototype._init = function() {
this.body = document.body;
this.wrapper = document.querySelector(this.options.wrapper);
this.mask = document.querySelector(this.options.maskId);
this.menu = document.querySelector('#c-menu--' + this.options.type);
this.closeBtn = this.menu.querySelector('.c-menu__close');
this.menuOpeners = document.querySelectorAll(this.options.menuOpenerClass);
this._initEvents();
};
/**
* Initialise Menu Events.
*/
Menu.prototype._initEvents = function() {
// Event for clicks on the close button inside the menu.
this.closeBtn.addEventListener('click', function(e) {
e.preventDefault();
this.close();
}.bind(this));
// Event for clicks on the mask.
this.mask.addEventListener('click', function(e) {
e.preventDefault();
this.close();
}.bind(this));
};
/**
* Open Menu.
*/
Menu.prototype.open = function() {
this.body.classList.add('has-active-menu');
this.wrapper.classList.add('has-' + this.options.type);
this.menu.classList.add('is-active');
this.mask.classList.add('is-active');
this.disableMenuOpeners();
};
/**
* Close Menu.
*/
Menu.prototype.close = function() {
this.body.classList.remove('has-active-menu');
this.wrapper.classList.remove('has-' + this.options.type);
this.menu.classList.remove('is-active');
this.mask.classList.remove('is-active');
this.enableMenuOpeners();
};
/**
* Disable Menu Openers.
*/
Menu.prototype.disableMenuOpeners = function() {
each(this.menuOpeners, function(item) {
item.disabled = true;
});
};
/**
* Enable Menu Openers.
*/
Menu.prototype.enableMenuOpeners = function() {
each(this.menuOpeners, function(item) {
item.disabled = false;
});
};
/**
* Add to global namespace.
*/
window.Menu = Menu;
})(window);