All files / web/bundles/pimui/js/product/form/attributes/add-select/attribute select.js

100% Statements 17/17
100% Branches 4/4
100% Functions 10/10
100% Lines 17/17

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                  175x                                         175x                                                       98x                   137x 2x     137x   137x   137x   137x                                   196x 218x                     196x 218x 218x 218x   218x               119x          
'use strict';
 
/**
 * Product add attribute select extension view
 *
 * @author    Alexandr Jeliuc <alex@jeliuc.com>
 * @copyright 2017 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',
        'pim/common/add-select',
        'pim/product/add-select/attribute/line',
        'pim/fetcher-registry',
        'pim/attribute-manager',
        'pim/formatter/choices/base'
    ],
    function (
        $,
        _,
        __,
        BaseAddSelect,
        LineView,
        FetcherRegistry,
        AttributeManager,
        ChoicesFormatter
    ) {
        return BaseAddSelect.extend({
            className: 'AknButtonList-item add-attribute',
            lineView: LineView,
            defaultConfig: {
                select2: {
                    placeholder: 'pim_common.add_attributes',
                    title: 'pim_common.select2.search',
                    buttonTitle: 'pim_common.add',
                    countTitle: 'pim_enrich.entity.attribute.module.add_attribute.attributes_selected',
                    emptyText: 'pim_enrich.entity.attribute.module.add_attribute.no_available_attributes',
                    classes: 'pim-add-attributes-multiselect',
                    minimumInputLength: 0,
                    dropdownCssClass: 'add-attribute',
                    closeOnSelect: false
                },
                resultsPerPage: 10,
                mainFetcher: 'attribute',
                events: {
                    disable: null,
                    enable: null,
                    add: 'add-attribute:add'
                }
            },
 
            /**
             * {@inheritdoc}
             */
            getItemsToExclude: function () {
                return $.Deferred().resolve(_.keys(this.getFormData().values));
            },
 
 
            /**
             * This method is overridden to fetch attribute groups and set it inside attribute items.
             *
             * {@inheritdoc}
             */
            fetchItems: function (searchParameters) {
                if (undefined !== this.config.attributeTypes && Array.isArray(this.config.attributeTypes)) {
                    searchParameters.types = this.config.attributeTypes.join(',');
                }
 
                return BaseAddSelect.prototype.fetchItems.apply(this, [searchParameters])
                    .then(function (attributes) {
                        var groupCodes = _.unique(_.pluck(attributes, 'group'));
 
                        return FetcherRegistry.getFetcher('attribute-group').fetchByIdentifiers(groupCodes)
                            .then(function (attributeGroups) {
                                return this.populateGroupProperties(attributes, attributeGroups);
                            }.bind(this));
                    }.bind(this));
            },
 
            /**
             * Transforms each attribute
             *
             * { code: 'name', group: 'marketing', ...  }
             *
             * into
             *
             * { code: 'name', group: { code: 'marketing', sort_order: 2, ... }, ...  }
             *
             * @param {Array} attributes
             * @param {Array} attributeGroups
             */
            populateGroupProperties: function (attributes, attributeGroups) {
                return _.map(attributes, function (attribute) {
                    return _.extend(
                        attribute,
                        {group: _.findWhere(attributeGroups, {code: attribute.group})}
                    );
                });
            },
 
            /**
             * {@inheritdoc}
             */
            prepareChoices: function (items) {
                return _.chain(items).map(function (item) {
                    var group = ChoicesFormatter.formatOne(item.group);
                    var choice = ChoicesFormatter.formatOne(item);
                    choice.group = group;
 
                    return choice;
                }).value();
            },
 
            /**
             * Triggers configured event with items codes selected
             */
            addItems: function () {
                this.trigger(this.addEvent, { codes: this.selection });
            }
        });
    }
);