All files / web/bundles/pimui/js/form/common save.js

79.17% Statements 19/24
55.56% Branches 5/9
87.5% Functions 7/8
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 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                  546x                             546x                     708x   708x 122x   708x 122x     708x             706x                 706x                           409x 409x             407x                 294x   294x                         96x   96x       96x                     96x                
'use strict';
 
/**
 * Save extension
 *
 * @author    Julien Sanchez <julien@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(
    [
        'oro/translator',
        'pim/form',
        'oro/mediator',
        'oro/loading-mask',
        'oro/messenger'
    ],
    function (
        __,
        BaseForm,
        mediator,
        LoadingMask,
        messenger
    ) {
        return BaseForm.extend({
            loadingMask: null,
            updateFailureMessage: __('pim_enrich.entity.fallback.flash.update.fail'),
            updateSuccessMessage: __('pim_enrich.entity.fallback.flash.update.success'),
            isFlash: true,
            label: __('pim_common.save'),
 
            /**
             * {@inheritdoc}
             */
            initialize: function (config) {
                this.config = config.config;
 
                if (this.config.hasOwnProperty('updateSuccessMessage')) {
                    this.updateSuccessMessage = __(this.config.updateSuccessMessage);
                }
                if (this.config.hasOwnProperty('updateFailureMessage')) {
                    this.updateFailureMessage = __(this.config.updateFailureMessage);
                }
 
                BaseForm.prototype.initialize.apply(this, arguments);
            },
 
            /**
             * {@inheritdoc}
             */
            configure: function () {
                this.trigger('save-buttons:register-button', {
                    className: 'save',
                    priority: 200,
                    label: this.label,
                    events: {
                        'click .save': this.save.bind(this)
                    }
                });
 
                return BaseForm.prototype.configure.apply(this, arguments);
            },
 
            /**
             * Save the current form
             */
            save: function () {
                throw new Error('This method must be implemented');
            },
 
            /**
             * Show the loading mask
             */
            showLoadingMask: function () {
                this.loadingMask = new LoadingMask();
                this.loadingMask.render().$el.appendTo(this.getRoot().$el).show();
            },
 
            /**
             * Hide the loading mask
             */
            hideLoadingMask: function () {
                this.loadingMask.hide().$el.remove();
            },
 
            /**
             * What to do after a save
             *
             * @param {any} data
             */
            postSave: function (data) {
                this.getRoot().trigger('pim_enrich:form:entity:post_save', data);
 
                messenger.notify(
                    'success',
                    this.updateSuccessMessage,
                    {flash: this.isFlash}
                );
            },
 
            /**
             * On save fail
             *
             * @param {Object} response
             */
            fail: function (response) {
                switch (response.status) {
                    case 400:
                        this.getRoot().trigger(
                            'pim_enrich:form:entity:bad_request',
                            {'sentData': this.getFormData(), 'response': response.responseJSON}
                        );
                        break;
                    case 500:
                        /* global console */
                        const message = response.responseJSON ? response.responseJSON : response;
 
                        console.error('Errors:', message);
                        this.getRoot().trigger('pim_enrich:form:entity:error:save', message);
                        break;
                    default:
                }
 
                messenger.notify(
                    'error',
                    this.updateFailureMessage
                );
            }
        });
    }
);