All files / web/bundles/pimui/js/attribute-group/form/tab attribute.js

98.21% Statements 55/56
80% Branches 8/10
100% Functions 20/20
98.18% Lines 54/55

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                  13x                                                   13x                           13x   13x             13x         13x   13x     13x   13x               10x     10x 43x     43x 2x     43x     42x   10x               10x           3x 3x 3x 18x     3x       10x   10x     10x             3x 19x 19x   19x   3x 3x   3x   3x                 1x 1x 1x   1x   1x                 1x       1x   1x       1x                         1x 1x 1x 1x   1x   1x                 19x                 10x   10x                   10x   10x          
'use strict';
 
/**
 * Attribute selection tab
 *
 * @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/security-context',
        'pim/dialog',
        'pim/template/form/attribute-group/tab/attribute',
        'jquery-ui'
    ],
    function (
        $,
        _,
        __,
        BaseForm,
        i18n,
        UserContext,
        FetcherRegistry,
        SecurityContext,
        Dialog,
        template
    ) {
        return BaseForm.extend({
            className: 'tabbable tabs-left',
            template: _.template(template),
            otherAttributes: [],
            attributeSortOrderKey: 'attributes_sort_order',
 
            events: {
                'click .remove-attribute': 'removeAttributeRequest'
            },
 
            /**
             * {@inheritdoc}
             */
            initialize: function (config) {
                this.config = config.config;
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.trigger('tab:register', {
                    code: this.config.tabCode ? this.config.tabCode : this.code,
                    label: __(this.config.title)
                });
 
                this.onExtensions('add-attribute:add', this.addAttributes.bind(this));
 
                return FetcherRegistry.getFetcher('attribute')
                    .search({attribute_groups: 'other'})
                    .then(function (attributes) {
                        this.otherAttributes = _.pluck(attributes, 'code');
                    }.bind(this)).then(function () {
                        return BaseForm.prototype.configure.apply(this, arguments);
                    }.bind(this));
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                FetcherRegistry.getFetcher('attribute')
                    .fetchByIdentifiers(this.getFormData().attributes, {rights: 0})
                    .then(function (attributes) {
                        attributes = attributes.map((attribute) => {
                            let sortOrder = this.getFormData().attributes_sort_order[attribute.code];
 
                            //This updates the sort order if the attribute is new on the collection
                            if (undefined === sortOrder) {
                                sortOrder = Object.keys(this.getFormData().attributes_sort_order).length + 1;
                            }
 
                            return Object.assign({}, attribute, { sort_order: sortOrder });
                        });
 
                        attributes.sort((first, second) => first.sort_order - second.sort_order);
 
                        this.$el.empty().append(this.template({
                            attributes: attributes,
                            i18n: i18n,
                            UserContext: UserContext,
                            __: __,
                            hasRightToRemove: this.hasRightToRemove()
                        }));
 
                        this.$('.attribute-list').sortable({
                            handle: '.handle',
                            containment: 'parent',
                            tolerance: 'pointer',
                            update: this.updateAttributeOrders.bind(this),
                            helper: function(e, tr) {
                                var $originals = tr.children();
                                var $helper = tr.clone();
                                $helper.children().each(function(index) {
                                    $(this).width($originals.eq(index).width());
                                });
 
                                return $helper;
                            }
                        });
 
                        this.delegateEvents();
 
                        BaseForm.prototype.render.apply(this, arguments);
                    }.bind(this));
 
                return this;
            },
 
            /**
             * Update the sort order of attributes
             */
            updateAttributeOrders: function () {
                var sortOrder = _.reduce(this.$('.attribute'), function (previous, current, order) {
                    var next = _.extend({}, previous);
                    next[current.dataset.attributeCode] = order;
 
                    return next;
                }, {});
                var attributeGroup = _.extend({}, this.getFormData());
                attributeGroup.attributes_sort_order = sortOrder;
 
                this.setData(attributeGroup);
 
                this.render();
            },
 
            /**
             * Add attributes to the model
             *
             * @param {Event}
             */
            addAttributes: function (event) {
                var attributeGroup = _.extend({}, this.getFormData());
                attributeGroup.attributes = _.union(attributeGroup.attributes, event.codes);
                this.otherAttributes = _.difference(this.otherAttributes, event.codes);
 
                this.setData(attributeGroup);
 
                this.render();
            },
 
            /**
             * Add attributes to the model
             *
             * @param {Event}
             */
            removeAttributeRequest: function (event) {
                Iif (!SecurityContext.isGranted(this.config.removeAttributeACL)) {
                    return;
                }
 
                var code = event.currentTarget.dataset.attributeCode;
 
                Dialog.confirmDelete(
                    __(this.config.confirmation.message, {attribute: code}),
                    __(this.config.confirmation.title),
                    function () {
                        this.removeAttribute(code);
                    }.bind(this),
                    __(this.config.confirmation.subTitle),
                    __(this.config.confirmation.buttonText)
                );
            },
 
            /**
             * Remove attribute from collection
             *
             * @param {string} code
             */
            removeAttribute: function (code) {
                var attributeGroup = _.extend({}, this.getFormData());
                attributeGroup.attributes = _.without(attributeGroup.attributes, code);
                delete attributeGroup.attributes_sort_order[code];
                this.otherAttributes = _.union(this.otherAttributes, [code]);
 
                this.setData(attributeGroup);
 
                this.render();
            },
 
            /**
             * Get the list of attributes that are in the 'other' group
             *
             * @return {array}
             */
            getOtherAttributes: function () {
                return this.otherAttributes;
            },
 
            /**
             * Does the user has right to remove an attribute
             *
             * @return {Boolean}
             */
            hasRightToRemove: function () {
                var currentAttributeGroupIsNotOther = this.config.otherGroup !== this.getFormData().code;
 
                return currentAttributeGroupIsNotOther &&
                    SecurityContext.isGranted(this.config.removeAttributeACL)
            },
 
            /**
             * Does the user has right to add an attribute
             *
             * @return {Boolean}
             */
            hasRightToAdd: function () {
                var currentAttributeGroupIsNotOther = this.config.otherGroup !== this.getFormData().code;
 
                return currentAttributeGroupIsNotOther &&
                    SecurityContext.isGranted(this.config.addAttributeACL)
            }
        });
    });