All files / web/bundles/pimui/js/product/field metric-field.js

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

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                  98x                 98x           348x 348x   348x     348x       348x   348x       16x     219x 219x   219x              
'use strict';
/**
 * Metric field
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @author    Filips Alpe <filips@akeneo.com>
 * @copyright 2015 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define([
    'jquery',
    'pim/field',
    'underscore',
    'oro/translator',
    'pim/fetcher-registry',
    'pim/template/product/field/metric',
    'pim/initselect2'
], function ($, Field, _, __, FetcherRegistry, fieldTemplate, initSelect2) {
    return Field.extend({
        fieldTemplate: _.template(fieldTemplate),
        events: {
            'change .field-input:first .data, .field-input:first .unit': 'updateModel'
        },
        renderInput: function (context) {
            const $element = $(this.fieldTemplate(_.extend({}, context, {__: __})));
            initSelect2.init($element.find('.unit'));
 
            return $element;
        },
        getTemplateContext: function () {
            return $.when(
                Field.prototype.getTemplateContext.apply(this, arguments),
                FetcherRegistry.getFetcher('measure').fetchAll()
            ).then(function (templateContext, measures) {
                templateContext.measures = measures;
 
                return templateContext;
            });
        },
        setFocus: function () {
            this.$('.data:first').focus();
        },
        updateModel: function () {
            var amount = this.$('.field-input:first .data').val();
            var unit = this.$('.field-input:first .unit').select2('val');
 
            this.setCurrentValue({
                unit: '' !== unit ? unit : this.attribute.default_metric_unit,
                amount: '' !== amount ? amount : null
            });
        }
    });
});