All files / web/bundles/pimui/js/product/form product-and-product-model-picker.js

95.65% Statements 22/23
83.33% Branches 5/6
100% Functions 9/9
95.65% Lines 22/23

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                        20x                           20x         48x             574x             686x 686x 686x 85x 85x 7x   78x 78x       686x       686x 686x               85x 85x       85x             85x             170x 14x   156x            
'use strict';
 
/**
 * This extension allows user to display a fullscreen item picker.
 * It overrides the default item picker because we have to manage 2 types of entities:
 * - products (identified by their identifier)
 * - product models (identifier by their code)
 *
 * @author    Pierre Allard <pierre.allard@akeneo.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',
        'oro/translator',
        'pim/common/item-picker',
        'pim/fetcher-registry',
        'pim/media-url-generator'
    ], function (
        $,
        __,
        ItemPicker,
        FetcherRegistry,
        MediaUrlGenerator
    ) {
        return ItemPicker.extend({
            /**
             * {@inheritdoc}
             */
            selectModel: function (model) {
                this.addItem(`${model.attributes.document_type}_${model.get(this.config.columnName)}`);
            },
 
            /**
             * {@inheritdoc}
             */
            unselectModel: function (model) {
                this.removeItem(`${model.attributes.document_type}_${model.get(this.config.columnName)}`);
            },
 
            /**
             * {@inheritdoc}
             */
            updateBasket: function () {
                let productIds = [];
                let productModelIds = [];
                this.getItems().forEach((item) => {
                    const matchProductModel = item.match(/^product_model_(.*)$/);
                    if (matchProductModel) {
                        productModelIds.push(matchProductModel[1]);
                    } else {
                        const matchProduct = item.match(/^product_(.*)$/);
                        productIds.push(matchProduct[1]);
                    }
                });
 
                $.when(
                    FetcherRegistry.getFetcher('product-model').fetchByIdentifiers(productModelIds),
                    FetcherRegistry.getFetcher('product').fetchByIdentifiers(productIds)
                ).then(function (productModels, products) {
                    this.renderBasket(products.concat(productModels));
                    this.delegateEvents();
                }.bind(this));
            },
 
            /**
             * {@inheritdoc}
             */
            imagePathMethod: function (item) {
                let filePath = null;
                Iif (item.meta.image !== null) {
                    filePath = item.meta.image.filePath;
                }
 
                return MediaUrlGenerator.getMediaShowUrl(filePath, 'thumbnail_small');
            },
 
            /**
             * {@inheritdoc}
             */
            labelMethod: function (item) {
                return item.meta.label[this.getLocale()];
            },
 
            /**
             * {@inheritdoc}
             */
            itemCodeMethod: function (item) {
                if (item.code) {
                    return `product_model_${item.code}`;
                } else {
                    return `product_${item[this.config.columnName]}`;
                }
            }
        });
    }
);