/
dropdown.js
75 lines (59 loc) · 1.84 KB
/
dropdown.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
/**
* Dropdown menu module
*
* @module elgg/menus/dropdown
*/
define(function (require) {
var elgg = require('elgg');
var popup = require('elgg/popup');
var dropdown = {
init: function () {
// handles clicking on a menu item that has a dropdown menu
$(document).on('click', '.elgg-menu-item-has-dropdown > a', function (e) {
var $trigger = $(this);
if ($trigger.data('dropdownMenu')) {
var $target = $trigger.data('dropdownMenu');
} else {
var $target = $trigger.siblings('.elgg-child-menu').eq(0);
$trigger.data('dropdownMenu', $target);
$target.on('open', function () {
$trigger.addClass('elgg-menu-opened')
.removeClass('elgg-menu-closed');
$trigger.parent().addClass('elgg-state-selected');
});
$target.on('close', function () {
$trigger.addClass('elgg-menu-closed')
.removeClass('elgg-menu-opened');
$trigger.parent().removeClass('elgg-state-selected');
});
}
if (!$trigger.length || !$target.length) {
return;
}
e.preventDefault();
$target.addClass('elgg-menu-hover');
var position = $target.data('position') || {
at: 'center bottom',
my: 'center top',
collision: 'fit fit'
};
position.of = $trigger;
popup.open($trigger, $target, position);
});
// if an anchor also has its own link the text acts as the link, the before pseudo element handles the toggle
$(document).on('click', '.elgg-menu-item-has-dropdown > a > .elgg-anchor-label', function (e) {
var $anchor = $(this).closest('a');
var href = $anchor.attr('href');
if ($anchor.hasClass('elgg-non-link') || !href) {
return;
}
document.location = href;
e.preventDefault();
e.stopImmediatePropagation();
});
dropdown.init = elgg.nullFunction;
}
};
dropdown.init();
return dropdown;
});