All files / web/bundles/pimui/js/product/grid table.js

97.67% Statements 84/86
80% Branches 28/35
100% Functions 3/3
97.59% Lines 81/83

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 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 280300x                                                         300x               498x 498x   498x                 497x               497x   497x               497x                 497x       497x 497x 497x   497x 497x                     497x   497x         497x 497x 497x   497x   497x                               497x 497x 497x 497x   497x 492x     5x 5x   5x   5x               497x 497x 497x 497x   497x                   497x 497x 497x   497x 497x   497x                   18x 18x   18x 18x   18x                   206x 206x 206x 206x   206x 206x             206x 206x   206x 206x           206x 206x     206x 206x     206x   206x                 497x 497x   497x       497x   497x 2x 2x 2x   495x 495x 495x     497x   497x                   497x 497x   497x   497x            
define(
    [
        'underscore',
        'jquery',
        'pim/router',
        'oro/datagrid-builder',
        'oro/pageable-collection',
        'pim/datagrid/state',
        'require-context',
        'pim/form',
        'pim/user-context',
        'pim/fetcher-registry',
        'pim/datagrid/state-listener',
        'oro/loading-mask'
    ],
    function(
        _,
        $,
        Routing,
        datagridBuilder,
        PageableCollection,
        DatagridState,
        requireContext,
        BaseForm,
        UserContext,
        FetcherRegistry,
        StateListener,
        LoadingMask
    ) {
        return BaseForm.extend({
            config: {},
            loadingMask: null,
 
            /**
             * @inheritdoc
             */
            initialize(options) {
                this.config = options.config;
                this.loadingMask = new LoadingMask();
 
                return BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * Returns the stored display type for the given grid
             *
             * @return {String}
             */
            getStoredDisplayType() {
                return localStorage.getItem(`display-selector:${this.config.gridName}`);
            },
 
            /**
             * Fetch default view for grid
             * @return {Promise}
             */
            getDefaultView() {
                return FetcherRegistry.getFetcher('datagrid-view')
                    .defaultUserView(this.config.gridName)
                    .then(defaultUserView => defaultUserView.view);
            },
 
            /**
             * Fetch default columns for grid
             * @return {Promise}
             */
            getDefaultColumns() {
                return FetcherRegistry.getFetcher('datagrid-view')
                    .defaultColumns(this.config.gridName);
            },
 
            /**
             * Build the datagrid
             * @param  {Object} resp Datagrid load response
             */
            loadDataGrid(resp) {
                Iif (typeof resp === 'string' || null === resp) {
                    return;
                }
 
                const { gridName } = this.config;
                const dataLocale = UserContext.get('catalogLocale');
                const state = DatagridState.get(gridName, ['view', 'filters', 'columns']);
 
                Eif (state.columns) {
                    resp.metadata.state.parameters = _.extend({},
                        resp.metadata.state.parameters,
                        {
                            view: {
                                columns: state.columns,
                                id: state.view
                            }
                        }
                    );
                }
 
                resp.metadata = this.applyDisplayType(resp.metadata);
 
                $(`#grid-${gridName}`).data({
                    metadata: resp.metadata,
                    data: JSON.parse(resp.data)
                });
 
                const url = decodeURI(resp.metadata.options.url).split('?')[0];
                const localeParam = $.param({ dataLocale });
                resp.metadata.options.url =  `${url}?${localeParam}`;
 
                datagridBuilder([StateListener]);
 
                this.loadingMask.hide();
            },
 
            /**
             * Gets the allowed display types from the datagrid config and applies them
             * The allowed options are:
             *
             * manageColumns: Display column selector button or not
             * rowView: The module to display a row
             * label: The name of the display type in the display-selector
             *
             * @param  {Object} gridMetadata
             * @param  {Object} selectedType
             * @return {Object}
             */
            applyDisplayType(gridMetadata) {
                const selectedType = this.getStoredDisplayType();
                const metadata = Object.assign({}, gridMetadata);
                const displayTypes = metadata.options.displayTypes || {};
                const displayType = displayTypes[selectedType];
 
                if (selectedType === 'default' || undefined === displayType) {
                    return gridMetadata;
                }
 
                metadata.options.manageColumns = displayType.manageColumns;
                metadata.options.rowView = displayType.rowView;
 
                $('#product-grid').addClass(`AknGrid--${selectedType}`);
 
                return metadata;
            },
 
            /**
             * Get the initial grid params with locale
             * @return {Object} urlParams
             */
            getInitialParams() {
                const dataLocale = UserContext.get('catalogLocale');
                const alias = this.config.gridName;
                const urlParams = { dataLocale, alias };
                urlParams.params = { dataLocale };
 
                return urlParams;
            },
 
            /**
             * Set the columns on the datagrid state
             * @param  {Array} columns   An array of columns
             * @param  {Object} urlParams Url params
             * @return {Object}
             */
            applyColumns(columns, urlParams) {
                urlParams = _.clone(urlParams);
                const { gridName } = this.config;
                if (_.isArray(columns)) columns = columns.join();
 
                urlParams[`${gridName}[_parameters][view][columns]`] = columns;
                DatagridState.set(gridName, { columns: columns });
 
                return urlParams;
            },
 
            /**
             * Set the selected view on the datagrid state
             * @param  {String} viewId    The id of the view
             * @param  {Object} urlParams Url params
             * @return {Object}
             */
            applyView(viewId, urlParams) {
                urlParams = _.clone(urlParams);
                const { gridName } = this.config;
 
                urlParams[`${gridName}[_parameters][view][id]`] = viewId;
                DatagridState.set(gridName, { view: viewId });
 
                return urlParams;
            },
 
            /**
             * Apply filters to the datagrid params
             * @param  {String} rawFilters Filters as string
             * @param  {Object} urlParams  Url params
             * @return {Object}
             */
            applyFilters(rawFilters, urlParams) {
                urlParams = _.clone(urlParams);
                const { gridName } = this.config;
                let filters = PageableCollection.prototype.decodeStateData(rawFilters);
                let options = {};
 
                Eif (!_.isEmpty(filters.filters)) {
                    options = {
                        state: {
                            filters: _.omit(filters.filters, 'scope')
                        }
                    };
                }
 
                let collection = new PageableCollection(null, options);
                collection.processFiltersParams(urlParams, filters, `${gridName}[_filter]`);
 
                for (let column in filters.sorters) {
                    urlParams[`${gridName}[_sort_by][${column}]`] =
                    1 === parseInt(filters.sorters[column]) ?
                    'DESC' :
                    'ASC';
                }
 
                Eif (filters.pageSize) {
                    urlParams[`${gridName}[_pager][_per_page]`] = 25;
                }
 
                Eif (filters.currentPage) {
                    urlParams[`${gridName}[_pager][_page]`] = filters.currentPage;
                }
 
                DatagridState.set(gridName, { filters: rawFilters });
 
                return urlParams;
            },
 
            /**
             * Apply filters columns and view for the datagrid
             * @param {Array} defaultColumns
             * @param {String} defaultView
             */
            setDatagridState(defaultColumns, defaultView) {
                const { gridName, datagridLoadUrl} = this.config;
                let params = this.getInitialParams();
 
                Iif (!DatagridState.get(gridName, ['view'])) {
                    DatagridState.refreshFiltersFromUrl(gridName);
                }
 
                const state = DatagridState.get(gridName, ['view', 'filters', 'columns']);
 
                if (defaultView && ('0' === state.view || null === state.view)) {
                    params = this.applyView(defaultView.id, params);
                    params = this.applyFilters(defaultView.filters, params);
                    params = this.applyColumns(defaultView.columns, params);
                } else {
                    if (state.view) params = this.applyView(state.view, params);
                    if (state.filters) params = this.applyFilters(state.filters, params);
                    params = this.applyColumns(state.columns || defaultColumns, params);
                }
 
                this.getRoot().trigger('datagrid:getParams', params);
 
                return $.get(
                    Routing.generate(datagridLoadUrl, params),
                    this.loadDataGrid.bind(this)
                );
            },
 
            /**
             * @inheritdoc
             */
            render() {
                this.$el.empty().append(this.loadingMask.$el);
                this.loadingMask.render().show();
 
                $.when(this.getDefaultColumns(), this.getDefaultView())
                    .then((defaultColumns, defaultView) => {
                        return this.setDatagridState(defaultColumns, defaultView);
                    });
            }
        });
    }
);