forked from abitgone/jQuery-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabitgone-classtoggle.js
314 lines (258 loc) · 13.7 KB
/
abitgone-classtoggle.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
abitgone-classtoggle.js
=======================
ClassToggle toggles, on a target element, a single class on and off or between two classes.
Usage
-----
As a bare minimum, your trigger element must have the following attributes:
- **href** (for anchors) or **data-classtoggle-target**
This specifies the target element
- **data-classtoggle-class**
The class, or comma-separated classes, to be toggled, on and off, by the trigger
Additionally, you may specify the following additional class names:
- **data-classtoggle-altclass**
The class you'd like to alternate with the main class. For example, data-classtoggle-class="Red" and
data-classtoggle-altclass="Green" will alternate the .Red and .Green classes each time the trigger element
is activated.
- **data-classtoggle-trigger-activeclass**
Specifies a class to be added to the trigger when it activates the ClassToggle.
- **data-classtoggle-trigger-selector**
Specifies a jQuery selector which data-classtoggle-trigger-activeclass will be toggled on when the trigger
elements are activated. Useful if you have more than one trigger which could trigger the target element.
For more information on how to use this plugin and for live examples, visit
http://abitgone.github.com/jQuery-Plugins/ClassToggle
License
-------
Author: Anthony Williams
Web: http://abitgone.github.com/jQuery-Plugins/ClassToggle
Copyright (c) 2012 Anthony Williams
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of
the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
!function (jQuery) {
// ClassToggle Public Class Definition
var ClassToggle = function(element, options) {
this.element = element;
this.$element = $(element);
this.options = $.extend({}, $.fn.classToggle.defaults, options);
if (this.options.parent) {
this.$parent = $(this.options.parent);
}
this.options.classToggle && this.classToggle();
}
ClassToggle.prototype = {
constructor: ClassToggle,
classToggle: function() {
var $trigger = $(this.options.trigger),
$triggerTarget = $(this.options.triggerTarget);
var tcClass = this.options.classtoggleClass,
tcClassAlt = this.options.classtoggleAltclass,
tcTriggerClass = this.options.classtoggleTriggerActiveclass,
tcTriggerSelector = this.options.classtoggleTriggerSelector,
tcTarget = this.options.classtoggleTarget,
$tcTarget;
// ClassToggle behaves differently depending on the type of element that it is applied to
switch (this.element.nodeName.toLowerCase()) {
case "input":
switch (this.$element.attr("type").toLowerCase()) {
case "radio":
this.options.tcMode = "radio";
break;
case "checkbox":
this.options.tcMode = "checkbox";
break;
default:
this.options.tcMode = "input";
break;
}
break;
case "select":
this.options.tcMode = "select";
break;
default:
this.options.tcMode = "";
break;
}
// We only need to obtain target information if we're not in "select" mode
if (this.options.tcMode != "select") {
// If there's no target and a href exists, use that instead
if (!tcTarget) {
tcTarget = $trigger.attr('href') == undefined ? $trigger.attr('href') : this.options.trigger.href;
tcTarget = tcTarget && tcTarget.replace(/.*(?=#[^\s]+$)/, ''); // Strip for IE7
}
if (tcTarget == undefined) return;
$tcTarget = $(tcTarget);
}
if (tcClass == undefined || $tcTarget == undefined && this.options.tcMode != "select") return;
tcClass = tcClass.split(',');
var targetMain = false;
for (var i=0;i<tcClass.length && !targetMain; i++) {
targetMain = $tcTarget && $tcTarget.hasClass(tcClass[i]);
}
var targetAlt = tcClassAlt == undefined ? false : $tcTarget.hasClass(tcClassAlt);
var triggerNode = $trigger[0];
var tcOptions = {
"triggerNode": triggerNode,
"$tcTarget": $tcTarget,
"$trigger": $trigger,
"tcClass": tcClass,
"tcClassAlt": tcClassAlt,
"tcTriggerClass": tcTriggerClass,
"tcTriggerSelector": tcTriggerSelector,
"targetMain": targetMain,
"targetAlt": targetAlt,
"tcThis": this
};
switch (this.options.tcMode) {
case "radio":
this.toggleClassesFromInput(tcOptions, true);
break;
case "checkbox":
this.toggleClassesFromInput(tcOptions, false);
break;
case "input":
this.toggleClassesFromElement(tcOptions, true);
break;
case "select":
this.toggleClassesFromSelect(tcOptions, true);
break;
default:
this.toggleClassesFromElement(tcOptions, false);
break;
}
},
toggleClassesFromElement: function(tcOptions, isInputElement) {
// Normal behaviour -- this will just indiscriminately toggle classes on a per-click basis, though for input
// elements, toggling a class on the trigger is not yet available (I haven't had a chance to test it well
// enough yet to include it right now).
if (tcOptions.tcClassAlt == undefined || isInputElement) {
tcOptions.tcThis.toggleClasses(tcOptions.$tcTarget, tcOptions.tcClass);
} else {
if ((tcOptions.targetMain && tcOptions.targetAlt) || (!tcOptions.targetMain && !tcOptions.targetAlt)) {
tcOptions.$tcTarget.toggleClass(tcOptions.tcClassAlt);
} else {
tcOptions.$tcTarget.toggleClass(tcOptions.tcClassAlt);
tcOptions.tcThis.toggleClasses(tcOptions.$tcTarget, tcOptions.tcClass);
}
}
// If there is no trigger class, or if the trigger is an input element, don't bother with the triggers
if (tcOptions.tcTriggerClass == undefined || isInputElement) return;
var $tcTriggers;
if (tcOptions.tcTriggerSelector == undefined) {
$tcTriggers = tcOptions.$trigger;
} else {
$tcTriggers = $(tcOptions.tcTriggerSelector);
}
var triggerClass = tcOptions.tcTriggerClass.split(",");
tcOptions.tcThis.toggleClasses($tcTriggers, triggerClass);
},
toggleClassRegex: /([\+-]{2})?([^\s,$]+)/g,
toggleClassesFromInput: function(tcOptions, isRadioButton) {
// Checkbox behaviour -- this is different to the normal behaviour and attempts to modify the classes that
// are toggled when clicked. The class list to toggle - tcClassList - will be rewritten as follows:
//
// element.checked | original class name | modified class name
// =================|=======================|==============================================
// false (default) | class-name | --class-name (thus explicitly removing it)
// false (default) | ++class-name | --class-name (thus explicitly removing it)
// false (default) | --class-name | ++class-name (thus explicitly adding it)
// -----------------|-----------------------|----------------------------------------------
// true | class-name | ++class-name (thus explicitly adding it)
// true | ++class-name | ++class-name (no change)
// true | --class-name | --class-name (thus explicitly removing it)
tcOptions.tcClassOriginal = tcOptions.tcClass;
tcOptions.tcClass = tcOptions.tcClass.join(",").replace(this.toggleClassRegex, tcOptions.triggerNode.checked ? this.toggleClassEvaluator_Checked : this.toggleClassEvaluator_Unchecked).split(",");
this.toggleClassesFromElement(tcOptions);
if (isRadioButton) {
var $tcRadioButtons = $("input[type=radio][name=" + tcOptions.triggerNode.name + "]").not($(tcOptions.triggerNode)),
tcOtherInputClass = tcOptions.tcClassOriginal.join(",").replace(this.toggleClassRegex, tcOptions.triggerNode.checked ? this.toggleClassEvaluator_Unchecked : this.toggleClassEvaluator_Checked).split(",");
for (var i = 0; i < $tcRadioButtons.length; i++) {
tcOtherTargets = $($tcRadioButtons[i]).attr("data-classtoggle-target");
this.toggleClasses($(tcOtherTargets), tcOtherInputClass);
};
}
},
toggleClassesFromSelect: function(tcOptions) {
var $allOptions = this.$element.find("option[data-classtoggle-target]");
for (var i = 0; i < $allOptions.length; i++) {
var selectOption = $allOptions[i];
var $selectOption = $(selectOption);
var optionClass = $selectOption.attr("data-classtoggle-class");
optionClass = (optionClass && optionClass.split(",")) || tcOptions.tcClass;
optionClass = optionClass.join(",").replace(this.toggleClassRegex, selectOption.selected ? this.toggleClassEvaluator_Checked : this.toggleClassEvaluator_Unchecked).split(",");
this.toggleClasses($($selectOption.attr("data-classtoggle-target")), optionClass);
}
},
toggleClassEvaluator_Checked: function(match, p1, p2) {
if (p1 == "--") {
return "--" + p2;
}
else {
return "++" + p2;
}
},
toggleClassEvaluator_Unchecked: function(match, p1, p2) {
if (p1 == "--") {
return "++" + p2;
}
else {
return "--" + p2;
}
},
toggleClasses: function($element, tcClassList) {
for (var i=0;i<tcClassList.length;i++) {
var match = tcClassList[i].match(/([-+]{2})?(\S+)/);
switch(match[1]) {
case '--':
$element.removeClass(match[2]);
break;
case '++':
$element.addClass(match[2]);
break;
default:
$element.toggleClass(match[2]);
break;
}
}
}
}
// ClassToggle Plugin Definition
$.fn.classToggle = function (option) {
this.each(function () {
// $this is the jQuery wrapped link that is clicked
var $this = $(this),
data = $this.data('classToggle'),
options = typeof option == 'object' && option;
if (!data) $this.data('classToggle', (data = new ClassToggle(this, options)));
if (typeof option == 'string') data[option]();
});
}
$.fn.classToggle.defaults = {
classToggle: true
};
$.fn.classToggle.Constructor = ClassToggle;
// ClassToggle Data-Api
$(function () {
$('body').on('click.classtoggle.data-api', '[data-classtoggle-class]', function (e) {
var $this = $(this),
href,
target = $this.attr('data-classtoggle-target')
|| e.preventDefault()
|| (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, ''),
option = $this.data('classToggle') ? 'classToggle' : $this.data();
option.trigger = e.target;
option.triggerTarget = target;
$(this).classToggle(option);
if ($this.attr('data-classtoggle-target') && $this.attr('href')) e.preventDefault();
});
})
}(window.jQuery);