All files / web/bundles/pimui/js/product/form scope-switcher.js

100% Statements 21/21
75% Branches 3/4
100% Functions 9/9
100% Lines 21/21

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                  327x                                     327x                           780x 780x     780x             766x 50x 44x 44x       766x             492x     492x       492x   492x   492x                               492x     492x                 34x         34x                 13x                 13x          
'use strict';
/**
 * Scope switcher 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(
    [
        'underscore',
        'oro/translator',
        'pim/form',
        'pim/template/product/scope-switcher',
        'pim/fetcher-registry',
        'pim/user-context',
        'pim/i18n'
    ],
    function (
        _,
        __,
        BaseForm,
        template,
        FetcherRegistry,
        UserContext,
        i18n
    ) {
        return BaseForm.extend({
            template: _.template(template),
            className: 'AknDropdown AknButtonList-item scope-switcher',
            events: {
                'click li a': 'changeScope'
            },
            displayInline: false,
            displayLabel: true,
            config: {},
 
            /**
             * {@inheritdoc}
             */
            initialize: function (config) {
                Eif (undefined !== config) {
                    this.config = config.config;
                }
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.listenTo(this.getRoot(), 'pim_enrich:form:locale_switcher:change', function (localeEvent) {
                    if ('base_product' === localeEvent.context) {
                        UserContext.set('catalogLocale', localeEvent.localeCode);
                        this.render();
                    }
                }.bind(this));
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                FetcherRegistry.getFetcher('channel')
                    .fetchAll()
                    .then(function (channels) {
                        const params = {
                            scopeCode: channels[0].code,
                            context: this.config.context
                        };
                        this.getRoot().trigger('pim_enrich:form:scope_switcher:pre_render', params);
 
                        var scope = _.findWhere(channels, { code: params.scopeCode });
 
                        this.$el.html(
                            this.template({
                                channels: channels,
                                currentScope: i18n.getLabel(
                                    scope.labels,
                                    UserContext.get('catalogLocale'),
                                    scope.code
                                ),
                                catalogLocale: UserContext.get('catalogLocale'),
                                i18n: i18n,
                                displayInline: this.displayInline,
                                displayLabel: this.displayLabel,
                                label: __('pim_enrich.entity.channel.uppercase_label')
                            })
                        );
 
                        this.delegateEvents();
                    }.bind(this));
 
                return this;
            },
 
            /**
             * Set the current selected scope
             *
             * @param {Event} event
             */
            changeScope: function (event) {
                this.getRoot().trigger('pim_enrich:form:scope_switcher:change', {
                    scopeCode: event.currentTarget.dataset.scope,
                    context: this.config.context
                });
 
                this.render();
            },
 
            /**
             * Updates the inline display value
             *
             * @param {Boolean} value
             */
            setDisplayInline: function (value) {
                this.displayInline = value;
            },
 
            /**
             * Updates the display label value
             *
             * @param {Boolean} value
             */
            setDisplayLabel: function (value) {
                this.displayLabel = value;
            }
        });
    }
);