-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmenu.js
More file actions
122 lines (114 loc) · 4.45 KB
/
menu.js
File metadata and controls
122 lines (114 loc) · 4.45 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
/**
* Copyright © 2015 STRG.AT GmbH, Vienna, Austria
*
* This file is part of the The SCORE Framework.
*
* The SCORE Framework and all its parts are free software: you can redistribute
* them and/or modify them under the terms of the GNU Lesser General Public
* License version 3 as published by the Free Software Foundation which is in the
* file named COPYING.LESSER.txt.
*
* The SCORE Framework and all its parts are distributed without any WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. For more details see the GNU Lesser General Public
* License.
*
* If you have not received a copy of the GNU Lesser General Public License see
* http://www.gnu.org/licenses/.
*
* The License-Agreement realised between you as Licensee and STRG.AT GmbH as
* Licenser including the issue of its valid conclusion and its pre- and
* post-contractual effects is governed by the laws of Austria. Any disputes
* concerning this License-Agreement including the issue of its valid conclusion
* and its pre- and post-contractual effects are exclusively decided by the
* competent court, in whose district STRG.AT GmbH has its registered seat, at
* the discretion of STRG.AT GmbH also the competent court, in whose district the
* Licensee has his registered seat, an establishment or assets.
*/
define('lib/score/menu', ['lib/score/oop'], function(oop) {
return oop.Class({
__name__: 'Menu',
__events__: ['open', 'close'],
__init__: function(self, ul) {
self.ul = ul;
self.hoverItem = null;
self.hoverTimeout = null;
var resolve = function() {
if (self.hoverItem === null) {
self.close();
} else {
self.open(self.hoverItem);
}
self.hoverTimeout = null;
};
var setResolveTimeout = function() {
if (self.hoverTimeout !== null) {
window.clearTimeout(self.hoverTimeout);
self.hoverTimeout = null;
}
self.hoverTimeout = window.setTimeout(resolve, 200);
};
var mouseLeave = function(event) {
self.hoverItem = null;
setResolveTimeout();
};
var mouseEnter = function(event) {
self.hoverItem = this;
if (self.activeItem()) {
setResolveTimeout();
} else {
resolve();
}
};
for (var i = 0; i < ul.children.length; i++) {
var li = ul.children.item(i);
li.addEventListener('mouseleave', mouseLeave);
li.addEventListener('mouseenter', mouseEnter);
li.addEventListener('click', mouseEnter);
}
if ('ontouchstart' in document.documentElement) {
setResolveTimeout = resolve;
document.addEventListener('touchstart', function(event) {
var target = document.elementFromPoint(event.clientX, event.clientY);
if (!self.ul.contains(target)) {
self.close();
} else {
self.hoverItem = this;
resolve();
}
});
}
},
close: function(self) {
var active = self.activeItem();
if (!active) {
return true;
}
if (!self.trigger('close', active)) {
return false;
}
active.className = active.className.replace(/\bmenu-item-active\b/, '');
self.hoverItem = null;
return true;
},
activeItem: function(self) {
return self.ul.querySelector('li.menu-item-active');
},
open: function(self, item) {
if (typeof item === 'number') {
item = self.ul.children.item(item);
} else if (typeof item !== 'object') {
throw 'Argument must be numeric or a DOM node';
}
if (item == self.activeItem()) {
return;
}
if (!self.close()) {
return;
}
item.className = item.className + ' menu-item-active';
self.hoverItem = item;
self.trigger('open', item);
}
});
});