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

91.89% Statements 34/37
75% Branches 9/12
100% Functions 5/5
91.89% Lines 34/37

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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167              300x                                                         300x                         498x 498x   498x             49x 49x 49x                 547x 1094x                       50x       50x 50x   50x 1x   1x     49x 49x 98x 98x     49x                   49x 49x   49x             349x 42x     349x                       50x   50x 49x 49x                   497x   497x           497x            
/**
 * Create product and product-model extension
 *
 * @author    Tamara Robichet <tamara.robichet@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)
 */
define(
    [
        'jquery',
        'underscore',
        'oro/translator',
        'backbone',
        'pim/form',
        'pim/template/product/create-button',
        'pim/template/product/create-modal-content',
        'pim/fetcher-registry',
        'bootstrap-modal',
        'pim/form-builder',
        'pim/security-context',
        'pim/template/common/modal-centered'
    ],
    function (
        $,
        _,
        __,
        Backbone,
        BaseForm,
        template,
        modalContentTemplate,
        FetcherRegistry,
        BootstrapModal,
        FormBuilder,
        SecurityContext,
        modalTemplate
    ) {
        return BaseForm.extend({
            template: _.template(template),
            modalTemplate: _.template(modalTemplate),
            modalContentTemplate: _.template(modalContentTemplate),
 
            events: {
                'click .create-product-button': 'openModal'
            },
 
            /**
             * {@inheritdoc}
             */
            initialize(config) {
                this.config = config.config;
                this.modal = null;
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * Closes the selection modal and unbinds the click events
             */
            closeModal() {
                Eif (this.modal) {
                    this.modal.close();
                    this.modal.$el.off();
                }
            },
 
            /**
             * Returns a list of choices that are allowed by permissions
             * @return {Object} choices
             */
            getAllowedChoices(choices) {
                return Object.values(choices).filter(choice => {
                    return SecurityContext.isGranted(choice.aclResourceId);
                });
            },
 
            /**
             * Opens the selection modal with the configured choices
             * If there's only one available choice, directly open the form
             * for that choice.
             *
             * @return {Backbone.BootstrapModal} The modal
             */
            openModal() {
                Iif (this.modal) {
                    this.closeModal();
                }
 
                const { choices, modalTitle, subTitle } = this.config;
                const allowedChoices = this.getAllowedChoices(choices);
 
                if (1 === allowedChoices.length) {
                    const firstChoice = allowedChoices[0];
 
                    return this.openFormModal(null, firstChoice.form);
                }
 
                const translatedChoices = [];
                Object.keys(allowedChoices).forEach((key) => {
                    translatedChoices[key] = allowedChoices[key];
                    translatedChoices[key].title = __(translatedChoices[key].title);
                });
 
                this.modal = new Backbone.BootstrapModal({
                    title: __(modalTitle),
                    subtitle: __(subTitle),
                    okText: '',
                    template: this.modalTemplate,
                    content: this.modalContentTemplate({
                        choices: translatedChoices,
                    })
                }).open();
 
                this.modal.$el.on('click', '.cancel', this.closeModal.bind(this));
                this.modal.$el.on('click', '.product-choice', this.openFormModal.bind(this));
 
                return this.modal;
            },
 
            /**
             * {@inheritdoc}
             */
            shutdown: function () {
                if (this.modal) {
                    this.modal.$el.off();
                }
 
                BaseForm.prototype.shutdown.apply(this, arguments);
            },
 
            /**
             * Opens a form model for the selected choice. If formName is passed in, it
             * overrides the formName from the event target element.
             *
             * @param  {jQuery.Event} event The click event from the selection modal
             * @param  {String} formName The name of the form extension defined for a choice
             * @return {Promise}
             */
            openFormModal(event, formName) {
                const form = formName || $(event.currentTarget).attr('data-form');
 
                return FormBuilder.build(form).then(modal => {
                    this.closeModal();
                    modal.open();
                });
            },
 
            /**
             * Render the create button
             * If the user is not allowed to access the forms for the choices
             * don't render the create button.
             */
            render() {
                const { choices, buttonTitle } = this.config;
 
                Iif (0 === this.getAllowedChoices(choices).length) {
                    this.$el.hide();
 
                    return;
                }
 
                this.$el.html(this.template({
                    buttonTitle: __(buttonTitle)
                }));
            }
        });
    });