All files / web/bundles/pimui/js/form/common/fields metric.js

85.71% Statements 18/21
100% Branches 2/2
83.33% Functions 5/6
85.71% Lines 18/21

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              2x                             2x     6x 6x                               10x 10x   10x             10x             10x             40x                           80x                 40x       40x                             40x                                             40x   40x   440x               6x              
/**
 * @author    Yohan Blain <yohan.blain@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)
 */
'use strict';
 
define([
    'jquery',
    'underscore',
    'pim/form/common/fields/field',
    'oro/translator',
    'pim/fetcher-registry',
    'pim/template/form/common/fields/metric'
], function (
    $,
    _,
    BaseField,
    __,
    FetcherRegistry,
    template
) {
    return BaseField.extend({
        events: {
            'keyup input': function () {
                this.errors = [];
                this.updateModel(this.getValue());
            },
            'change select': function () {
                this.errors = [];
                this.updateModel(this.getValue());
                this.getRoot().render();
            }
        },
        template: _.template(template),
        metricFamily: null,
        defaultMetricUnit: null,
 
        /**
         * {@inheritdoc}
         */
        initialize() {
            this.metricFamily = null;
            this.defaultMetricUnit = null;
 
            return BaseField.prototype.initialize.apply(this, arguments);
        },
 
        /**
         * @param {String} metricFamily
         */
        setMetricFamily(metricFamily) {
            this.metricFamily = metricFamily;
        },
 
        /**
         * @param {String} defaultMetricUnit
         */
        setDefaultMetricUnit(defaultMetricUnit) {
            this.defaultMetricUnit = defaultMetricUnit;
        },
 
        /**
         * {@inheritdoc}
         */
        renderInput(templateContext) {
            return this.template(_.extend(templateContext, {
                value: {
                    amount: this.getModelValuePart('amount'),
                    unit: this.getModelValuePart('unit')
                }
            }));
        },
 
        /**
         * @param {String} name
         *
         * @returns {*|null}
         */
        getModelValuePart(name) {
            return undefined !== this.getModelValue()
                ? this.getModelValue()[name]
                : null;
        },
 
        /**
         * {@inheritdoc}
         */
        getTemplateContext: function () {
            return $.when(
                BaseField.prototype.getTemplateContext.apply(this, arguments),
                FetcherRegistry.getFetcher('measure').fetchAll()
            ).then((parentContext, measures) => {
                return Object.assign(
                    {},
                    parentContext,
                    {
                        unitChoices: this.formatChoices(measures[this.metricFamily].units),
                        defaultUnit: this.defaultMetricUnit
                    }
                );
            });
        },
 
        /**
         * {@inheritdoc}
         */
        postRender() {
            this.$('select.select2').select2({allowClear: true});
        },
 
        /**
         * Transforms:
         *
         * {
         *     BIT: {...},
         *     BYTE: {...}
         * }
         *
         * into:
         *
         * {
         *     BIT: "Bit",
         *     BYTE: "Octet"
         * }
         *
         * (for locale fr_FR)
         *
         * @param {Object} units
         */
        formatChoices(units) {
            const unitCodes = Object.keys(units);
 
            return _.object(
                unitCodes,
                unitCodes.map(unitCode => __(unitCode))
            );
        },
 
        /**
         * @return {Object}
         */
        getValue() {
            return {
                amount: this.$('.amount').val(),
                unit: this.$('.unit').val()
            };
        }
    });
});