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

95.83% Statements 23/24
66.67% Branches 4/6
100% Functions 11/11
95.65% Lines 22/23

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                  327x                                 327x                           795x 795x     795x             766x 50x 44x                 500x   500x   500x       500x   794x 500x       500x                   500x     500x                 500x                 27x         27x                 28x                 28x          
'use strict';
/**
 * Locale 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/locale-switcher',
        'pim/fetcher-registry',
        'pim/i18n'
    ],
    function (
        _,
        __,
        BaseForm,
        template,
        FetcherRegistry,
        i18n
    ) {
        return BaseForm.extend({
            template: _.template(template),
            className: 'AknDropdown AknButtonList-item locale-switcher',
            events: {
                'click li span[data-locale]': 'changeLocale'
            },
            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) {
                        this.render();
                    }
                }.bind(this));
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.getDisplayedLocales()
                    .done(function (locales) {
                        this.$el.removeClass('open');
 
                        const params = {
                            localeCode: _.first(locales).code,
                            context: this.config.context
                        };
                        this.getRoot().trigger('pim_enrich:form:locale_switcher:pre_render', params);
 
                        let currentLocale = locales.find(locale => locale.code === params.localeCode);
                        Iif (undefined === currentLocale) {
                            currentLocale = _.first(locales);
                        }
 
                        this.$el.html(
                            this.template({
                                locales: locales,
                                currentLocale,
                                i18n: i18n,
                                displayInline: this.displayInline,
                                displayLabel: this.displayLabel,
                                label: __('pim_enrich.entity.locale.uppercase_label')
                            })
                        );
                        this.delegateEvents();
                    }.bind(this));
 
                return this;
            },
 
            /**
             * Retrieve locales to display in the locale switcher
             *
             * @returns {Promise}
             */
            getDisplayedLocales: function () {
                return FetcherRegistry.getFetcher('locale').fetchActivated();
            },
 
            /**
             * Method triggered on the 'change locale' event
             *
             * @param {Object} event
             */
            changeLocale: function (event) {
                this.getRoot().trigger('pim_enrich:form:locale_switcher:change', {
                    localeCode: event.currentTarget.dataset.locale,
                    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;
            }
        });
    }
);