GroupStore

class webix.GroupStore()

Groupstore mixin

References

mixins
GroupMethods().
helpers
assert(), toArray().

Referenced by

mixins
Group().

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
webix.GroupStore = {
    $init:function(){
        this.attachEvent("onClearAll", this._reset_groups);
    },
    _reset_groups:function(){
        this._not_grouped_order = this._not_grouped_pull = null;
        this._group_level_count = 0;
    },
    ungroup:function(skipRender){
        if (this.getBranchIndex)
            return this._ungroup_tree.apply(this, arguments);

        if (this._not_grouped_order){
            this.order = this._not_grouped_order;
            this.pull = this._not_grouped_pull;
            this._not_grouped_pull = this._not_grouped_order = null;
            if(!skipRender)
                this.callEvent("onStoreUpdated",[]);
        }

    },
    _group_processing:function(scheme){
        this.blockEvent();
        this.group(scheme);
        this.unblockEvent();
    },
    _group_prop_accessor:function(val){
        if (typeof val == "function")
            return val;
        var acc = function(obj){ return obj[val]; };
        acc.$name = val;
        return acc;
    },
    group:function(stats){
        if (this.getBranchIndex)
            return this._group_tree.apply(this, arguments);

        var key = this._group_prop_accessor(stats.by);
        if (!stats.map[key])
            stats.map[key] = [key, this._any];

        var groups = {};
        var labels = [];
        this.each(function(data){
            var current = key(data);
            if (!groups[current]){
                labels.push({ id:current, $group:true, $row:stats.row });
                groups[current] = webix.toArray();
            }
            groups[current].push(data);
        });
        for (var prop in stats.map){
            var functor = (stats.map[prop][1]||"any");
            var property = this._group_prop_accessor(stats.map[prop][0]);
            if (typeof functor != "function"){
                webix.assert(webix.GroupMethods[functor], "unknown grouping rule: "+functor);
                functor = webix.GroupMethods[functor];
            }

            for (var i=0; i < labels.length; i++) {
                labels[i][prop]=functor.call(this, property, groups[labels[i].id]);
            }
        }

        this._not_grouped_order = this.order;
        this._not_grouped_pull = this.pull;

        this.order = webix.toArray();
        this.pull = {};
        for (var i=0; i < labels.length; i++){
            var id = this.id(labels[i]);
            this.pull[id] = labels[i];
            this.order.push(id);
            if (this._scheme_init)
                this._scheme_init(labels[i]);
        }

        this.callEvent("onStoreUpdated",[]);
    },
    _group_tree:function(input, parent){
        this._group_level_count = (this._group_level_count||0) + 1;

        //supports simplified group by syntax
        var stats;
        if (typeof input == "string"){
            stats = { by:this._group_prop_accessor(input), map:{} };
            stats.map[input] = [input];
        } else if (typeof input == "function"){
            stats = { by:input, map:{} };
        } else
            stats = input;

        //prepare
        var level;
        if (parent)
            level = this.getItem(parent).$level;
        else {
            parent  = 0;
            level = 0;
        }

        var order = this.branch[parent];
        var key = this._group_prop_accessor(stats.by);

        //run
        var topbranch = [];
        var labels = [];
        for (var i=0; i<order.length; i++){
            var data = this.getItem(order[i]);
            var current = key(data);
            var current_id = level+"$"+current;
            var ancestor = this.branch[current_id];

            if (!ancestor){
                var newitem = this.pull[current_id] = { id:current_id, value:current, $group:true, $row:stats.row};
                if (this._scheme_init)
                    this._scheme_init(newitem);
                labels.push(newitem);
                ancestor = this.branch[current_id] = [];
                ancestor._formath = [];
                topbranch.push(current_id);
            }
            ancestor.push(data.id);
            ancestor._formath.push(data);
        }

        this.branch[parent] = topbranch;
        for (var prop in stats.map){
            var functor = (stats.map[prop][1]||"any");
            var property = this._group_prop_accessor(stats.map[prop][0]);
            if (typeof functor != "function"){
                webix.assert(webix.GroupMethods[functor], "unknown grouping rule: "+functor);
                functor = webix.GroupMethods[functor];
            }

            for (var i=0; i < labels.length; i++)
                labels[i][prop]=functor.call(this, property, this.branch[labels[i].id]._formath);
        }

        for (var i=0; i < labels.length; i++){
            var group = labels[i];

            if (this.hasEvent("onGroupCreated"))
                this.callEvent("onGroupCreated", [group.id, group.value, this.branch[group.id]._formath]);

            if (stats.footer){
                var id = "footer$"+group.id;
                var footer = this.pull[id] = { id:id, $footer:true, value: group.value, $level:level, $count:0, $parent:group.id, $row:stats.footer.row};
                for (var prop in stats.footer){
                    var functor = (stats.footer[prop][1]||"any");
                    var property = this._group_prop_accessor(stats.footer[prop][0]);
                    if (typeof functor != "function"){
                        webix.assert(webix.GroupMethods[functor], "unknown grouping rule: "+functor);
                        functor = webix.GroupMethods[functor];
                    }

                    footer[prop]=functor.call(this, property, this.branch[labels[i].id]._formath);
                }

                this.branch[group.id].push(footer.id);
                this.callEvent("onGroupFooter", [footer.id, footer.value, this.branch[group.id]._formath]);
            }

            delete this.branch[group.id]._formath;
        }


        this._fix_group_levels(topbranch, parent, level+1);

        this.callEvent("onStoreUpdated",[]);
    },
    _ungroup_tree:function(skipRender, parent, force){
        //not grouped
        if (!force && !this._group_level_count) return;
        this._group_level_count = Math.max(0, this._group_level_count -1 );

        parent = parent || 0;
        var order = [];
        var toporder = this.branch[parent];
        for (var i=0; i<toporder.length; i++){
            var id = toporder[i];
            var branch = this.branch[id];
            if (branch)
                order = order.concat(branch);

            delete this.pull[id];
            delete this.branch[id];
        }

        this.branch[parent] = order;
        for (var i = order.length - 1; i >= 0; i--) {
            if (this.pull[order[i]].$footer)
                order.splice(i,1);
        }
        this._fix_group_levels(order, 0, 1);

        if (!skipRender)
            this.callEvent("onStoreUpdated",[]);
    },
    _fix_group_levels:function(branch, parent, level){
        if (parent)
            this.getItem(parent).$count = branch.length;

        for (var i = 0; i < branch.length; i++) {
            var item = this.pull[branch[i]];
            item.$level = level;
            item.$parent = parent;
            var next = this.branch[item.id];
            if (next)
                this._fix_group_levels(next, item.id, level+1);
        }
    }
};