-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbigSlide.js
289 lines (244 loc) · 9.14 KB
/
bigSlide.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*! bigSlide - v0.10.0 - 2016-03-30
* http://ascott1.github.io/bigSlide.js/
* Copyright (c) 2016 Adam D. Scott; Licensed MIT */
(function (factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function($) {
'use strict';
// where inlineCSS is the string value of an element's style attribute
// and toRemove is a string of space-separated CSS properties,
// _cleanInlineCSS removes the CSS declaration for each property in toRemove from inlineCSS
// and returns the resulting string
function _cleanInlineCSS(inlineCSS, toRemove){
var inlineCSSArray = inlineCSS.split(';');
var toRemoveArray = toRemove.split(' ');
var cleaned = '';
var keep;
for (var i = 0, j = inlineCSSArray.length; i < j; i++) {
keep = true;
for (var a = 0, b = toRemoveArray.length; a < b; a++) {
if (inlineCSSArray[i] === '' || inlineCSSArray[i].indexOf(toRemoveArray[a]) !== -1) {
keep = false;
}
}
if(keep) {cleaned += inlineCSSArray[i] + '; ';}
}
return cleaned;
}
$.fn.bigSlide = function(options) {
// store the menuLink in a way that is globally accessible
var menuLink = this;
// plugin settings
var settings = $.extend({
'menu': ('#menu'),
'push': ('.push'),
'shrink': ('.shrink'),
'side': 'left',
'menuWidth': '15.625em',
'speed': '300',
'state': 'closed',
'activeBtn': 'active',
'easyClose': false,
'saveState': false,
'beforeOpen': function () {},
'afterOpen': function() {},
'beforeClose': function() {},
'afterClose': function() {}
}, options);
// CSS properties set by bigSlide.js on all implicated DOM elements
var baseCSSDictionary = 'transition -o-transition -ms-transition -moz-transitions webkit-transition ' + settings.side;
var model = {
//CSS properties set by bigSlide.js on this.$menu
menuCSSDictionary: baseCSSDictionary + ' position top bottom height width',
//CSS properties set by bigSlide.js on this.$push
pushCSSDictionary: baseCSSDictionary,
// store the menu's state in the model
'state': settings.state
};
// talk back and forth between the view and state
var controller = {
init: function(){
view.init();
},
// remove bigSlide behavior from the menu
_destroy: function(){
view._destroy();
delete menuLink.bigSlideAPI;
// return a reference to the DOM selection bigSlide.js was called on
// so that the destroy method is chainable
return menuLink;
},
// update the menu's state
changeState: function(){
if (model.state === 'closed') {
model.state = 'open'
} else {
model.state = 'closed'
}
},
// set the menu's state
setState: function(state){
model.state = state;
},
// check the menu's state
getState: function(){
return model.state;
}
};
// the view contains all of the visual interactions
var view = {
init: function(){
// cache DOM values
this.$menu = $(settings.menu);
this.$push = $(settings.push);
this.$shrink = $(settings.shrink);
this.width = settings.menuWidth;
// CSS for how the menu will be positioned off screen
var positionOffScreen = {
'position': 'fixed',
'top': '0',
'bottom': '0',
'height': '100%'
};
// css for the sliding animation
var animateSlide = {
'-webkit-transition': settings.side + ' ' + settings.speed + 'ms ease',
'-moz-transition': settings.side + ' ' + settings.speed + 'ms ease',
'-ms-transition': settings.side + ' ' + settings.speed + 'ms ease',
'-o-transition': settings.side + ' ' + settings.speed + 'ms ease',
'transition': settings.side + ' ' + settings.speed + 'ms ease'
};
// css for the shrink animation
var animateShrink = {
'-webkit-transition': 'all ' + settings.speed + 'ms ease',
'-moz-transition': 'all ' + settings.speed + 'ms ease',
'-ms-transition': 'all ' + settings.speed + 'ms ease',
'-o-transition': 'all ' + settings.speed + 'ms ease',
'transition': 'all ' + settings.speed + 'ms ease'
};
// we want to add the css sliding animation when the page is loaded (on the first menu link click)
var animationApplied = false;
// manually add the settings values
positionOffScreen[settings.side] = '-' + settings.menuWidth;
positionOffScreen.width = settings.menuWidth;
// get the initial state based on the last saved state or on the state option
var initialState = 'closed';
if (settings.saveState) {
initialState = localStorage.getItem('bigSlide-savedState');
if (!initialState) initialState = settings.state;
} else {
initialState = settings.state;
}
// set the initial state on the controller
controller.setState(initialState);
// add the css values to position things offscreen or inscreen depending on the initial state value
this.$menu.css(positionOffScreen);
if (initialState === 'closed') {
this.$push.css(settings.side, '0');
} else if (initialState === 'open') {
this.$menu.css(settings.side, '0');
this.$push.css(settings.side, this.width);
this.$shrink.css('width', '100%').css('width', '-=' + this.$menu.width());
menuLink.addClass(settings.activeBtn);
}
var that = this;
// register a click listener for desktop & touchstart for mobile
menuLink.on('click.bigSlide touchstart.bigSlide', function(e) {
// add the animation css if not present
if (!animationApplied) {
that.$menu.css(animateSlide);
that.$push.css(animateSlide);
that.$shrink.css(animateShrink);
animationApplied = true;
}
e.preventDefault();
if (controller.getState() === 'open') {
view.toggleClose();
} else {
view.toggleOpen();
}
});
// this makes my eyes bleed, but adding it back in as it's a highly requested feature
if (settings.easyClose) {
$(document).on('click.bigSlide', function(e) {
if (!$(e.target).parents().andSelf().is(menuLink) && !$(e.target).closest(settings.menu).length && controller.getState() === 'open') {
view.toggleClose();
}
});
}
},
_destroy: function(){
//remove inline styles generated by bigSlide.js while preserving any other inline styles
this.$menu.each(function(){
var $this = $(this);
$this.attr( 'style', _cleanInlineCSS($this.attr('style'), model.menuCSSDictionary).trim() );
});
this.$push.each(function(){
var $this = $(this);
$this.attr( 'style', _cleanInlineCSS($this.attr('style'), model.pushCSSDictionary).trim() );
});
this.$shrink.each(function(){
var $this = $(this);
$this.attr( 'style', _cleanInlineCSS($this.attr('style'), model.pushCSSDictionary).trim() );
});
//remove active class and unbind bigSlide event handlers
menuLink
.removeClass(settings.activeBtn)
.off('click.bigSlide touchstart.bigSlide');
//release DOM references to avoid memory leaks
this.$menu = null;
this.$push = null;
this.$shrink = null;
//remove the local storage state
localStorage.removeItem('bigSlide-savedState');
},
// toggle the menu open
toggleOpen: function() {
settings.beforeOpen();
controller.changeState();
this.$menu.css(settings.side, '0');
this.$push.css(settings.side, this.width);
this.$shrink.css('width', '100%').css('width', '-=' + this.$menu.width());
menuLink.addClass(settings.activeBtn);
settings.afterOpen();
// save the state
if (settings.saveState) {
localStorage.setItem('bigSlide-savedState', 'open');
}
},
// toggle the menu closed
toggleClose: function() {
settings.beforeClose();
controller.changeState();
this.$menu.css(settings.side, '-' + this.width);
this.$push.css(settings.side, '0');
this.$shrink.css('width', '100%');
menuLink.removeClass(settings.activeBtn);
settings.afterClose();
// save the state
if (settings.saveState) {
localStorage.setItem('bigSlide-savedState', 'closed');
}
}
}
controller.init();
this.bigSlideAPI = {
settings: settings,
model: model,
controller: controller,
view: view,
destroy: controller._destroy
};
return this;
};
}));