All files / web/bundles/pimdatagrid/js datagrid-builder.js

96.51% Statements 83/86
87.5% Branches 28/32
100% Functions 23/23
96.51% Lines 83/86

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    1123x                       1123x 1123x 1123x 1123x 1123x 1123x           1123x   1123x   7392x     14784x     11628x       1123x             1245x   1245x                 1245x   1245x     1245x 1245x 1245x 1245x               1245x 1245x 1245x 12212x     1245x 7392x 7392x       7392x 7392x     1245x 3294x     1245x 532x     1245x 3514x 994x                       1245x 1245x     1245x 1245x 1245x 1245x     1245x 1245x             1245x                 1245x                                   1245x 1245x 1245x     1245x 1245x     1245x 7392x 7392x 7392x 7392x 7392x 7392x 266x 266x   7392x 7392x   7392x       1245x 3294x       1245x 3514x 3514x       1245x                                           1245x                     1123x 1273x 1333x 1333x 1333x 88x   1245x   1245x       1245x 1245x 1564x     1564x            
'use strict';
 
define([
        'jquery',
        'underscore',
        'oro/tools',
        'oro/mediator',
        'oro/pageable-collection',
        'oro/datagrid/grid',
        'oro/datagrid/grid-views/view',
        'require-context'
    ],
    function ($, _, tools, mediator, PageableCollection, Grid, GridViewsView, requireContext) {
 
    var gridSelector = '[data-type="datagrid"]:not([data-rendered])',
        gridGridViewsSelector = '.page-title > .AknTitleContainer .span10:last',
        headerCellModuleName = 'oro/datagrid/{{type}}-header-cell',
        cellModuleName = 'oro/datagrid/{{type}}-cell',
        actionModuleName = 'oro/datagrid/{{type}}-action',
        cellTypes = {
            integer:   'integer',
            decimal:   'number',
            percent:   'number'
        },
 
        reservedActions = ['export', 'ajax', 'redirect', 'edit', 'delete'],
 
        helpers = {
            headerCellType: function (type) {
                return type + 'HeaderCell';
            },
            cellType: function (type) {
                return type + 'Cell';
            },
            actionType: function (type) {
                return type + 'Action';
            }
        },
 
        methods = {
            /**
             * Reads data from grid container, collects required modules and runs grid builder
             *
             * @param {Function} initBuilders
             */
            initBuilder: function (initBuilders) {
                var self = this;
 
                self.metadata = _.extend({
                    columns: [],
                    options: {},
                    state: {},
                    rowActions: {},
                    massActionsGroups: [],
                    massActions: {}
                }, self.$el.data('metadata'));
 
                self.modules = {};
 
                methods.collectModules.call(self);
 
                // load all dependencies and build grid
                tools.loadModules(self.modules, function () {
                    methods.buildGrid.call(self);
                    initBuilders();
                    methods.afterBuild.call(self);
                });
            },
 
            /**
             * Collects required modules
             */
            collectModules: function () {
                var modules = this.modules,
                    metadata = this.metadata,
                    moduleName = function (template, type) {
                        return template.replace('{{type}}', type);
                    };
                // header cells & cells
                _.each(metadata.columns, function (column) {
                    var headerCellType = column.headerCell;
                    Iif (undefined !== headerCellType) {
                        modules[helpers.headerCellType(headerCellType)] = moduleName(headerCellModuleName, headerCellType);
                    }
 
                    var type = column.type;
                    modules[helpers.cellType(type)] = moduleName(cellModuleName, cellTypes[type] || type);
                });
                // row actions
                _.each(_.values(metadata.rowActions), function (action) {
                    modules[helpers.actionType(action.type)] = moduleName(actionModuleName, action.type);
                });
                // default mass actions
                if (!$.isEmptyObject(metadata.massActions)) {
                    modules[helpers.actionType('mass')] = moduleName(actionModuleName, 'mass');
                }
                // mass actions
                _.each(_.values(metadata.massActions), function (massAction) {
                    if (!_.contains(reservedActions, massAction.type)) {
                        modules[helpers.actionType(massAction.type)] = moduleName(actionModuleName, massAction.type);
                    }
                });
            },
 
            /**
             * Build grid
             */
            buildGrid: function () {
                var options, collection, grid;
 
                // create collection
                options = methods.combineCollectionOptions.call(this);
                collection = new PageableCollection(this.$el.data('data'), options);
 
                // create grid
                options = methods.combineGridOptions.call(this);
                grid = new Grid(_.extend({collection: collection}, options));
                this.grid = grid;
                this.$el.append(grid.render().$el);
 
                // create grid view
                options = methods.combineGridViewsOptions.call(this);
                $(gridGridViewsSelector).append((new GridViewsView(_.extend({collection: collection}, options))).render().$el);
            },
 
            /**
             * After build
             */
            afterBuild: function () {
                mediator.trigger('datagrid_collection_set_after', this.grid.collection, this.$el);
            },
 
            /**
             * Process metadata and combines options for collection
             *
             * @returns {Object}
             */
            combineCollectionOptions: function () {
                return _.extend({
                    inputName: this.metadata.options.gridName,
                    parse: true,
                    url: '\/user\/json',
                    state: _.extend({
                        filters: {},
                        sorters: {}
                    }, this.metadata.state)
                }, this.metadata.options);
            },
 
            /**
             * Process metadata and combines options for datagrid
             *
             * @returns {Object}
             */
            combineGridOptions: function () {
                var columns,
                    rowActions = {},
                    massActions = {},
                    defaultOptions = {
                        sortable: false
                    },
                    modules = this.modules,
                    metadata = this.metadata;
 
                // columns
                columns = _.map(metadata.columns, function (cell) {
                    var cellOptionKeys = ['name', 'label', 'renderable', 'editable', 'sortable', 'headerCell'],
                        cellOptions = _.extend({}, defaultOptions, _.pick.apply(null, [cell].concat(cellOptionKeys))),
                        extraOptions = _.omit.apply(null, [cell].concat(cellOptionKeys.concat('type'))),
                        headerCellType = modules[helpers.headerCellType(cell.headerCell)],
                        cellType = modules[helpers.cellType(cell.type)];
                    if (!_.isEmpty(extraOptions)) {
                        cellOptions.extraOptions = _.extend({}, extraOptions);
                        cellType = cellType.extend(extraOptions);
                    }
                    cellOptions.headerCell = headerCellType;
                    cellOptions.cell = cellType;
 
                    return cellOptions;
                });
 
                // row actions
                _.each(metadata.rowActions, function (options, action) {
                    rowActions[action] = modules[helpers.actionType(options.type)].extend(options);
                });
 
                // mass actions
                _.each(metadata.massActions, function (options, action) {
                    var optionType = _.contains(reservedActions, options.type) ? 'mass' : options.type;
                    massActions[action] = modules[helpers.actionType(optionType)].extend(options);
                });
 
 
                return {
                    name: metadata.options.gridName,
                    columns: columns,
                    rowActions: rowActions,
                    massActionsGroups: metadata.massActionsGroups,
                    massActions: massActions,
                    toolbarOptions: metadata.options.toolbarOptions || {},
                    multipleSorting: metadata.options.multipleSorting || false,
                    entityHint: metadata.options.entityHint,
                    row: metadata.options.rowView ? requireContext(metadata.options.rowView) : null,
                    displayTypes: metadata.options.displayTypes,
                    manageColumns: metadata.options.manageColumns,
                    emptyGridOptions: metadata.options.emptyGridOptions
                };
            },
 
            /**
             * Process metadata and combines options for datagrid views
             *
             * @returns {Object}
             */
            combineGridViewsOptions: function () {
                return this.metadata.gridViews || {};
            }
        };
 
 
    /**
     * Process datagirid's metadata and creates datagrid
     *
     * @export oro/datagrid-builder
     * @name   oro.datagridBuilder
     */
    return function (builders) {
        $(gridSelector).each(function (i, el) {
            var $el = $(el);
            var gridName = (($el.data('metadata') || {}).options || {}).gridName;
            if (!gridName) {
                return;
            }
            $el.attr('data-rendered', true);
 
            Iif (!_.isArray(builders)) {
                builders = [builders];
            }
 
            methods.initBuilder.call({ $el: $el }, function () {
                _.each(builders, function (builder) {
                    Iif (!_.has(builder, 'init') || !$.isFunction(builder.init)) {
                        throw new TypeError('Builder does not have init method');
                    }
                    builder.init($el, gridName);
                });
            });
        }).end();
    };
});