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

90.32% Statements 28/31
50% Branches 6/12
100% Functions 11/11
90.32% Lines 28/31

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                45x                                             45x                     55x   55x                 141x       141x 140x       140x                   140x       140x   140x     141x                     140x   140x 293x   293x                   140x                     3x                 3x 3x   3x 3x   3x 3x                   280x   280x       280x                 283x          
'use strict';
/**
 * Scope 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/scope',
        'pim/form',
        'pim/fetcher-registry',
        'pim/user-context',
        'jquery.select2',
        'pim/i18n'
    ],
    function (
        $,
        _,
        __,
        template,
        BaseForm,
        fetcherRegistry,
        UserContext,
        select2,
        i18n
    ) {
        return BaseForm.extend({
            config: {},
            className: 'AknFieldContainer',
            template: _.template(template),
 
            /**
             * Initializes configuration.
             *
             * @param {Object} config
             */
            initialize: function (config) {
                this.config = config.config;
 
                return BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * Renders scopes dropdown.
             *
             * @return {Object}
             */
            render: function () {
                Iif (!this.configured) {
                    return this;
                }
 
                fetcherRegistry.getFetcher('channel').fetchAll().then(function (channels) {
                    Iif (!this.getScope()) {
                        this.setScope(_.first(channels).code);
                    }
 
                    this.$el.html(
                        this.template({
                            isEditable: this.isEditable(),
                            __: __,
                            channels: this.setChannelLabels(channels),
                            scope: this.getScope(),
                            errors: this.getParent().getValidationErrorsForField('scope')
                        })
                    );
 
                    this.$('.select2')
                        .select2({minimumResultsForSearch: -1})
                        .on('change', this.updateState.bind(this));
 
                    this.$('[data-toggle="tooltip"]').tooltip();
 
                    this.renderExtensions();
                }.bind(this));
 
                return this;
            },
 
            /**
             * Sets fallback labels for channels without a translation
             *
             * @param {Array} channels
             *
             * @return {Array}
             */
            setChannelLabels: function (channels) {
                var locale = UserContext.get('uiLocale');
 
                return _.map(channels, function (channel) {
                    channel.label = i18n.getLabel(channel.labels, locale, channel.code);
 
                    return channel;
                });
            },
 
            /**
             * Returns whether this filter is editable.
             *
             * @returns {boolean}
             */
            isEditable: function () {
                return undefined !== this.config.readOnly ?
                    !this.config.readOnly :
                    true;
            },
 
            /**
             * Sets new scope on field change.
             *
             * @param {Object} event
             */
            updateState: function (event) {
                this.setScope(event.target.value);
            },
 
            /**
             * Sets specified scope into root model.
             *
             * @param {String} code
             */
            setScope: function (code) {
                var data = this.getFilters();
                var before = data.structure.scope;
 
                data.structure.scope = code;
                this.setData(data);
 
                Eif (before !== code) {
                    this.getRoot().trigger('channel:update:after', data.structure.scope);
                }
            },
 
            /**
             * Gets scope from root model.
             *
             * @returns {String}
             */
            getScope: function () {
                var structure = this.getFilters().structure;
 
                Iif (_.isUndefined(structure)) {
                    return null;
                }
 
                return _.isUndefined(structure.scope) ? null : structure.scope;
            },
 
            /**
             * Get filters
             *
             * @return {object}
             */
            getFilters: function () {
                return this.getFormData().configuration.filters;
            }
        });
    }
);