All files / web/bundles/pimui/js/form/common/properties translation.js

100% Statements 26/26
75% Branches 3/4
100% Functions 12/12
100% Lines 26/26

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                  184x                           184x                         229x   229x             229x           229x           229x           229x     229x                   125x   125x                 12x   12x             411x                 411x   411x             246x             66x   66x 2x     66x   66x             16x   16x   16x                       245x            
'use strict';
 
/**
 * Module used to display the localized properties of an object
 *
 * @author    Nicolas Dupont <nicolas@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',
        'pim/form',
        'pim/fetcher-registry',
        'pim/template/form/properties/translation'
    ],
    function (
        $,
        _,
        BaseForm,
        FetcherRegistry,
        template
    ) {
        return BaseForm.extend({
            className: 'translation-container',
            template: _.template(template),
            events: {
                'change .label-field': 'updateModel'
            },
            validationErrors: {},
            locales: [],
 
            /**
             * {@inheritdoc}
             */
            initialize: function (config) {
                this.config = config.config;
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.listenTo(
                    this.getRoot(),
                    'pim_enrich:form:entity:pre_save',
                    this.onPreSave
                );
 
                this.listenTo(
                    this.getRoot(),
                    'pim_enrich:form:entity:bad_request',
                    this.onValidationError
                );
 
                this.listenTo(
                    this.getRoot(),
                    'pim_enrich:form:entity:locales_updated',
                    this.onLocalesUpdated.bind(this)
                );
 
                return $.when(
                    this.getLocales(true)
                        .then(function (locales) {
                            this.locales = locales;
                        }.bind(this)),
                    BaseForm.prototype.configure.apply(this, arguments)
                );
            },
 
            /**
             * Pre save callback
             */
            onPreSave: function () {
                this.validationErrors = {};
 
                this.render();
            },
 
            /**
             * On validation callback
             *
             * @param {Event} event
             */
            onValidationError: function (event) {
                this.validationErrors = event.response.translations ? event.response.translations : {};
 
                this.render();
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.$el.html(this.template({
                    model: this.getFormData(),
                    locales: this.locales,
                    errors: this.validationErrors,
                    label: this.config.label,
                    fieldBaseId: this.config.fieldBaseId,
                    isReadOnly: this.isReadOnly()
                }));
 
                this.delegateEvents();
 
                this.renderExtensions();
            },
 
            /**
             * @returns {Boolean}
             */
            isReadOnly: function () {
                return false;
            },
 
            /**
             * @param {Object} event
             */
            updateModel: function (event) {
                var data = this.getFormData();
 
                if (Array.isArray(data.labels)) {
                    data.labels = {};
                }
 
                data.labels[event.target.dataset.locale] = event.target.value;
 
                this.setData(data);
            },
 
            /**
             * Updates locales if were updated
             */
            onLocalesUpdated: function () {
                this.getLocales(false)
                    .then(function (locales) {
                            this.locales = locales;
 
                            return this.render();
                    }.bind(this));
            },
 
            /**
             * Fetches and returns activated locales.
             *
             * @param {Boolean} cached
             *
             * @returns {Promise}
             */
            getLocales: function (cached) {
                return FetcherRegistry.getFetcher('locale')
                    .search({activated: true, cached: cached});
            }
        });
    }
);