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

89.66% Statements 26/29
90% Branches 9/10
100% Functions 2/2
89.66% Lines 26/29

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              299x                                 299x                             497x 497x   497x             1935x 993x     942x             942x             349x   349x             13x             13x   13x 12x 12x     13x 13x                 30x 30x   30x                             1412x 1412x   1412x             13x 13x          
/**
 * Displays the categories selector in grid column
 *
 * @author    Pierre Allard <pierre.allard@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/translator',
        'pim/form',
        'pim/template/product/grid/category-switcher',
        'pim/menu/resizable'
    ],
    function(
        $,
        _,
        __,
        BaseForm,
        template,
        Resizable
    ) {
        return BaseForm.extend({
            template: _.template(template),
            className: 'AknDropdown AknColumn-block category-switcher',
            events: {
                'click': 'toggleThirdColumn'
            },
            isOpen: false,
            categoryLabel: null,
            treeLabel: null,
            outsideEventListener: null,
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.listenTo(this.getRoot(), 'pim_enrich:form:category_updated', this.updateValue);
                this.listenTo(this.getRoot(), 'grid:third_column:toggle', this.updateHighlight);
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render() {
                if (null === this.treeLabel || '' === this.treeLabel.trim()) {
                    return;
                }
 
                this.$el.html(this.template({
                    label: __('pim_enrich.entity.category.uppercase_label'),
                    isOpen: this.isOpen,
                    categoryLabel: this.categoryLabel,
                    treeLabel: this.treeLabel
                }));
 
                this.renderExtensions();
            },
 
            /**
             * {@inheritdoc}
             */
            shutdown() {
                Resizable.destroy();
 
                return BaseForm.prototype.shutdown.apply(this, arguments);
            },
 
            /**
             * Toggle the thrid column
             */
            toggleThirdColumn() {
                Resizable.set({
                    maxWidth: 500,
                    minWidth: 300,
                    container: '.AknDefault-thirdColumn',
                    storageKey: 'category-switcher'
                });
 
                this.getRoot().trigger('grid:third_column:toggle');
 
                if (!this.isOpen) {
                    this.outsideEventListener = this.outsideClickListener.bind(this);
                    document.addEventListener('mousedown', this.outsideEventListener);
                }
 
                this.isOpen = !this.isOpen;
                this.render();
            },
 
            /**
             * Closes the criteria if the user clicks on the rest of the document.
             *
             * @param {Event} event
             */
            outsideClickListener(event) {
                const isOpen = $('.AknDefault-thirdColumnContainer--open').length > 0;
                const clickedFilter = $(event.target).closest('.AknFilterBox-addFilterButton').length > 0;
 
                Iif (isOpen && clickedFilter) {
                    Resizable.destroy();
                    this.toggleThirdColumn();
                    document.removeEventListener('mousedown', this.outsideEventListener);
                }
            },
 
            /**
             * Updates the current category and tree
             *
             * @param {Object} value
             * @param {String} value.categoryLabel
             * @param {String} value.treeLabel
             */
            updateValue(value) {
                this.categoryLabel = value.categoryLabel;
                this.treeLabel = value.treeLabel;
 
                this.render();
            },
 
            /**
             * Updates the highlighted categories
             */
            updateHighlight() {
                this.isHighlited = !this.isHighlited;
                this.render();
            }
        });
    }
);