All files / web/bundles/pimui/js/mass-edit/form/product category.js

93.88% Statements 46/49
66.67% Branches 8/12
92.86% Functions 13/14
93.88% Lines 46/49

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                89x                                                     89x                                 330x   330x             9x   9x 9x 9x             14x 9x 9x       9x   9x 9x     9x               9x   9x   9x                 5x   5x     14x               14x 5x   9x                   6x 6x 7x   6x                 15x   15x         15x                                       4x   4x 4x   4x   4x                       7x 6x 6x 6x           7x             5x 5x   5x   5x       5x          
'use strict';
/**
 * Mass change category
 *
 * @author    Julien Sanchez <julien@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',
        'oro/messenger',
        'pim/i18n',
        'pim/user-context',
        'pim/fetcher-registry',
        'pim/mass-edit-form/product/operation',
        'pim/tree/associate',
        'pim/common/property',
        'pim/template/mass-edit/product/category'
    ],
    function (
        $,
        _,
        __,
        messenger,
        i18n,
        UserContext,
        FetcherRegistry,
        BaseOperation,
        TreeAssociate,
        propertyAccessor,
        template
    ) {
        return BaseOperation.extend({
            template: _.template(template),
            currentTree: null,
            categoryCache: {},
            selectedCategories: [],
            treePromise: null,
            view: null,
            trees: [],
            events: {
                'click .nav-tabs .tree-selector': 'changeTree',
                'change #hidden-tree-input': 'updateModel'
            },
 
            /**
             * {@inheritdoc}
             */
            initialize: function () {
                this.trees = [];
 
                BaseOperation.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            reset: function () {
                this.setValue([]);
 
                this.treePromise        = null;
                this.currentTree        = null;
                this.selectedCategories = [];
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                if (null === this.treePromise) {
                    FetcherRegistry.getFetcher(this.config.fetcher).clear();
                    this.treePromise = FetcherRegistry
                        .getFetcher(this.config.fetcher)
                        .fetchAll()
                        .then(function (trees) {
                        this.trees = trees;
 
                        Eif (null === this.currentTree) {
                            this.currentTree = _.first(trees).code;
                        }
 
                        this.$el.html(this.template({
                            i18n: i18n,
                            locale: UserContext.get('catalogLocale'),
                            trees: trees,
                            currentTree: _.findWhere(trees, {code: this.currentTree}),
                            selectedCategories: this.selectedCategories
                        }));
 
                        this.delegateEvents();
 
                        this.toggleContentCache();
 
                        return {
                            treeAssociate: new TreeAssociate('#trees', '#hidden-tree-input', {
                                list_categories: this.config.listRoute,
                                children:        this.config.childrenRoute
                            }),
                            trees: trees
                        };
                    }.bind(this));
                } else {
                    this.toggleContentCache();
 
                    this.delegateEvents();
                }
 
                return this;
            },
 
            /**
             * In this method, we don't re-render the trees because select elements on several trees is hell.
             * We simply hide or show the cache to avoid clicking on new elements during the confirm.
             **/
            toggleContentCache: function () {
                if (this.readOnly) {
                    this.$el.find('.content-cache').removeClass('AknTabContainer-contentCache--hidden');
                } else {
                    this.$el.find('.content-cache').addClass('AknTabContainer-contentCache--hidden');
                }
            },
 
            /**
             * Update the mass edit model
             *
             * @param {Event} event
             */
            updateModel: function (event) {
                this.selectedCategories = event.target.value.split(',');
                const selectedCategories = this.selectedCategories
                    .filter((category) => '' !== category)
                    .map(this.getCategoryCode.bind(this));
                this.setValue(selectedCategories);
            },
 
            /**
             * Update the model after dom event triggered
             *
             * @param {string} categories
             */
            setValue: function (categories) {
                let data = this.getFormData();
 
                data.actions = [{
                    field: 'categories',
                    value: categories
                }];
 
                this.setData(data);
            },
 
            /**
             * Get current value from mass edit model
             *
             * @return {string}
             */
            getValue: function () {
                const action = _.findWhere(this.getFormData().actions, {field: 'categories'});
 
                return action ? action.value : null;
            },
 
            /**
             * Change the current tree
             *
             * @param {Event} event
             */
            changeTree: function (event) {
                this.currentTree = event.currentTarget.dataset.tree;
 
                this.treePromise.then(function (elements) {
                    const tree = _.findWhere(elements.trees, {code: this.currentTree});
 
                    elements.treeAssociate.switchTree(tree.id);
 
                    this.delegateEvents();
                }.bind(this));
            },
 
            /**
             * Fetch category code from cache
             *
             * @param {integer} id
             *
             * @returns {string}
             */
            getCategoryCode: function (id) {
                if (!this.categoryCache[id]) {
                    const $categoryElement = this.$('#node_' + id);
                    const $rootElement     = $categoryElement.closest('.root-unselectable');
                    this.categoryCache[id] = {
                        code: String($categoryElement.data('code')),
                        rootId: $rootElement.data('tree-id')
                    };
                }
 
                return this.categoryCache[id].code;
            },
 
            /**
             * Checks there is at least one category selected to go to the next step
             */
            validate: function () {
                const data = this.getFormData();
                const categories = propertyAccessor.accessProperty(data, 'actions.0.value', []);
 
                const hasUpdates = 0 !== categories.length;
 
                Iif (!hasUpdates) {
                    messenger.notify('error', __(`pim_enrich.mass_edit.product.operation.${data.operation}.no_update`));
                }
 
                return $.Deferred().resolve(hasUpdates);
            }
        });
    }
);