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

90% Statements 18/20
50% Branches 1/2
100% Functions 2/2
90% Lines 18/20

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                  299x                                           299x                           497x   497x             497x   497x 497x 497x 497x                         497x 497x   497x                           497x   497x                   1646x               26x 26x 26x        
'use strict';
 
/**
 * Extension to render a list of activated locales used for the product grid.
 *
 * @author    Tamara Robichet <tamara.robichet@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/template/product/grid/locale-switcher',
        'pim/fetcher-registry',
        'pim/i18n',
        'pim/router',
        'pim/user-context'
    ], function (
        $,
        _,
        __,
        BaseForm,
        template,
        FetcherRegistry,
        i18n,
        router,
        UserContext
    ) {
        return BaseForm.extend({
            template: _.template(template),
            config: {},
            locales: [],
            id: 'locale-switcher',
            className: 'AknDropdown AknColumn-block locale-switcher',
            events: {
                'click [data-locale]': 'changeLocale'
            },
 
            /**
             * {@inheritdoc}
             */
            initialize(config) {
                this.config = config.config;
 
                return BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
            */
            configure() {
                return $.when(
                    this.fetchLocales().then(locales => {
                        this.locales = locales;
                        const currentLocaleCode = UserContext.get('catalogLocale');
                        let currentLocale = _.find(this.locales, {code: currentLocaleCode});
                        Iif (undefined === currentLocale) {
                            currentLocale = _.first(this.locales);
                            UserContext.set('catalogLocale', currentLocale.code);
                        }
                    }),
                    BaseForm.prototype.configure.apply(this, arguments)
                );
            },
 
            /**
             * {@inheritdoc}
             */
            render() {
                const currentLocaleCode = UserContext.get('catalogLocale');
                let currentLocale = _.find(this.locales, { code: currentLocaleCode });
 
                this.$el.empty().append(this.template({
                    localeLabel: __('pim_enrich.entity.locale.uppercase_label'),
                    locales: this.locales,
                    currentLocale,
                    i18n,
                    getDisplayName: this.getDisplayName
                }));
            },
 
            /**
             * Fetch the activated locales to render in the list
             * @return {Array} An array of activated locales
             */
            fetchLocales() {
                const localeFetcher = FetcherRegistry.getFetcher('locale');
 
                return localeFetcher.fetchActivated();
            },
 
            /**
             * Returns the string to display for a locale
             *
             * @param  {Object} locale The original locale
             * @return {String}        The translated locale
             */
            getDisplayName(locale) {
                return locale.language;
            },
 
            /**
             * Switches locales by visiting the product grid route
             * @param  {Event} event The click event coming from the locale dropdown list
             */
            changeLocale(event) {
                const { localeParamName } = this.config;
                const localeCode = this.$(event.currentTarget).attr('data-locale');
                router.redirectToRoute(this.config.routeName, { [localeParamName]: localeCode });
            }
        });
    });