TreeStateCheckbox

class webix.TreeStateCheckbox()

Treestatecheckbox mixin

References

helpers
extend(), type().
views
tree().

Referenced by

views
treetable().

External references

Official documentation page.

Code

  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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
webix.TreeStateCheckbox = {
    _init_render_tree_state: function(){
        if (this._branch_render_supported){
            var old_render = this.render;
            this.render = function(id,data,mode){
                var updated = old_render.apply(this,arguments);

                if(this._settings.threeState && updated && data != "checkbox")
                    this._setThirdState.apply(this,arguments);
            };
            this._init_render_tree_state=function(){};
        }
    },
    threeState_setter:function(value){
        if (value)
            this._init_render_tree_state();
        return value;
    },
    _setThirdState:function(id){
        var i,leaves,parents,checkedParents,tree;
        parents = [];
        tree = this;

        /*if item was removed*/
        if(id&&!tree.data.pull[id]){
            id = 0;
        }
        /*sets checkbox states*/
        /*if branch or full reloading*/
        if(!id||tree.data.pull[id].$count){
            leaves = this._getAllLeaves(id);
            leaves.sort(function(a,b){
                return tree.data.pull[b].$level - tree.data.pull[a].$level;
            });
            for(i=0;i < leaves.length;i++){
                if(!i||tree.data.pull[leaves[i]].$parent!=tree.data.pull[leaves[i-1]].$parent)
                    parents = parents.concat(tree._setParentThirdState(leaves[i]));
            }
        }
        else{
            /*an item is a leaf */
            parents = parents.concat(tree._setParentThirdState(id));
        }

        checkedParents = {};
        for(i=0;i<parents.length;i++){
            if(!checkedParents[parents[i]]){
                checkedParents[parents[i]] = 1;
                this._setCheckboxIndeterminate(parents[i]);
            }
        }

        tree = null;
    },
    _setCheckboxIndeterminate:function(id){
        var chElem, elem;
        elem = this.getItemNode(id);
        if(elem){
            this.render(id,"checkbox","update");
            /*needed to get the new input obj and to set indeterminate state*/
            if(this.getItem(id).indeterminate){
                elem = this.getItemNode(id);
                chElem = elem.getElementsByTagName("input")[0];
                if(chElem)
                    chElem.indeterminate = this.getItem(id).indeterminate;
            }
        }
    },
    _setParentThirdState:function(itemId){
        //we need to use dynamic function creating
        //jshint -W083:true

        var checked, checkedCount,indeterminate, parentId,result,tree,unsureCount,needrender;
        parentId = this.getParentId(itemId);
        tree = this;
        result = [];
        while(parentId && parentId != "0"){
            unsureCount = 0;
            checkedCount = 0;
            this.data.eachChild(parentId,function(obj){
                if(obj.indeterminate){
                    unsureCount++;
                }
                else if(obj.checked){
                    checkedCount++;
                }
            });

            checked = indeterminate = needrender = false;

            var item = this.getItem(parentId);
            if(checkedCount==item.$count){
                checked = true;
            }
            else if(checkedCount>0||unsureCount>0){
                indeterminate = true;
            }

            //we need to reset indeterminate in any case :(
            if (indeterminate || indeterminate != item.indeterminate)
                needrender = true;
            item.indeterminate = indeterminate;
            if (checked || item.checked != checked)
                needrender = true;
            item.checked = checked;

            if (needrender){
                result.push(parentId);
                parentId = this.getParentId(parentId);
            } else
                parentId = 0;
        }

        return result;
    },
    /*get all checked items in tree*/
    getChecked:function(){
        var result=[];
        var tree = this;
        this.data.eachSubItem(0,function(obj){
            if (tree.isChecked(obj.id))
                result.push(obj.id);
        });
        return result;
    },
    _tree_check_uncheck_3:function(id, mode){
        var item = this.getItem(id);
        if(item){
            if (mode === "")
                mode = !item.checked;
            if(item.checked != mode || item.indeterminate){
                item.checked = mode;
                this._correctThreeState(id);
                var parents = this._setParentThirdState(id);
                if (this._branch_render_supported && parents.length < 5){
                    for (var i=0; i<parents.length; i++)
                        this._setCheckboxIndeterminate(parents[i]);
                } else
                    this.refresh();
                this.callEvent("onItemCheck", [id, mode]);
            }
        }
    },
    /*set checked state for item checkbox*/
    checkItem:function(id){
        this._tree_check_uncheck(id, true);
        this.updateItem(id);
    },
    /*uncheckes an item checkbox*/
    uncheckItem:function(id){
        this._tree_check_uncheck(id, false);
        this.updateItem(id);
    },
    _checkUncheckAll: function(id,mode,all){
        var method = mode?"checkItem":"uncheckItem";
        if(!id)
            id = 0;
        else
            this[method](id);
        if(this._settings.threeState){
            if(!id)
                this.data.eachChild(0,function(item){
                    this[method](item.id);
                },this,all);
        }
        else
            this.data.each(function(item){
                this[method](item.id);
            },this,all,id);

    },
    /*checkes checkboxes of all items in a branch/tree*/
    checkAll: function(id, all){
        this._checkUncheckAll(id,true,all);

    },
    /*uncheckes checkboxes of all items in a branch/tree*/
    uncheckAll: function(id, all){
        this._checkUncheckAll(id,false,all);
    },
    _correctThreeState:function(id){
        var i,leaves,state;
        var item = this.getItem(id);

        item.indeterminate = false;
        state = item.checked;

        this.data.eachSubItem(id, function(child){
            child.indeterminate = false;
            child.checked = state;
        });

        if(this._branch_render_supported && this.isBranchOpen(item.$parent)){ //for tree-render only
            this.render(id,0,"branch");
        }
    },
    /*returns checked state of item checkbox*/
    isChecked:function(id){
        return this.getItem(id).checked;
    },
    /*gets all leaves in a certain branch (in the whole tree if id is not set)*/
    _getAllLeaves:function(parentId){
        var result = [];
        this.data.eachSubItem(parentId, function(obj, branch){
            if (!branch)
                result.push(obj.id);
        });
        return result;
    }
};

