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

40% Statements 10/25
25% Branches 1/4
45.45% Functions 5/11
40% Lines 10/25

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                  6x                                                       6x                       6x       6x               6x 6x   24x             6x 6x                                 6x                                                                                      
'use strict';
 
/**
 * Display the list of attribute groups
 *
 * @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/fetcher-registry',
    'pim/common/property',
    'routing',
    'pim/router',
    'pim/user-context',
    'pim/i18n',
    'pim/security-context',
    'pim/template/form/attribute-group/list'
],
    function (
        $,
        _,
        __,
        BaseForm,
        FetcherRegistry,
        propertyAccessor,
        Routing,
        router,
        UserContext,
        i18n,
        securityContext,
        template
    ) {
        return BaseForm.extend({
            className: 'tabsection',
            template: _.template(template),
            attributeGroups: [],
            events: {
                'click .attribute-group-link': 'redirectToGroup'
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                return $.when(
                    FetcherRegistry.getFetcher('attribute-group').fetchAll(),
                    BaseForm.prototype.configure.apply(this, arguments)
                ).then(function (attributeGroups) {
                    this.attributeGroups = attributeGroups;
                }.bind(this));
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                const canSortAttributeGroup = securityContext.isGranted('pim_enrich_attributegroup_sort');
                this.$el.html(this.template({
                    attributeGroups: _.sortBy(_.values(this.attributeGroups), function (attributeGroup) {
                        return attributeGroup.sort_order;
                    }),
                    i18n: i18n,
                    uiLocale: UserContext.get('catalogLocale'),
                    canSortAttributeGroup
                }));
 
                Eif (canSortAttributeGroup) {
                    this.$('tbody').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.renderExtensions();
            },
 
            /**
             * Update the attribute order based on the dom
             */
            updateAttributeOrders: function () {
                var sortOrder = _.reduce(this.$('.attribute-group'), function (previous, current, order) {
                    var next = _.extend({}, previous);
                    next[current.dataset.attributeGroupCode] = order;
 
                    return next;
                }, {});
 
                $.ajax({
                    url: Routing.generate('pim_enrich_attributegroup_rest_sort'),
                    type: 'PATCH',
                    data: JSON.stringify(sortOrder)
                }).then(function (attributeGroups) {
                    this.attributeGroups = attributeGroups;
 
                    FetcherRegistry.getFetcher('attribute-group').clear();
 
                    this.render();
                }.bind(this));
            },
 
            /**
             * Redirect to attribute group page
             *
             * @param {event} event
             */
            redirectToGroup: function (event) {
                if (securityContext.isGranted('pim_enrich_attributegroup_edit')) {
                    router.redirectToRoute(
                        'pim_enrich_attributegroup_edit',
                        {identifier: event.target.dataset.attributeGroupCode}
                    )
                }
            }
        });
    }
);