frontend.js
7.0 KB
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
(function($) {
/**
* Class for Menu Module
*
* @since 1.6.1
*/
FLBuilderMenu = function( settings ){
// set params
this.nodeClass = '.fl-node-' + settings.id;
this.wrapperClass = this.nodeClass + ' .fl-menu';
this.type = settings.type;
this.mobileToggle = settings.mobile;
this.breakPoints = settings.breakPoints;
this.currentBrowserWidth = $( window ).width();
// initialize the menu
this._initMenu();
// check if viewport is resizing
$( window ).on( 'resize', $.proxy( function( e ){
var width = $( window ).width();
// if screen width is resized, reload the menu
if( width != this.currentBrowserWidth ){
this._resizeDebounce();
this.currentBrowserWidth = width;
}
}, this ) );
};
FLBuilderMenu.prototype = {
nodeClass : '',
wrapperClass : '',
type : '',
breakPoints : {},
$submenus : null,
/**
* Check if the screen size fits a mobile viewport.
*
* @since 1.6.1
* @return bool
*/
_isMobile: function(){
return window.innerWidth < this.breakPoints.small ? true : false;
},
/**
* When screen is resized, reloads the menu in a determinded interval.
*
* @see this._initMenu()
* @see this._clickOrHover()
* @since 1.6.1
* @return void
*/
_resizeDebounce: function(){
clearTimeout( this.resizeTimer );
this.resizeTimer = setTimeout( $.proxy( function() {
this._initMenu();
this._clickOrHover();
}, this ), 250 );
},
/**
* Initialize the toggle logic for the menu.
*
* @see this._isMobile()
* @see this._menuOnCLick()
* @see this._clickOrHover()
* @see this._submenuOnRight()
* @see this._toggleForMobile()
* @since 1.6.1
* @return void
*/
_initMenu: function(){
if( this._isMobile() || this.type == 'accordion' ){
$( this.wrapperClass ).off( 'mouseenter mouseleave' );
this._menuOnClick();
this._clickOrHover();
} else {
$( this.wrapperClass ).off( 'click' );
this._submenuOnRight();
}
if( this.mobileToggle != 'expanded' ){
this._toggleForMobile();
}
},
/**
* Logic for submenu toggling on accordions or mobile menus (vertical, horizontal)
*
* @since 1.6.1
* @return void
*/
_menuOnClick: function(){
$( this.wrapperClass ).off().on( 'click', '.fl-has-submenu-container', $.proxy( function( e ){
var $link = $( e.target ).parents( '.fl-has-submenu' ).first(),
$subMenu = $link.children( '.sub-menu' ).first(),
$href = $link.children('.fl-has-submenu-container').first().find('> a').attr('href');
if( !$subMenu.is(':visible') || $(e.target).hasClass('fl-menu-toggle') ){
e.preventDefault();
}
else {
window.location.href = $href;
return;
}
if ($(this.wrapperClass).hasClass('fl-menu-accordion-collapse')) {
if ( !$link.parents('.menu-item').hasClass('fl-active') ) {
$('.fl-active', this.wrapperClass).not($link).removeClass('fl-active');
$('.sub-menu', this.wrapperClass).not($subMenu).slideUp('normal');
}
else if ($link.parents('.menu-item').hasClass('fl-active') && $link.parent('.sub-menu').length) {
var $mainMenuParent = $( e.target ).parents( '.fl-has-submenu.fl-active' ).last(),
$activeSubMenu = $mainMenuParent.children( '.sub-menu' ).last();
$('.fl-active', this.wrapperClass).not($link).not($mainMenuParent).removeClass('fl-active');
$('.sub-menu', this.wrapperClass).not($subMenu).not($activeSubMenu).slideUp('normal');
}
}
$subMenu.slideToggle();
$link.toggleClass( 'fl-active' );
}, this ) );
},
/**
* Changes general styling and behavior of menus based on mobile / desktop viewport.
*
* @see this._isMobile()
* @since 1.6.1
* @return void
*/
_clickOrHover: function(){
this.$submenus = this.$submenus || $( this.wrapperClass ).find( '.sub-menu' );
var $wrapper = $( this.wrapperClass ),
$menu = $wrapper.find( '.menu' );
$li = $wrapper.find( '.fl-has-submenu' );
if( this._isMobile() ){
$li.each( function( el ){
if( !$(this).hasClass('fl-active') ){
$(this).find( '.sub-menu' ).fadeOut();
}
} );
}
},
/**
* Logic to prevent submenus to go outside viewport boundaries.
*
* @since 1.6.1
* @return void
*/
_submenuOnRight: function(){
$( this.wrapperClass )
.on( 'mouseenter', '.fl-has-submenu', $.proxy( function( e ){
if( $ ( e.currentTarget ).find('.sub-menu').length === 0 ) {
return;
}
var $link = $( e.currentTarget ),
$parent = $link.parent(),
$subMenu = $link.find( '.sub-menu' ),
subMenuWidth = $subMenu.width(),
subMenuPos = 0,
winWidth = $( window ).width();
if( $link.closest( '.fl-menu-submenu-right' ).length !== 0) {
$link.addClass( 'fl-menu-submenu-right' );
} else if( $( 'body' ).hasClass( 'rtl' ) ) {
subMenuPos = $parent.is( '.sub-menu' ) ?
$parent.offset().left - subMenuWidth:
$link.offset().left - subMenuWidth;
if( subMenuPos <= 0 ) {
$link.addClass( 'fl-menu-submenu-right' );
}
} else {
subMenuPos = $parent.is( '.sub-menu' ) ?
$parent.offset().left + $parent.width() + subMenuWidth :
$link.offset().left + subMenuWidth;
if( subMenuPos > winWidth ) {
$link.addClass('fl-menu-submenu-right');
}
}
}, this ) )
.on( 'mouseleave', '.fl-has-submenu', $.proxy( function( e ){
$( e.currentTarget ).removeClass( 'fl-menu-submenu-right' );
}, this ) );
},
/**
* Logic for the mobile menu button.
*
* @since 1.6.1
* @return void
*/
_toggleForMobile: function(){
var $wrapper = $( this.wrapperClass ),
$menu = $wrapper.children( '.menu' );
if( this._isMobile() ){
if( !$wrapper.find( '.fl-menu-mobile-toggle' ).hasClass( 'fl-active' ) ){
$menu.css({ display: 'none' });
}
$wrapper.on( 'click', '.fl-menu-mobile-toggle', function( e ){
$( this ).toggleClass( 'fl-active' );
$menu.slideToggle();
} );
$menu.on( 'click', '.menu-item > a[href*="#"]', function(e){
var $href = $(this).attr('href'),
$targetID = '';
if ( $href !== '#' ) {
$targetID = $href.split('#')[1];
if ( $('body').find('#'+ $targetID).length > 0 ) {
e.preventDefault();
$( this ).toggleClass( 'fl-active' );
$menu.slideToggle('fast', function(){
setTimeout(function(){
$('html, body').animate({
scrollTop: $('#'+ $targetID).offset().top
}, 1000, function() {
window.location.hash = $targetID;
});
}, 500);
});
}
}
});
} else {
$wrapper.find( '.fl-menu-mobile-toggle' ).removeClass( 'fl-active' );
$menu.css({ display: '' });
}
}
};
})(jQuery);