All files / web/bundles/pimui/js/job/product/edit/content/structure locales.js

90.63% Statements 29/32
50% Branches 6/12
93.33% Functions 14/15
90.63% Lines 29/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                45x                                   45x                   55x   55x                 55x   55x                 144x       144x     143x                   143x 143x   143x     144x                 143x                                       3x 3x   3x 3x   3x 3x                   143x   143x       143x             3x   3x                   3x     3x                   3x                 293x          
'use strict';
/**
 * Locale structure filter
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @copyright 2016 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/template/export/product/edit/content/structure/locales',
        'pim/form',
        'pim/fetcher-registry',
        'jquery.select2'
    ],
    function (
        $,
        _,
        __,
        template,
        BaseForm,
        fetcherRegistry
    ) {
        return BaseForm.extend({
            className: 'AknFieldContainer',
            template: _.template(template),
 
            /**
             * Initializes configuration.
             *
             * @param {Object} config
             */
            initialize: function (config) {
                this.config = config.config;
 
                return BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * Configures this extension.
             *
             * @return {Promise}
             */
            configure: function () {
                this.listenTo(this.getRoot(), 'channel:update:after', this.channelUpdated.bind(this));
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * Renders locales dropdown.
             *
             * @returns {Object}
             */
            render: function () {
                Iif (!this.configured) {
                    return this;
                }
 
                fetcherRegistry.getFetcher('channel')
                    .fetch(this.getFilters().structure.scope)
                    .always(function (scope) {
                        this.$el.html(
                            this.template({
                                isEditable: this.isEditable(),
                                __: __,
                                locales: this.getLocales(),
                                availableLocales: !scope ? [] : scope.locales,
                                errors: this.getParent().getValidationErrorsForField('locales')
                            })
                        );
 
                        this.$('.select2').select2().on('change', this.updateState.bind(this));
                        this.$('[data-toggle="tooltip"]').tooltip();
 
                        this.renderExtensions();
                    }.bind(this));
 
                return this;
            },
 
            /**
             * Returns whether this filter is editable.
             *
             * @returns {boolean}
             */
            isEditable: function () {
                return undefined !== this.config.readOnly ?
                    !this.config.readOnly :
                    true;
            },
 
            /**
             * Sets new locales on field change.
             *
             * @param {Object} event
             */
            updateState: function (event) {
                this.setLocales(event.val);
            },
 
            /**
             * Sets specified locales into root model.
             *
             * @param {Array} codes
             */
            setLocales: function (codes) {
                var data = this.getFilters();
                var before = data.structure.locales;
 
                data.structure.locales = codes;
                this.setData(data);
 
                Eif (before !== codes) {
                    this.getRoot().trigger('locales:update:after', codes);
                }
            },
 
            /**
             * Gets locales from root model.
             *
             * @returns {Array}
             */
            getLocales: function () {
                var structure = this.getFilters().structure;
 
                Iif (_.isUndefined(structure)) {
                    return [];
                }
 
                return _.isUndefined(structure.locales) ? [] : structure.locales;
            },
 
            /**
             * Resets locales after channel has been modified then re-renders the view.
             */
            channelUpdated: function () {
                this.initializeDefaultLocales()
                    .then(function () {
                        this.render();
                    }.bind(this));
            },
 
            /**
             * Sets locales corresponding to the current scope (default state).
             *
             * @return {Promise}
             */
            initializeDefaultLocales: function () {
                return fetcherRegistry.getFetcher('channel')
                    .fetch(this.getCurrentScope())
                    .then(function (scope) {
                        this.setLocales(_.pluck(scope.locales, 'code'));
                    }.bind(this));
            },
 
            /**
             * Gets current scope from root model.
             *
             * @return {String}
             */
            getCurrentScope: function () {
                return this.getFilters().structure.scope;
            },
 
            /**
             * Get filters
             *
             * @return {object}
             */
            getFilters: function () {
                return this.getFormData().configuration.filters;
            }
        });
    }
);