All files / web/bundles/pimui/js/family-variant/form attribute-set.js

86.36% Statements 76/88
92.86% Branches 13/14
88.57% Functions 31/35
85.71% Lines 72/84

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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320                  8x                                                 1063x         22x 64x     384x   8760x       384x 502x     384x         384x           22x 948x   8x                       22x       22x 22x     22x   42x     22x 502x 687x   22x               22x 502x 502x   22x   42x 20x   22x   502x 502x   502x   88x   22x                         215x                         22x                         22x                         22x   22x     22x                     22x 3x 3x 3x 3x   3x                               22x                                         1x 1x 1x   1x                                                 3x 1x   1x     2x 2x 4x 1x 5x       4x 2x     4x     2x   2x                   1x       1x 1x 2x 1x 4x         1x 1x                    
'use strict';
 
/**
 * Family variant attribute set form
 *
 * @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',
        'pim/form',
        'pim/i18n',
        'pim/user-context',
        'pim/fetcher-registry',
        'pim/dialog',
        'pim/template/family-variant/attribute-set',
        'pim/template/family-variant/attribute-group'
    ],
    function (
        $,
        _,
        __,
        BaseForm,
        i18n,
        UserContext,
        fetcherRegistry,
        Dialog,
        template,
        attributeGroupTemplate
    ) {
        const sortOrdered = (first, second) => first.sort_order - second.sort_order;
 
        /**
         * Group attributes by attribute group
         */
        const groupAttributes = (attributes, attributeGroups) => (attributeCodes, lockedAttributes) => {
            return Object.values(attributeGroups)
                .sort(sortOrdered)
                .map(attributeGroup => {
                    const groupAttributes = attributes.filter(
                        attribute =>
                            attribute.group === attributeGroup.code &&
                            attributeCodes.indexOf(attribute.code) !== -1
                    ).sort(sortOrdered);
 
                    const locked = groupAttributes.filter(
                        attribute => !lockedAttributes.includes(attribute.code)
                    ).length === 0;
 
                    return {
                        attributeGroup: Object.assign({}, attributeGroup, {locked}),
                        attributes: groupAttributes
                    };
                })
                .filter(section => section.attributes.length !== 0);
        };
 
        /**
         * Get attribute from attribute code
         */
        const getAttribute = attributes => attributeCode =>
            attributes.find(attribute => attribute.code === attributeCode);
 
        return BaseForm.extend({
            events: {
                'click .delete-attribute': 'removeAttributeFromVariantAttributeSet',
                'click .delete-attribute-group': 'removeAttributeGroupFromVariantAttributeSet'
            },
            template: _.template(template),
            attributeGroupTemplate: _.template(attributeGroupTemplate),
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                Iif (!this.configured) {
                    return this;
                }
 
                const familyVariant = this.getFormData();
                fetcherRegistry.getFetcher('family')
                    .fetch(familyVariant.family)
                    .then((family) => {
                        const axesAttributeCodes = familyVariant.variant_attribute_sets
                            .reduce(
                                (result, attributeSet) => result.concat(attributeSet.attributes),
                                []
                            );
                        const attributeCodes = axesAttributeCodes.concat(
                            family.attributes.map(attribute => attribute.code)
                        ).filter((code, index, codes) => codes.indexOf(code) === index);
 
                        return $.when(
                            fetcherRegistry.getFetcher('attribute-group').fetchAll(),
                            fetcherRegistry.getFetcher('attribute').fetchByIdentifiers(attributeCodes, {rights: 0}),
                            axesAttributeCodes,
                            family
                        );
                    })
                    .then((attributeGroups, attributes, axesAttributeCodes, family) => {
                        const commonAttributes = family.attributes
                            .map(attribute => attribute.code)
                            .filter(attributeCode => axesAttributeCodes.indexOf(attributeCode) === -1);
 
                        const axesAttributes = familyVariant
                            .variant_attribute_sets
                            .map(set => set.axes)
                            .reduce((allAxes, axes) => allAxes.concat(axes));
 
                        const lockedAttributes = family.attributes
                            .filter(attribute => {
                                const isUnique = attribute.unique;
                                const isAxis = axesAttributes.includes(attribute.code);
 
                                return isAxis || isUnique;
                            })
                            .map(attribute => attribute.code);
 
                        this.$el.empty().append(this.template({
                            lockedAttributes,
                            axesAttributes,
                            familyVariant,
                            attributeGroups,
                            family,
                            commonAttributes,
                            UserContext,
                            i18n,
                            __,
                            groupAttributes: groupAttributes(attributes, attributeGroups),
                            getAttribute: getAttribute(attributes),
                            renderSection: (level, attributes, movable) => {
                                return this.attributeGroupTemplate({
                                    lockedAttributes,
                                    level,
                                    movable,
                                    attributes,
                                    i18n,
                                    UserContext,
                                    __,
                                    axesAttributes
                                });
                            }
                        }));
 
                        this.$(
                            `#attributes-column-level-0,
                            #attributes-column-level-1,
                            #attributes-column-level-2`
                        ).sortable({
                            connectWith: '.connected-sortable',
                            tolerance: 'pointer',
                            cursor: 'move',
                            cancel: 'div.alert',
                            items: '.movable',
                            receive: this.moveAttribute(lockedAttributes)
                        }).disableSelection();
 
                        this.$(
                            `#attribute-groups-column-level-0,
                            #attribute-groups-column-level-1,
                            #attribute-groups-column-level-2`
                        ).sortable({
                            connectWith: '.connected-group-sortable',
                            tolerance: 'pointer',
                            cursor: 'move',
                            cancel: 'div.alert',
                            items: '.movable-group',
                            receive: this.moveAttributes(lockedAttributes)
                        }).disableSelection();
 
                        this.renderExtensions();
 
                        this.delegateEvents();
                    });
 
                return this;
            },
 
            /**
             * Handle single attribute drop on a landing zone
             *
             * @param {Array} lockedAttributes
             *
             * @return {Function}
             */
            moveAttribute(lockedAttributes) {
                return (event, ui) => {
                    const originLevel = parseInt(ui.sender[0].dataset.level);
                    const destinationLevel = parseInt(ui.item[0].parentNode.dataset.level);
                    const movedAttributes = [ui.item[0].dataset.attributeCode]
                        .filter(movedAttribute => !lockedAttributes.includes(movedAttribute));
 
                    this.handleAttributesMove(
                        originLevel,
                        destinationLevel,
                        movedAttributes
                    );
                };
            },
 
            /**
             * Handle multiple attributes drop on a landing zone
             *
             * @param {Array} lockedAttributes
             *
             * @return {Function}
             */
            moveAttributes(lockedAttributes) {
                return (event, ui) => {
                    const destinationLevel = parseInt(ui.item[0].parentNode.dataset.level);
                    const originLevel = parseInt(ui.sender[0].dataset.level);
                    const movedAttributes = Object.values(ui.item[0].querySelectorAll('li')).map(
                        domElement => domElement.dataset.attributeCode
                    ).filter(movedAttribute => !lockedAttributes.includes(movedAttribute));
 
                    this.handleAttributesMove(
                        originLevel,
                        destinationLevel,
                        movedAttributes
                    );
                };
            },
 
            /**
             * Handle click on single attribute removal
             *
             * @param {Event} event
             */
            removeAttributeFromVariantAttributeSet(event) {
                const $attributeToRemove = $(event.currentTarget.parentElement);
                const variantAttributeSetLevel = $attributeToRemove.closest('[data-level]').data('level');
                const removedAttributes = [$attributeToRemove.data('attribute-code')];
 
                this.handleAttributesRemoval(variantAttributeSetLevel, removedAttributes);
            },
 
            /**
             * Handle click on attribute group removal
             *
             * @param {Event} event
             */
            removeAttributeGroupFromVariantAttributeSet(event) {
                const $attributeGroupToRemove = $(event.currentTarget).parents('.attribute-group-section');
                const variantAttributeSetLevel = $attributeGroupToRemove.parent().data('level');
                const removedAttributes = $attributeGroupToRemove.find('.attribute.deletable').toArray()
                    .map(element => element.dataset.attributeCode);
 
                this.handleAttributesRemoval(variantAttributeSetLevel, removedAttributes);
            },
 
            /**
             * Handle multiple attribute move
             *
             * @param {Number} originLevel
             * @param {Number} destinationLevel
             * @param {Array} movedAttributes
             */
            handleAttributesMove(originLevel, destinationLevel, movedAttributes) {
                if (originLevel >= destinationLevel) {
                    this.render();
 
                    return;
                }
 
                const data = this.getFormData();
                data.variant_attribute_sets.forEach((attributeSet) => {
                    if (attributeSet.level === originLevel) {
                        attributeSet.attributes = attributeSet.attributes.filter(
                            attributeCode => movedAttributes.indexOf(attributeCode) === -1
                        );
                    }
 
                    if (attributeSet.level === destinationLevel) {
                        attributeSet.attributes.push(...movedAttributes);
                    }
 
                    return attributeSet;
                });
 
                this.setData(data);
 
                this.render();
            },
 
            /**
             * Handle multiple attribute remove
             *
             * @param {Number} level
             * @param {Array} removedAttributes
             */
            handleAttributesRemoval(level, removedAttributes) {
                Dialog.confirm(
                    __('pim_enrich.entity.family_variant.module.edit.confirm_attribute_removal_message'),
                    __('pim_enrich.entity.family_variant.module.edit.confirm_attribute_removal_title'),
                    () => {
                        const data = this.getFormData();
                        data.variant_attribute_sets.forEach((attributeSet) => {
                            if (attributeSet.level === level) {
                                attributeSet.attributes = attributeSet.attributes.filter(
                                    item => -1 === removedAttributes.indexOf(item)
                                );
                            }
                        });
 
                        this.setData(data);
                        this.render();
                    },
                    null,
                    null,
                    null,
                    'families');
            }
        });
    }
);