-
Notifications
You must be signed in to change notification settings - Fork 161
/
mobile-menu.js
320 lines (246 loc) · 9.72 KB
/
mobile-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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* ===== Zeste de Savoir ====================================================
Mobile sidebar menu, swipe open/close
---------------------------------
Author: Alex-D / Alexandre Demode
========================================================================== */
(function($) {
'use strict'
/**
* Add class for mobile navigator does not support fixed position
*/
var navU = navigator.userAgent
// Android Mobile
var isAndroidMobile = navU.indexOf('Android') > -1 && navU.indexOf('Mozilla/5.0') > -1 && navU.indexOf('AppleWebKit') > -1
// Android Browser (not Chrome)
var regExAppleWebKit = new RegExp(/AppleWebKit\/([\d.]+)/)
var resultAppleWebKitRegEx = regExAppleWebKit.exec(navU)
var appleWebKitVersion = (resultAppleWebKitRegEx === null ? null : parseFloat(regExAppleWebKit.exec(navU)[1]))
var disableMobileMenu = isAndroidMobile && appleWebKitVersion !== null && appleWebKitVersion < 537
if (disableMobileMenu) { $('html').removeClass('enable-mobile-menu') }
/**
* Get prefix to support CSS transform
*/
var transform = ''
var prefixedPropertyNames = ['transform', 'msTransform', 'MozTransform', 'WebkitTransform', 'OTransform']
var prefixes = ['', '-ms-', '-moz-', '-webkit-', '-o-']
var tempDiv = document.createElement('div')
for (var i = 0; i < prefixedPropertyNames.length; ++i) {
if (typeof tempDiv.style[prefixedPropertyNames[i]] !== 'undefined') {
transform = prefixes[i]
break
}
}
transform = transform + 'transform'
/**
* Manage mobile sidebar on resize
*/
$(window).on('resize', function() {
if (parseInt($('html').css('width')) < 960 && !disableMobileMenu) {
$('.page-container').css('width', $('html').css('width'))
if (!$('#mobile-menu').hasClass('initialized')) {
$('#mobile-menu').addClass('initialized')
/**
* Manage menu button
*/
$('.mobile-menu-btn').on('click', function(e) {
if (!$('html').hasClass('show-mobile-menu')) {
toggleMobileMenu(true)
e.preventDefault()
e.stopPropagation()
}
})
/**
* Build sidebar menu from page
*/
appendToSidebar($('#search'), true)
var $myaccount = appendToSidebar($('#my-account'), true)
$myaccount.addClass('mobile-menu-link mobile-menu-bloc')
appendToSidebar($('.header-menu'))
$('.page-container .mobile-menu-bloc .mobile-menu-bloc').each(function() {
appendToSidebar($(this))
})
$('.page-container .mobile-menu-bloc:not(.my-account-dropdown)').each(function() {
appendToSidebar($(this))
})
appendToSidebar($('.my-account-dropdown'))
}
/**
* Manage touch events for mobile sidebar
*/
if (!$('#mobile-menu').hasClass('initialized-events')) {
var beginTouchDown = 0
var borderWidth = 50
var swipping = false
$('body')
.on('touchstart', function(e) {
beginTouchDown = parseInt(e.originalEvent.touches[0].pageX, 10) - $('.page-container').offset().left
})
$('.page-container')
.on('touchmove', function(e) {
if (swipping || parseInt(e.originalEvent.touches[0].pageX, 10) - $(this).offset().left < borderWidth) {
e.preventDefault()
$('body:not(.swipping)').addClass('swipping')
swipping = true
var toMove = parseInt(e.originalEvent.touches[0].pageX, 10) - beginTouchDown
toMove = (toMove * 100) / parseInt($('html').width())
if (toMove > 0 && toMove < 90) {
var props = {}
props[transform] = 'translate3d(' + toMove + '%, 0, 0)'
$(this).css(props)
toMove = ((toMove * 20) / 90) - 20
props[transform] = 'translate3d(' + toMove + '%, 0, 0)'
$('#mobile-menu').css(props)
}
}
})
.on('touchend touchleave touchcancel', function() {
if (swipping) {
var offset = parseInt($(this).offset().left)
var width = parseInt($('html').width())
var visible = (offset > width / 3 && !$('html').hasClass('show-mobile-menu')) || (offset > width - width / 3 && $('html').hasClass('show-mobile-menu'))
toggleMobileMenu(visible)
swipping = false
$('body').removeClass('swipping')
var props = {}
props[transform] = ''
$('.page-container, #mobile-menu').css(props)
}
})
$('.page-container').on('click', function(e) {
if ($('html').hasClass('show-mobile-menu')) {
toggleMobileMenu(false)
e.preventDefault()
e.stopPropagation()
}
})
$('#mobile-menu').addClass('initialized-events')
}
} else {
$('html').removeClass('show-mobile-menu')
$('#mobile-menu').removeClass('initialized-events')
$('.page-container').removeAttr('style')
$('.page-container').off('click touchstart touchmove touchend')
}
})
$(window).trigger('resize')
function appendToSidebar($elem, onlyCloneThisElement) {
if ($elem.hasClass('mobile-menu-imported')) { return false }
if (onlyCloneThisElement) {
$elem.addClass('mobile-menu-imported')
$elem = $elem.clone().removeAttr('id').appendTo('#mobile-menu')
return $elem
}
var $div = $('<div/>')
$div.addClass('mobile-menu-bloc')
$div.attr('data-title', $elem.attr('data-title'))
if ($elem.hasClass('mobile-show-ico')) { $div.addClass('mobile-show-ico') }
var addAllLinks = $elem.hasClass('mobile-all-links')
var $links = $elem.find((addAllLinks) ? 'a, button, span.disabled' : '.mobile-menu-link')
if (addAllLinks) { $links = $links.not('.action-hover') }
$links.each(function() {
if ($(this).parents('.mobile-menu-imported, .modal').length === 0) {
var $elem = $(this).clone().addClass('light')
var formId
if ($(this).is('button')) {
var $form = $(this).parents('form:first')
if (!$form.attr('id')) {
formId = 'form' + $('.identified-form').length
$form.attr('id', formId).addClass('identified-form')
} else {
formId = $form.attr('id')
}
$elem.attr('form', formId)
}
$div.append($elem)
}
})
if (addAllLinks) { $div.find('a, button, span.disabled').addClass('mobile-menu-link') }
$elem.addClass('mobile-menu-imported')
$div.appendTo($('#mobile-menu'))
return $div
}
/**
* Toggle sidebar for mobiles
*/
function toggleMobileMenu(visible) {
var scrollTop
if (visible === null) { visible = !$('html').hasClass('show-mobile-menu') }
$('body').removeClass('swipping')
var viewportmeta = document.querySelector('meta[name="viewport"]')
if (visible) {
if (!$('html').hasClass('show-mobile-menu')) {
scrollTop = $(document).scrollTop()
$('.page-container').css({
'margin-top': '-' + scrollTop + 'px',
'padding-bottom': scrollTop + 'px'
})
$('html').addClass('show-mobile-menu')
viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0'
}
// Manage back button to close sidebar
/*
if(history && history.pushState){
history.pushState(null, document.title, this.href);
$(window).off("popstate").one("popstate", function(e){
if($("html").hasClass("show-mobile-menu"))
toggleMobileMenu(false);
else
window.history.back();
});
}
*/
} else {
$('html').removeClass('show-mobile-menu')
// Reset CSS modifications for restore scroll
scrollTop = parseInt($('.page-container').css('padding-bottom'))
$('.page-container').css({
'margin-top': '',
'padding-bottom': ''
})
if (scrollTop > 0) { $(document).scrollTop(scrollTop) }
// Stop swipping
$('body').removeClass('swipping')
setTimeout(function() {
// Reinit mobile menu at top
$('#mobile-menu').scrollTop(0)
// Restore zoom
viewportmeta.content = 'width=device-width, minimum-scale=1.0, initial-scale=1.0'
// Bugfix <html> element
$('html').css({
position: 'absolute',
left: '0'
})
setTimeout(function() {
$('html').removeAttr('style')
}, 500)
}, 200)
}
}
/**
* Manage actions buttons, move them at the top af core of page
*/
$(window).on('resize', function() {
if (parseInt($('html').css('width')) < 960 && !disableMobileMenu) {
var $newBtns = $('.sidebar .new-btn:not(.mobile-btn-imported)')
if ($newBtns.length > 0) {
var $prevElem = $('#content')
.find('> .content-wrapper, > .full-content-wrapper, > .content-col-2')
.first()
.find('h1, h2')
.first()
if ($prevElem.next('.license').length > 0) { $prevElem = $prevElem.next('.license') }
if ($prevElem.next('.subtitle').length > 0) { $prevElem = $prevElem.next('.subtitle') }
if ($prevElem.next('.taglist').length > 0) { $prevElem = $prevElem.next('.taglist') }
var $newBtnContainer = $('<div/>', {
class: 'new-btn-container'
})
$newBtns.each(function() {
$newBtnContainer.append($(this).clone().removeAttr('id').removeClass('blue'))
$(this).addClass('mobile-btn-imported')
})
$prevElem.after($newBtnContainer)
}
}
})
$(window).trigger('resize')
})(jQuery)