This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
/
tendina.js
203 lines (177 loc) · 6.38 KB
/
tendina.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
/*
Tendina jQuery plugin v0.11.1
Copyright (c) 2015 Ivan Prignano
Released under the MIT License
*/
(function() {
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__slice = [].slice;
(function($, window) {
var Tendina;
Tendina = (function() {
Tendina.prototype.defaults = {
animate: true,
speed: 500,
onHover: false,
hoverDelay: 200,
activeMenu: null
};
function Tendina(el, options) {
this._eventHandler = __bind(this._eventHandler, this);
this.options = $.extend({}, this.defaults, options);
this.$el = $(el);
this.elSelector = this._getSelector(this.$el);
this.$el.addClass('tendina');
this.linkSelector = "" + this.elSelector + " a";
this.$listElements = $(this.linkSelector).parent('li');
this._hideSubmenus();
this.mouseEvent = this.options.onHover === true ? 'mouseenter.tendina' : 'click.tendina';
this._bindEvents();
if (this.options.activeMenu !== null) {
this._openActiveMenu(this.options.activeMenu);
}
}
Tendina.prototype._bindEvents = function() {
return $(document).on(this.mouseEvent, this.linkSelector, this._eventHandler);
};
Tendina.prototype._unbindEvents = function() {
return $(document).off(this.mouseEvent);
};
Tendina.prototype._getSelector = function(el) {
var elId, firstClass, _ref;
firstClass = (_ref = $(el).attr('class')) != null ? _ref.split(' ')[0] : void 0;
elId = $(el).attr('id');
if (elId !== void 0) {
return "#" + elId;
} else {
return "." + firstClass;
}
};
Tendina.prototype._isFirstLevel = function(targetEl) {
if ($(targetEl).parent().parent().hasClass('tendina')) {
return true;
}
};
Tendina.prototype._eventHandler = function(event) {
var targetEl;
targetEl = event.currentTarget;
if (this._hasChildren(targetEl) && this._IsChildrenHidden(targetEl)) {
event.preventDefault();
if (this.options.onHover) {
return setTimeout((function(_this) {
return function() {
if ($(targetEl).is(':hover')) {
return _this._openSubmenu(targetEl);
}
};
})(this), this.options.hoverDelay);
} else {
return this._openSubmenu(targetEl);
}
} else if (this._isCurrentlyOpen(targetEl) && this._hasChildren(targetEl)) {
event.preventDefault();
if (!this.options.onHover) {
return this._closeSubmenu(targetEl);
}
}
};
Tendina.prototype._openSubmenu = function(el) {
var $openMenus, $targetMenu;
$targetMenu = $(el).next('ul');
$openMenus = this.$el.find('> .selected ul').not($targetMenu).not($targetMenu.parents('ul'));
$(el).parent('li').addClass('selected');
this._close($openMenus);
this.$el.find('.selected').not($targetMenu.parents('li')).removeClass('selected');
this._open($targetMenu);
if (this.options.openCallback) {
return this.options.openCallback($(el).parent());
}
};
Tendina.prototype._closeSubmenu = function(el) {
var $nestedMenus, $targetMenu;
$targetMenu = $(el).next('ul');
$nestedMenus = $targetMenu.find('li.selected');
$(el).parent().removeClass('selected');
this._close($targetMenu);
$nestedMenus.removeClass('selected');
this._close($nestedMenus.find('ul'));
if (this.options.closeCallback) {
return this.options.closeCallback($(el).parent());
}
};
Tendina.prototype._open = function($el) {
if (this.options.animate) {
return $el.stop(true, true).slideDown(this.options.speed);
} else {
return $el.show();
}
};
Tendina.prototype._close = function($el) {
if (this.options.animate) {
return $el.stop(true, true).slideUp(this.options.speed);
} else {
return $el.hide();
}
};
Tendina.prototype._hasChildren = function(el) {
return $(el).next('ul').length > 0;
};
Tendina.prototype._IsChildrenHidden = function(el) {
return $(el).next('ul').is(':hidden');
};
Tendina.prototype._isCurrentlyOpen = function(el) {
return $(el).parent().hasClass('selected');
};
Tendina.prototype._hideSubmenus = function() {
return this.$el.find('ul').hide();
};
Tendina.prototype._showSubmenus = function() {
this.$el.find('ul').show();
return this.$el.find('li').addClass('selected');
};
Tendina.prototype._openActiveMenu = function(element) {
var $activeMenu, $activeParents;
$activeMenu = element instanceof jQuery ? element : this.$el.find(element);
$activeParents = $activeMenu.closest('ul').parents('li').find('> a');
if (this._hasChildren($activeParents) && this._IsChildrenHidden($activeParents)) {
$activeParents.next('ul').show();
} else {
$activeMenu.next('ul').show();
}
$activeMenu.parent().addClass('selected');
return $activeParents.parent().addClass('selected');
};
Tendina.prototype.destroy = function() {
this.$el.removeData('tendina');
this._unbindEvents();
this._showSubmenus();
this.$el.removeClass('tendina');
return this.$el.find('.selected').removeClass('selected');
};
Tendina.prototype.hideAll = function() {
return this._hideSubmenus();
};
Tendina.prototype.showAll = function() {
return this._showSubmenus();
};
return Tendina;
})();
return $.fn.extend({
tendina: function() {
var args, option;
option = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
return this.each(function() {
var $this, data;
$this = $(this);
data = $this.data('tendina');
if (!data) {
$this.data('tendina', (data = new Tendina(this, option)));
}
if (typeof option === 'string') {
return data[option].apply(data, args);
}
});
}
});
})(window.jQuery, window);
}).call(this);