All files / web/bundles/pimui/js/mass-edit/form/product product-model-field.js

100% Statements 24/24
90% Branches 9/10
100% Functions 3/3
100% Lines 24/24

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                  89x                                             89x               220x           220x               1694x 10x 10x   10x                     30x   30x 30x 42x 42x         42x         30x             4x 4x   4x             50x                     30x             8x 8x 8x       8x              
/**
 * This module displays a product model select2
 *
 * @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)
 */
'use strict';
 
define(
    [
        'jquery',
        'underscore',
        'oro/translator',
        'pim/mass-edit-form/product/mass-edit-field',
        'pim/router',
        'pim/user-context',
        'pim/fetcher-registry',
        'pim/media-url-generator',
        'pim/template/product/form/variant-navigation/product-model-item'
    ],
    function (
        $,
        _,
        __,
        MassEditField,
        Routing,
        UserContext,
        FetcherRegistry,
        MediaUrlGenerator,
        templateProductModel
    ) {
        return MassEditField.extend({
            previousFamilyVariant: null,
            templateProductModel: _.template(templateProductModel),
 
            /**
             * {@inheritdoc}
             */
            configure() {
                this.listenTo(
                    this.getRoot(),
                    'pim_enrich:form:entity:post_update',
                    this.onPostUpdate.bind(this)
                );
 
                return MassEditField.prototype.configure.apply(this, arguments);
            },
 
            /**
             * When the model data is updated with a new family variant, drops the previous value and re-render the
             * field.
             */
            onPostUpdate() {
                if (this.getFormData().family_variant !== this.previousFamilyVariant) {
                    this.previousFamilyVariant = this.getFormData().family_variant;
                    this.setData({[this.fieldName]: null});
 
                    this.render();
                }
            },
 
            /**
             * {@inheritdoc}
             *
             * This method overrides the previous one to be able to format the result and add an image and set a
             * custom template.
             */
            getSelect2Options() {
                let options = MassEditField.prototype.getSelect2Options.apply(this, arguments);
 
                options.dropdownCssClass = 'variant-navigation';
                options.formatResult = (item, $container) => {
                    const filePath = (null !== item.image) ? item.image.filePath : null;
                    const entity = {
                        label: item.text,
                        image: MediaUrlGenerator.getMediaShowUrl(filePath, 'thumbnail_small')
                    };
 
                    $container.append(
                        this.templateProductModel({entity: entity, getClass: this.getCompletenessBadgeClass})
                    );
                };
 
                return options;
            },
 
            /**
             * {@inheritdoc}
             */
            select2Data() {
                let result = MassEditField.prototype.select2Data.apply(this, arguments);
                result.options.family_variant = this.getFormData().family_variant;
 
                return result;
            },
 
            /**
             * {@inheritdoc}
             */
            convertBackendItem(item) {
                return {
                    id: item.code,
                    text: `${item.code} - ${item.meta.label[UserContext.get('uiLocale')]}`,
                    image: item.meta.image || null
                };
            },
 
            /**
             * {@inheritdoc}
             */
            isReadOnly() {
                return !this.getFormData().family_variant || MassEditField.prototype.isReadOnly.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            select2InitSelection(element, callback) {
                const id = $(element).val();
                Eif ('' !== id) {
                    FetcherRegistry
                        .getFetcher('product-model-by-code')
                        .fetch(id)
                        .then((productModel) => {
                            callback(this.convertBackendItem(productModel));
                        });
                }
            }
        });
    }
);