TreeRenderStack

class webix.TreeRenderStack()

Treerenderstack mixin

References

helpers
assert(), log().

Referenced by

views
tree().

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
webix.TreeRenderStack={
    $init:function(){
        webix.assert(this.render,"TreeRenderStack :: Object must use RenderStack first");
    },
    _toHTMLItem:function(obj){
        var mark = this.data._marks[obj.id];
        this.callEvent("onItemRender",[obj]);
        return this.type.templateStart(obj,this.type,mark)+(obj.$template?this.type["template"+obj.$template](obj,this.type,mark):this.type.template(obj,this.type,mark))+this.type.templateEnd();
    },
    _toHTMLItemObject:function(obj){
        this._html.innerHTML = this._toHTMLItem(obj);
        return this._html.firstChild;
    },
    //convert single item to HTML text (templating)
    _toHTML:function(obj){
        //check if related template exist
        webix.assert((!obj.$template || this.type["template"+obj.$template]),"RenderStack :: Unknown template: "+obj.$template);
        var html="<div role='presentation' class='webix_tree_branch_"+obj.$level+"'>"+this._toHTMLItem(obj);

        if (obj.open)
            html+=this._toHTMLLevel(obj.id);

        html+="</div>";

        return html;
    },
    _toHTMLLevel:function(id){
        var html = "";
        var leaves = this.data.branch[id];
        if (leaves){
            html+="<div role='presentation' class='webix_tree_leaves'>";
            var last = leaves.length-1;
            for (var i=0; i <= last; i++){
                var obj = this.getItem(leaves[i]);
                this.type._tree_branch_render_state[obj.$level] = (i == last);
                html+=this._toHTML(obj);
            }
            html+="</div>";
        }
        return html;
    },
    //return true when some actual rendering done
    render:function(id,data,type){
        webix.TreeRenderStack._obj = this;    //can be used from complex render

        if (!this.isVisible(this._settings.id) || this.$blockRender)
            return;

        if (webix.debug_render)
            webix.log("Render: "+this.name+"@"+this._settings.id);

        if (id){
            var cont;
            var item = this.getItem(id);
            if (type!="add"){
                cont = this.getItemNode(id);
                if (!cont) return;
            }

            switch(type){
                case "branch":
                    var branch = cont.parentNode;
                    var node = this._toHTMLObject(item);

                    webix.html.insertBefore(node, branch);
                    webix.html.remove(branch);
                    this._htmlmap = null;
                break;
                case "paint":
                case "update":
                    var node = this._htmlmap[id] = this._toHTMLItemObject(item);
                    webix.html.insertBefore(node, cont);
                    webix.html.remove(cont);
                break;
                case "delete":
                    //deleting not item , but full branch
                    webix.html.remove(cont.parentNode);
                break;
                case "add":
                    var parent;
                    //we want process both empty value and 0 as string
                    //jshint -W041:true
                    if (item.$parent == 0){
                        parent = this._dataobj.firstChild;
                    } else if(this.getItem(item.$parent).open){
                        parent  = this.getItemNode(item.$parent);
                        if (parent){
                            //when item created by the script, it will miss the container for child notes
                            //create it on demand
                            if (!parent.nextSibling){
                                var leafs = webix.html.create("DIV", { "class" : "webix_tree_leaves" },"");
                                parent.parentNode.appendChild(leafs);
                            }
                            parent = parent.nextSibling;
                        }
                    }

                    if (parent){
                        var next = this.data.getNextSiblingId(id);
                        next = this.getItemNode(next);
                        if (next)
                            next = next.parentNode;

                        var node = this._toHTMLObject(item);
                        this._htmlmap[id] = node.firstChild;
                        webix.html.insertBefore(node, next, parent);
                    }
                break;
                default:
                    return false;
            }
            this.callEvent("onPartialRender", [id,data,type]);
        } else
            //full reset
            if (this.callEvent("onBeforeRender",[this.data])){
                //will be used for lines management
                this.type._tree_branch_render_state = [];
                //getTopRange - returns all elements on top level
                this._dataobj.innerHTML = this._toHTMLLevel(0);

                this._htmlmap = null; //clear map, it will be filled at first getItemNode
                this.callEvent("onAfterRender",[]);
            }

        //clear after usage
        this.type._tree_branch_render_state = 0;
        webix.TreeRenderStack._obj = null;
        return true;
    },
    getItemNode:function(search_id){
        if (this._htmlmap)
            return this._htmlmap[search_id];

        //fill map if it doesn't created yet
        this._htmlmap={};

        var t = this._dataobj.getElementsByTagName("DIV");
        for (var i=0; i < t.length; i++){
            var id = t[i].getAttribute(this._id); //get item's
            if (id)
                this._htmlmap[id]=t[i];
        }
        //call locator again, when map is filled
        return this.getItemNode(search_id);
    },
    _branch_render_supported:1
};



/*
    Behavior:SelectionModel - manage selection states
    @export
        select
        unselect
        selectAll
        unselectAll
        isSelected
        getSelectedId
*/