From 17a5fa58771cb2926647c65cca201d1c5403e976 Mon Sep 17 00:00:00 2001 From: Isaiah Fischer Date: Wed, 20 Mar 2013 11:45:10 -0300 Subject: [PATCH] Allow input to have wrappers Such that it doesn't need to be a direct descendant of the LI --- README | 5 +- jquery.tristate.js | 140 ++++++++++++++++++++++------------------- jquery.tristate.min.js | 2 +- 3 files changed, 79 insertions(+), 68 deletions(-) diff --git a/README b/README index 4f7e9be..128ecb9 100644 --- a/README +++ b/README @@ -3,5 +3,8 @@ jquery.tristate.js is a jquery plugin for adding "tristate" indeterminate functi To use it, simply call it on your parent list: $('ul.tristate').tristate(); +By default, the inputs must be direct descendants of the list elements. You may optionally pass in an array of wrappers that represents the tree of elements the inputs are nested within, e.g.: +$('ul.tristate').tristate({wrappers:['span.classname','label']}); + Example available here: -http://jlbruno.github.com/jQuery-Tristate-Checkbox-plugin/ \ No newline at end of file +http://jlbruno.github.com/jQuery-Tristate-Checkbox-plugin/ diff --git a/jquery.tristate.js b/jquery.tristate.js index 8d36682..5eee505 100644 --- a/jquery.tristate.js +++ b/jquery.tristate.js @@ -19,73 +19,81 @@ **********************************************************************************/ (function($){ - $.fn.tristate = function(options){ - - var config = { - selector: $(this).selector, - checked: null, - container: null, - siblings: null - }; - var opts = $.extend(config, options); - - return this.each(function(){ - var obj = $(this); - - var triState = function() { - - var pub = {}; - - pub.init = function(){ - $('input[type="checkbox"]', obj).change(function(e) { - config.checked = $(this).prop("checked"); - config.container = $(this).parent(); - config.siblings = config.container.siblings(); + $.fn.tristate = function(options){ - config.container.find('input[type="checkbox"]').prop({ - indeterminate: false, - checked: config.checked - }); - - pub.checkSiblings(config.container); - }); - // run checkSiblings for every checked checkbox when the page loads - $('input[type=checkbox]:checked', obj).trigger('change'); - }; - - pub.checkSiblings = function(el) { - var parent = el.parent().parent(); - var all = true; + var config = { + selector: $(this).selector, + checked: null, + container: null, + siblings: null, + wrappers: [] + }; + var opts = $.extend(config, options); - el.siblings().each(function() { - return all = ($(this).children('input[type="checkbox"]').prop("checked") === config.checked); - }); + return this.each(function(){ + var obj = $(this); - if (all && config.checked) { - parent.children('input[type="checkbox"]').prop({ - indeterminate: false, - checked: config.checked - }); - pub.checkSiblings(parent); - } else if (all && !config.checked) { - parent.children('input[type="checkbox"]').prop("checked", config.checked); - parent.children('input[type="checkbox"]').prop("indeterminate", (parent.find('input[type="checkbox"]:checked').length > 0)); - pub.checkSiblings(parent); - } else { - el.parents("li").children('input[type="checkbox"]').prop({ - indeterminate: true, - checked: false - }); - } - }; - - return pub; - }(); - - triState.init(); - triState.checkSiblings(obj); - - }); - }; -})(jQuery); + var triState = function() { + + var pub = {}; + + pub.init = function(){ + $('input[type="checkbox"]', obj).change(function(e) { + config.checked = $(this).prop("checked"); + config.container = $(this).closest('li'); + config.siblings = config.container.siblings(); + + config.container.find('input[type="checkbox"]').prop({ + indeterminate: false, + checked: config.checked + }); + + pub.checkSiblings(config.container); + }); + // run checkSiblings for every checked checkbox when the page loads + $('input[type=checkbox]:checked', obj).trigger('change'); + }; + + pub.checkSiblings = function(el) { + var parent = el.parent().parent(); + var all = true; + var childInputs = function(elem) { + var ths = elem; + $.each(config.wrappers, function(ind, wrap) { + ths = ths.children(wrap); + }); + return ths.children('input[type="checkbox"]'); + } + + el.siblings().each(function() { + return all = (childInputs($(this)).prop("checked") === config.checked); + }); + + if (all && config.checked) { + childInputs(parent).prop({ + indeterminate: false, + checked: config.checked + }); + pub.checkSiblings(parent); + } else if (all && !config.checked) { + childInputs(parent).prop("checked", config.checked); + childInputs(parent).prop("indeterminate", (parent.find('input[type="checkbox"]:checked').length > 0)); + pub.checkSiblings(parent); + } else { + childInputs(el.parents("li")).prop({ + indeterminate: true, + checked: false + }); + } + }; + + return pub; + }(); + + triState.init(); + triState.checkSiblings(obj); + + }); + }; +})(jQuery); diff --git a/jquery.tristate.min.js b/jquery.tristate.min.js index 207bd00..cb03aa3 100644 --- a/jquery.tristate.min.js +++ b/jquery.tristate.min.js @@ -1,4 +1,4 @@ /*! jQuery Tristate Checkbox - v1.0.1 - 2012-12-04 * http://jlbruno.github.com/jQuery-Tristate-Checkbox-plugin/ * Copyright (c) 2012 Jeff Leombruno; Licensed MIT, GPLv2 */ -(function(e){e.fn.tristate=function(t){var n={selector:e(this).selector,checked:null,container:null,siblings:null},r=e.extend(n,t);return this.each(function(){var t=e(this),r=function(){var r={};return r.init=function(){e('input[type="checkbox"]',t).change(function(t){n.checked=e(this).prop("checked"),n.container=e(this).parent(),n.siblings=n.container.siblings(),n.container.find('input[type="checkbox"]').prop({indeterminate:!1,checked:n.checked}),r.checkSiblings(n.container)}),e("input[type=checkbox]:checked",t).trigger("change")},r.checkSiblings=function(t){var i=t.parent().parent(),s=!0;t.siblings().each(function(){return s=e(this).children('input[type="checkbox"]').prop("checked")===n.checked}),s&&n.checked?(i.children('input[type="checkbox"]').prop({indeterminate:!1,checked:n.checked}),r.checkSiblings(i)):s&&!n.checked?(i.children('input[type="checkbox"]').prop("checked",n.checked),i.children('input[type="checkbox"]').prop("indeterminate",i.find('input[type="checkbox"]:checked').length>0),r.checkSiblings(i)):t.parents("li").children('input[type="checkbox"]').prop({indeterminate:!0,checked:!1})},r}();r.init(),r.checkSiblings(t)})}})(jQuery); \ No newline at end of file +(function(b){b.fn.tristate=function(h){var a={selector:b(this).selector,checked:null,container:null,siblings:null,wrappers:[]};b.extend(a,h);return this.each(function(){var d=b(this),e={init:function(){b('input[type="checkbox"]',d).change(function(){a.checked=b(this).prop("checked");a.container=b(this).closest("li");a.siblings=a.container.siblings();a.container.find('input[type="checkbox"]').prop({indeterminate:!1,checked:a.checked});e.checkSiblings(a.container)});b("input[type=checkbox]:checked", d).trigger("change")},checkSiblings:function(d){var c=d.parent().parent(),g=!0,f=function(d){var c=d;b.each(a.wrappers,function(a,b){c=c.children(b)});return c.children('input[type="checkbox"]')};d.siblings().each(function(){return g=f(b(this)).prop("checked")===a.checked});g&&a.checked?(f(c).prop({indeterminate:!1,checked:a.checked}),e.checkSiblings(c)):g&&!a.checked?(f(c).prop("checked",a.checked),f(c).prop("indeterminate",0