-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickable.js
189 lines (173 loc) · 7.12 KB
/
clickable.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
/* CLICKABLE - Plugin to make elements on a page respond to click events, including targeting other elements and running a callback
* Docs - http://www.adamanthony.co.uk/resources/clickablejs/
* Version 1.2 - amended to work with swipe
* Version 1.3 - added event binding on init if no event object is passed in
* Version 1.4 - added an off-class and support for reaping elements from other instances of Clickable
* Version 1.5 - added support for multiple classes
* Version 1.6.0 - made .clickable() look at element data for settings
* Version 1.6.1 - added a "select" case for reaping, whereby an jQuery selector can be passed in
* Version 1.6.2 - Made reap function without reaping itself
*/
var Clickable = function (el, settings, event, callback) {
this.elem = $(el);
settings = settings || {};
this.config = $.extend({}, this.defaults, settings);
this.callback = callback || settings.callback || false;
var self = this;
typeof (event) != "undefined" && this.clicker(event) || this.elem.unbind('click.Clickable').on('click.Clickable', function (e) {
self.elem.clickable({}, self.callback, e);
});
this.config.bindtoswipe && this.bindToSwipe();
};
Clickable.prototype = {
defaults: {
classname: "clicked",
offclassname: false,
target: false,
bindtoswipe: false,
reap: false,
contain: false,
containtotarget: false,
containandexit: true,
containmaxwidth:0,
swipeEscape: ".no-swipe",
swipeOnEvent: 'swipeleft',
swipeOffEvent: 'swiperight',
reapingClass: 'reaping',
swipeElement: '.touch body',
callback: false
},
bindToSwipe: function () {
var self = this;
$(this.config.swipeElement).on(self.config.swipeOnEvent, function (e) {
var noSwipe = $(e.target).closest(self.config.swipeEscape);
noSwipe.length == 0 && self.turnOn(e);
});
$(this.config.swipeElement).on(self.config.swipeOffEvent, function (e) {
var noSwipe = $(e.target).closest(self.config.swipeEscape);
noSwipe.length == 0 && self.turnOff(e);
});
},
clicker: function (event) {
var self = this;
if (this.config.contain && typeof (event) == "object" && $(event.target).is(this.elem) == this.config.containtotarget) {
if (!this.config.containmaxwidth || ($(window).width() < this.config.containmaxwidth)) {
event.preventDefault(), event.stopPropagation();
if (this.config.containandexit) {
return false;
}
}
}
typeof (event) == "string" && this[event]() || this.controller();
},
controller: function () {
this.config.reap && this.reaper();
if (this.elem.hasClass(this.config.classname)) {
this.turnOff();
}
else {
this.turnOn();
}
this.callback && this.callback(this);
$('.' + this.config.reapingClass).removeClass(this.config.reapingClass);
},
turnOn: function () {
if (typeof this.config.offclassname == "string") {
this.elem.removeClass(this.config.offclassname);
this.config.target && $(this.config.target).removeClass(this.config.offclassname);
}
this.config.target && $(this.config.target).addClass(this.config.classname);
this.elem.addClass(this.config.classname);
this.status = true;
this.elem.trigger('clickabled');
return true;
},
turnOff: function () {
this.elem.removeClass(this.config.classname);
this.config.target && $(this.config.target).removeClass(this.config.classname);
if (typeof this.config.offclassname == "string") {
this.elem.addClass(this.config.offclassname);
this.config.target && $(this.config.target).addClass(this.config.offclassname);
}
this.status = false;
this.elem.trigger('unclickabled');
return true;
},
reaper: function () {
var self = this;
var reapees = false;
var reap = this.config.reap.split(':');
var classes = this.config.classname.split(" ");
classes = classes.join(".");
switch (reap[0]) {
case "siblings":
reapees = this.elem.siblings('.' + classes);
reapees = reapees.add(reapees.children('.' + classes));
break;
case "parents":
reapees = this.elem.parent().siblings('.' + classes);
break;
case "family":
reapees = this.elem.parent().siblings('.' + classes);
reapees = reapees.add(reapees.children('.' + classes));
break;
case "select":
var selectors = reap[1].split(';');
reapees = this.elem;
$.each(selectors, function (i, selector) {
var arr = selector.trim().split('=');
var sel = arr.shift()
reapees = reapees[sel](arr.join(' '));
});
console.log(reapees);
break;
case "all":
reapees = $('.' + classes);
break;
case "others":
$(reap[1]).each(function () {
if (this !== self.elem[0]) {
var c = $(this).data('clickable');
c && c.turnOff();
}
});
break;
default:
reapees = $(this.config.reap).filter('.' + classes);
}
if (reapees) {
console.log(reapees)
reapees.first().parent().addClass(self.config.reapingClass);
reapees.each(function () {
if (this !== self.elem[0]) {
$(this).removeClass(self.config.classname);
}
});
}
},
destroy: function () {
this.turnOff();
$('.' + this.config.reapingClass).removeClass(this.config.reapingClass);
$(this.config.swipeElement).unbind(self.config.swipeOnEvent);
$(this.config.swipeElement).unbind(self.config.swipeOffEvent);
this.elem.unbind('click.Clickable');
this.elem.data('clickable', false);
}
};
$.fn.clickable = function (settings, callback, event) {
return this.each(function () {
typeof settings == "function" && (callback = settings) && (settings = {});
typeof settings == "string" && (event = settings) && (settings = {});
typeof callback == "string" && (event = callback) && (callback = false);
var el = $(this),
c = el.data('clickable'),
route = c ? 'clicker' : el.data();
c ? c.clicker(event) : el.data('clickable', c = new Clickable(this, $.extend({}, typeof settings == "object" && settings || {}, route), event, callback));
});
};
$('[data-plugin="clickable"]').on('click.Clickable', function (event) {
var el = $(this),
d = el.data('clickable'),
e = d ? 'clicker' : el.data();
typeof e == 'string' ? d[e](event) : el.clickable(e, false, event);
});