forked from htshah/jquery.open-toggle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.open-toggle.js
56 lines (48 loc) · 1.62 KB
/
jquery.open-toggle.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
/**
* jQuery open-toggle
* A very lightweight jQuery plugin to toggle open state
* via toggling '.open' or other user-defined class
*
* Licensed under the MIT license.
* Copyright 2019 Het Shah
* https://github.com/htshah
*/
(function($) {
$.fn.openToggle = function(className = "open") {
var wasClicked = false,
that = this,
docToggle = false;
toggleEle = $(null);
closeEle = false;
function getEle(dataId, defaultVal){
return dataId? $('#' + dataId): defaultVal;
}
that.click(function(e) {
wasClicked = true;
var self = $(this);
docToggle = self.hasClass('no-doc-toggle')? true: false;
toggleEle = getEle(self.attr('data-open-toggle-id'), self);
closeEle = getEle(self.attr('data-close-toggle-id'), false);
console.log(toggleEle);
that.not(self).map(function(curVal){
rmClassOf = getEle($(this).attr('data-open-toggle-id'), $(this));
rmClassOf.removeClass(className);
});
if (toggleEle.is("." + className + ".no-self-toggle")) {
return;
}
// console.log('Before: ' + toggleEle.hasClass(className) + toggleEle.attr('class'));
toggleEle.toggleClass(className);
// console.log('After: ' + toggleEle.hasClass(className) + toggleEle.attr('class'));
});
$(document).click(function(e) {
e.stopPropagation();
// var isCloseEle = closeEle && ($(e.target).attr('id') === closeEle || false);
if (!wasClicked) {
toggleEle.removeClass(className);
}
wasClicked = false;
closeEle = false;
});
};
})(window.jQuery || window.Zepto);