All files / web/bundles/pimui/js/product-model/form complete-variant-product.js

100% Statements 22/22
100% Branches 10/10
100% Functions 5/5
100% Lines 22/22

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              49x                             49x               61x   61x   61x             68x 68x 68x 68x   68x 68x   68x     50x     68x                                 68x 68x   68x 3x 65x 62x     68x                       68x                  
/**
 * On a Product Model Edit Form, this module displays number of product variant in the subtree of this Product Model,
 * eg: 2 / 10.
 *
 * @copyright 2017 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'underscore',
        'oro/translator',
        'pim/form',
        'pim/template/product-model/complete-variant-product',
        'pim/user-context'
    ],
    function (
        _,
        __,
        BaseForm,
        template,
        UserContext
    ) {
        return BaseForm.extend({
            className: 'AknButtonList-item',
            template: _.template(template),
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                UserContext.off('change:catalogLocale change:catalogScope', this.render);
 
                this.listenTo(UserContext, 'change:catalogLocale change:catalogScope', this.render);
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                const variantProductCompleteness = this.getFormData().meta.variant_product_completenesses;
                const completenesses = variantProductCompleteness.completenesses;
                const channel = UserContext.get('catalogScope');
                const locale = UserContext.get('catalogLocale');
 
                const totalProducts = variantProductCompleteness.total;
                let completeProducts = 0;
 
                if (_.has(completenesses, channel) &&
                    _.has(completenesses[channel], locale)
                ) {
                    completeProducts = completenesses[channel][locale];
                }
 
                this.$el.html(
                    this.template({
                        color: this.badgeCssClass(completeProducts, totalProducts),
                        label: this.badgeLabel(completeProducts, totalProducts)
                    })
                );
            },
 
            /**
             * Return the color of the badge
             *
             * @param {int} completeProducts
             * @param {int} totalProducts
             *
             * @returns {string}
             */
            badgeCssClass: function (completeProducts, totalProducts) {
                const ratio = completeProducts / totalProducts;
                let color = 'warning';
 
                if (1 === ratio) {
                    color = 'success';
                } else if (0 === ratio || 0 === totalProducts) {
                    color = 'important';
                }
 
                return color;
            },
 
            /**
             * Return the label of the badge
             *
             * @param {int} completeProducts
             * @param {int} totalProducts
             *
             * @returns {string}
             */
            badgeLabel: function (completeProducts, totalProducts) {
                return __(
                    'pim_enrich.entity.product_model.module.completeness.variant_product',
                    { complete: completeProducts, total: totalProducts },
                    completeProducts
                );
            }
        });
    }
);