All files / web/bundles/pimui/js pim-dialog.js

71.43% Statements 20/28
82.35% Branches 14/17
70% Functions 7/10
71.43% Lines 20/28

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                        1183x                       1183x         10x                   1x                   1x   1x                                                                                         81x   81x                       81x   81x 74x 74x     81x 4x 4x 4x     81x   81x                         71x                       1183x      
/**
 * Dialog class purposes an easier way to call ModalDialog components
 *
 * @author    Romain Monceau <romain@akeneo.com>
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 *
 * @uses Backbone.BootstrapModal
 *
 * Example:
 *      Dialog.alert('{{ 'MyMessage'|trans }}', 'MyTitle');
 */
define(
    [
        'jquery',
        'underscore',
        'backbone',
        'oro/translator',
        'pim/router',
        'bootstrap-modal'
    ],
    function ($, _, Backbone, __, router) {
        'use strict';
 
        const Dialog = {
            /**
             * Returns class name for modal illustration
             */
            getIllustrationClass: function(entityType = '') {
                return entityType.toLowerCase().split(' ').join('-');
            },
 
            /**
             * Open a modal dialog without cancel button
             * @param {String} content The message in the modal
             * @param {String} title The title of the modal
             * @param {String} subTitle The subtitle for the modal
             */
            alert: function (content, title, subTitle) {
                const alert = new Backbone.BootstrapModal({
                    type: __(subTitle) || '',
                    allowCancel: false,
                    title: title,
                    content: content,
                    okText: __('pim_common.ok'),
                    buttonClass: 'AknButton--action',
                    illustrationClass: this.getIllustrationClass(subTitle)
                });
 
                alert.$el.addClass('modal--fullPage');
 
                alert.open();
            },
 
            /**
             * Open a modal dialog with cancel button and specific redirection when
             * @param {String} content
             * @param {String} title
             * @param {String} okText
             * @param {String} location
             */
            redirect: function (content, title, okText, location) {
                if (!_.isUndefined(Backbone.BootstrapModal)) {
                    var redirectModal = new Backbone.BootstrapModal({
                        title: title,
                        content: content,
                        okText: okText,
                    });
 
                    redirectModal.on('ok', function () {
                        router.redirect(location);
                    });
 
                    $('.modal-body a', redirectModal.el).on('click', function () {
                        redirectModal.close();
                    });
 
                    redirectModal.open();
                } else {
                    window.alert(content);
                }
            },
 
            /**
            * Open a confirm modal dialog to validate the action made by user
            * If user validate its action, a js callback function is called
             * @param  {String}   content           Message inside the modal
             * @param  {String}   title             Title of the modal
             * @param  {Function} callback          Action to execute after validation
             * @param  {String}   subTitle          The subtitle (can be entity type)
             * @param  {String}   buttonClass       The class for OK button
             * @param  {String}   buttonText        The OK button label
             * @param  {String}   illustrationClass Class for the illustration
             * @return {Promise}
             */
            confirm: function (content, title, callback, subTitle, buttonClass, buttonText, illustrationClass) {
                const deferred = $.Deferred();
 
                const confirm = new Backbone.BootstrapModal({
                    type: __(subTitle || ''),
                    title: __(title),
                    innerDescription: __(content),
                    content: '',
                    okText: __(buttonText) || __('pim_common.ok'),
                    cancelText: __('pim_common.cancel'),
                    buttonClass: buttonClass || 'AknButton--action',
                    allowCancel: true,
                    illustrationClass: illustrationClass || this.getIllustrationClass(subTitle)
                });
 
                confirm.$el.addClass('modal--fullPage');
 
                confirm.on('ok', function () {
                    deferred.resolve();
                    (callback || $.noop)();
                });
 
                confirm.on('cancel', function () {
                    this.close();
                    this.remove();
                    deferred.reject();
                });
 
                confirm.open();
 
                return deferred.promise();
            },
 
            /**
             * Open a Confirm deletion modal and execute callback after
             * user validation
             * @param  {String}   content    The message text
             * @param  {String}   title      The title
             * @param  {Function} callback   Action to execute after validation
             * @param  {String}   subTitle   The entity type
             * @param  {String}   buttonText The 'ok' button label
             */
            confirmDelete: function(content, title, callback, subTitle, buttonText) {
                return Dialog.confirm.apply(this, [
                    content,
                    title,
                    callback,
                    subTitle,
                    'AknButton--important',
                    __(buttonText) || __('Delete'),
                    'delete'
                ]);
            }
        };
 
        return Dialog;
    }
);