Skip to content

Commit

Permalink
ClassToggle: Add ability to explicitly add/remove classes
Browse files Browse the repository at this point in the history
ClassToggle can now explicitly add or remove classnames from target
elements. To do this, simply prepend -- or ++ to explicitly
remove or add class names to the target element(s). For example, to
toggle one class whilst explicitly removing another, use:

data-classtoggle-class="toggle-this,--remove-this"

To remove one class whilst adding another, use:

data-classtoggle-cass="++add-this,--remove-this"
  • Loading branch information
abitgone committed May 1, 2013
1 parent b4736f1 commit 7366704
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
25 changes: 12 additions & 13 deletions ClassToggle/abitgone-classtoggle-min.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,46 @@
- **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. Unlike multiple classes, this will check for the existence of data-classtoggle-class and
data-classtoggle-altclass before toggling the main class or alternate class.
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(){var e=function(e,t){this.$element=$(e);this.options=$.extend({},$.fn.classToggle.defaults,t);this.options.parent&&(this.$parent=$(this.options.parent));this.options.classToggle&&this.classToggle()};e.prototype={constructor:e,classToggle:function(){var e,t,n,r,i,s=$(this.options.trigger),o=$(this.options.triggerTarget),u=this.options.classtoggleClass,a=this.options.classtoggleAltclass,f=this.options.classtoggleTriggerActiveclass,l=this.options.classtoggleTriggerSelector,c=this.options.classtoggleTarget;if(c==undefined){c=s.attr("href")==undefined?s.attr("href"):this.options.trigger.href;c=c.replace(/.*(?=#[^\s]+$)/,"")}if(c==undefined)return;e=$(c);if(u==undefined||e==undefined)return;u=u.split(",");t=!1;for(n=0;n<u.length&&!t;n++)t=e.hasClass(u[n]);r=a==undefined?!1:e.hasClass(a);if(a==undefined)for(n=0;n<u.length;n++)e.toggleClass(u[n]);else if(t&&r||!t&&!r)e.toggleClass(a);else{e.toggleClass(a);for(n=0;n<u.length;n++)e.toggleClass(u[n])}if(f==undefined)return;l==undefined?i=s:i=$(l);i.toggleClass(f)}};$.fn.classToggle=function(t){this.each(function(){var n=$(this),r=n.data("classToggle"),i=typeof t=="object"&&t;r||n.data("classToggle",r=new e(this,i));typeof t=="string"&&r[t]()})};$.fn.classToggle.defaults={classToggle:!0};$.fn.classToggle.Constructor=e;$(function(){$("body").on("click.classtoggle.data-api","[data-classtoggle-class]",function(e){var t,n=$(this),r=n.attr("data-classtoggle-target")||e.preventDefault()||(t=n.attr("href"))&&t.replace(/.*(?=#[^\s]+$)/,""),i=n.data("classToggle")?"classToggle":n.data();i.trigger=e.target;i.triggerTarget=r;$(this).classToggle(i);n.attr("data-classtoggle-target")&&n.attr("href")&&e.preventDefault()})})}(window.jQuery);
*/!function(){var e=function(e,t){this.$element=$(e);this.options=$.extend({},$.fn.classToggle.defaults,t);this.options.parent&&(this.$parent=$(this.options.parent));this.options.classToggle&&this.classToggle()};e.prototype={constructor:e,classToggle:function(){var e,t,n,r,i,s=$(this.options.trigger),o=$(this.options.triggerTarget),u=this.options.classtoggleClass,a=this.options.classtoggleAltclass,f=this.options.classtoggleTriggerActiveclass,l=this.options.classtoggleTriggerSelector,c=this.options.classtoggleTarget;if(c==undefined){c=s.attr("href")==undefined?s.attr("href"):this.options.trigger.href;c=c.replace(/.*(?=#[^\s]+$)/,"")}if(c==undefined)return;e=$(c);if(u==undefined||e==undefined)return;u=u.split(",");t=!1;for(n=0;n<u.length&&!t;n++)t=e.hasClass(u[n]);r=a==undefined?!1:e.hasClass(a);if(a==undefined)this.toggleClasses(e,u);else if(t&&r||!t&&!r)e.toggleClass(a);else{e.toggleClass(a);this.toggleClasses(e,u)}if(f==undefined)return;l==undefined?i=s:i=$(l);i.toggleClass(f)},toggleClasses:function(e,t){var n,r;for(n=0;n<t.length;n++){r=t[n].match(/([-+]{2})?(\S+)/);switch(r[1]){case"--":e.removeClass(r[2]);break;case"++":e.addClass(r[2]);break;default:e.toggleClass(r[2])}}}};$.fn.classToggle=function(t){this.each(function(){var n=$(this),r=n.data("classToggle"),i=typeof t=="object"&&t;r||n.data("classToggle",r=new e(this,i));typeof t=="string"&&r[t]()})};$.fn.classToggle.defaults={classToggle:!0};$.fn.classToggle.Constructor=e;$(function(){$("body").on("click.classtoggle.data-api","[data-classtoggle-class]",function(e){var t,n=$(this),r=n.attr("data-classtoggle-target")||e.preventDefault()||(t=n.attr("href"))&&t.replace(/.*(?=#[^\s]+$)/,""),i=n.data("classToggle")?"classToggle":n.data();i.trigger=e.target;i.triggerTarget=r;$(this).classToggle(i);n.attr("data-classtoggle-target")&&n.attr("href")&&e.preventDefault()})})}(window.jQuery);
48 changes: 30 additions & 18 deletions ClassToggle/abitgone-classtoggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,48 @@
- **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. Unlike multiple classes, this will check for the existence of data-classtoggle-class and
data-classtoggle-altclass before toggling the main class or alternate class.
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
Expand Down Expand Up @@ -100,17 +99,13 @@
var targetAlt = tcClassAlt == undefined ? false : $tcTarget.hasClass(tcClassAlt);

if (tcClassAlt == undefined) {
for (var i=0;i<tcClass.length;i++) {
$tcTarget.toggleClass(tcClass[i]);
}
this.toggleClasses($tcTarget, tcClass);
} else {
if ((targetMain && targetAlt) || (!targetMain && !targetAlt)) {
$tcTarget.toggleClass(tcClassAlt);
} else {
$tcTarget.toggleClass(tcClassAlt);
for (var i=0;i<tcClass.length;i++) {
$tcTarget.toggleClass(tcClass[i]);
}
this.toggleClasses($tcTarget, tcClass);
}
}

Expand All @@ -124,6 +119,23 @@
}
$tcTriggers.toggleClass(tcTriggerClass);

},

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;
}
}
}

}
Expand Down

0 comments on commit 7366704

Please sign in to comment.