aboutsummaryrefslogtreecommitdiffstats
path: root/app/assets/javascripts/banners/jquery.tristate.js
blob: 2d2903dcfd0b80c36509ef786972a093d4c558f3 (plain) (blame)
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
/**********************************************************************************
 **
 **              jQuery Tristate Checkbox Plugin
 **              version: 1.0
 **
 **              Dual licensed under the MIT and GPL licenses:
 **              http://www.opensource.org/licenses/mit-license.php
 **              http://www.gnu.org/licenses/gpl.html
 **
 **              author: Jeff Leombruno
 **              creation date: 09.20.2011
 **              dependencies: jQuery v1.6 or higher
 **
 **              This file contains the functionality for implementing 3 state checkboxes.
 **              Inspired by:
 **                   http://code.google.com/p/jquery-tristate-checkbox/
 **                   http://css-tricks.com/13467-indeterminate-checkboxes/
 **
 **********************************************************************************/

(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();

                        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).each( function() {
                        pub.checkSiblings($(this).parent());
                    });
                };

                pub.checkSiblings = function(el) {
                    var parent = el.parent().parent();
                    var all = true;

                    el.siblings().each(function() {
                        return all = ($(this).children('input[type="checkbox"]').prop("checked") === config.checked);
                    });

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