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

75% Statements 24/32
33.33% Branches 2/6
66.67% Functions 6/9
75% Lines 24/32

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                        54x                                                 54x                   75x 75x 75x   75x                                           80x   80x             80x   80x 81x 81x                   80x   80x                                                     1x     1x 1x 1x   1x   1x   1x   1x 1x   1x          
'use strict';
 
/**
 * Override of the attributes module.
 *
 * Purpose of this override is to avoid XHR call when removing an attribute
 * from the Product Edit Form (as we simply want to remove it from the DOM).
 *
 * @author    Adrien Pétremann <adrien.petremann@akeneo.com>
 * @copyright 2015 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'jquery',
        'underscore',
        'pim/field-manager',
        'pim/security-context',
        'pim/form/common/attributes',
        'pim/fetcher-registry',
        'pim/attribute-manager',
        'pim/user-context',
        'oro/mediator',
        'pim/template/product/form/mass-edit/attributes'
    ],
    function (
        $,
        _,
        FieldManager,
        SecurityContext,
        BaseAttributes,
        FetcherRegistry,
        AttributeManager,
        UserContext,
        mediator,
        template
    ) {
        return BaseAttributes.extend({
            template: _.template(template),
            locked: false,
 
            /**
             * Listen to mass edit form unlock and lock events
             *
             * {@inheritdoc}
             */
            configure: function () {
                mediator.on('mass-edit:form:lock', this.onLock.bind(this));
                mediator.on('mass-edit:form:unlock', this.onUnlock.bind(this));
                this.onExtensions('add-attribute:add', this.addAttributes.bind(this));
 
                return BaseAttributes.prototype.configure.apply(this, arguments);
            },
 
            /**
             * Override for field render to maintain form locked state
             * @param  {jQueryElement} panel Attribute panel element
             * @param  {Object} field Attribute field
             */
            appendField: function (panel, field) {
                if (field.canBeSeen()) {
                    field.setLocked(this.locked);
                    field.render();
                    panel.append(field.$el);
                }
            },
 
            /**
             * Add an attribute to the current attribute list
             *
             * @param {Event} event
             */
            addAttributes: function (event) {
                var attributeCodes = event.codes;
 
                $.when(
                    FetcherRegistry.getFetcher('attribute').fetchByIdentifiers(attributeCodes),
                    FetcherRegistry.getFetcher('locale').fetch(UserContext.get('catalogLocale')),
                    FetcherRegistry.getFetcher('channel')
                        .fetch(UserContext.get('catalogScope'), {force_list_method: true}),
                    FetcherRegistry.getFetcher('currency').fetchAll()
                ).then(function (attributes, locale, channel, currencies) {
                    var formData = this.getFormData();
 
                    _.each(attributes, function (attribute) {
                        Eif (!formData.values[attribute.code]) {
                            formData.values[attribute.code] = AttributeManager.generateMissingValues(
                                [],
                                attribute,
                                [locale],
                                [channel],
                                currencies
                            );
                        }
                    });
 
                    this.setData(formData);
 
                    this.getRoot().trigger('pim_enrich:form:add-attribute:after');
                }.bind(this));
            },
 
            /**
             * Set mass edit form as locked
             *
             * {@inheritdoc}
             */
            onLock: function () {
                this.locked = true;
            },
 
            /**
             * Set mass edit form as unlocked
             *
             * {@inheritdoc}
             */
            onUnlock: function () {
                this.locked = false;
                this.render();
            },
 
            /**
             * {@inheritdoc}
             */
            removeAttribute: function (event) {
                Iif (!SecurityContext.isGranted('pim_enrich_product_remove_attribute')) {
                    return;
                }
                var attributeCode = event.currentTarget.dataset.attribute;
                var product = this.getFormData();
                var fields = FieldManager.getFields();
 
                this.triggerExtensions('add-attribute:update:available-attributes');
 
                delete product.values[attributeCode];
                // TODO: the manager's internal state shouldn't be modified by reference
                delete fields[attributeCode];
 
                this.setData(product);
                this.getRoot().trigger('pim_enrich:form:remove-attribute:after');
 
                this.render();
            }
        });
    }
);