-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjumpmenu.js
More file actions
367 lines (327 loc) · 12.9 KB
/
Copy pathjumpmenu.js
File metadata and controls
367 lines (327 loc) · 12.9 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
var PhorumJumpMenu =
{
active : {},
visible : {},
menuspacing : -1,
timer : null,
vroot : 0, // will be set separately from the jumpmenu.php code
init: function ()
{
// Make sure that events will not fire at unload time, because that
// generates JavaScript warnings in Firefox about PhorumJumpMenu
// that does not exist anymore at that time.
var oldunload = window.onunload;
window.onunload = function ()
{
if (oldunload) {
oldunload();
}
var root = document.getElementById('mod_jumpmenu_root');
if (root) {
root.onmouseover = null;
root.onmouseout = null;
}
var ahrefs = document.getElementsByTagName('a');
for (var i = 0; i < ahrefs.length; i += 1)
{
if (!ahrefs[i].rel) {
continue;
}
var obj = ahrefs[i];
if (obj.rel.indexOf('mod_jumpmenu_') === 0) {
obj.onmouseover = null;
obj.onmouseout = null;
}
}
}
// Events for the root jumpmenu item.
var r = document.getElementById('mod_jumpmenu_root');
if (!r) {
return;
}
r.onmouseover = function () {
PhorumJumpMenu.stopCloseTimer();
PhorumJumpMenu.openSubMenu(null);
};
r.onmouseout = function () {
PhorumJumpMenu.startCloseTimer();
};
// Events for the folders and forums.
var ahrefs = document.getElementsByTagName('a');
for (var i = 0; i < ahrefs.length; i += 1)
{
if (!ahrefs[i].rel) {
continue;
}
var obj = ahrefs[i];
if (obj.rel === 'mod_jumpmenu_forum')
{
obj.onmouseover = function () {
PhorumJumpMenu.stopCloseTimer();
PhorumJumpMenu.openSubMenu(this);
};
obj.onmouseout = function () {
PhorumJumpMenu.startCloseTimer();
};
}
else if (obj.rel.substring(0, 19) === 'mod_jumpmenu_folder')
{
var info = obj.rel.substring(20, obj.rel.length - 1).split(',');
obj.menu_id = info[0];
obj.parent_id = info[1];
obj.onmouseover = function () {
PhorumJumpMenu.stopCloseTimer();
PhorumJumpMenu.openSubMenu(this);
};
obj.onmouseout = function () {
PhorumJumpMenu.startCloseTimer();
};
}
}
},
hideAll: function ()
{
for (var depth in PhorumJumpMenu.visible) {
for (var idx in PhorumJumpMenu.visible[depth]) {
var o = PhorumJumpMenu.visible[depth][idx];
$PJ(o).hide();
}
}
PhorumJumpMenu.active = {};
PhorumJumpMenu.visible = {};
},
startCloseTimer: function ()
{
if (PhorumJumpMenu.timer !== null) {
clearTimeout(PhorumJumpMenu.timer);
}
PhorumJumpMenu.timer = setTimeout(function () {
PhorumJumpMenu.timer = null;
PhorumJumpMenu.hideAll();
}, 500);
},
stopCloseTimer: function ()
{
if (PhorumJumpMenu.timer !== null) {
clearTimeout(PhorumJumpMenu.timer);
PhorumJumpMenu.timer = null;
}
},
getScreenInfo: function ()
{
var width = $PJ(window).width();
var height = $PJ(window).height();
var scroll_y = $PJ(window).scrollTop();
var scroll_x = $PJ(window).scrollLeft();
return {
'width' : width,
'height' : height,
'scroll_x' : scroll_x,
'scroll_y' : scroll_y,
'visible_x' : width + scroll_x,
'visible_y' : height + scroll_y
};
},
openSubMenu: function (menu_item)
{
// If the menu item is not a folder, then find its parent menu.
// This way, deeper submenus that are possibly opened
// will be collapsed.
var menu = menu_item;
if (menu_item && menu_item.className.indexOf('mod_jumpmenu_folder') == -1) {
var n = menu.parentNode.id;
if (n.indexOf('mod_jumpmenu_menu_content_') == 0) {
var menu_id = n.substr(26);
if (menu_id == PhorumJumpMenu.vroot) {
menu = null;
} else {
var pid = 'mod_jumpmenu_item_' + menu_id;
var p = document.getElementById(pid);
if (p) menu = p;
}
}
}
// Find the parent menu, parent item and child menu.
var menu_id = menu == null ? PhorumJumpMenu.vroot : menu.menu_id;
var parent_id = menu == null ? null : menu.parent_id;
var p_menu = menu_id == PhorumJumpMenu.vroot
? null
: document.getElementById('mod_jumpmenu_menu_' + parent_id);
var p_item = menu_id == PhorumJumpMenu.vroot
? document.getElementById('mod_jumpmenu_root')
: document.getElementById('mod_jumpmenu_item_' + menu_id);
var c_menu = document.getElementById('mod_jumpmenu_menu_' + menu_id);
// Handle highlighting of the active menu item.
// Clear all highlights in the child menu.
if (c_menu) {
var ahrefs = c_menu.getElementsByTagName('a');
for (var i=0; i<ahrefs.length; i += 1) {
var idx = ahrefs[i].className.indexOf(' mod_jumpmenu_highlighted');
if (idx != -1) {
ahrefs[i].className = ahrefs[i].className.substring(0,idx);
}
}
}
// If the menu item is a folder, then we also cleanup the
// parent menu (which is the menu that the user is currently
// hovering over).
if (menu_item && menu_item.className.indexOf('mod_jumpmenu_folder') != -1) {
var ahrefs = p_menu.getElementsByTagName('a');
for (var i=0; i<ahrefs.length; i += 1) {
var idx = ahrefs[i].className.indexOf(' mod_jumpmenu_highlighted');
if (idx != -1) {
ahrefs[i].className = ahrefs[i].className.substring(0,idx);
}
}
}
// Highlight the active menu item.
if (menu_item && menu_item.className.indexOf(' mod_jumpmenu_highlighted') == -1) {
menu_item.className += ' mod_jumpmenu_highlighted';
}
// If we did not find the required objects, then silently ignore
// the problem and stop processing.
if (!p_item || !c_menu) return;
if (menu_id != PhorumJumpMenu.vroot && !p_menu) return;
// Do some dimension handling and storage the first time that the
// menu is opened.
if (!c_menu.jumpmenu_init_done)
{
$c_menu = $PJ(c_menu);
// Without making the menu visible, we don't know the width/height
// of the menu div, because those are only available after rendering
// the menu HTML code. So for positioning the menu, we have a
// chicken/egg problem to fix. I fix it by moving the egg out of
// the visible screen area before making it visible.
$c_menu.css({
top : '-500px',
left : '-500px',
zIndex : 1000 // This should put the menus on top at most pages.
}).show();
// force the widths for the <a href="..."> tags. this is needed to
// make the links hoverable and clickable over the full menu width
// in msie6 :(
// the "var w" is used to prevent variable tag widths due to
// accidental resizing of the menu while setting the tag widths
// (i've seen that happening in opera).
var w = null;
$c_menu.find('a').each(function () {
$a = $PJ(this);
if (!w) w = $a.outerWidth() + 'px';
$a.css('width', w);
});
// store the displaying dimensions in the div object.
c_menu.jumpmenu_width = $c_menu.outerWidth();
c_menu.jumpmenu_height = $c_menu.outerHeight();
c_menu.jumpmenu_init_done = 1;
$c_menu.hide();
}
// keep track of the menu depth and menu drawing direction.
// first level menu.
if (menu_id == PhorumJumpMenu.vroot)
{
c_menu.depth = 1;
// at the first level, we look at the main menu item. if that one
// uses rel="mod_jumpmenu_left", then the direction of the whole
// menu structure will be left. by default, this direction is right.
c_menu.direction = 'right';
if (p_item.rel && p_item.rel == 'mod_jumpmenu_left') {
c_menu.direction = 'left';
}
}
// second level and deeper.
else
{
c_menu.depth = p_menu.depth + 1;
// inherit the parent's menu direction.
c_menu.direction = p_menu.direction;
}
// determine the screen dimensions.
var scr = PhorumJumpMenu.getScreenInfo();
// determine the top-left of the parent menu item.
var p_item_pos = $PJ(p_item).offset();
var top = p_item_pos.top;
var left = p_item_pos.left;
// determine the location of the child menu.
// the first level menu is placed directly beneath the main menu object.
if (menu_id == PhorumJumpMenu.vroot)
{
top = top + $PJ(p_item).outerHeight();
if (c_menu.direction == 'left')
{
left = left - (c_menu.jumpmenu_width - $PJ(p_item).outerWidth());
if (left < scr.scroll_x) left = scr.scroll_x + 2;
}
else
{
left = left;
if ((left + c_menu.jumpmenu_width) > scr.width) {
left = scr.width - c_menu.jumpmenu_width - 2;
}
}
}
// second level and deeper levels are handled as standard menu popups
// (placed to the right or left of the selected item).
else
{
// The left pos is derived from the outer menu container.
var $menu = $PJ(p_item).closest('.mod_jumpmenu_menu');
var pos = $menu.offset();
left = pos.left;
var dir = c_menu.direction;
var left_pos =
left // the left side of the parent menu item
- PhorumJumpMenu.menuspacing // handle menu spacing
- c_menu.jumpmenu_width; // add child menu width;
var right_pos =
left // the left side of the parent menu item
+ PhorumJumpMenu.menuspacing // handle menu spacing
+ $menu.outerWidth(); // add parent menu width
;
if (dir == 'left') {
if (left_pos < scr.scroll_x) {
dir = 'right';
} else {
left = left_pos;
}
}
if (dir == 'right') {
if ((right_pos + c_menu.jumpmenu_width) > scr.visible_x) {
left = left_pos;
} else {
left = right_pos;
}
}
}
// take extra vertical space into account for adjusting for
// padding of the menu div.
if (menu_id != PhorumJumpMenu.vroot) {
top -= p_item.parentNode.offsetTop;
}
// move the menu up if needed.
if ((top + c_menu.jumpmenu_height) > scr.visible_y) {
top = scr.visible_y - c_menu.jumpmenu_height - 2;
if (top < scr.scroll_y) top = scr.scroll_y + 2;
}
// move the menu into position and make it visible.
// The bgiframe is used for working around an MSIE problem.
$PJ(c_menu)
.css({ top : top + 'px', left : left + 'px' })
.bgiframe()
.show()
// keep track of the active menu for the current menu level.
PhorumJumpMenu.active[c_menu.depth] = c_menu;
// cleanup the menus that should not be visible anymore.
for (var depth in PhorumJumpMenu.visible) {
for (var idx in PhorumJumpMenu.visible[depth]) {
var o = PhorumJumpMenu.visible[depth][idx];
if (depth > c_menu.depth ||
o.id != PhorumJumpMenu.active[depth].id) {
$PJ(o).hide();
}
}
}
// keep track of visible menus.
PhorumJumpMenu.visible[c_menu.depth] = {};
PhorumJumpMenu.visible[c_menu.depth][c_menu.id] = c_menu;
}
};