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

80.95% Statements 51/63
66.67% Branches 12/18
90.48% Functions 19/21
80.95% Lines 51/63

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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249                45x                                             45x                                 4x 15x                   8x       8x 8x 12x   8x 8x     8x 8x 48x           8x                   8x   8x 8x 12x       8x                   15x   15x                 214x                                           194x 194x 194x 194x 194x             4x 4x   4x             202x 202x   202x       202x 202x 202x 202x                   202x       202x   202x       202x 580x     202x 187x   15x             15x 15x 15x                   8x           7x 12x                                                
'use strict';
/**
 * Attribute selector
 *
 * @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/i18n',
        'pim/user-context',
        'pim/fetcher-registry',
        'pim/template/export/product/edit/content/structure/attributes-selector',
        'pim/template/export/product/edit/content/structure/attribute-list'
    ],
    function (
        $,
        _,
        __,
        Backbone,
        i18n,
        userContext,
        fetcherRegistry,
        template,
        attributeListTemplate
    ) {
        return Backbone.View.extend({
            events: {
                'click .attribute-groups li': 'changeAttributeGroup',
                'keyup .search-field': 'updateSearch',
                'click .clear': 'clear',
                'click .remove': 'removeAttribute'
            },
            search: '',
            curentFetchId: 0,
            attributeListPage: 1,
            isFetching: false,
            selected: [],
            currentGroup: null,
            template: _.template(template),
            attributeListTemplate: _.template(attributeListTemplate),
 
            initialize: function () {
                this.listenTo(this, 'selected:update:after', function (selected) {
                    this.$('.empty-message')
                        .addClass(0 === selected.length ? '' : 'AknMessageBox--hide')
                        .removeClass(0 === selected.length ? 'AknMessageBox--hide' : '');
                });
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                $.when(
                    fetcherRegistry.getFetcher('attribute-group').fetchAll(),
                    fetcherRegistry.getFetcher('attribute').fetchByIdentifiers(this.getSelected())
                ).then(function (attributeGroups, selectedAttributes) {
                    var scrollPositions = {};
                    _.each(this.$('[data-scroll-container]'), function (element) {
                        scrollPositions[element.dataset.scrollContainer] = element.scrollTop;
                    });
                    this.attributeListPage = 1;
                    this.isFetching        = false;
 
                    var attributeCount;
                    Eif (null === this.currentGroup) {
                        attributeCount = _.reduce(attributeGroups, function (count, attributeGroup) {
                            return count + attributeGroup.attributes.length;
                        }, 0);
                    } else {
                        attributeCount = _.findWhere(attributeGroups, {code: this.currentGroup}).attributes.length;
                    }
 
                    this.$el.empty().append(this.template({
                        __: __,
                        i18n: i18n,
                        userContext: userContext,
                        attributeGroups: attributeGroups,
                        attributeCount: attributeCount,
                        currentGroup: this.currentGroup,
                        selectedAttributes: selectedAttributes
                    }));
 
                    this.initializeSortable();
 
                    this.updateAttributeList().then(function () {
                        _.each(scrollPositions, function (scrollPosition, key) {
                            this.$('[data-scroll-container="' + key + '"]').scrollTop(scrollPosition);
                        }.bind(this));
                    }.bind(this));
 
                    this.$('.attributes div').on('scroll', this.updateAttributeList.bind(this));
                }.bind(this));
            },
 
            /**
             * Set currently selected attributes
             *
             * @param {array} selected
             */
            setSelected: function (selected) {
                this.selected = selected;
 
                this.trigger('selected:update:after', this.selected);
            },
 
            /**
             * Get currently selected attributes
             *
             * @return {array}
             */
            getSelected: function () {
                return this.selected;
            },
 
            /**
             * Change the current attribute group
             *
             * @param {event} event
             */
            changeAttributeGroup: function (event) {
                var newGroup = event.currentTarget.dataset.attributeGroupCode;
                newGroup = '' !== newGroup ? newGroup : null;
                this.search = '';
 
                this.currentGroup = newGroup;
 
                this.render();
            },
 
            /**
             * Called each time the user type a letter on the search field
             */
            updateSearch: function () {
                this.search            = this.$('.search-field').val();
                this.attributeListPage = 1;
                this.isFetching        = false;
                this.$('.attributes ul').empty();
                this.updateAttributeList();
            },
 
            /**
             * Clear the selected attributes
             */
            clear: function () {
                this.setSelected([]);
                this.search = '';
 
                this.render();
            },
 
            /**
             * Called on each render, each, search event and each scroll event
             */
            updateAttributeList: function () {
                var attributeContainer = this.$('.attributes > .AknColumnConfigurator-listContainer');
                var attributeList = attributeContainer.children('.AknVerticalList');
 
                var needFetching = 0 > (
                    attributeList.height() - attributeContainer.scrollTop() - 2 * attributeContainer.height()
                );
 
                Eif (needFetching && !this.isFetching) {
                    this.curentFetchId++;
                    var fetchId = Number(this.curentFetchId);
                    var searchOptions = {
                        search: this.search,
                        options: {
                            excluded_identifiers: this.getSelected(),
                            page: this.attributeListPage,
                            limit: 20,
                            locale: userContext.get('catalogLocale')
                        }
                    };
 
                    Iif (null !== this.currentGroup) {
                        searchOptions.options.attribute_groups = [this.currentGroup];
                    }
 
                    this.isFetching = true;
 
                    return fetcherRegistry
                        .getFetcher('attribute')
                        .search(searchOptions)
                        .then(function (attributes) {
                            attributes = _.filter(attributes, function (attribute) {
                                return attribute.type !== 'pim_catalog_identifier';
                            });
 
                            if (fetchId !== this.curentFetchId) {
                                return;
                            }
                            attributeList.append(this.attributeListTemplate({
                                    attributes: attributes,
                                    i18n: i18n,
                                    userContext: userContext
                                })
                            );
 
                            Eif (0 !== attributes.length) {
                                this.attributeListPage++;
                                this.isFetching = false;
                            }
                        }.bind(this));
                }
            },
 
            /**
             * Called to initialize the dropzones
             */
            initializeSortable: function () {
                this.$('.attributes ul, .selected-attributes ul').sortable({
                    connectWith: '.attributes ul, .selected-attributes ul',
                    containment: this.$el,
                    tolerance: 'pointer',
                    cursor: 'move',
                    stop: function () {
                        this.setSelected(_.map(this.$('.selected-attributes li'), function (element) {
                            return element.dataset.attributeCode;
                        }));
                    }.bind(this)
                }).disableSelection();
            },
 
            /**
             * Remove the clicked row from the selected attributes
             *
             * @param {event} event
             */
            removeAttribute: function (event) {
                var element = $(event.currentTarget).parents('li');
                var selectedAttributes = this.getSelected();
 
                selectedAttributes = _.without(selectedAttributes, element.data('attributeCode'));
 
                this.setSelected(selectedAttributes);
 
                this.$('.attributes > div ul').append(element);
            }
        });
    }
);