-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathjquery.listmenu.js
More file actions
275 lines (229 loc) · 9.3 KB
/
Copy pathjquery.listmenu.js
File metadata and controls
275 lines (229 loc) · 9.3 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
274
275
/*
*
* jQuery listmenu plugin
* Copyright (c) 2009 iHwy, Inc.
* Author: Jack Killpatrick
*
* Version 1.1 (08/09/2009)
* Requires jQuery 1.3.2 or jquery 1.2.6
*
* Visit http://www.ihwy.com/labs/jquery-listmenu-plugin.aspx for more information.
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
(function($) {
$.fn.listmenu = function(options) {
var opts = $.extend({}, $.fn.listmenu.defaults, options);
var alph = ['_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '-'];
return this.each(function() {
var $wrapper, list, $list, $letters, letters = {}, $letterCount, id, $menu, colOpts = $.extend({}, $.fn.listmenu.defaults.cols, opts.cols), onNav = false, onMenu = false, currentLetter = '';
id = this.id;
$list = $(this);
function init() {
$list.css('visibility', 'hidden'); // hiding to prevent pre-load flicker. Using visibility:hidden so that list item dimensions remain available
setTimeout(function() {
$list.before(createWrapperHtml());
$wrapper = $('#' + id + '-menu');
$wrapper.append(createLettersHtml());
$letters = $('.lm-letters', $wrapper).slice(0, 1); // will always be a single item
if (opts.showCounts) $letterCount = $('.lm-letter-count', $wrapper).slice(0, 1); // will always be a single item
$wrapper.append(createMenuHtml());
$menu = $('.lm-menu', $wrapper);
populateMenu();
if (opts.flagDisabled) addDisabledClass(); // run after populateMenu(): needs some data from there
bindHandlers();
// decide whether to include num and/or other links
//
if (!opts.includeNums) $('._', $letters).remove();
if (!opts.includeOther) $('.-', $letters).remove();
$(':last', $letters).addClass('lm-last');
$wrapper.show();
}, 50);
}
// positions the letter count div above the letter links (so we only have to do it once: after this we just change it's left position via mouseover)
//
function setLetterCountTop() {
$letterCount.css({ top: $('.a', $letters).slice(0, 1).offset({ margin: false, border: true }).top - $letterCount.outerHeight({ margin: true }) });
}
function addDisabledClass() {
for (var i = 0; i < alph.length; i++) {
if (letters[alph[i]] == undefined) $('.' + alph[i], $letters).addClass('lm-disabled');
}
}
function populateMenu() {
var gutter = colOpts.gutter,
cols = colOpts.count,
menuWidth,
colWidth;
if (opts.menuWidth) menuWidth = opts.menuWidth; // use user defined menu width if one provided, else calculate one
else menuWidth = $('.lm-letters', $wrapper).width() - ($menu.outerWidth() - $menu.width());
colWidth = (cols == 1) ? menuWidth : Math.floor((menuWidth - (gutter * (cols - 1))) / cols);
$menu.width(menuWidth); // prevents it from resizing based on content
$list.width(colWidth);
var letter, outerHeight;
$list.children().each(function() {
str = $(this).text().replace(/\s+/g, ''); // strip all white space from text (including tabs and linebreaks that might have been in the HTML) // thanks to Liam Byrne, liam@onsight.ie
if (str != '') {
firstChar = str.slice(0, 1).toLowerCase();
if (/\W/.test(firstChar)) firstChar = '-'; // not A-Z, a-z or 0-9, so considered "other"
if (!isNaN(firstChar)) firstChar = '_'; // use '_' if the first char is a number
}
outerHeight = $(this).outerHeight();
if (letters[firstChar] == undefined) letters[firstChar] = { totHeight: 0, count: 0, colHeight: 0, hasMenu: false };
letter = letters[firstChar];
letter.totHeight += outerHeight;
letter.count++;
$.data($(this)[0], id, { firstChar: firstChar, height: outerHeight });
});
$.each(letters, function() {
this.colHeight = (this.count > 1) ? Math.ceil(this.totHeight / cols) : this.totHeight;
});
var $this, data, iHeight, iLetter, cols = {}, c;
var tagName = $list[0].tagName.toLowerCase();
$list.children().each(function() {
$this = $(this);
data = $.data($this.get(0), id); iLetter = data.firstChar; iHeight = data.height;
if (!letters[l = iLetter].hasMenu) {
$menu.append('<div class="lm-submenu ' + iLetter + '" style="display:none;"></div>');
letters[iLetter].hasMenu = true;
}
if (cols[iLetter] == undefined) cols[iLetter] = { height: 0, colNum: 0, itemCount: 0, $colRoot: null };
c = cols[iLetter];
c.itemCount++;
if (c.height == 0) {
c.colNum++;
$('.lm-submenu.' + iLetter, $menu).append('<div class="lm-col c' + c.colNum + '"><' + tagName + ((tagName == 'ol') ? ' start="' + c.itemCount + '"' : '') + ' id="lm-' + id + '-' + iLetter + '-' + c.colNum + '" class="lm-col-root"></' + tagName + '></div>'); // reset start number for OL lists (deprecacted, but no reliable css solution). Creating an id to make lookups for appending list items faster
}
$('#lm-' + id + '-' + iLetter + '-' + c.colNum).append($(this));
c.height += iHeight;
if (c.height >= letters[iLetter].colHeight) c.height = 0; // forces another column to get started if this letter comes up again
});
$.each(letters, function(idx) {
if (this.hasMenu) {
$('.lm-submenu.' + idx + ' .lm-col', $menu).css({ 'width': colWidth, 'float': 'left' });
$('.lm-submenu.' + idx + ' .lm-col:not(:last)', $menu).css({ 'marginRight': gutter });
}
});
$menu.append('<div class="lm-no-match" style="display:none">' + opts.noMatchText + '</div>');
$list.remove();
}
function getLetterCount(el) {
var letter = letters[$(el).attr('class').split(' ')[0]];
return (letter != undefined) ? letter.count : 0; // some letters may not be in the hash
}
function hideCurrentSubmenu() {
if (currentLetter != '') $('.lm-submenu.' + currentLetter, $menu).hide();
$('.lm-no-match', $menu).hide(); // hiding each time, rather than checking to see if we need to hide it
}
function bindHandlers() {
// sets the top position of the count div in case something above it on the page has resized
//
if (opts.showCounts) {
$wrapper.mouseover(function() {
setLetterCountTop();
});
}
// kill letter clicks
//
$('a', $letters).click(function() {
$(this).blur();
return false;
});
$letters.hover(
function() { onNav = true; },
function() {
onNav = false;
setTimeout(function() {
if (!onMenu) {
$('a.lm-selected', $letters).removeClass('lm-selected');
$('.lm-menu', $wrapper).hide();
hideCurrentSubmenu();
currentLetter = '';
}
}, 10);
}
);
$('a', $letters).mouseover(function() {
var count = getLetterCount(this);
var $this = $(this);
if (opts.showCounts) {
var left = $this.position().left;
var width = $this.outerWidth({ margin: true });
if (opts.showCounts) $letterCount.css({ left: left, width: width + 'px' }).text(count).show(); // set left position and width of letter count, set count text and show it
}
var newLetter = $this.attr('class').split(' ')[0];
if (newLetter != currentLetter) {
if (currentLetter != '') {
hideCurrentSubmenu();
$('a.lm-selected', $letters).removeClass('lm-selected');
}
$this.addClass('lm-selected');
if (count > 0) $('.lm-submenu.' + newLetter, $wrapper).show();
else $('.lm-no-match', $wrapper).show();
if (currentLetter == '') $('.lm-menu', $wrapper).show();
currentLetter = newLetter;
}
});
$('a', $letters).mouseout(function() {
if (opts.showCounts) $letterCount.hide();
});
$menu.hover(
function() {
onMenu = true;
},
function() {
onMenu = false;
setTimeout(function() {
if (!onNav) {
$('a.lm-selected', $letters).removeClass('lm-selected');
$('.lm-menu', $wrapper).hide();
hideCurrentSubmenu();
currentLetter = '';
}
}, 10);
}
);
if (opts.onClick != null) {
$menu.click(function(e) {
var $target = $(e.target);
opts.onClick($target);
return false;
});
}
}
// creates the HTML for the letter links
//
function createLettersHtml() {
var html = [];
for (var i = 1; i < alph.length; i++) {
if (html.length == 0) html.push('<a class="_" href="#">0-9</a>');
html.push('<a class="' + alph[i] + '" href="#">' + ((alph[i] == '-') ? '...' : alph[i].toUpperCase()) + '</a>');
}
return '<div class="lm-letters">' + html.join('') + '</div>' + ((opts.showCounts) ? '<div class="lm-letter-count" style="display:none; position:absolute; top:0; left:0; width:20px;">0</div>' : ''); // the styling for letterCount is to give us a starting point for the element, which will be repositioned when made visible (ie, should not need to be styled by the user)
}
function createMenuHtml() {
return '<div class="lm-menu"></div>';
}
function createWrapperHtml() {
return '<div id="' + id + '-menu" class="lm-wrapper"></div>';
}
init();
});
};
$.fn.listmenu.defaults = {
includeNums: true,
includeOther: false,
flagDisabled: true,
noMatchText: 'No matching entries',
showCounts: true,
menuWidth: null,
cols: {
count: 4,
gutter: 40
},
onClick: null
};
})(jQuery);