All files / web/bundles/pimui/js/formatter/choices base.js

100% Statements 7/7
100% Branches 0/0
100% Functions 4/4
100% Lines 7/7

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    1183x 1183x                                                                     66x 66x 174x     66x                                               678x              
'use strict';
 
define(['underscore', 'pim/user-context', 'pim/i18n'], function (_, UserContext, i18n) {
    return {
        /**
         * Format a collection of entities into a list of choices as follows.
         * From :
         * [
         *     {
         *         code: 'webcams',
         *         labels: {
         *             en_US:'Webcams',
         *             fr_FR:'Webcams',
         *             de_DE:'Webcams'
         *         }
         *     },
         *     {
         *         code: 'mugs',
         *         labels: {
         *             en_US: 'Mugs',
         *             fr_FR: 'Chopes\/Mugs',
         *             de_DE: 'Tassen'
         *         }
         *     }
         * ]
         *
         * to (for locale "de_DE") :
         *
         * [
         *     { id: 'webcams', text: 'Webcams' },
         *     { id: 'mugs', text: 'Tassen' }
         * ]
         *
         * @param {Array} entities
         *
         * @return {Array}
         */
        format: function (entities) {
            var choices = [];
            _.each(entities, function (entity) {
                choices.push(this.formatOne(entity));
            }.bind(this));
 
            return choices;
        },
 
        /**
         * Format an entity into a choice as follows.
         * From :
         * {
         *     code: 'webcams',
         *     label: {
         *         en_US:'Webcams',
         *         fr_FR:'Webcams',
         *         de_DE:'Webcams'
         *     }
         * }
         *
         * to (for locale "de_DE") :
         *
         * { id: 'webcams', text: 'Webcams' }
         *
         * @param {Object} entity
         *
         * @return {Object}
         */
        formatOne: function (entity) {
            return {
                id: entity.code,
                text: i18n.getLabel(entity.labels, UserContext.get('catalogLocale'), entity.code)
            };
        }
    };
});