All files / web/bundles/pimdashboard/js abstract-widget.js

86.84% Statements 33/38
87.5% Branches 7/8
86.67% Functions 13/15
86.84% Lines 33/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1161110x                           1110x                                   2220x   2220x 5098x 46x 46x           4514x   4514x       2264x   2264x   2264x       2252x         2252x 2252x   2252x   2250x 2250x 2250x                     2310x 2266x 2252x           2252x 2252x       2250x 2250x 2250x 2250x 374x         2264x 44x   2264x 2264x                  
define(
    [
        'jquery',
        'underscore',
        'oro/translator',
        'backbone',
        'routing',
        'oro/loading-mask',
        'oro/mediator',
        'pim/router'
    ],
    function ($, _, __, Backbone, Routing, LoadingMask, mediator) {
        'use strict';
 
        return Backbone.View.extend({
            defaults: {
                delayedLoadTimeout: 1000,
                minRefreshInterval: 20000,
                alias:              null
            },
 
            options: {},
 
            data: {},
 
            loadingMask: null,
 
            loadTimeout: null,
 
            needsData: true,
 
            initialize: function (options) {
                this.options = _.extend({}, this.defaults, this.options, options);
 
                mediator.on('route_complete', function (loadedRoute) {
                    if (loadedRoute === 'pim_dashboard_index') {
                        this.needsData = true;
                        this.delayedLoad();
                    }
                }, this);
            },
 
            render: function () {
                this.$el.html(this.template({ data: this.data, options: this.options, __: __ }));
 
                return this;
            },
 
            setElement: function () {
                Backbone.View.prototype.setElement.apply(this, arguments);
 
                this._createLoadingMask();
 
                return this;
            },
 
            loadData: function () {
                Iif (!this.needsData) {
                    this.loadTimeout = null;
 
                    return;
                }
                this.needsData = false;
                this._beforeLoad();
 
                $.get(Routing.generate('pim_dashboard_widget_data', { alias: this.options.alias }))
                    .then(_.bind(function (resp) {
                        this.data = this._processResponse(resp);
                        this.render();
                        this._afterLoad();
                    }, this));
            },
 
            reload: function () {
                this.needsData = true;
 
                this.loadData();
            },
 
            delayedLoad: function () {
                if (!this.loadTimeout) {
                    this.loadTimeout = setTimeout(_.bind(function () {
                        this.loadData();
                    }, this), this.options.delayedLoadTimeout);
                }
            },
 
            _beforeLoad: function () {
                this.$el.parent().addClass('loading');
                this.loadingMask.show();
            },
 
            _afterLoad: function () {
                this.$el.parent().removeClass('loading');
                this.loadingMask.hide();
                this.loadTimeout = null;
                setTimeout(_.bind(function () {
                    this.needsData = true;
                }, this), this.options.minRefreshInterval);
            },
 
            _createLoadingMask: function () {
                if (this.loadingMask) {
                    this.loadingMask.remove();
                }
                this.loadingMask = new LoadingMask();
                this.loadingMask.render().$el.insertAfter(this.$el);
            },
 
            _processResponse: function (data) {
                return data;
            }
        });
    }
);