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

94.44% Statements 17/18
75% Branches 3/4
90% Functions 9/10
94.44% Lines 17/18

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                507x                                         507x                                                 645x                                 726x 726x   726x             31x                         31x 31x 31x   31x   29x 29x     2x     2x     31x                   17x          
'use strict';
/**
 * Delete extension
 *
 * @author    Clement Gautier <clement.gautier@akeneo.com>
 * @copyright 2016 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
define(
    [
        'underscore',
        'oro/translator',
        'pim/form',
        'pim/template/form/delete',
        'pim/router',
        'oro/loading-mask',
        'oro/messenger',
        'pim/dialog'
    ],
    function (
        _,
        __,
        BaseForm,
        template,
        router,
        LoadingMask,
        messenger,
        Dialog
    ) {
        return BaseForm.extend({
            tagName: 'button',
 
            className: 'AknDropdown-menuLink delete',
 
            template: _.template(template),
 
            events: {
                'click': 'delete'
            },
 
            /**
             * The remover should be injected / overridden by the concrete implementation
             * It is an object that define a remove function
             */
            remover: {
                remove: function () {
                    throw 'Remove function should be implemented in remover';
                }
            },
 
            /**
             * @param {Object} meta
             */
            initialize: function (meta) {
                this.config = _.extend({}, {
                    trans: {
                        title: 'pim_enrich.entity.fallback.module.delete.item',
                        content: 'pim_common.confirm_deletion',
                        success: 'pim_enrich.entity.fallback.flash.delete.success',
                        fail: 'pim_enrich.entity.fallback.flash.delete.error',
                        subTitle: '',
                        buttonText: 'pim_common.delete'
                    },
                    redirect: 'oro_default'
                }, meta.config);
            },
 
            /**
             * {@inheritdoc}
             */
            render: function () {
                this.$el.html(this.template({'__': __}));
                this.delegateEvents();
 
                return this;
            },
 
            /**
             * Open a dialog to ask the user to confirm the deletion
             */
            delete: function () {
                return Dialog.confirmDelete(
                    __(this.config.trans.title),
                    __(this.config.trans.content),
                    this.doDelete.bind(this),
                    __(this.config.trans.subTitle),
                    __(this.config.trans.buttonText)
                );
            },
 
            /**
             * Send a request to the backend in order to delete the element
             */
            doDelete: function () {
                var config = this.config;
                var loadingMask = new LoadingMask();
                loadingMask.render().$el.appendTo(this.getRoot().$el).show();
 
                this.remover.remove(this.getIdentifier())
                    .done(function () {
                        messenger.notify('success', __(this.config.trans.success));
                        router.redirectToRoute(this.config.redirect);
                    }.bind(this))
                    .fail(function (xhr) {
                        var message = xhr.responseJSON && xhr.responseJSON.message ?
                            xhr.responseJSON.message : __(config.trans.fail);
 
                        messenger.notify('error', message);
                    }.bind(this))
                    .always(function () {
                        loadingMask.hide().$el.remove();
                    });
            },
 
            /**
             * Get the current form identifier
             *
             * @return {String}
             */
            getIdentifier: function () {
                return this.getFormData().code;
            }
        });
    }
);