All files / web/bundles/pimui/js/form/common grid.js

87.88% Statements 58/66
66.67% Branches 16/24
88.24% Functions 15/17
87.88% Lines 58/66

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 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            629x                                                   629x               615x 615x 615x 3x   615x   615x             615x   615x 2x     615x 1x               633x   633x   633x                   633x 633x 633x 633x   633x 633x 633x     633x 633x 633x 633x       633x 633x         633x   633x 633x 723x   633x                                           2x 2x                 1x 1x               2x                 633x 633x                               633x 624x     9x 9x   9x 7x             9x 9x   9x 9x           9x 9x     9x 9x     9x                     633x 633x                              
/**
 * @deprecated
 * @TODO - Will be removed in TIP-733-2
 */
'use strict';
 
define([
        'jquery',
        'underscore',
        'backbone',
        'oro/datagrid-builder',
        'routing',
        'oro/mediator',
        'pim/template/form/grid',
        'oro/pageable-collection',
        'pim/datagrid/state',
        'oro/error',
        'require-context'
    ],
    function (
        $,
        _,
        Backbone,
        datagridBuilder,
        Routing,
        mediator,
        template,
        PageableCollection,
        DatagridState,
        Error,
        requireContext
    ) {
        return Backbone.View.extend({
            template: _.template(template),
            urlParams: {},
 
            /**
             * {@inheritdoc}
             */
            initialize: function (alias, options) {
                this.alias = alias;
                this.selection = options.selection || [];
                this.selection = _.map(this.selection, function (item) {
                    return String(item);
                });
                this.options = options;
 
                var selectionIdentifier = options.selectionIdentifier || 'id';
 
                /*
                 * Removing to be sure that this property will not be used in URLs generated to load the data
                 * The selection is never used back side to load the data and it can generate an URL too long.
                 * The rightful usages of the selection are done with the property "this.selection"
                 */
                delete this.options.selection;
 
                mediator.on('datagrid:selectModel:' + this.alias, function (model) {
                    this.addElement(model.get(selectionIdentifier));
                }.bind(this));
 
                mediator.on('datagrid:unselectModel:' + this.alias, function (model) {
                    this.removeElement(model.get(selectionIdentifier));
                }.bind(this));
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.$el.html(this.template({}));
 
                this.renderGrid(this.alias, this.options);
 
                return this;
            },
 
            /**
             * Render the given grid
             *
             * @param {String} alias
             * @param {Object} params
             */
            renderGrid: function (alias, params) {
                this.urlParams = $.extend(true, {}, params);
                this.urlParams.alias = alias;
                this.urlParams.params = $.extend(true, {}, params);
                this.urlParams[alias] = $.extend(true, {}, params);
 
                var viewStored = DatagridState.get(alias, ['view']);
                Eif (!viewStored.view) {
                    DatagridState.refreshFiltersFromUrl(alias);
                }
 
                var state = DatagridState.get(alias, ['view', 'filters', 'columns']) || {};
                this.applyView(state.view, alias);
                this.applyFilters(state.filters, alias);
                this.applyColumns(state.columns, alias);
 
                //TODO Manage columns for product form (when refactoring product form index)
                //TODO Manage category filter (when refactoring category index)
                $.get(Routing.generate('pim_datagrid_load', this.urlParams)).then(function (response) {
                    this.$el.find('.grid-drop').data({
                        metadata: response.metadata,
                        data: JSON.parse(response.data)
                    });
 
                    var modules = response.metadata.requireJSModules.concat('pim/datagrid/state-listener');
 
                    var resolvedModules = []
                    _.each(modules, function(module) {
                        resolvedModules.push(requireContext(module))
                    })
                    datagridBuilder(resolvedModules)
                }.bind(this))
                .fail(function(response) {
                    Error.dispatch(null, response);
                });
            },
 
            /**
             * Get the current grid selection
             *
             * @return {Array}
             */
            getSelection: function () {
                return this.selection;
            },
 
            /**
             * Add an element to the selection
             *
             * @param {Object} element
             */
            addElement: function (element) {
                this.selection = _.union(this.selection, [element]);
                this.trigger('grid:selection:updated', this.selection);
            },
 
            /**
             * Remove an element to the selection
             *
             * @param {Object} element
             */
            removeElement: function (element) {
                this.selection = _.without(this.selection, element);
                this.trigger('grid:selection:updated', this.selection);
            },
 
            /**
             * Ask for a refresh of the grid (aware that we should not call the mediator for that but we don't have
             * the choice for now)
             */
            refresh: function () {
                mediator.trigger('datagrid:doRefresh:' + this.alias);
            },
 
            /**
             * Apply the view to the DatagridState
             * @param viewId
             * @param alias
             */
            applyView: function (viewId, alias) {
                Eif (!viewId) {
                    return;
                }
 
                this.urlParams[alias + '[_parameters][view][id]'] = viewId;
 
                DatagridState.set(alias, {
                    view: viewId
                });
            },
 
            /**
             * Apply the filters to the DatagridState
             * @param rawFilters
             * @param alias
             */
            applyFilters: function (rawFilters, alias) {
                if (!rawFilters) {
                    return;
                }
 
                var filters = PageableCollection.prototype.decodeStateData(rawFilters);
                var options = {};
 
                if (!_.isEmpty(filters.filters)) {
                    options = {
                        state: {
                            filters: _.omit(filters.filters, 'scope')
                        }
                    };
                }
 
                var collection = new PageableCollection(null, options);
                collection.processFiltersParams(this.urlParams, filters, alias + '[_filter]');
 
                for (var column in filters.sorters) {
                    this.urlParams[alias + '[_sort_by][' + column + ']'] =
                        1 === parseInt(filters.sorters[column]) ?
                            'DESC' :
                            'ASC';
                }
 
                Eif (undefined !== filters.pageSize) {
                    this.urlParams[alias + '[_pager][_per_page]'] = 25;
                }
 
                Eif (undefined !== filters.currentPage) {
                    this.urlParams[alias + '[_pager][_page]'] = filters.currentPage;
                }
 
                DatagridState.set(alias, {
                    filters: rawFilters
                });
            },
 
            /**
             * Apply the columns to the DatagridState
             * @param columns
             * @param alias
             */
            applyColumns: function (columns, alias) {
                Eif (!columns) {
                    return;
                }
 
                if (_.isArray(columns)) {
                    columns = columns.join();
                }
                this.urlParams[alias + '[_parameters][view][columns]'] = columns;
 
                DatagridState.set(alias, {
                    columns: columns
                });
            }
        });
    }
);