All files / web/bundles/pimdatagrid/js/datagrid/grid-views view.js

40.54% Statements 15/37
37.5% Branches 6/16
37.5% Functions 3/8
40.54% Lines 15/37

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 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  1123x                     1123x                                                                             1245x   1245x       1245x       1245x 1245x   1245x 1245x   1245x 1245x   1245x                                                                                                                     3187x   3187x                                         3187x        
/* global define */
define(['backbone', 'underscore', 'oro/translator', 'oro/datagrid/grid-views/collection'],
function (Backbone, _, __, GridViewsCollection) {
    'use strict';
 
    /**
     * Datagrid views widget
     *
     * @export oro/datagrid/grid-views
     * @class   oro.datagrid.GridViews
     * @extends Backbone.View
     */
    return Backbone.View.extend({
        /** @property */
        events: {
            "click a": "onChange"
        },
 
        /** @property */
        template: _.template(
            '<div class="btn-group ">' +
                '<button data-toggle="dropdown" class="btn dropdown-toggle <% if (disabled) { %>disabled<% } %>">' +
                    '<%=  current %>' + '<span class="caret"></span>' +
                '</button>' +
                '<ul class="dropdown-menu pull-right">' +
                    '<% _.each(choices, function (choice) { %>' +
                        '<li><a href="#" data-value="' + '<%= choice.value %>' + '">' + '<%= choice.label %>' + '</a></li>' +
                    '<% }); %>' +
                '</ul>' +
            '</div>'
        ),
 
        /** @property */
        enabled: true,
 
        /** @property */
        choices: [],
 
        /** @property */
        viewsCollection: GridViewsCollection,
 
        /**
         * Initializer.
         *
         * @param {Object} options
         * @param {Backbone.Collection} options.collection
         * @param {Boolean} [options.enable]
         * @param {Array}   [options.choices]
         * @param {Array}   [options.views]
         */
        initialize: function (options) {
            options = options || {};
 
            Iif (!options.collection) {
                throw new TypeError("'collection' is required");
            }
 
            Iif (options.choices) {
                this.choices = _.union(this.choices, options.choices);
            }
 
            this.collection = options.collection;
            this.enabled = options.enable != false;
 
            this.listenTo(this.collection, "updateState", this.render);
            this.listenTo(this.collection, "beforeFetch", this.render);
 
            options.views = options.views || [];
            this.viewsCollection = new this.viewsCollection(options.views);
 
            Backbone.View.prototype.initialize.call(this, options);
        },
 
        /**
         * Disable view selector
         *
         * @return {*}
         */
        disable: function () {
            this.enabled = false;
            this.render();
 
            return this;
        },
 
        /**
         * Enable view selector
         *
         * @return {*}
         */
        enable: function () {
            this.enabled = true;
            this.render();
 
            return this;
        },
 
        /**
         * Select change event handler
         *
         * @param {Event} e
         */
        onChange: function (e) {
            e.preventDefault();
            var value = $(e.target).data('value');
            if (value !== this.collection.state.gridView) {
                this.changeView(value);
            }
        },
 
        /**
         * Updates collection
         *
         * @param gridView
         * @returns {*}
         */
        changeView: function (gridView) {
            var view = this.viewsCollection.get(gridView);
 
            if (view) {
                var viewState = _.extend({}, this.collection.initialState, view.toGridState());
                this.collection.updateState(viewState);
                this.collection.fetch();
            }
 
            return this;
        },
 
        render: function () {
            this.$el.empty();
 
            Iif (this.choices.length > 0) {
                var currentView = _.filter(
                    this.choices,
                    _.bind(function (item) {
                        return item.value == this.collection.state.gridView;
                    }, this)
                );
 
                var currentViewLabel = currentView.length ? _.first(currentView).label : __('pim_datagrid.view_selector.select');
 
                this.$el.append(
                    $(
                        this.template({
                            disabled: !this.enabled,
                            choices: this.choices,
                            current: currentViewLabel
                        })
                    )
                );
            }
 
            return this;
        }
    });
});