All files / web/bundles/pimui/js/product/form/meta groups.js

43.75% Statements 14/32
66.67% Branches 4/6
50% Functions 6/12
43.75% Lines 14/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 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                  265x                                                                       265x                                 322x   322x             805x       805x 805x   805x 26x             779x       805x                     805x   805x 26x                                                                                                                                                                
 'use strict';
/**
 * Product groups extension
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @author    Filips Alpe <filips@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',
        'oro/translator',
        'oro/mediator',
        'backbone',
        'pim/form',
        'routing',
        'pim/template/product/meta/groups',
        'pim/template/product/meta/group-modal',
        'pim/user-context',
        'pim/fetcher-registry',
        'pim/group-manager',
        'pim/router',
        'pim/i18n',
        'oro/loading-mask',
        'bootstrap-modal'
    ],
    function (
        $,
        _,
        __,
        mediator,
        Backbone,
        BaseForm,
        Routing,
        formTemplate,
        modalTemplate,
        UserContext,
        FetcherRegistry,
        GroupManager,
        router,
        i18n,
        LoadingMask
    ) {
        return BaseForm.extend({
            tagName: 'span',
 
            className: 'AknColumn-block product-groups',
 
            template: _.template(formTemplate),
 
            modalTemplate: _.template(modalTemplate),
 
            events: {
                'click .product-group': 'displayModal'
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.listenTo(this.getRoot(), 'pim_enrich:form:entity:post_update', this.render);
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                Iif (!this.configured) {
                    return this;
                }
 
                GroupManager.getProductGroups(this.getFormData()).done(function (groups) {
                    groups = this.prepareGroupsForTemplate(groups);
 
                    if (groups.length) {
                        this.$el.html(
                            this.template({
                                label: __('pim_enrich.entity.product.module.meta.groups'),
                                groups: groups
                            })
                        );
                    } else {
                        this.$el.html('');
                    }
                }.bind(this));
 
                return this;
            },
 
            /**
             * Prepare groups for being displayed in the template
             *
             * @param {Array} groups
             *
             * @returns {Array}
             */
            prepareGroupsForTemplate: function (groups) {
                var locale = UserContext.get('catalogLocale');
 
                return _.map(groups, function (group) {
                    return {
                        label: group.labels[locale] || '[' + group.code + ']',
                        code: group.code,
                        isVariant: 'VARIANT' === group.type
                    };
                });
            },
 
            /**
             * Get the product list for the given group
             *
             * @param {integer} groupCode
             *
             * @returns {Array}
             */
            getProductList: function (groupCode) {
                return $.getJSON(
                    Routing.generate('pim_enrich_group_rest_list_products', { identifier: groupCode })
                ).then(_.identity);
            },
 
            /**
             * Show the modal which displays infos about produt groups
             *
             * @param {Object} event
             */
            displayModal: function (event) {
                var loadingMask = new LoadingMask();
                loadingMask.render().$el.appendTo(this.getRoot().$el).show();
 
                GroupManager.getProductGroups(this.getFormData()).done(function (groups) {
                    var group = _.findWhere(groups, { code: event.currentTarget.dataset.group });
 
                    $.when(
                        this.getProductList(group.code),
                        FetcherRegistry.getFetcher('attribute').getIdentifierAttribute()
                    ).done(function (productList, identifierAttribute) {
                        loadingMask.remove();
                        this.groupModal = new Backbone.BootstrapModal({
                            okText: __('pim_enrich.entity.product.module.show_group.view_group'),
                            cancelText: __('pim_common.cancel'),
                            title: __(
                                'pim_enrich.entity.product.module.show_group.title',
                                { group: i18n.getLabel(
                                    group.labels,
                                    UserContext.get('catalogLocale'),
                                    group.code
                                )}
                            ),
                            content: this.modalTemplate({
                                products:     productList.products,
                                productCount: productList.productCount,
                                identifier:   identifierAttribute,
                                locale:       UserContext.get('catalogLocale')
                            }),
                            illustrationClass: 'group',
                        });
 
                        this.groupModal.on('ok', function visitGroup() {
                            this.groupModal.close();
                            var route = 'pim_enrich_group_edit';
                            var parameters = { code: group.code };
 
                            router.redirectToRoute(route, parameters);
                        }.bind(this));
                        this.groupModal.open();
 
                        this.groupModal.$el.on('click', 'a[data-product-id]', function visitProduct(event) {
                            this.groupModal.close();
                            router.redirectToRoute(
                                'pim_enrich_product_edit',
                                { id: event.currentTarget.dataset.productId }
                            );
                        }.bind(this));
                    }.bind(this));
                }.bind(this));
            }
        });
    }
);