proxy.local

webix.proxy.local

webix.proxy.local helper.

Please look into the linked official documentation.

References

helpers
assert(), copy(), delay(), extend(), markup, offline, ui(), hasMethod(), uid().

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
webix.proxy.local = {
    init:function(){
        webix.extend(this, webix.proxy.offline);
    },
    cache:true,
    data:[]
};

if (window.angular)

(function(){

  function id_helper($element){
    //we need uniq id as reference
    var id = $element.attr("id");
    if (!id){
      id = webix.uid();
      $element.attr("id", id);
    }
    return id;
  }

  function locate_view_id($element){
    if (typeof $element.attr("webix-ui") != "undefined")
      return $element.attr("id");
    return locate_view_id($element.parent());
  }




//creates webix ui components
angular.module("webix", [])
  .directive('webixUi', [ "$parse", function($parse) {
    return {
      restrict: 'A',
      scope: false,
      link:function ($scope, $element, $attrs, $controller){
        var dataname = $attrs["webixUi"];
        var callback = $attrs["webixReady"];
        var watch = $attrs["webixWatch"];
        var wxRoot = null;
        var id = id_helper($element);

        $element.ready(function(){
          if (wxRoot) return;

          if (callback)
            callback = $parse(callback);

          //destruct components
          $element.bind('$destroy', function() {
            if(wxRoot) wxRoot.destructor();
          });

          //webix-ui attribute has some value - will try to use it as configuration
          if (dataname){
            //configuration
            var watcher = function(data){
              if (wxRoot) wxRoot.destructor();
              if ($scope[dataname]){
                var config = webix.copy($scope[dataname]);
                config.$scope =$scope;
                $element[0].innerHTML = "";
                wxRoot = webix.ui(config, $element[0]);
                if (callback)
                  callback($scope, { root: wxRoot });
              }
            };
            if (watch !== "false")
              $scope.$watch(dataname, watcher);
            watcher();
          } else {
          //if webix-ui is empty - init inner content as webix markup
            if (!$attrs["view"])
              $element.attr("view", "rows");

            var ui = webix.markup;
            var tmp_a = ui.attribute; ui.attribute = "";
            //FIXME - memory leaking, need to detect the moment of dom element removing and destroy UI
            if (typeof $attrs["webixRefresh"] != "undefined")
              wxRoot = ui.init($element[0], $element[0], $scope);
            else
              wxRoot = ui.init($element[0], null, $scope);

            ui.attribute = tmp_a;

            if (callback)
              callback($scope, { root: wxRoot });
          }

          //size of ui
          $scope.$watch(function() {
            return $element[0].offsetWidth + "." + $element[0].offsetHeight;
          }, function() {
            if (wxRoot) wxRoot.adjust();
          });

        });
      }
    };
  }])

  .directive('webixShow', [ "$parse", function($parse) {
    return {
      restrict: 'A',
      scope: false,

      link:function ($scope, $element, $attrs, $controller){
        var attr = $parse($attrs["webixShow"]);
        var id = id_helper($element);

        if (!attr($scope))
            $element.attr("hidden", "true");

        $scope.$watch($attrs["webixShow"], function(){
          var view = webix.$$(id);
          if (view){
            if (attr($scope)){
              webix.$$(id).show();
              $element[0].removeAttribute("hidden");
            } else
              webix.$$(id).hide();
          }
        });

      }
    };
  }])

  .directive('webixEvent', [ "$parse", function($parse) {
    var wrap_helper = function($scope, view, eventobj){
      var ev = eventobj.split("=");
      var action = $parse(ev[1]);
      var name = ev[0].trim();
      view.attachEvent(name, function(){
        return action($scope, { id:arguments[0], details:arguments });
      });
    };

    return {
      restrict: 'A',
      scope: false,

      link:function ($scope, $element, $attrs, $controller){
        var events = $attrs["webixEvent"].split(";");
        var id = id_helper($element);

        setTimeout(function(){
          var first = $element[0].firstChild;
          if (first && first.nodeType == 1)
            id = first.getAttribute("view_id") || id;

          var view = webix.$$(id);
          for (var i = 0; i < events.length; i++) {
            wrap_helper($scope, view, events[i]);
          }
        });

      }
    };
  }])

  .directive('webixElements', [ "$parse", function($parse) {
    return {
      restrict: 'A',
      scope: false,

      link:function ($scope, $element, $attrs, $controller){

        var data = $attrs["webixElements"];
        var id = id_helper($element);

        if ($scope.$watchCollection)
          $scope.$watchCollection(data, function(collection){
            setTimeout(function(){
              var view = webix.$$(id);
              if (view){
                view.define("elements", collection);
                view.refresh();
              }
            },1);
          });
      }

    };
  }])

  .directive('webixData', [ "$parse", function($parse) {
    return {
      restrict: 'A',
      scope: false,

      link:function ($scope, $element, $attrs, $controller){

        var data = $attrs["webixData"];
        var id = id_helper($element);

        if ($scope.$watchCollection)
          $scope.$watchCollection(data, function(collection){
            if (collection){
              setTimeout(function(){
                loadData($element, id, collection, 0);
              },1);
            }
          });
      }

    };
  }]);

  function loadData($element, id, collection, num){
    if (num > 10) return;
    var first = $element[0].firstChild;
    if (first && first.nodeType == 1)
    id = first.getAttribute("view_id") || id;

    var view = webix.$$(id);
    if (view){
      if (view.options_setter){
        view.define("options", collection);
        view.refresh();
      }else{
        if (view.clearAll)
          view.clearAll();
        view.parse(collection);
      }
    } else {
      webix.delay(loadData, this, [$element, id, collection], 100, num+1);
    }
  }

})();
if (window.Backbone)
(function(){

    var cfg = {
        use_id : false
    };

    function _start_ext_load(cal){
        cal._backbone_loading = true;
        cal.callEvent("onBeforeLoad", []);
        cal.blockEvent();
    }
    function _finish_ext_load(cal){
        cal.unblockEvent();
        cal._backbone_loading = false;
        cal.refresh();
    }

webix.attachEvent("onUnSyncUnknown", function(wData, bData){
    var whandlers = wData._sync_events;
    var handlers = wData._sync_backbone_events;

    for (var i = 0; i < whandlers.length; i++)
        wData.detachEvent(whandlers[i]);

    for (var i = 0; i < handlers.length; i++)
        bData.off.apply(bData, handlers[i]);
});

webix.attachEvent("onSyncUnknown", function(wData, bData, config){
    if (config) cfg = config;
    if (cfg.get && typeof cfg.get == "string")
        cfg.get = cfg.get.split(",");

    //remove private properties
    function sanitize(ev){
        if (cfg.use_id)
            return ev;

        var obj = {};
        for (var key in ev)
            if (key != "id")
                obj[key] = ev[key];

        return obj;
    }

    function _get_id(model){
        return cfg.use_id ? model.id : model.cid;
    }

    function datareset(wData, bData){
        var data = [];
        for (var i = 0; i < bData.models.length; i++){
            var model = bData.models[i];
            var cid = _get_id(model);
            var ev =  copymodel(model);
            ev.id = cid;
            data.push(ev);
        }
        wData.clearAll();
        wData._parse(data);
    }

    function copymodel(model){
        if (cfg.get){
            var data = {};
            for (var i = 0; i < cfg.get.length; i++){
                var key = cfg.get[i];
                data[key] = model.get(key);
            }
            return data;
        }
        return model.toJSON();
    }

    var handlers = [
        ["change", function(model, info){
            var cid = _get_id(model);
            var ev = wData.pull[cid] = copymodel(model);
            ev.id = cid;

            if (wData._scheme_update)
                wData._scheme_update(ev);
            wData.refresh(ev.id);
        }],
        ["remove", function(model, changes){
            var cid = _get_id(model);
            if (wData.pull[cid])
                wData.remove(cid);
        }],
        ["add", function(model, changes){
            var cid = _get_id(model);
            if (!wData.pull[cid]){
                var ev =  copymodel(model);
                ev.id = cid;
                if (wData._scheme_init)
                    wData._scheme_init(ev);
                wData.add(ev);
            }
        }],
        ["reset", function(model, changes){
            datareset(wData, bData);
        }],
        ["request", function(obj){
            if (obj instanceof Backbone.Collection)
                _start_ext_load(wData);
        }],
        ["sync", function(obj){
            if (obj instanceof Backbone.Collection)
                _finish_ext_load(wData);
        }],
        ["error", function(obj){
            if (obj instanceof Backbone.Collection)
                _finish_ext_load(wData);
        }]
    ];

    for (var i = 0; i < handlers.length; i++)
        bData.bind.apply(bData, handlers[i]);


    var whandlers = [
        wData.attachEvent("onAfterAdd", function(id){
            if (!bData.get(id)){
                var data = sanitize(wData.getItem(id));
                var model = new bData.model(data);

                var cid = _get_id(model);
                if (cid != id)
                    this.changeId(id, cid);

                bData.add(model);
                bData.trigger("webix:add", model);
            }
            return true;
        }),
        wData.attachEvent("onDataUpdate", function(id){
            var ev = bData.get(id);
            var upd = sanitize(wData.getItem(id));

            ev.set(upd);
            bData.trigger("webix:change", ev);

            return true;
        }),
        wData.attachEvent("onAfterDelete", function(id){
            var model = bData.get(id);
            if (model){
                bData.trigger("webix:remove", model);
                bData.remove(id);
            }
            return true;
        })
    ];

    wData._sync_source = bData;
    wData._sync_events = whandlers;
    wData._sync_backbone_events = handlers;

    if (bData.length || wData.count()){
        datareset(wData, bData);
    }
});

window.WebixView = Backbone.View.extend({
    //starting from backbone 1.1, this.options is not saved automatically
    initialize : function (options) {
        this.options = options || {};
    },
    render:function(){
        if (this.beforeRender) this.beforeRender.apply(this, arguments);

        var config = this.config || this.options.config;
        var el;

        if (!config.view || !webix.ui.hasMethod(config.view, "setPosition")){
            el = window.$ ? $(this.el)[0] : this.el;
            //clear previous content if any
            if (el && !el.config) el.innerHTML = "";
        }

        var ui = webix.copy(config);
        ui.$scope = this;
        this.root = webix.ui(ui, el);

        if (this.afterRender) this.afterRender.apply(this, arguments);
        return this;
    },
    destroy:function(){
        if (this.root) this.root.destructor();
    },
    getRoot:function(){
        return this.root;
    },
    getChild:function(id){
        webix.assert(this.root.$$, "You need to set isolate property for top view");
        return this.root.$$(id);
    }
});

})();