~/WalkThru/CascadeCheckmarks.js

JavaScript
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
function CascadeCheckmarks(e)
{
    // obj gives us the node on which check or uncheck operation has performed
    var element = e.srcElement || e.target; 
    var parentState = true; //default true;
    
    //checking whether obj consists of checkbox to avoid exception
    if (isCheckBox(element))
    {
        var checkedState = element.checked;
        
        //work our way back to the parent <li> element 
        while (!isListItem(element))
            element = element.parentNode;
        recurseThroughChildren(element.firstChild, checkedState); //set child nodes to checkedState
        
        //from the <li> work our way back to the parent <ul> element 
        while (!isList(element))
            element = element.parentNode;
        recurseThroughParents(element, parentState); //set parent nodes accordingly
    }
}

function recurseThroughChildren(element, checkedState)
{
    var chkbox;
    while(!isNull(element))
    {
        //mark the child checkbox the same as the parent
        chkbox = getCheckBox(element);
        if (!isNull(chkbox))
            chkbox.checked = checkedState;
        //recurse through children of current element
        recurseThroughChildren(element.firstChild, checkedState);
        element = element.nextSibling;
    }
}

function recurseThroughParents(parent, parentState)
{
    //if it is an unorderd list, process children
    if(isList(parent))
    { 
        //get all child elements that are list items
        var item = parent.getElementsByTagName('LI')[0];
        var chkbox;
        
        //foreach list item...
        while(!isNull(item))
        {
            //whle the children are not check boxes (only 1 checkbox per list item)
            chkbox = getCheckBox(item.firstChild)
            if ((!isNull(chkbox)) && (!chkbox.checked))
                parentState = false;
            item = item.nextSibling;
        }
        if (!isDiv(parent.parentNode))
        {
            var parentCheckBox = getCheckBox(parent.parentNode);
            if (!isNull(parentCheckBox))
                parentCheckBox.checked = parentState;
        }
    }
    if (!isDiv(parent)) //if it is not a div tag countinue to the next parent
        recurseThroughParents(parent.parentNode, parentState);
}

//return the first checkbox found
function getCheckBox(obj)
{
    var ret = null;
    while (!isNull(obj))
    {
        if (isCheckBox(obj))
            ret = obj;
        else if (obj.childNodes.length > 0)
            ret = getCheckBox(obj.firstChild);
        if (isCheckBox(ret))
            break;
        obj = obj.nextSibling;
    }
    return ret;
}


//helper functions
function isDiv(obj)
{
    if (isNull(obj))
        return false;
    return (obj.tagName == 'DIV');
}

function isCheckBox(obj)
{
    if (isNull(obj))
        return false;
    return (obj.tagName == 'INPUT' && obj.type == 'checkbox');
}

function isList(obj)
{
    if (isNull(obj))
        return false;
    return (obj.tagName == 'UL');
}

function isListItem(obj)
{
    if (isNull(obj))
        return false;
    return (obj.tagName == 'LI');
}

function isNull(obj)
{
    return (obj == null);
}