-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrvnm.js
executable file
·349 lines (299 loc) · 13.7 KB
/
rvnm.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* rvnm - v1.5.0
* Responsive vertical navigation menu
* https://github.com/4xmen/rvnm#readme
*
* Made by
* Under GPL-3.0 License
*/
/**
* rvnm Responsive vertical navigation menu
*
* Copyright (C) 2017 4xmen team <[email protected]>
*
* LICENSE: This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option) any
* later version. This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program.
* If not, see <http://opensource.org/licenses/gpl-license.php>.
*
* @package rvnm
* @author 4xmen team <www.4xmen.ir>
* @author A1Gard <[email protected]>
* @link https://github.com/4xmen/rvnm
*/
;
(function ($) {
$.fn.rvnm = function (options) {
/**
* settings ofplgin
* @type Object
*/
var settings = $.extend({
wrapper: '#wrapper', // main page wrapper
mode: 'default', // mode of menu (default = desktop| minimal = tablet | mobile)
responsive: true, // repsonsve mode only work in default mode
theme: '',
searchable: false,
sticky: 1.85,
}, options);
/**
* sizetrigger is function to change nav box size
* or control reponsive & mode of menu
* @returns {undefined}
*/
this.sizetrigger = function () {
// repsonvive mode controller
if (settings.responsive && settings.mode === 'default') {
// if window size between 450 and 768 active minimal
if ($(window).width() > 450 && $(window).width() < 768) {
if (!$(self).hasClass('rvnm-minimal')) {
// fix extended after switch from defualt to minimal
$(self).find('.rvnm-collapseable ul').attr('style', '');
$(self).find('.rvnm-collapseable').addClass('rvnm-expandable').removeClass('rvnm-collapseable');
}
$(settings.wrapper).removeClass('rvnm-mobile-wrapper');
$(self).removeClass('rvnm-mobile');
$(settings.wrapper).addClass('rvnm-minimal');
$(self).addClass('rvnm-minimal');
}
// if window size less than 450 active mobile mode
if ($(window).width() <= 450) {
$(settings.wrapper).removeClass('rvnm-minimal');
$(self).removeClass('rvnm-minimal');
$(settings.wrapper).addClass('rvnm-mobile-wrapper');
$(self).addClass('rvnm-mobile');
}
// if window size greater than 768 active desktop mode by
// remove minimal & mobile calss
if ($(window).width() >= 768) {
$(settings.wrapper).removeClass('rvnm-minimal');
$(self).removeClass('rvnm-minimal');
$(settings.wrapper).removeClass('rvnm-mobile-wrapper');
$(self).removeClass('rvnm-mobile');
}
// grater than mobile
if ($(window).width() > 450) {
let menuHeight = $(self).find('> ul').height() ;
if (menuHeight / 2 < $(window).scrollTop()){
$(self).find('> ul').css('top', ($(window).scrollTop() - menuHeight ) + (menuHeight / settings.sticky) + 'px');
}else{
$(self).find('> ul').css('top',0);
}
}
}
$(".rvnm-navbar-box").css('height', '');
if (settings.mode !== 'mobile' && !$(self).hasClass('rvnm-mobile')) {
if ($(".rvnm-navbar-box").height() < $("body").height() || $(".rvnm-navbar-box").height() < $(window).height() || $(".rvnm-navbar-box").height() < $("html").height()) {
$(".rvnm-navbar-box").height(Math.max($('body').height(), $(window).height(), $("html").height()));
}
}
};
// set plugn selector to self for use in other place of plugin
var self = this;
var lastScroll = 0;
this.each(function () {
// add rvnm-navbar-box to menu
$(this).addClass('rvnm-navbar-box');
if (settings.searchable) {
$(this).find('> ul').prepend('<li class="search"> <i class="fa fa-search"></i> <input class="rvnm-search" type="search" placeholder="Search..." /> </li>');
}
// add theme if extis
if (settings.theme !== '') {
$(this).addClass(settings.theme);
}
// add expandable class to li's has ul child
$(this).find('li:has(> ul)').addClass('rvnm-expandable');
// rvnm-wrapper class to main content element
$(settings.wrapper).addClass('rvnm-wrapper');
// check if minimal mode active change mode
if ($(this).hasClass('rvnm-minimal') || settings.mode === 'minimal') {
$(settings.wrapper).addClass('rvnm-minimal');
$(this).addClass('rvnm-minimal');
settings.mode = 'minimal';
}
// check if mobile mode active change mode
if ($(this).hasClass('rvnm-minimal') || settings.mode === 'mobile') {
$(settings.wrapper).addClass('rvnm-mobile-wrapper');
$(this).addClass('rvnm-mobile');
settings.mode = 'mobile';
}
// resize navbar box
self.sizetrigger();
// add triger windows resize
$window = $(window);
$window.on('scroll', function () {
self.sizetrigger();
});
$window.on('resize', function () {
self.sizetrigger();
});
/**
* ripple effect for links
*/
$(document).on('click', '.rvnm-navbar-box ul li a', function (e) {
// Remove any old one
$(".rvnm-ripple").remove();
// Setup
var posX = $(this).offset().left,
posY = $(this).offset().top,
buttonWidth = $(this).width(),
buttonHeight = $(this).height();
// Add the element
$(this).prepend("<span class='rvnm-ripple'></span>");
// Make it round!
if (buttonWidth >= buttonHeight) {
buttonHeight = buttonWidth;
} else {
buttonWidth = buttonHeight;
}
// Get the center of the element
var x = e.pageX - posX - buttonWidth / 2;
var y = e.pageY - posY - buttonHeight / 2;
// Add the ripples CSS and start the animation
$(".rvnm-ripple").css({
width: buttonWidth,
height: buttonHeight,
top: y + 'px',
left: x + 'px'
}).addClass("rvnm-rippleEffect");
setTimeout(function () {
$(".rvnm-ripple").remove();
}, 600);
});
// add click event to expandable link
$(document).on('click', '.rvnm-expandable > a', function (e) {
// check click only this element
if (e.target !== e.currentTarget)
return false;
// check is first level of li child and minimal mode siable
if ($(this).parent().hasClass('rvnm-minimal-expand')) {
return false;
}
// check is first level of li child
// try to close other expanded items
if ($(this).parent().closest('.rvnm-collapseable').length === 0) {
// slide up first level ul of this
$(".rvnm-collapseable > a").parent().find('> ul').slideUp(300);
// add expandable class to parent of link and remove collapseable
$(".rvnm-collapseable > a").parent().addClass('rvnm-expandable').removeClass('rvnm-collapseable');
}
// add collapseable class to parent of link and remove expandable
$(this).parent().addClass('rvnm-collapseable').removeClass('rvnm-expandable');
// slide down first level ul
$(this).parent().find('> ul').slideDown(300, function () {
// then use size triger
self.sizetrigger();
});
// if href is # link should not be work
if ($(this).attr('href') === '#') {
return false;
}
});
$(document).on('keyup mouseup change', '.rvnm-search', function (e) {
var word = $(this).val();
if (word.length == 0) {
$(this).closest('ul').find('> li').show();
} else {
$(this).closest('ul').find('> li').each(function () {
if (!$(this).hasClass('search')) {
$(this).show();
var txt = $(this).text();
if (txt.indexOf(word) == -1) {
$(this).hide();
}
}
});
self.sizetrigger();
}
});
// add click event to collapseable link
$(document).on('click', '.rvnm-collapseable > a', function (e) {
// check click only this element
if (e.target !== e.currentTarget)
return false;
// add expandable class to parent of link and remove collapseable
$(this).parent().addClass('rvnm-expandable').removeClass('rvnm-collapseable');
// slide up first level ul of this
$(this).parent().find('> ul').slideUp(300, function () {
self.sizetrigger();
});
// if href is # link should not be work
if ($(this).attr('href') === '#') {
return false;
}
});
// click an nav box when has rvnm-mobile class
$(document).on('click', '.rvnm-mobile', function (e) {
if (e.target === e.currentTarget && $(e.currentTarget).hasClass('rvnm-navbar-box')) {
$(this).toggleClass('rvnm-mobile-expand');
// go top and restore postion
if ($(this).hasClass('rvnm-mobile-expand')){
lastScroll = $(window).scrollTop();
window.scrollTo({top: 0, behavior: 'smooth'});
}else {
console.log(lastScroll);
window.scrollTo({top: lastScroll, behavior: 'smooth'});
}
}
});
// on mouseenter when menu is minimal
$(document).on('mouseenter', '.rvnm-navbar-box.rvnm-minimal li', function (e) {
// if menu is first level li
if ($(this).closest('.rvnm-minimal-expand').length === 0) {
// show menu
$(this).addClass('rvnm-minimal-expand');
}
});
// on mouseleave when menu is minimal
$(document).on('mouseleave', '.rvnm-navbar-box.rvnm-minimal li.rvnm-minimal-expand', function (e) {
$(".rvnm-minimal-expand .rvnm-collapseable ul").slideUp();
$(".rvnm-minimal-expand .rvnm-collapseable").addClass('rvnm-expandable').removeClass('rvnm-collapseable');
$(this).removeClass('rvnm-minimal-expand');
});
$(window).on('load', function () {
setTimeout(function () {
self.sizetrigger();
}, 100);
});
});
return {
settings: settings,
setMode: function (mode) {
$(settings.wrapper).removeClass('rvnm-mobile-wrapper');
$(self).removeClass('rvnm-mobile');
$(settings.wrapper).removeClass('rvnm-minimal');
$(self).removeClass('rvnm-minimal');
settings.responsive = false;
settings.mode = mode;
if (mode === 'default') {
return true;
}
if (mode === 'minimal') {
$(settings.wrapper).addClass('rvnm-minimal');
$(self).addClass('rvnm-minimal');
return true;
}
if (mode === 'mobile') {
$(settings.wrapper).addClass('rvnm-mobile-wrapper');
$(self).addClass('rvnm-mobile');
return true;
}
},
setTheme: function (theme) {
$(self).removeClass('dark');
$(self).removeClass('dark-lesb');
$(self).removeClass('dark-doder');
$(self).removeClass('dark-beryl');
$(self).removeClass('dark-ruby');
$(self).addClass(theme);
settings.theme = theme;
},
$this: this
};
};
}(jQuery));