Project

General

Profile

Download (4.39 KB) Statistics
| Branch: | Revision:
1
define([
2
"dojo/_base/array",
3
"dojo/_base/connect",
4
"dojo/_base/lang",
5
"dojo/_base/declare",
6
"dojo/on",
7
"dijit/Tree",
8
"dijit/form/CheckBox",
9
"fileTree/MixedCheckBox"],
10
function(array, connect, lang, declare, on, Tree, CheckBox, MixedCheckBox){
11

    
12
var _TreeNode = declare("fileTree._TreeNode", dijit._TreeNode, {
13

    
14
    _checkbox: null,
15

    
16
    _createCheckbox: function() {
17
        this._checkbox = new MixedCheckBox();
18
        this._checkbox.placeAt(this.expandoNode, "after");
19
        this._mixed = false;
20
        on(this._checkbox, "change", lang.hitch(this, this._onChange));
21

    
22
        // check lazy-loaded children of checked nodes
23
        connect.connect(this, "setChildItems", this, this._updateChildren);
24
    },
25

    
26
    postCreate: function() {
27
        this._createCheckbox();
28
        this.inherited(arguments);
29
    },
30

    
31
    _onChange: function(value){
32
        this._checkbox.value = value;
33
        value ? this.tree.onNodeChecked(this.item, this) : this.tree.onNodeUnchecked(this.item, this);
34
        this._updateParents();
35
        this._updateChildren();
36
    },
37

    
38
    _updateChildren: function() {
39
        // summary:
40
        //    Updates all children to the same checked value as this node.
41
        array.forEach(this.getChildren(), dojo.hitch(this, function(child, idx){
42
            child._checkbox.set("value", this._checkbox.get("value"));
43
            child._updateChildren();
44
        }));
45
    },
46

    
47
    _updateParents: function(){
48
        // summary:
49
        //     Updates the parents. Parents are checked if all children are
50
        //     checked otherwise unchecked.
51
        var parent = this.getParent();
52

    
53
        // in dojo 1.7, getParent just walks up the tree.
54
        // therefore check if parent is a tree node and not
55
        // just the tree isself.
56
        if(parent && parent.isTreeNode){
57
            var hasChecked = false;
58
            var hasUnchecked = false;
59
            var hasMixed = false;
60

    
61
            array.forEach(parent.getChildren(), function(child, idx){
62
                switch(child.getState()){
63
                    case "mixed":
64
                        hasMixed = true;
65
                        break;
66
                    case "checked":
67
                        hasChecked = true;
68
                        break;
69
                    case "unchecked":
70
                       hasUnchecked = true;
71
                       break;
72
                }
73
                if(hasMixed || (hasChecked && hasUnchecked)){
74
                    return false; // skip checking the rest
75
                }
76
            });
77

    
78
            parent._mixed = hasMixed || (hasChecked && hasUnchecked);
79
            parent._checkbox.state = parent._mixed ? "Mixed" : null;
80

    
81
            // using _set to bypass _onChange
82
            parent._checkbox._set("checked", parent._mixed ? false : this._checkbox.get("checked"));
83
            parent._checkbox._setStateClass();
84
            parent._updateParents();
85
        }
86
    },
87

    
88
    getState: function(){
89
        // summary:
90
        //     Get the current state of this node
91
        // returns:
92
        //     The state, checked, unchecked, or mixed.
93
        if(this._mixed){
94
            return 'mixed';
95
        }
96
        else{
97
            return this._checkbox.checked ? 'checked' : 'unchecked';
98
        }
99
    }
100
});
101

    
102
return declare("fileTree.Tree", Tree, {
103

    
104
    _createTreeNode: function( args ) {
105
        return new _TreeNode(args);
106
    },
107

    
108
    getChecked: function(){
109

    
110
        function _getChecked(/*fileTree._TreeNode*/node, /*Array*/ret){
111
            var state = node.getState();
112

    
113
            if(state === 'mixed'){
114
                var childrenLen = node.item.children.length;
115
                for(var i = 0; i < childrenLen; i++){
116

    
117
                    var child = node.item.children[i];
118
                    var nodes = tree._itemNodesMap[tree.model.getIdentity(child)];
119
                    var nodesLen = nodes.length;
120
                    for(var j = 0; j < nodesLen; j++){
121
                        _getChecked(nodes[j], ret);
122
                    }
123
                }
124
            }
125
            else if(state === 'checked'){
126
                ret.push(tree.model.getIdentity(node.item));
127
            }
128
            else{}
129
        }
130

    
131
        var checked = [];
132
        var tree = this;
133
        _getChecked(tree.rootNode, checked);
134
        return checked;
135
    },
136

    
137
    onNodeChecked: function(/*dojo.data.Item*/ storeItem, /*treeNode*/ nodeWidget){
138
    },
139

    
140
    onNodeUnchecked: function(/*dojo.data.Item*/ storeItem, /* treeNode */ nodeWidget){
141
    }
142
});
143

    
144
});
145

    
(4-4/5)