if (webix.ui.tree)
    webix.extend(webix.ui.tree, webix.TreeStateCheckbox, true);
webix.type(webix.ui.tree, {
    name:"lineTree",
    css:"webixLineTree",
    icon:function(obj, common){
        var html = "";
        var open = "";
        for (var i=1; i<=obj.$level; i++){
            if (i==obj.$level)
                var open = (obj.$count?(obj.open?'webix_tree_open ':'webix_tree_close '):'webix_tree_none ');

            var icon = this._icon_src(obj, common, i);
            if (icon)
                html+="<div class='"+open+"webix_tree_img webix_tree_"+icon+"'></div>";
        }
        return html;
    },
    _icon_src:function(obj, common, level){
        var lines = common._tree_branch_render_state;
        var tree = webix.TreeRenderStack._obj;

        if (lines === 0 && tree){
            //we are in standalone rendering
            //need to reconstruct rendering state
            var lines_level = obj.$level;
            var branch_id = obj.id;

            lines = [];
            while (lines_level){
                var parent_id = tree.getParentId(branch_id);
                var pbranch = tree.data.branch[parent_id];
                if (pbranch[pbranch.length-1] == branch_id)
                    lines[lines_level] = true;

                branch_id = parent_id;
                lines_level--;
            }

            //store for next round
            common._tree_branch_render_state = lines;
        }
        if (!lines)
            return 0;
        //need to be replaced with image urls
        if (level == obj.$level){
            var mode = 3; //3-way line
            if (!obj.$parent){ //top level
                if (obj.$index === 0)
                    mode = 4; //firts top item
            }

            if (lines[obj.$level])
                mode = 2;

            if (obj.$count){
                if (obj.open)
                    return "minus"+mode;
                else
                    return "plus"+mode;
            } else
                return "line"+mode;
        } else {
            if (!lines[level])
                return "line1";
            return "blank";
        }
    }
});









/*
    UI: navigation control
*/