DataProcessor

class webix.DataProcessor(data)
Arguments:
  • data (object) – A configuration object

Dataprocessor component.

Referenced by

helpers
dp().

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
webix.DataProcessor = webix.proto({
    defaults: {
        autoupdate:true,
        updateFromResponse:false,
        mode:"post",
        operationName:"webix_operation",
        trackMove:false
    },


    /*! constructor
     **/
    $init: function() {
        this.reset();
        this._ignore = false;
        this.name = "DataProcessor";
        this.$ready.push(this._after_init_call);
    },
    reset:function(){
        this._updates = [];
    },
    url_setter:function(value){
        /*
            we can use simple url or mode->url
        */
        var mode = "";
        if (typeof value == "string"){
            var parts = value.split("->");
            if (parts.length > 1){
                value = parts[1];
                mode = parts[0];
            }
        } else if (value && value.mode){
            mode = value.mode;
            value = value.url;
        }

        if (mode)
            return webix.proxy(mode, value);

        return value;
    },
    master_setter:function(value){
        var store = value;
        if (value.name != "DataStore")
            store = value.data;

        this._settings.store = store;
        return value;
    },
    /*! attaching onStoreUpdated event
     **/
    _after_init_call: function(){
        webix.assert(this._settings.store, "store or master need to be defined for the dataprocessor");
        this._settings.store.attachEvent("onStoreUpdated", webix.bind(this._onStoreUpdated, this));
        this._settings.store.attachEvent("onDataMove", webix.bind(this._onDataMove, this));
    },
    ignore:function(code,master){
        var temp = this._ignore;
        this._ignore = true;
        code.call((master||this));
        this._ignore = temp;
    },
    off:function(){
        this._ignore = true;
    },
    on:function(){
        this._ignore = false;
    },

    _copy_data:function(source){
        var obj = {};
        for (var key in source)
            if (key.indexOf("$")!==0)
                obj[key]=source[key];
        return obj;
    },
    save:function(id, operation, obj){
        operation = operation || "update";
        this._save_inner(id, (obj || this._settings.store.getItem(id)), operation);
    },
    _save_inner:function(id, obj, operation){
        if (typeof id == "object") id = id.toString();
        if (!id || this._ignore === true || !operation || operation == "paint") return true;

        var store = this._settings.store;
        if (store && store._scheme_serialize)
            obj = store._scheme_serialize(obj);

        var update = { id: id, data:this._copy_data(obj), operation:operation };
        //save parent id
        if (!webix.isUndefined(obj.$parent)) update.data.parent = obj.$parent;

        if (update.operation != "delete"){
            //prevent saving of not-validated records
            var master = this._settings.master;
            if (master && master.data && master.data.getMark && master.data.getMark(id, "webix_invalid"))
                update._invalid = true;

            if (!this.validate(null, update.data))
                update._invalid = true;
        }

        if (this._check_unique(update))
            this._updates.push(update);

        if (this._settings.autoupdate)
            this.send();

        return true;
    },
    _onDataMove:function(sid, tindex, parent, targetid){
        if (this._settings.trackMove){
            var obj = webix.copy(this._settings.store.getItem(sid));
            var order = this._settings.store.order;

            obj.webix_move_index = tindex;
            obj.webix_move_id = targetid;
            obj.webix_move_parent = parent;
            this._save_inner(sid, obj, "order");
        }
    },
    _onStoreUpdated: function(id, obj, operation){
        switch (operation) {
            case 'save':
                operation = "update";
                break;
            case 'update':
                operation = "update";
                break;
            case 'add':
                operation = "insert";
                break;
            case 'delete':
                operation = "delete";
                break;
            default:
                return true;
        }
        return this._save_inner(id, obj, operation);
    },
    _check_unique:function(check){
        for (var i = 0; i < this._updates.length; i++){
            var one = this._updates[i];
            if (one.id == check.id){
                if (check.operation == "delete"){
                    if (one.operation == "insert")
                        this._updates.splice(i,1);
                    else
                        one.operation = "delete";
                }
                one.data = check.data;
                one._invalid = check._invalid;
                return false;
            }
        }
        return true;
    },
    send:function(){
        this._sendData();
    },
    _sendData: function(){
        if (!this._settings.url)
            return;

        var marked = this._updates;
        var to_send = [];
        var url = this._settings.url;

        for (var i = 0; i < marked.length; i++) {
            var tosave = marked[i];

            if (tosave._in_progress) continue;
            if (tosave._invalid) continue;

            var id = tosave.id;
            var operation = tosave.operation;
            var precise_url = (typeof url == "object" && !url.$proxy) ? url[operation] : url;
            var proxy = precise_url && (precise_url.$proxy || typeof precise_url === "function");

            if (!precise_url) continue;

            if (this._settings.store._scheme_save)
                this._settings.store._scheme_save(tosave.data);

            if (!this.callEvent("onBefore"+operation, [id, tosave]))
                continue;
            tosave._in_progress = true;

            if (!this.callEvent("onBeforeDataSend", [tosave])) return;

            tosave.data = this._updatesData(tosave.data);

            var callback = this._send_callback({ id:tosave.id, status:tosave.operation });
            if (precise_url.$proxy){
                if (precise_url.save)
                    precise_url.save(this.config.master, tosave, this, callback);
                else
                    to_send.push(tosave);
            } else {
                if (operation == "insert") delete tosave.data.id;


                if (proxy){
                    //promise
                    precise_url(tosave.id, tosave.operation, tosave.data).then(
                        function(data){
                            if (data && typeof data.json == "function")
                                data = data.json();
                            callback.success("", data, -1);
                        },
                        function(error){
                            callback.error("", null, error);
                        }
                    );
                } else {
                    //normal url
                    tosave.data[this._settings.operationName] = operation;

                    this._send(precise_url, tosave.data, this._settings.mode, operation, callback);
                }
            }

            this.callEvent("onAfterDataSend", [tosave]);
        }

        if (url.$proxy && url.saveAll && to_send.length)
            url.saveAll(this.config.master, to_send, this, this._send_callback({}));
    },


    /*! process updates list to POST and GET params according dataprocessor protocol
     *    @param updates
     *        list of objects { id: "item id", data: "data hash", operation: "type of operation"}
     *    @return
     *        object { post: { hash of post params as name: value }, get: { hash of get params as name: value } }
     **/



    _updatesData:function(source){
        var target = {};
        for (var j in source){
            if (j.indexOf("$")!==0)
                target[j] = source[j];
        }
        return target;
    },



    /*! send dataprocessor query to server
     *    and attach event to process result
     *    @param url
     *        server url
     *    @param get
     *        hash of get params
     *    @param post
     *        hash of post params
     *    @mode
     *        'post' or 'get'
     **/
    _send: function(url, post, mode, operation, callback) {
        webix.assert(url, "url was not set for DataProcessor");

        if (typeof url == "function")
            return url(post, operation, callback);

        webix.ajax()[mode](url, post, callback);
    },
    _send_callback:function(id){
        var self = this;
        return {
            success:function(t,d,l){ return self._processResult(id, t,d,l); },
            error  :function(t,d,l){ return self._processError(id, t,d,l); }
        };
    },
    attachProgress:function(start, end, error){
        this.attachEvent("onBeforeDataSend", start);
        this.attachEvent("onAfterSync", end);
        this.attachEvent("onAfterSaveError", error);
        this.attachEvent("onLoadError", error);
    },
    _processError:function(id, text, data, loader){
        if (id)
            this._innerProcessResult(true, id.id, false, id.status, false, {text:text, data:data, loader:loader});
        else {
            this.callEvent("onLoadError", arguments);
            webix.callEvent("onLoadError", [text, data, loader, this]);
        }
    },
    _innerProcessResult:function(error, id, newid, status, obj, details){
        var master = this._settings.master;
        var update = this.getItemState(id);
        update._in_progress = false;

        if (error){
            if (this.callEvent("onBeforeSaveError", [id, status, obj, details])){
                update._invalid = true;
                if(this._settings.undoOnError && master._settings.undo)
                    master.undo(id);
                this.callEvent("onAfterSaveError", [id, status, obj, details]);
                return;
            }
        } else
            this.setItemState(id, false);

        //update from response
        if (newid && id != newid)
            this._settings.store.changeId(id, newid);

        if (obj && status != "delete" && this._settings.updateFromResponse)
            this.ignore(function(){
                this._settings.store.updateItem(newid || id, obj);
            });


        //clean undo history, for the saved record
        if(this._settings.undoOnError && master._settings.undo)
            master.removeUndo(newid||id);

        this.callEvent("onAfterSave",[obj, id, details]);
        this.callEvent("onAfter"+status, [obj, id, details]);
    },
    processResult: function(state, hash, details){
        //compatibility with custom json response
        var error = (hash && (hash.status == "error" || hash.status == "invalid"));
        var newid = (hash ? ( hash.newid || hash.id ) : false);

        this._innerProcessResult(error, state.id, newid, state.status, hash, details);
    },
    // process saving from result
    _processResult: function(state, text, data, loader){
        this.callEvent("onBeforeSync", [state, text, data, loader]);

        if (loader === -1){
            //callback from promise
            this.processResult(state, data, {});
        } else {
            var proxy = this._settings.url;
            if (proxy.$proxy && proxy.result)
                proxy.result(state, this._settings.master, this, text,  data, loader);
            else {
                var hash;
                if (text){
                    hash = data.json();
                    //invalid response
                    if (text && typeof hash == "undefined")
                        hash = { status:"error" };
                }
                this.processResult(state, hash,  {text:text, data:data, loader:loader});
            }
        }

        this.callEvent("onAfterSync", [state, text, data, loader]);
    },


    /*! if it's defined escape function - call it
     *    @param value
     *        value to escape
     *    @return
     *        escaped value
     **/
    escape: function(value) {
        if (this._settings.escape)
            return this._settings.escape(value);
        else
            return encodeURIComponent(value);
    },
    getState:function(){
        if (!this._updates.length) return false;
        for (var i = this._updates.length - 1; i >= 0; i--)
            if (this._updates[i]._in_progress)
                return "saving";

        return true;
    },
    getItemState:function(id){
        var index = this._get_stack_index(id);
        return this._updates[index] || null;
    },
    setItemState:function(id, state){
        if (state)
            this.save(id, state);
        else{
            var index = this._get_stack_index(id);
            if (index > -1)
                this._updates.splice(index, 1);
        }
    },
    _get_stack_index: function(id) {
        var index = -1;
        var update = null;
        for (var i=0; i < this._updates.length; i++)
            if (this._updates[i].id == id) {
                index = i;
                break;
            }

        return index;
    }

}, webix.Settings, webix.EventSystem, webix.ValidateData);


(function(){

var timers = {};