All files / web/bundles/pimdatagrid/js/datagrid product-row.js

97.37% Statements 37/38
100% Branches 12/12
66.67% Functions 2/3
97.37% Lines 37/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 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              35x                                 35x                       125x   125x                 125x 125x   125x                       239x 239x 239x   239x   239x 239x   239x   239x       239x   239x   239x               967x               728x                 239x   239x 219x     20x                       239x   239x 2971x   2971x 2482x     489x 489x   489x                     489x   489x 239x 250x 125x          
/*
* This module is a custom row for a product in the datagrid.
*
* @author    Tamara Robichet <tamara.robichet@akeneo.com>
* @copyright 2017 Akeneo SAS (http://www.akeneo.com)
* @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*/
define(
    [
        'jquery',
        'underscore',
        'oro/datagrid/row',
        'pim/template/datagrid/row/product',
        'pim/template/datagrid/row/product-thumbnail',
        'pim/media-url-generator'
    ],
    function(
        $,
        _,
        BaseRow,
        rowTemplate,
        thumbnailTemplate,
        mediaUrlGenerator
    ) {
        return BaseRow.extend({
            tagName: 'div',
            rowTemplate: _.template(rowTemplate),
            thumbnailTemplate: _.template(thumbnailTemplate),
            renderedRow: null,
 
            /**
             * Return the columns for the cells that should be rendered
             *
             * @return {Array} An array of column names
             */
            getRenderableColumns() {
                const type = this.getCompletenessCellType();
 
                return [type, 'massAction', 'rowActions'];
            },
 
            /**
             * Return an object containing the template options
             *
             * @return {Object}
             */
            getTemplateOptions() {
                const isProductModel = this.isProductModel();
                const label = this.model.get('label');
 
                return {
                    useLayerStyle: isProductModel,
                    label,
                    identifier: this.model.get('identifier'),
                    imagePath: this.getThumbnailImagePath()
                };
            },
 
            /**
             * {@inheritdoc}
             */
            render() {
                const templateOptions = this.getTemplateOptions();
                const row = $(this.rowTemplate(templateOptions));
                const thumbnail = this.thumbnailTemplate(templateOptions);
 
                row.html(thumbnail);
 
                this.renderCells(row);
                this.$el.empty().html(row);
 
                row.on('click', this.onClick.bind(this));
 
                this.listenTo(this.model, 'backgrid:selected', (model, checked) => {
                    row.toggleClass('AknGrid-bodyRow--selected', checked);
                });
 
                this.delegateEvents();
 
                this.renderedRow = row;
 
                return this;
            },
 
            /**
             * Returns true if the model is a product model
             * @return {Boolean}
             */
            isProductModel() {
                return this.model.get('document_type') === 'product_model';
            },
 
            /**
             * Get the name of the completeness cell based on product type
             * @return {String}
             */
            getCompletenessCellType() {
                return this.isProductModel() ? 'complete_variant_products' : 'completeness';
            },
 
            /**
             * Returns the 'thumbnail' size image path for a product OR the dummy image
             *
             * @return {String}
             */
            getThumbnailImagePath() {
                const image = this.model.get('image');
 
                if (undefined === image || null === image) {
                    return '/media/show/undefined/preview';
                }
 
                return mediaUrlGenerator.getMediaShowUrl(image.filePath, 'thumbnail');
            },
 
            /**
             * Renders the completeness, row actions and checkbox cells
             *
             * Adds modifier classes for the completeness cell and the icons
             * inside the row actions cell.
             *
             * @param  {HTMLElement} row
             */
            renderCells(row) {
                const columnsToRender = this.getRenderableColumns();
 
                this.cells.forEach(cell => {
                    const columnName = cell.column.get('name');
 
                    if (false === columnsToRender.includes(columnName)) {
                        return;
                    }
 
                    const cellElement = cell.render().el;
                    this.setCellModifiers(columnName, cellElement);
 
                    row.append(cellElement);
                });
            },
 
            /**
             * Set modifiers on cells within a cell element
             *
             * @param {String} columnName  The name of a column e.g. completeness
             * @param {HTMLElement} cellElement The element for the cell
             */
            setCellModifiers(columnName, cellElement) {
                const type = this.getCompletenessCellType();
 
                if (columnName === type) {
                    $(cellElement).addClass('AknBadge--topRight');
                } else if (columnName === 'rowActions') {
                    $('.AknIconButton', cellElement).addClass('AknIconButton--white');
                }
            }
        });
    });