This repository has been archived by the owner on Jul 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.menu-aim.js
336 lines (275 loc) · 10.4 KB
/
jquery.menu-aim.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
/**
* Plugin: Amazon Like-nav
* Author: Morozov Igor (http://www.igormorozov.com)
* https://github.com/igormorozov/aim
*/
;(function ($, window, document, notDefined) {
var pluginName = "aim",
activeRow,
MOUSE_LOCS_TRACKED = 3,
mouseLocs = [],
lastDelayLoc = null,
activeMouse = false,
DELAY = 300,
defaults = {
closestItem: '',
activeClass: 'active',
itemsSelector: '> li',
dropPosition: 'right',
delayShow: 0,
delayHide: 0,
tolerance: 75,
setHeight: false,
click: false,
containerDropNav: ''
};
/* The actual plugin constructor */
function Plugin (element, options) {
this.element = element;
this.options = $.extend( {}, defaults, options );
this.init();
}
Plugin.prototype = {
constructor: Plugin,
init: function () {
var element = $(this.element),
_this = this;
if(_this.options.click){
element.on('click', _this.options.itemsSelector, function () {
var elemRow = (_this.options.closestItem) ? $(this).closest(_this.options.closestItem) : $(this);
if(elemRow.hasClass(_this.options.activeClass)) {
_this._hideNav();
}
else {
if($(document).data("imaimopened") && ($(document).data("imaimopened")[0] !== $(_this.element)[0])){
$($(document).data("imaimopened")).aim('hideNav');
}
clearTimeout(element.data('delayHide'));
clearTimeout(element.data('delayActivate'));
_this._activeRow(elemRow);
$(document).on('click.aim', {elem: elemRow}, $.proxy( _this._hideClick, _this ));
}
return false;
});
}
else {
element.on('mouseenter', _this.options.itemsSelector, function () {
var elemRow = (_this.options.closestItem) ? $(this).closest(_this.options.closestItem) : $(this);
clearTimeout(element.data('delayHide'));
clearTimeout(element.data('delayActivate'));
_this._checkActivate(elemRow);
});
element.on('mouseleave', _this.options.itemsSelector, function () {
var elemRow = (_this.options.closestItem) ? $(this).closest(_this.options.closestItem) : $(this);
clearTimeout(element.data('delayHide'));
clearTimeout(element.data('delayActivate'));
element.data('delayHide', setTimeout(function () {
_this._hideNav();
}, _this.options.delayHide));
});
element.on({
mouseenter: function () {
if(!activeMouse){
activeMouse = true;
$(document).on('mousemove.aim', _this._mouseMove);
}
},
mouseleave: function () {
if(activeMouse){
activeMouse = false;
$(document).off('mousemove.aim');
}
}
});
}
},
hideNav: function () {
this._hideNav();
},
_hideClick: function (event) {
var _this = this,
elemRow = event.data.elem;
var checkTarget = (event.target === elemRow[0]) || $(event.target).closest(elemRow).length;
if(!checkTarget){
this._hideNav()
}
},
_hideNav: function () {
var element = $(this.element),
_this = this;
_this._removeOldActive();
clearTimeout(element.data('delayHide'));
clearTimeout(element.data('delayActivate'));
element.trigger('hidenav');
$(document).removeData("imaimopened");
$(document).off('click.aim');
},
_mouseMove: function (e) {
mouseLocs.push({x: e.pageX, y: e.pageY});
if (mouseLocs.length > MOUSE_LOCS_TRACKED) {
mouseLocs.shift();
}
},
_checkActivate: function (row) {
var _this = this,
delay = _this._checkMove();
if (delay) {
$(_this.element).data('delayActivate', setTimeout(function() {
_this._checkActivate(row);
}, delay));
} else {
_this._activeRow(row);
}
},
_activeRow: function(row){
var _this = this,
elemRow = row;
if(!elemRow.hasClass(_this.options.activeClass)){
if($(document).data("imaimopened") && ($(document).data("imaimopened")[0] !== $(_this.element)[0])){
$($(document).data("imaimopened")).aim('hideNav');
}
clearTimeout($(_this.element).data('delayHide'));
clearTimeout($(_this.element).data('delayActivate'));
$(document).off('click.aim');
$(_this.element).data('delayActivate', setTimeout(function() {
_this._removeOldActive();
elemRow.addClass(_this.options.activeClass);
activeRow = elemRow;
$(_this.element).trigger('shownav', [elemRow]);
$(document).data('imaimopened', $(_this.element));
}, _this.options.delayShow));
}
},
_removeOldActive: function () {
var _this = this;
clearTimeout($(_this.element).data('delayHide'));
var elemItem = (_this.options.closestItem) ? $(_this.options.itemsSelector,_this.element).closest(_this.options.closestItem) : _this.options.itemsSelector;
if(elemItem !== ''){
$(_this.element).find(elemItem).removeClass(_this.options.activeClass);
}
else {
$(_this.element).removeClass(_this.options.activeClass);
}
activeRow = null;
},
_checkMove: function() {
var _this = this,
element = $(this.element);
if (!activeRow) {
// If there is no other submenu row already active, then
// go ahead and activate immediately.
return 0;
}
var offset = element.offset(),
upperLeft = {
x: offset.left,
y: offset.top - _this.options.tolerance
},
upperRight = {
x: offset.left + element.outerWidth(),
y: upperLeft.y
},
lowerLeft = {
x: offset.left,
y: offset.top + element.outerHeight() + _this.options.tolerance
},
lowerRight = {
x: offset.left + element.outerWidth(),
y: lowerLeft.y
},
loc = mouseLocs[mouseLocs.length - 1],
prevLoc = mouseLocs[0];
if (!loc) {
return 0;
}
if (!prevLoc) {
prevLoc = loc;
}
if (prevLoc.x < offset.left || prevLoc.x > lowerRight.x ||
prevLoc.y < offset.top || prevLoc.y > lowerRight.y) {
// If the previous mouse location was outside of the entire
// menu's bounds, immediately activate.
return 0;
}
if (lastDelayLoc &&
loc.x == lastDelayLoc.x && loc.y == lastDelayLoc.y) {
// If the mouse hasn't moved since the last time we checked
// for activation status, immediately activate.
return 0;
}
// Detect if the user is moving towards the currently activated
// submenu.
//
// If the mouse is heading relatively clearly towards
// the submenu's content, we should wait and give the user more
// time before activating a new row. If the mouse is heading
// elsewhere, we can immediately activate a new row.
//
// We detect this by calculating the slope formed between the
// current mouse location and the upper/lower right points of
// the menu. We do the same for the previous mouse location.
// If the current mouse location's slopes are
// increasing/decreasing appropriately compared to the
// previous's, we know the user is moving toward the submenu.
//
// Note that since the y-axis increases as the cursor moves
// down the screen, we are looking for the slope between the
// cursor and the upper right corner to decrease over time, not
// increase (somewhat counterintuitively).
function slope(a, b) {
return (b.y - a.y) / (b.x - a.x);
}
var decreasingCorner = upperRight,
increasingCorner = lowerRight;
// Our expectations for decreasing or increasing slope values
// depends on which direction the submenu opens relative to the
// main menu. By default, if the menu opens on the right, we
// expect the slope between the cursor and the upper right
// corner to decrease over time, as explained above. If the
// submenu opens in a different direction, we change our slope
// expectations.
if (_this.options.dropPosition == "left") {
decreasingCorner = lowerLeft;
increasingCorner = upperLeft;
} else if (_this.options.dropPosition == "below") {
decreasingCorner = lowerRight;
increasingCorner = lowerLeft;
} else if (_this.options.dropPosition == "above") {
decreasingCorner = upperLeft;
increasingCorner = upperRight;
}
var decreasingSlope = slope(loc, decreasingCorner),
increasingSlope = slope(loc, increasingCorner),
prevDecreasingSlope = slope(prevLoc, decreasingCorner),
prevIncreasingSlope = slope(prevLoc, increasingCorner);
if (decreasingSlope < prevDecreasingSlope &&
increasingSlope > prevIncreasingSlope) {
// Mouse is moving from previous location towards the
// currently activated submenu. Delay before activating a
// new menu row, because user may be moving into submenu.
lastDelayLoc = loc;
return DELAY;
}
lastDelayLoc = null;
return 0;
}
};
$.fn[pluginName] = function (options) {
var args = arguments, _return;
if (options === notDefined || typeof options === 'object') {
_return = this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin( this, options ));
}
});
} else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
_return = this.each(function () {
var instance = $.data(this, 'plugin_' + pluginName);
if (instance instanceof Plugin && typeof instance[options] === 'function') {
instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) );
}
});
}
return _return;
};
}(jQuery, window, document));