-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
115 lines (89 loc) · 2.39 KB
/
index.js
File metadata and controls
115 lines (89 loc) · 2.39 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
var $ = require('jquery');
var Emitter = require('emitter');
module.exports = Togglify;
var defaults = {
toggleClass: 'is-open',
clickEvent: 'click.togglify.api'
};
function Togglify(Element, options){
if (!Element) throw new TypeError('togglify(): expects an element');
this.options = options || {};
for (var i in defaults) {
if (!(this.options[i])) this.options[i] = defaults[i];
}
this._element = Element;
this._$element = $(this._element);
this._dataTarget = null;
return this;
}
Emitter(Togglify.prototype);
Togglify.prototype.hasClass = function(_self, className) {
var check = className || this.options.toggleClass;
return _self.parents(this._getDataTarget(_self)).hasClass(check);
};
Togglify.prototype._getDataTarget = function(_self) {
this._dataTarget = _self.data('target') || 'li';
return this._dataTarget;
};
Togglify.prototype.toggle = function(_self, className) {
if (this.hasClass(_self, className)) {
this.close(_self);
} else {
this.open(_self);
}
this.emit('toggle');
return this;
};
Togglify.prototype.open = function(_self) {
_self.parents(this._getDataTarget(_self)).addClass(this.options.toggleClass);
this.emit('open');
return this;
};
Togglify.prototype.close = function(_self) {
_self.parents().removeClass(this.options.toggleClass);
this.emit('close');
return this;
};
Togglify.prototype.toggleAll = function(_self) {
if (this.hasClass(_self)) {
this.closeAll();
} else {
this.closeAll().open(_self);
}
this.emit('toggleAll');
return this;
};
Togglify.prototype.closeAll = function() {
this._$element.parents().removeClass(this.options.toggleClass);
this.emit('closeAll');
return this;
};
Togglify.prototype.onClickToggle = function() {
var _this = this;
$('body').on(this.options.clickEvent, this._element, this, function(e){
e.stopPropagation();
e.preventDefault();
e.data.toggle($(this));
e.data.emit('onClickToggle');
});
return this;
};
Togglify.prototype.onClickToggleAll = function() {
var _this = this;
$('body').on(this.options.clickEvent, this._element, this, function(e){
e.stopPropagation();
e.preventDefault();
e.data.toggleAll($(this));
e.data.emit('onClickToggle');
});
return this;
};
Togglify.prototype.offClickCloseAll = function() {
var _this = this;
$(document).on(this.options.clickEvent, function(e){
e.stopPropagation();
_this.closeAll();
_this.emit('offClickCloseAll');
});
return this;
};