This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpgwmenu.js
198 lines (164 loc) · 7.22 KB
/
pgwmenu.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
/**
* PgwMenu - Version 2.0
*
* Copyright 2014, Jonathan M. Piat
* http://pgwjs.com - http://pagawa.com
*
* Released under the GNU GPLv3 license - http://opensource.org/licenses/gpl-3.0
*/
;(function($){
$.fn.pgwMenu = function(options) {
var defaults = {
mainClassName : 'pgwMenu',
dropDownLabel : '<span class="icon"></span>',
viewMoreEnabled : true,
viewMoreLabel : 'View more <span class="icon"></span>',
viewMoreMaxWidth : 480
};
if (this.length == 0) {
return this;
} else if(this.length > 1) {
this.each(function() {
$(this).pgwMenu(options);
});
return this;
}
var pgwMenu = this;
pgwMenu.plugin = this;
pgwMenu.config = {};
pgwMenu.resizeEvent = null;
pgwMenu.window = $(window);
// Init function
var init = function() {
// Merge user options with default configuration
pgwMenu.config = $.extend({}, defaults, options);
// Setup
setup();
pgwMenu.checkMenu();
// Resize trigger
pgwMenu.window.resize(function() {
pgwMenu.plugin.css('overflow', 'hidden');
clearTimeout(pgwMenu.resizeEvent);
pgwMenu.resizeEvent = setTimeout(function() {
pgwMenu.checkMenu();
}, 100);
});
pgwMenu.plugin.find('.pm-dropDown').click(function(e) {
pgwMenu.enableMobileDropDown();
e.stopPropagation();
});
pgwMenu.plugin.find('.pm-viewMore').click(function(e) {
pgwMenu.enableViewMoreDropDown();
e.stopPropagation();
});
$(document).click(function() {
pgwMenu.disableMobileDropDown();
pgwMenu.disableViewMoreDropDown();
});
};
// Setup
var setup = function() {
var wrapClass = pgwMenu.config.mainClassName;
var defaultClass = pgwMenu.plugin.attr('class');
if (defaultClass && defaultClass.indexOf('light') > -1) {
wrapClass += ' light';
}
pgwMenu.plugin.removeClass().addClass('pm-links');
pgwMenu.plugin.wrap('<div class="' + wrapClass + '"></div>');
pgwMenu.plugin = pgwMenu.plugin.parent();
pgwMenu.plugin.prepend('<div class="pm-dropDown"><a href="javascript:void(0)">' + pgwMenu.config.dropDownLabel + '</a></div>');
if (pgwMenu.config.viewMoreEnabled) {
pgwMenu.plugin.append('<div class="pm-viewMore" style="display:inline-block"><a href="javascript:void(0)">' + pgwMenu.config.viewMoreLabel + '</a><ul></ul></div>');
}
};
// Check menu
pgwMenu.checkMenu = function() {
var pluginMaxWidth = pgwMenu.plugin.width();
if (pgwMenu.config.viewMoreEnabled) {
var viewMoreLinkWidth = pgwMenu.plugin.find('.pm-viewMore').outerWidth(true);
}
function getContentWidth() {
var menuContentWidth = 0;
pgwMenu.plugin.find('.pm-links').removeClass('mobile').show();
pgwMenu.plugin.find('.pm-links > li').each(function() {
menuContentWidth += $(this).outerWidth(true);
});
return menuContentWidth;
}
function switchMenu(type) {
if (type == 'viewmore') {
var viewMoreMenuWidth = viewMoreLinkWidth;
pgwMenu.plugin.find('.pm-links').removeClass('mobile').show();
pgwMenu.plugin.find('.pm-viewMore > ul > li').remove();
pgwMenu.plugin.find('.pm-links > li').show().each(function() {
if (viewMoreMenuWidth + $(this).outerWidth(true) < pluginMaxWidth) {
viewMoreMenuWidth += $(this).outerWidth(true);
} else {
pgwMenu.plugin.find('.pm-viewMore > ul').append($(this).clone().show());
$(this).hide();
}
});
pgwMenu.plugin.find('.pm-dropDown, .pm-viewMore > ul').hide();
pgwMenu.plugin.find('.pm-viewMore').show();
} else if (type == 'dropdown') {
pgwMenu.plugin.find('.pm-links > li').show();
pgwMenu.plugin.find('.pm-links').addClass('mobile').hide();
pgwMenu.plugin.find('.pm-viewMore, .pm-viewMore > ul').hide();
pgwMenu.plugin.find('.pm-viewMore > ul > li').remove();
pgwMenu.plugin.find('.pm-dropDown').show();
} else {
pgwMenu.plugin.find('.pm-links > li').show();
pgwMenu.plugin.find('.pm-links').removeClass('mobile').show();
pgwMenu.plugin.find('.pm-dropDown, .pm-viewMore, .pm-viewMore > ul').hide();
pgwMenu.plugin.find('.pm-viewMore > ul > li').remove();
}
pgwMenu.plugin.find('.pm-dropDown > a, .pm-viewMore > a').removeClass('active');
}
if (getContentWidth() > pluginMaxWidth) {
if (pgwMenu.config.viewMoreEnabled && (pluginMaxWidth > pgwMenu.config.viewMoreMaxWidth)) {
switchMenu('viewmore');
} else {
switchMenu('dropdown');
}
} else {
switchMenu('normal');
}
pgwMenu.plugin.css('overflow', '');
};
// Enable view more drop down
pgwMenu.enableViewMoreDropDown = function() {
if (pgwMenu.plugin.find('.pm-viewMore > a').hasClass('active')) {
pgwMenu.disableViewMoreDropDown();
return false;
}
pgwMenu.plugin.find('.pm-viewMore > a').addClass('active');
pgwMenu.plugin.find('.pm-viewMore > ul').show();
};
// Disable view more drop down
pgwMenu.disableViewMoreDropDown = function() {
if (pgwMenu.plugin.find('.pm-viewMore > a').hasClass('active')) {
pgwMenu.plugin.find('.pm-viewMore > a').removeClass('active');
pgwMenu.plugin.find('.pm-viewMore > ul').hide();
}
};
// Enable mobile drop down
pgwMenu.enableMobileDropDown = function() {
if (pgwMenu.plugin.find('.pm-dropDown > a').hasClass('active')) {
pgwMenu.disableMobileDropDown();
return false;
}
pgwMenu.plugin.find('.pm-dropDown > a').addClass('active');
pgwMenu.plugin.find('.pm-links').show();
};
// Disable mobile drop down
pgwMenu.disableMobileDropDown = function() {
if (pgwMenu.plugin.find('.pm-dropDown > a').hasClass('active')) {
pgwMenu.plugin.find('.pm-dropDown > a').removeClass('active');
pgwMenu.plugin.find('.pm-links').hide();
}
};
// Menu initialization
init();
return this;
}
})(window.Zepto || window.jQuery);