All files / web/bundles/pimui/js/job/product/edit/content/structure attributes.js

96.88% Statements 31/32
75% Branches 6/8
100% Functions 7/7
96.88% Lines 31/32

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                45x                                                     45x                           55x   55x             145x       145x   145x                             145x   145x 145x                 145x           4x 4x 4x 4x 4x 4x 4x   4x                         4x 4x   4x 4x   4x 4x 4x   4x   4x 4x 4x                   153x          
'use strict';
/**
 * Attributes structure filter
 *
 * @author    Julien Sanchez <julien@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',
        'oro/translator',
        'backbone',
        'pim/template/export/product/edit/content/structure/attributes',
        'pim/template/common/modal-centered',
        'pim/form',
        'oro/loading-mask',
        'pim/fetcher-registry',
        'pim/user-context',
        'pim/job/product/edit/content/structure/attributes-selector'
    ],
    function (
        $,
        _,
        __,
        Backbone,
        template,
        modalTemplate,
        BaseForm,
        LoadingMask,
        fetcherRegistry,
        UserContext,
        AttributeSelector
    ) {
        return BaseForm.extend({
            className: 'AknFieldContainer attributes',
            template: _.template(template),
            modalTemplate: _.template(modalTemplate),
            events: {
                'click button': 'openSelector'
            },
 
            /**
             * Initializes configuration.
             *
             * @param {Object} config
             */
            initialize: function (config) {
                this.config = config.config;
 
                return BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                Iif (!this.configured) {
                    return this;
                }
 
                var attributes = this.getFilters().structure.attributes || [];
 
                this.$el.html(
                    this.template({
                        __: __,
                        isEditable: this.isEditable(),
                        titleEdit: __('pim_enrich.entity.attribute.plural_label'),
                        labelEdit: __('pim_common.edit'),
                        labelInfo: __(
                            'pim_enrich.export.product.filter.attributes.label',
                            {count: attributes.length},
                            attributes.length
                        ),
                        errors: this.getParent().getValidationErrorsForField('attributes')
                    })
                );
 
                this.delegateEvents();
 
                this.$('[data-toggle="tooltip"]').tooltip();
                this.renderExtensions();
            },
 
            /**
             * Returns whether this filter is editable.
             *
             * @returns {boolean}
             */
            isEditable: function () {
                return undefined !== this.config.readOnly ?
                    !this.config.readOnly :
                    true;
            },
 
            openSelector: function (e) {
                e.preventDefault();
                var loadingMask = new LoadingMask();
                loadingMask.render().$el.appendTo(this.getRoot().$el);
                loadingMask.show();
                var selectedAttributes = this.getFilters().structure.attributes || [];
                var attributeSelector = new AttributeSelector();
                attributeSelector.setSelected(selectedAttributes);
 
                var modal = new Backbone.BootstrapModal({
                    modalOptions: {
                        backdrop: 'static',
                        keyboard: false
                    },
                    okCloses: false,
                    title: __('pim_enrich.entity.attribute.plural_label'),
                    innerDescription: __('pim_enrich.export.product.filter.attributes_selector.description'),
                    content: '',
                    okText: __('pim_common.apply'),
                    template: this.modalTemplate,
                });
 
                loadingMask.hide();
                loadingMask.$el.remove();
 
                modal.open();
                attributeSelector.setElement('.modal-body').render();
 
                modal.on('ok', function () {
                    var values = attributeSelector.getSelected();
                    var data = this.getFilters();
 
                    data.structure.attributes = values;
 
                    this.setData(data);
                    modal.close();
                    this.render();
                }.bind(this));
            },
 
            /**
             * Get filters
             *
             * @return {object}
             */
            getFilters: function () {
                return this.getFormData().configuration.filters;
            }
        });
    }
);