All files / web/bundles/pimui/js/attribute-option create.js

79.17% Statements 19/24
25% Branches 1/4
88.89% Functions 8/9
79.17% Lines 19/24

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    153x                                     153x         2x     2x   2x 2x               2x   2x 2x 2x               2x 2x       2x                                       2x       153x 2x       2x   2x 2x          
'use strict';
 
define(
    [
        'jquery',
        'underscore',
        'backbone',
        'routing',
        'pim/form-builder',
        'oro/messenger',
        'pim/template/attribute-option/validation-error'
    ],
    function (
        $,
        _,
        Backbone,
        Routing,
        FormBuilder,
        messenger,
        errorTemplate
    ) {
        var CreateOptionView = Backbone.View.extend({
            errorTemplate: _.template(errorTemplate),
            attribute: null,
 
            initialize: function (options) {
                this.attribute = options.attribute;
            },
            createOption: function () {
                var deferred = $.Deferred();
 
                FormBuilder.build('pim-attribute-option-form').done((form) => {
                    var modal = new Backbone.BootstrapModal({
                        title: _.__('pim_enrich.entity.product.module.attribute.add_attribute_option'),
                        content: form,
                        cancelText: _.__('pim_common.cancel'),
                        okText: _.__('pim_common.add'),
                        picture: 'illustrations/Attribute.svg',
                        okCloses: false
                    });
                    modal.open();
 
                    modal.on('cancel', deferred.reject);
                    modal.on('ok', () => {
                        $.ajax({
                            method: 'POST',
                            url: Routing.generate(
                                'pim_enrich_attributeoption_create',
                                { attributeId: this.attribute.meta.id }
                            ),
                            data: JSON.stringify(form.getFormData())
                        }).done((option) => {
                            modal.close();
                            messenger.notify(
                                'success',
                                _.__('pim_enrich.entity.attribute_option.flash.create.success')
                            );
                            deferred.resolve(option);
                        }).fail((xhr) => {
                            var response = xhr.responseJSON;
 
                            if (response.code) {
                                form.$('input[name="code"]').after(
                                    this.errorTemplate({
                                        errors: [response.code]
                                    })
                                );
                            } else {
                                messenger.notify(
                                    'error',
                                    _.__('pim_enrich.entity.attribute_option.flash.create.fail')
                                );
                            }
                        });
                    });
                });
 
                return deferred.promise();
            }
        });
 
        return function (attribute) {
            Iif (!attribute) {
                throw new Error('Attribute must be provided to create a new option');
            }
 
            var view = new CreateOptionView({ attribute: attribute });
 
            return view.createOption().always(function () {
                view.remove();
            });
        };
    }
);