All files / web/bundles/pimui/js/product/grid category-tree.js

100% Statements 18/18
50% Branches 1/2
100% Functions 5/5
100% Lines 17/17

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              299x                             299x                                 497x   497x             496x   496x             496x           496x       496x 303x     496x 613x     496x                       1412x                               1412x                   1412x                       2824x          
 /**
 * Extension to set up the category tree filter for the product grid
 *
 * @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(
    [
        'underscore',
        'jquery',
        'pim/form-builder',
        'pim/form',
        'oro/datafilter/product_category-filter'
    ],
    function(
        _,
        $,
        FormBuilder,
        BaseForm,
        CategoryFilter
    ) {
        return BaseForm.extend({
            config: {
                alias: 'product-grid',
                categoryTreeName: 'pim_enrich_product_grid_category_tree'
            },
            id: 'tree',
            className: 'filter-item',
            attributes: {
                'data-name': 'category',
                'data-type': 'tree',
                'data-relatedentity': 'product'
            },
 
            /**
             * @inheritdoc
             */
            initialize(options) {
                this.config = Object.assign(this.config, options.config || {});
 
                return BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * @inheritDoc
             */
            configure() {
                this.listenTo(this.getRoot(), 'datagrid:getParams', this.setupCategoryTree);
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * Render the category tree extensions when the datagrid is ready
             */
            setupCategoryTree(urlParams) {
                const categoryFilter = new CategoryFilter(
                    urlParams,
                    this.config.alias,
                    this.config.categoryTreeName,
                    this.$el,
                    (value) => {
                        this.valueUpdated(value);
                    }
                );
 
                this.listenTo(categoryFilter, 'update', function (value) {
                    this.valueUpdated(value);
                });
 
                this.listenTo(categoryFilter, 'update_label', function (value) {
                    this.valueUpdated(value);
                });
 
                return categoryFilter;
            },
 
            /**
             * Triggers a new event when the value of the category is updated
             *
             * @param {Object} value
             * @param {integer} value.type
             * @param {integer} value.value.categoryId
             * @param {integer} value.value.treeId
             */
            valueUpdated(value) {
                this.getRoot().trigger('pim_enrich:form:category_updated', {
                    categoryLabel: this.getCategoryLabel(value.value.categoryId),
                    treeLabel: this.getTreeLabel()
                });
            },
 
            /**
             * Get the category label from its id.
             * We search the matching DOM element in the JStree plugin directly, because it does not exist any fetcher
             * able to get the label from its id.
             *
             * @param {integer} id
             *
             * @returns {String}
             */
            getCategoryLabel(id) {
                return this.trimCount($('#node_' + id + ' > a:first').text().trim());
            },
 
            /**
             * Get the current tree label.
             * See this.getCategoryLabel
             *
             * @returns {String}
             */
            getTreeLabel() {
                return this.trimCount($('#tree_toolbar .select2-chosen').text().trim());
            },
 
            /**
             * Deletes the count of the category and the tree to only keep the label
             * For example, "Audio (123)" will return "Audio".
             *
             * @param {String} str
             *
             * @returns {String}
             */
            trimCount(str) {
                return str.replace(/(.*) \(\d+\)/, (match, text) => text);
            }
        });
    }
);