Skip to content
This repository has been archived by the owner on Mar 21, 2018. It is now read-only.

Commit

Permalink
Add support for reverse-toggling based on check status
Browse files Browse the repository at this point in the history
In some cases, you want to remove a class when a checkbox or radiobutton
is checked. This is now supported by using the explicit -- remove prefix
on the class.
  • Loading branch information
abitgone committed Dec 20, 2013
1 parent a15b3fe commit a384a53
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ClassToggle/abitgone-classtoggle-min.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
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,o=$(this.options.trigger),u=$(this.options.triggerTarget),a=this.options.classtoggleClass,f=this.options.classtoggleAltclass,l=this.options.classtoggleTriggerActiveclass,c=this.options.classtoggleTriggerSelector,h=this.options.classtoggleTarget;if(h==undefined){h=o.attr("href")==undefined?o.attr("href"):this.options.trigger.href;h=h.replace(/.*(?=#[^\s]+$)/,"")}if(h==undefined)return;e=$(h);if(a==undefined||e==undefined)return;a=a.split(",");t=!1;for(n=0;n<a.length&&!t;n++)t=e.hasClass(a[n]);r=f==undefined?!1:e.hasClass(f);i=o[0];s={triggerNode:i,$tcTarget:e,$trigger:o,tcClass:a,tcClassAlt:f,tcTriggerClass:l,tcTriggerSelector:c,targetMain:t,targetAlt:r,tcThis:this};if(i.nodeName.toLowerCase()=="input")switch(i.type.toLowerCase()){case"radio":this.toggleClassesFromInput(s,!0);break;case"checkbox":this.toggleClassesFromInput(s,!1);break;default:this.toggleClassesFromElement(s,!0)}else this.toggleClassesFromElement(s,!1)},toggleClassesFromElement:function(e,t){var n;if(e.tcClassAlt==undefined||t)e.tcThis.toggleClasses(e.$tcTarget,e.tcClass);else if(e.targetMain&&e.targetAlt||!e.targetMain&&!e.targetAlt)e.$tcTarget.toggleClass(e.tcClassAlt);else{e.$tcTarget.toggleClass(e.tcClassAlt);e.tcThis.toggleClasses(e.$tcTarget,e.tcClass)}if(e.tcTriggerClass==undefined||t)return;e.tcTriggerSelector==undefined?n=e.$trigger:n=$(e.tcTriggerSelector);n.toggleClass(e.tcTriggerClass)},toggleClassesFromInput:function(e,t){var n,r,i,s=/([+-]{2})?([^\s,$]+)/g;e.tcClassOriginal=e.tcClass;e.tcClass=e.tcClass.join(",").replace(s,(e.triggerNode.checked?"++":"--")+"$2").split(",");this.toggleClassesFromElement(e);if(t){n=$("input[type=radio][name="+e.triggerNode.name+"]").not($(e.triggerNode)),r=e.tcClassOriginal.join(",").replace(s,(e.triggerNode.checked?"--":"++")+"$2").split(",");for(i=0;i<n.length;i++){tcOtherTargets=$(n[i]).attr("data-classtoggle-target");this.toggleClasses($(tcOtherTargets),r)}}},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);
*/!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,o=$(this.options.trigger),u=$(this.options.triggerTarget),a=this.options.classtoggleClass,f=this.options.classtoggleAltclass,l=this.options.classtoggleTriggerActiveclass,c=this.options.classtoggleTriggerSelector,h=this.options.classtoggleTarget;if(h==undefined){h=o.attr("href")==undefined?o.attr("href"):this.options.trigger.href;h=h.replace(/.*(?=#[^\s]+$)/,"")}if(h==undefined)return;e=$(h);if(a==undefined||e==undefined)return;a=a.split(",");t=!1;for(n=0;n<a.length&&!t;n++)t=e.hasClass(a[n]);r=f==undefined?!1:e.hasClass(f);i=o[0];s={triggerNode:i,$tcTarget:e,$trigger:o,tcClass:a,tcClassAlt:f,tcTriggerClass:l,tcTriggerSelector:c,targetMain:t,targetAlt:r,tcThis:this};if(i.nodeName.toLowerCase()=="input")switch(i.type.toLowerCase()){case"radio":this.toggleClassesFromInput(s,!0);break;case"checkbox":this.toggleClassesFromInput(s,!1);break;default:this.toggleClassesFromElement(s,!0)}else this.toggleClassesFromElement(s,!1)},toggleClassesFromElement:function(e,t){var n;if(e.tcClassAlt==undefined||t)e.tcThis.toggleClasses(e.$tcTarget,e.tcClass);else if(e.targetMain&&e.targetAlt||!e.targetMain&&!e.targetAlt)e.$tcTarget.toggleClass(e.tcClassAlt);else{e.$tcTarget.toggleClass(e.tcClassAlt);e.tcThis.toggleClasses(e.$tcTarget,e.tcClass)}if(e.tcTriggerClass==undefined||t)return;e.tcTriggerSelector==undefined?n=e.$trigger:n=$(e.tcTriggerSelector);n.toggleClass(e.tcTriggerClass)},toggleClassesFromInput:function(e,t){var n,r,i,s=/([\+-]{2})?([^\s,$]+)/g;e.tcClassOriginal=e.tcClass;e.tcClass=e.tcClass.join(",").replace(s,e.triggerNode.checked?this.toggleClassEvaluator_Checked:this.toggleClassEvaluator_Unchecked).split(",");this.toggleClassesFromElement(e);if(t){n=$("input[type=radio][name="+e.triggerNode.name+"]").not($(e.triggerNode)),r=e.tcClassOriginal.join(",").replace(s,e.triggerNode.checked?this.toggleClassEvaluator_Unchecked:this.toggleClassEvaluator_Checked).split(",");for(i=0;i<n.length;i++){tcOtherTargets=$(n[i]).attr("data-classtoggle-target");this.toggleClasses($(tcOtherTargets),r)}}},toggleClassEvaluator_Checked:function(e,t,n){return t=="--"?"--"+n:"++"+n},toggleClassEvaluator_Unchecked:function(e,t,n){return t=="--"?"++"+n:"--"+n},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);
28 changes: 22 additions & 6 deletions ClassToggle/abitgone-classtoggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,21 @@
// =================|=======================|==============================================
// 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 (no change)
// 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 adding it)
// true | --class-name | --class-name (thus explicitly removing it)

var tcBaseRegex = /([+-]{2})?([^\s,$]+)/g;
var tcRegex = /([\+-]{2})?([^\s,$]+)/g;

tcOptions.tcClassOriginal = tcOptions.tcClass;
tcOptions.tcClass = tcOptions.tcClass.join(",").replace(tcBaseRegex, (tcOptions.triggerNode.checked ? "++" : "--") + "$2").split(",");
tcOptions.tcClass = tcOptions.tcClass.join(",").replace(tcRegex, 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(tcBaseRegex, (tcOptions.triggerNode.checked ? "--" : "++") + "$2").split(",");
tcOtherInputClass = tcOptions.tcClassOriginal.join(",").replace(tcRegex, 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");
Expand All @@ -199,6 +199,22 @@
}

},
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++) {
Expand Down

0 comments on commit a384a53

Please sign in to comment.