All files / web/bundles/pimui/js/form/common/attributes create-button.js

90.91% Statements 30/33
50% Branches 2/4
90.91% Functions 10/11
90.91% Lines 30/33

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 134 135 136 137 138 139 140                  51x                                                       51x                 54x   54x             53x   53x   53x   53x 30x     30x               420x           30x     30x 30x 30x 30x     30x                     53x       53x 53x 53x       53x     53x                 53x 53x 742x 742x             53x 1643x     53x        
'use strict';
 
/**
 * Create attribute button
 *
 * @author    Alban Alnot <alban.alnot@consertotech.pro>
 * @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',
        'backbone',
        'pim/form',
        'pim/template/form/tab/attribute/create-button',
        'pim/template/common/modal-centered',
        'pim/template/form/tab/attribute/create-modal-content',
        'routing',
        'pim/fetcher-registry',
        'pim/router',
        'bootstrap-modal'
    ],
    function (
        $,
        _,
        __,
        Backbone,
        BaseForm,
        template,
        templateModal,
        innerTemplateModal,
        Routing,
        FetcherRegistry,
        router
    ) {
        return BaseForm.extend({
            template: _.template(template),
            innerTemplateModal: _.template(innerTemplateModal),
            templateModal: _.template(templateModal),
 
            /**
             * {@inheritdoc}
             */
            initialize: function (config) {
                this.config = config.config;
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * Create the dialog modal and bind clicks
             */
            createModal: function (attributeTypesMap) {
                var attributeTypes = this.formatAndSortAttributeTypesByLabel(attributeTypesMap);
 
                var moduleConfig = __moduleConfig;
 
                var modal = null;
 
                $('#attribute-create-button').on('click', function () {
                    Iif (modal) {
                        modal.open();
                    } else {
                        modal = new Backbone.BootstrapModal({
                            // TODO translate this
                            title: __('pim_enrich.entity.attribute.property.type.choose'),
                            subtitle: __('pim_enrich.entity.attribute.module.create.button'),
                            content: this.innerTemplateModal({
                                attributeTypes: attributeTypes,
                                iconsMap: moduleConfig.attribute_icons,
                                generateRoute: function (route, params) {
                                    return Routing.generate(route, params);
                                }
                            }),
                            okText: '',
                            template: this.templateModal
                        });
                        modal.open();
                    }
 
                    modal.$el.on('click', '.attribute-choice', function () {
                        modal.close();
                        modal.$el.remove();
                        router.redirect($(this).attr('data-route'), {trigger: true});
                    });
 
                    modal.$el.on('click', '.cancel', () => {
                        modal.close();
                        modal.$el.remove();
                    });
                }.bind(this));
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                FetcherRegistry.getFetcher('attribute-type')
                    .fetchAll()
                    .then(function (attributeTypes) {
                        // TODO: Remove the "delete" lines when we fully support the asset attribute types
                        delete attributeTypes.akeneo_asset_multiple_link;
                        delete attributeTypes.akeneo_asset_single_link;
                        this.$el.html(this.template({
                            buttonTitle: __(this.config.buttonTitle)
                        }));
 
                        this.createModal(attributeTypes);
                    }.bind(this));
 
                return this;
            },
 
            /**
             * Format the map to an array and sort attributeTypes by label
             * @param attributeTypesMap
             * @returns {Array}
             */
            formatAndSortAttributeTypesByLabel: function (attributeTypesMap) {
                var sortedAttributeTypesByLabel = [];
                for (var key in attributeTypesMap) {
                    Eif (attributeTypesMap.hasOwnProperty(key)) {
                        sortedAttributeTypesByLabel.push({
                            code: key,
                            label: __('pim_enrich.entity.attribute.property.type.' + attributeTypesMap[key])
                        });
                    }
                }
 
                sortedAttributeTypesByLabel.sort(function (a, b) {
                    return a.label.localeCompare(b.label);
                });
 
                return sortedAttributeTypesByLabel;
            }
        });
    });