TreeAPI

class webix.TreeAPI()

Treeapi mixin

Referenced by

views
tree(), 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
webix.TreeAPI = {
    open: function(id, show) {
        if (!id) return;
        //ignore open for leaf items
        var item = this.getItem(id);
        if (!item.$count || item.open) return;

        if (this.callEvent("onBeforeOpen",[id])){
            item.open=true;
            this.data.callEvent("onStoreUpdated",[id, 0, "branch"]);
            this.callEvent("onAfterOpen",[id]);
        }

        if (show && id != "0")
            this.open(this.getParentId(id), show);
    },
    close: function(id) {
        if (!id) return;
        var item = this.getItem(id);
        if (!item.open) return;

        if (this.callEvent("onBeforeClose",[id])){
            item.open=false;
            this.data.callEvent("onStoreUpdated",[id, 0, "branch"]);
            this.callEvent("onAfterClose",[id]);
        }
    },
    openAll: function(id){
        this.data.eachSubItem((id||0), function(obj, branch){
            if (branch)
                obj.open = true;
        });
        this.data.refresh();
    },
    closeAll: function(id){
        this.data.eachSubItem((id||0), function(obj, branch){
            if (branch)
                obj.open = false;
        });
        this.data.refresh();
    },
    _tree_check_uncheck:function(id,mode,e){
        if(this._settings.threeState)
            return this._tree_check_uncheck_3(id,(mode !== null?mode:""));

        var value,
            item = this.getItem(id),
            trg = (e? (e.target|| e.srcElement):null);

        //read actual value from HTML tag when possible
        //as it can be affected by dbl-clicks
        if(trg && trg.type == "checkbox")
            value = trg.checked?true:false;
        else
            value = (mode !== null?mode:!item.checked);

        item.checked = value;
        this.callEvent("onItemCheck", [id, item.checked, e]);
    },
    isBranchOpen:function(search_id){
        if (search_id == "0") return true;

        var item = this.getItem(search_id);
        if (item.open)
            return this.isBranchOpen(item.$parent);
        return false;
    },
    getOpenItems: function() {
        var open = [];
        for (var id in this.data.branch) {
            if (this.exists(id) && this.getItem(id).open)
                open.push(id);
        }
        return open;
    },
    getState: function(){
        return {
            open: this.getOpenItems(),
            select: this.getSelectedId(true)
        };
    },
    _repeat_set_state:function(tree, open){
        var event = this.data.attachEvent("onStoreLoad", function(){
            tree.setState.call(tree,open);
            tree.data.detachEvent(event);
            tree = null;
        });
    },
    setState: function(state){
        var repeat = false;
        var dyn = false;

        if (state.open){
            this.closeAll();
            var open = state.open;
            for (var i = 0; i < open.length; i++){
                var item = this.getItem(open[i]);
                if (item && item.$count){
                    item.open=true;
                    //dynamic loading
                    if (item.$count == -1){
                        //call the same method after data loading
                        this._repeat_set_state(this, state);
                        this.refresh();
                        return 0;
                        //end processing
                    }
                }
            }
            this.refresh();
        }


        if (state.select && this.select){
            var select = state.select;
            this.unselect();
            for (var i = 0; i < select.length; i++)
                if (this.exists(select[i]))
                    this.select(select[i], true);
        }

        return 1;
    }
};