All files / web/bundles/pimui/js/channel/form/properties/general category-tree.js

73.68% Statements 14/19
75% Branches 3/4
66.67% Functions 4/6
73.68% Lines 14/19

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                  27x                                         27x                     31x   31x             83x       83x 83x 5x 5x 5x     83x                     83x   83x     83x                                                    
'use strict';
 
/**
 * Module used to display the category general properties field of a channel
 *
 * @author    Alexandr Jeliuc <alex@jeliuc.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',
        'underscore',
        'oro/translator',
        'pim/form',
        'pim/fetcher-registry',
        'pim/template/channel/tab/properties/general/category-tree',
        'pim/user-context',
        'pim/i18n',
        'jquery.select2'
    ],
    function (
        $,
        _,
        __,
        BaseForm,
        FetcherRegistry,
        template,
        UserContext,
        i18n
    ) {
        return BaseForm.extend({
            className: 'AknFieldContainer',
            template: _.template(template),
            catalogLocale: UserContext.get('catalogLocale'),
 
            /**
             * Initializes configuration.
             *
             * @param {Object} config
             */
            initialize: function (config) {
                this.config = config.config;
 
                return BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                Iif (!this.configured) {
                    return this;
                }
 
                FetcherRegistry.getFetcher('category').fetchAll().then(function (categories) {
                    if (0 === this.getFormData().category_tree.length) {
                        var data = this.getFormData();
                        data.category_tree = _.first(categories).code;
                        this.setData(data, {'silent': true});
                    }
 
                    this.$el.html(this.template({
                        categoryTree: this.getFormData().category_tree,
                        categories: categories,
                        catalogLocale: this.catalogLocale,
                        label: __('pim_enrich.entity.channel.property.category_tree'),
                        requiredLabel: __('pim_common.required_label'),
                        defaulValueLabel: __('pim_enrich.entity.channel.property.label_category_tree'),
                        errors: this.getParent().getValidationErrorsForField('category'),
                        i18n: i18n
                    }));
 
                    this.$('.select2').select2()
                        .on('change', this.updateState.bind(this));
                    this.renderExtensions();
                }.bind(this));
 
                return this;
            },
 
            /**
             * Sets new category tree on change.
             *
             * @param {Object} event
             */
            updateState: function (event) {
                this.setCategory(event.currentTarget.value);
            },
 
            /**
             * Sets specified category tree into root model.
             *
             * @param {Array} code
             */
            setCategory: function (code) {
                var data = this.getFormData();
 
                data.category_tree = code;
                this.setData(data);
            }
        });
    }
);