All files / web/bundles/pimui/js/product/form completeness.js

80.77% Statements 21/26
75% Branches 9/12
85.71% Functions 6/7
80.77% Lines 21/26

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                  265x                       265x                 323x   323x             322x         322x 322x   322x             203x 180x     23x 23x       23x 14x     23x                               23x       23x                     23x 1x   22x       22x                                    
'use strict';
/**
 * Completeness panel extension
 *
 * @author    Julien Sanchez <julien@akeneo.com>
 * @author    Filips Alpe <filips@akeneo.com>
 * @copyright 2015 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/form',
        'pim/template/product/completeness',
        'pim/fetcher-registry',
        'pim/i18n',
        'pim/user-context'
    ],
    function ($, _, __, BaseForm, template, FetcherRegistry, i18n, UserContext) {
        return BaseForm.extend({
            template: _.template(template),
            className: 'panel-pane completeness-panel AknCompletenessPanel',
            initialFamily: null,
 
            /**
             * {@inheritdoc}
             */
            initialize: function () {
                this.initialFamily = null;
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.trigger('tab:register', {
                    code: this.code,
                    label: __('pim_enrich.entity.product.module.completeness.title')
                });
 
                this.listenTo(this.getRoot(), 'pim_enrich:form:entity:post_fetch', this.render);
                this.listenTo(UserContext, 'change:catalogLocale', this.render);
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                if (!this.configured || this.code !== this.getParent().getCurrentTab()) {
                    return this;
                }
 
                Eif (this.getFormData().meta) {
                    $.when(
                        FetcherRegistry.getFetcher('channel').fetchAll(),
                        FetcherRegistry.getFetcher('locale').fetchActivated()
                    ).then(function (channels, locales) {
                        if (null === this.initialFamily) {
                            this.initialFamily = this.getFormData().family;
                        }
 
                        this.$el.html(
                            this.template({
                                __: __,
                                hasFamily: this.getFormData().family !== null,
                                completenesses: this.sortCompleteness(this.getFormData().meta.completenesses),
                                i18n: i18n,
                                channels: channels,
                                locales: locales,
                                catalogLocale: UserContext.get('catalogLocale'),
                                hasFamilyChanged: this.getFormData().family !== this.initialFamily,
                                missingValuesKey: 'pim_enrich.entity.product.module.completeness.missing_values',
                                noFamilyLabel: __('pim_enrich.entity.product.module.completeness.no_family'),
                                noCompletenessLabel:
                                    __('pim_enrich.entity.product.module.completeness.no_completeness')
                            })
                        );
                        this.delegateEvents();
                    }.bind(this));
                }
 
                return this;
            },
 
            /**
             * Sort completenesses. Put the user current catalog scope first.
             *
             * @param completenesses
             *
             * @returns {Array}
             */
            sortCompleteness: function (completenesses) {
                if (_.isEmpty(completenesses)) {
                    return [];
                }
                var sortedCompleteness = [
                    _.findWhere(completenesses, {channel: UserContext.get('catalogScope')})
                ];
 
                return _.union(sortedCompleteness, completenesses);
            },
 
            /**
             * On family change listener
             */
            onChangeFamily: function () {
                if (!_.isEmpty(this.getRoot().model._previousAttributes)) {
                    var data = this.getFormData();
                    data.meta.completenesses = [];
                    this.setData(data);
 
                    this.render();
                }
            }
        });
    }
);