All files / web/bundles/pimui/js/form/common/attributes copy-field.js

77.78% Statements 21/27
25% Branches 1/4
90% Functions 9/10
77.78% Lines 21/27

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                  302x                 302x                         35x 35x   35x                   73x   73x                   73x   73x 73x   73x 73x 73x     73x   73x               73x 88x                                         35x             1x 1x                 25x          
'use strict';
/**
 * Copy field extension
 *
 * @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([
        'backbone',
        'underscore',
        'pim/field',
        'pim/template/form/tab/attribute/copy-field',
        'pim/i18n',
        'oro/mediator'
    ],
    function (Backbone, _, Field, template, i18n, mediator) {
        return Field.extend({
            tagName: 'div',
            field: null,
            template: _.template(template),
            selected: false,
            events: {
                'click': 'onSelect'
            },
 
            /**
             * Initialize the view
             */
            initialize: function () {
                this.selected = false;
                this.field    = null;
 
                Field.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * Render the copy field view
             * Delegates the render of the input itself to the Field.renderCopyInput() method
             *
             * @returns {Object}
             */
            render: function () {
                this.$el.empty();
 
                var templateContext = {
                    type: this.field.attribute.field_type,
                    label: this.field.getLabel(),
                    config: this.field.config,
                    attribute: this.field.attribute,
                    selected: this.selected,
                    context: this.context,
                    i18n: i18n
                };
 
                mediator.trigger('pim_enrich:form:field:extension:add', {'field': this, 'promises': []});
 
                this.$el.html(this.template(templateContext));
                this.field.renderCopyInput(this.getCurrentValue())
                    .then(function (render) {
                        this.$('.field-input').html(render);
                        this.renderElements();
                        this.field.postRender();
                    }.bind(this));
 
                this.delegateEvents();
 
                return this;
            },
 
            /**
             * Render elements of this field in different available positions.
             * In the copy case, only implements extension on input position.
             */
            renderElements: function () {
                _.each(this.elements, function (elements, position) {
                    Iif ('field-input' === position) {
                        var $container = this.$('.field-input');
                        $container.empty();
 
                        _.each(elements, function (element) {
                            if (typeof element.render === 'function') {
                                $container.append(element.render().$el);
                            } else {
                                $container.append(element);
                            }
                        }.bind(this));
                    }
                }.bind(this));
            },
 
            /**
             * Bound this copy field to the original field
             *
             * @param {Field} field
             */
            setField: function (field) {
                this.field = field;
            },
 
            /**
             * Callback called when the copy field is clicked, toggle the select checkbox state
             */
            onSelect: function () {
                this.selected = !this.selected;
                this.$('.copy-field-selector').prop('checked', this.selected);
            },
 
            /**
             * Mark this copy field as selected or not
             *
             * @param {boolean} selected
             */
            setSelected: function (selected) {
                this.selected = selected;
            }
        });
    }
);