All files / web/bundles/pimui/js messenger.js

88.46% Statements 23/26
92.31% Branches 12/13
87.5% Functions 7/8
88.46% Lines 23/26

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    1183x             1183x                                             651x               1162x             651x 651x 651x                   651x 651x   651x 651x 651x 5113x 5113x   5113x 1039x     5113x 749x     5113x 229x 229x             651x                         651x                            
'use strict';
 
define(
    [
        'jquery',
        'underscore',
        'pim/template/flash-message'
    ],
    function ($, _, flashMessageTemplate) {
        return {
            queue: [],
            defaults: {
                container: '#flash-messages .flash-messages-holder',
                delay: false,
                template: _.template(flashMessageTemplate),
                flash: true
            },
 
            /**
             * Shows notification message
             *
             * @param {(string|boolean)} type 'error'|'success'|'warning'|false
             * @param {string} message text of message
             * @param {Object} options
             *
             * @param {(string|jQuery)} options.container selector of jQuery with container element
             * @param {(number|boolean)} options.delay time in ms to auto close message
             *      or false - means to not close automatically
             * @param {Function} options.template template function
             * @param {boolean} options.flash flag to turn on default delay close call, it's 5s
             */
            notify: function (type, message, options) {
                this.showMessage(type, message, options);
            },
 
            enqueueMessage: function () {
                this.queue.push(arguments);
            },
 
            showQueuedMessages: function () {
                while (this.queue.length) {
                    var args = this.queue.shift();
                    this.showMessage.apply(this, args);
                }
            },
 
            showMessage: function (type, message, options) {
                var opt = _.extend({}, this.defaults, options || {});
                var delay = opt.delay || (opt.flash && 5000);
                var $el = $(opt.template({
                    type: type,
                    message: message,
                    messageTitle: '',
                    delay: delay,
                    icon: this.getIcon(type),
                    closeIcon: this.getCloseIcon(type)
                })).appendTo(opt.container);
 
                // Used to force the browser to visually render the element's styles to be able to use CSS transitions
                $el.offset();
                $el.addClass('AknFlash--visible');
 
                Eif (delay) {
                    var timeLeft = delay;
                    var interval = setInterval(function () {
                        $el.find('.flash-timer:first').html(Math.max(Math.floor(timeLeft / 1000), 0));
                        timeLeft -= 500;
 
                        if (timeLeft <= 0) {
                            $el.removeClass('AknFlash--visible');
                        }
 
                        if (timeLeft <= -500) {
                            $el.addClass('AknFlash--crushed');
                        }
 
                        if (timeLeft <= -1500) {
                            $el.remove();
                            clearInterval(interval);
                        }
                    }, 500);
                }
            },
 
            getIcon: function(type) {
                return _.result(
                    {
                        'info': 'icon-infos.svg',
                        'success': 'icon-check.svg',
                        'error': 'icon-warning-redlight.svg',
                        'warning': 'icon-warning-orangelight.svg'
                    },
                    type,
                    'icon-infos.svg'
                );
            },
 
            getCloseIcon: function(type) {
                return _.result(
                    {
                        'info': 'icon-delete-bluedark.svg',
                        'success': 'icon-delete-greendark.svg',
                        'error': 'icon-delete-reddark.svg',
                        'warning': 'icon-delete-orangedark.svg'
                    },
                    type,
                    'icon-delete-bluedark.svg'
                );
            }
        };
    }
);