TreeStore

class webix.TreeStore()

Treestore mixin

Referenced by

components
TreeCollection().
views
grouplist(), 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
webix.TreeStore = {
    name:"TreeStore",
    $init:function() {
        this._filterMode={
            //level:1,
            showSubItems:true
        };
        this.branch = { 0:[] };
        this.attachEvent("onParse", function(driver, data){
            this._set_child_scheme(driver.child);
            var parent = driver.getInfo(data).parent;
        });
        this.attachEvent("onClearAll", webix.bind(function(){
            this._filter_branch = null;
        },this));
    },
    filterMode_setter:function(mode){
        return webix.extend(this._filterMode, mode, true);
    },
    _filter_reset:function(preserve){
        //remove previous filtering , if any
        if (this._filter_branch && !preserve){
            this.branch = this._filter_branch;
            this.order = webix.toArray(webix.copy(this.branch[0]));
            for (var key in this.branch)
                if (key != "0")    //exclude 0 - virtual root
                    this.getItem(key).$count = this.branch[key].length;
            delete this._filter_branch;
        }
    },
    _filter_core:function(filter, value, preserve, filterMode){
        //for tree we have few filtering options
        //- filter leafs only
        //- filter data on specific level
        //- filter data on all levels
        //- in all cases we can show or hide empty folder
        //- in all cases we can show or hide childs for matched item

        //set new order of items, store original
        if (!preserve ||  !this._filter_branch){
            this._filter_branch = this.branch;
            this.branch  = webix.clone(this.branch);
        }

        this.branch[0] = this._filter_branch_rec(filter, value, this.branch[0], 1, (filterMode||{}));
    },
    _filter_branch_rec:function(filter, value, branch, level, config){
        //jshint -W041
        var neworder = [];

        var allow = (config.level && config.level != level);

        for (var i=0; i < branch.length; i++){
            var id = branch[i];
            var item = this.getItem(id);
            var child_run = false;
            var sub = this.branch[id];

            if (allow){
                child_run = true;
            } else if (filter(this.getItem(id),value)){
                neworder.push(id);
                // open all parents of the found item
                if (config.openParents !== false){
                    var parentId = this.getParentId(id);
                    while(parentId && parentId != "0"){
                        this.getItem(parentId).open = 1;
                        parentId = this.getParentId(parentId);
                    }
                }
                //in case of of fixed level filtering - do not change child-items
                if (config.level || config.showSubItems)
                    continue;
            } else {
                //filtering level, not match
                child_run = true;
            }

            //if "filter by all levels" - filter childs
            if (allow || !config.level){
                if (sub){
                    var newsub = this.branch[id] = this._filter_branch_rec(filter, value, sub, level+1, config);
                    item.$count = newsub.length;
                    if (child_run && newsub.length)
                        neworder.push(id);
                }
            }
        }
        return neworder;
    },
    count:function(){
        if (this.order.length)
            return this.order.length;

        //we must return some non-zero value, or logic of selection will think that we have not data at all
        var count=0;
        this.eachOpen(function(){ count++; });
        return count;
    },
    changeId:function(old, newid){
        if (this.branch[old]){
            var branch = this.branch[newid] = this.branch[old];
            for (var i = 0; i < branch.length; i++)
                this.getItem(branch[i]).$parent = newid;
            delete this.branch[old];
        }
        var parent = this.getItem(old).$parent;
        if (parent !== "0"){
            var index = webix.PowerArray.find.call(this.branch[parent], old);
            this.branch[parent][index] = newid;
        }
        return webix.DataStore.prototype.changeId.call(this, old, newid);
    },
    clearAll:function(){
        this.branch = { 0:[] };
        webix.DataStore.prototype.clearAll.call(this);
    },
    getPrevSiblingId:function(id){
        var order = this.branch[this.getItem(id).$parent];
        var pos = webix.PowerArray.find.call(order, id)-1;
        if (pos>=0)
            return order[pos];
        return null;
    },
    getNextSiblingId:function(id){
        var order = this.branch[this.getItem(id).$parent];
        var pos = webix.PowerArray.find.call(order, id)+1;
        if (pos<order.length)
            return order[pos];
        return null;
    },
    getParentId:function(id){
        return this.getItem(id).$parent;
    },
    getFirstChildId:function(id){
        var order = this.branch[id];
        if (order && order.length)
            return order[0];
        return null;
    },
    isBranch:function(parent){
        return !!this.branch[parent];
    },
    getBranchIndex:function(child){
        var t = this.branch[this.pull[child].$parent];
        return webix.PowerArray.find.call(t, child);
    },
    _set_child_scheme:function(parse_name){

        if (typeof parse_name == "string")
            this._datadriver_child = function(obj){
                var t = obj[parse_name];
                if (t)
                    delete obj[parse_name];
                return t;
            };
        else
            this._datadriver_child = parse_name;
    },
    _inner_parse:function(info, recs){
        var parent  = info.parent || 0;

        for (var i=0; i<recs.length; i++){
            //get hash of details for each record
            var temp = this.driver.getDetails(recs[i]);
            var id = this.id(temp);     //generate ID for the record
            var update = !!this.pull[id]; //update mode

            if (update){
                temp = webix.extend(this.pull[id], temp, true);
                if (this._scheme_update)
                    this._scheme_update(temp);
            } else {
                if (this._scheme_init)
                    this._scheme_init(temp);
                this.pull[id]=temp;
            }

            this._extraParser(temp, parent, 0, update, info.from ? info.from*1+i : 0);
        }

        //fix state of top item after data loading
        var pItem = this.pull[parent] || {};
        var pBranch = this.branch[parent] || [];
        pItem.$count = pBranch.length;
        delete pItem.webix_kids;

        if (info.size && info.size != pBranch.length)
            pBranch[info.size] = null;
    },
    _extraParser:function(obj, parent, level, update, from){
        //processing top item
        obj.$count = 0;
    //using soft check, as parent can be a both 0 and "0" ( second one in case of loading from server side )
        obj.$parent = parent!="0"?parent:0;
        obj.$level = level||(parent!="0"?this.pull[parent].$level+1:1);

        var parent_branch = this.branch[obj.$parent];
        if (!parent_branch)
            parent_branch = this.branch[obj.$parent] = [];
            if (this._filter_branch)
                this._filter_branch[obj.$parent] = parent_branch;

        if (!update){
            var pos = from || parent_branch.length;
            parent_branch[pos] = obj.id;
        }

        var child = this._datadriver_child(obj);

        if (obj.webix_kids){
            return (obj.$count = -1);
        }

        if (!child) //ignore childless
            return (obj.$count = 0);

        //when loading from xml we can have a single item instead of an array
        if (!webix.isArray(child))
            child = [child];


        for (var i=0; i < child.length; i++) {
            //extra processing to convert strings to objects
            var item = webix.DataDriver.json.getDetails(child[i]);
            var itemid = this.id(item);
            update = !!this.pull[itemid];

            if (update){
                item = webix.extend(this.pull[itemid], item, true);
                if (this._scheme_update)
                    this._scheme_update(item);
            } else {
                if (this._scheme_init)
                    this._scheme_init(item);
                this.pull[itemid]=item;
            }
            this._extraParser(item, obj.id, obj.$level+1, update);
        }

        //processing childrens
        var branch = this.branch[obj.id];
        if (branch)
            obj.$count = branch.length;
    },
    _sync_to_order:function(master){
        this.order = webix.toArray();
        this._sync_each_child(0, master);
    },
    _sync_each_child:function(start, master){
        var branch = this.branch[start];
        for (var i=0; i<branch.length; i++){
            var id = branch[i];
            this.order.push(id);
            var item = this.pull[id];
            if (item){
                if (item.open){
                    if (item.$count == -1)
                        master.loadBranch(id);
                    else if (item.$count)
                        this._sync_each_child(id, master);
                }
            }
        }
    },
    provideApi:function(target,eventable){
        var list = ["getPrevSiblingId","getNextSiblingId","getParentId","getFirstChildId","isBranch","getBranchIndex","filterMode_setter"];
        for (var i=0; i < list.length; i++)
            target[list[i]]=this._methodPush(this,list[i]);

        if (!target.getIndexById)
            webix.DataStore.prototype.provideApi.call(this, target, eventable);
    },
    getTopRange:function(){
        return webix.toArray([].concat(this.branch[0])).map(function(id){
            return this.getItem(id);
        }, this);
    },
    eachChild:function(id, functor, master, all){
        var branch = this.branch;
        if (all && this._filter_branch)
            branch = this._filter_branch;

        var stack = branch[id];
        if (stack)
            for (var i=0; i<stack.length; i++)
                functor.call((master||this), this.getItem(stack[i]));
    },
    each:function(method,master, all, id){
        this.eachChild((id||0), function(item){
            var branch = this.branch;

            method.call((master||this), item);

            if (all && this._filter_branch)
                branch = this._filter_branch;

            if (item && branch[item.id])
                this.each(method, master, all, item.id);
        }, this, all);
    },
    eachOpen:function(method,master, id){
        this.eachChild((id||0), function(item){
            method.call((master||this), item);
            if (this.branch[item.id] && item.open)
                this.eachOpen(method, master, item.id);
        });
    },
    eachSubItem:function(id, functor){
        var top = this.branch[id||0];
        if (top)
            for (var i=0; i<top.length; i++){
                var key = top[i];
                if (this.branch[key]){
                    functor.call(this, this.getItem(key),true);
                    this.eachSubItem(key, functor);
                } else
                    functor.call(this, this.getItem(key), false);
            }
    },
    eachLeaf:function(id, functor){
        var top = this.branch[id||0];
        if (top)
            for (var i=0; i<top.length; i++){
                var key = top[i];
                if (this.branch[key]){
                    this.eachLeaf(key, functor);
                } else
                    functor.call(this, this.getItem(key), false);
            }
    },
    _sort_core:function(sort, order){
        var sorter = this.sorting.create(sort);
        for (var key in this.branch){
            var bset =  this.branch[key];
            var data = [];

            for (var i=0; i<bset.length; i++)
                data.push(this.pull[bset[i]]);

            data.sort(sorter);

            for (var i=0; i<bset.length; i++)
                data[i] = data[i].id;

            this.branch[key] = data;
        }
        return order;
    },
    add:function(obj, index, pid){
        var refresh_parent = false;

        var parent = this.getItem(pid||0);
        if(parent){
            //when adding items to leaf item - it need to be repainted
            if (!this.branch[parent.id])
                refresh_parent = true;
            parent.$count++;
        }

        this.branch[pid||0] = this.order = webix.toArray(this.branch[pid||0]);

        obj.$count = 0;
        obj.$level= (parent?parent.$level+1:1);
        obj.$parent = (parent?parent.id:0);

        if (this._filter_branch){    //adding during filtering
            var origin = this._filter_branch[pid||0];
            //newly created branch
            if (!origin) origin = this._filter_branch[pid] = this.order;

            //branch can be shared bettwen collections, ignore such cases
            if (this.order !== origin){
                //we can't know the location of new item in full dataset, making suggestion
                //put at end by default
                var original_index = origin.length;
                //put at start only if adding to the start and some data exists
                if (!index && this.branch[pid||0].length)
                    original_index = 0;

                origin = webix.toArray(origin);
                origin.insertAt(obj.id,original_index);
            }
        }

        //call original adding logic
        var result = webix.DataStore.prototype.add.call(this, obj, index);


        if (refresh_parent)
            this.refresh(pid);

        return result;
    },
    _rec_remove:function(id, inner){
        var obj = this.pull[id];
        if(this.branch[obj.id] && this.branch[obj.id].length > 0){
            var branch = this.branch[id];
            for(var i=0;i<branch.length;i++)
                this._rec_remove(branch[i], true);
        }
        delete this.branch[id];
        if(this._filter_branch)
            delete this._filter_branch[id];
        delete this.pull[id];
        if (this._marks[id])
            delete this._marks[id];
    },
    _filter_removed:function(pull, parentId, id){
        var branch = pull[parentId];
        if (branch.length == 1 && branch[0] == id && parentId){
            delete pull[parentId];
        } else
            webix.toArray(branch).remove(id);
    },
    remove:function(id){
        //id can be an array of IDs - result of getSelect, for example
        if (webix.isArray(id)){
            for (var i=0; i < id.length; i++)
                this.remove(id[i]);
            return;
        }

        webix.assert(this.exists(id), "Not existing ID in remove command"+id);
        var obj = this.pull[id];
        var parentId = (obj.$parent||0);

        if (this.callEvent("onBeforeDelete",[id]) === false) return false;
        this._rec_remove(id);
        this.callEvent("onAfterDelete",[id]);

        var parent = this.pull[parentId];
        this._filter_removed(this.branch, parentId, id);
        if (this._filter_branch)
            this._filter_removed(this._filter_branch, parentId, id);

        var refresh_parent = 0;
        if (parent){
            parent.$count--;
            if (parent.$count<=0){
                parent.$count=0;
                parent.open = 0;
                refresh_parent = 1;
            }
        }

        //repaint signal
        this.callEvent("onStoreUpdated",[id,obj,"delete"]);
        if (refresh_parent)
            this.refresh(parent.id);
    },
    /*
        serializes data to a json object
    */
    getBranch:function(id){
        var out = [];
        var items = (this._filter_branch || this.branch)[id];
        if (items)
            for (var i = 0; i < items.length; i++) out[i] = this.pull[items[i]];

        return out;
    },
    serialize: function(id, all){
        var coll = this.branch;
        //use original collection of branches
        if (all && this._filter_branch) coll = this._filter_branch;

        var ids = this.branch[id||0];
        var result = [];
        for(var i=0; i< ids.length;i++) {
            var obj = this.pull[ids[i]];
            var rel;

            if (this._scheme_serialize){
                rel = this._scheme_serialize(obj);
                if (rel===false) continue;
            } else
                rel = webix.copy(obj);

            if (this.branch[obj.id])
                rel.data = this.serialize(obj.id, all);

            result.push(rel);
        }
        return result;
    }
};