static/midcom.services.uimessages/jquery.midcom_services_uimessages.js   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 1.67

Size

Lines of Code 86
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 56
c 0
b 0
f 0
dl 0
loc 86
rs 10
wmc 10
mnd 4
bc 4
fnc 6
bpm 0.6666
cpm 1.6666
noi 0
1
const MIDCOM_SERVICES_UIMESSAGES_TYPE_INFO = 'info',
2
    MIDCOM_SERVICES_UIMESSAGES_TYPE_OK = 'ok',
3
    MIDCOM_SERVICES_UIMESSAGES_TYPE_WARNING = 'warning',
4
    MIDCOM_SERVICES_UIMESSAGES_TYPE_ERROR = 'error';
5
6
var MIDCOM_SERVICES_UIMESSAGES_REMOVE = true,
7
    MIDCOM_SERVICES_UIMESSAGES_ERROR_HIGHLIGHT = true,
8
    MIDCOM_SERVICES_UIMESSAGES_SLIDE_SPEED = 1000,
9
    MIDCOM_SERVICES_UIMESSAGES_SLIDE_DELAY = 3000,
10
11
    MIDCOM_SERVICES_UIMESSAGES_INDEX = 0;
12
13
$.midcom_services_uimessage_add = function(options) {
14
    $('#midcom_services_uimessages_wrapper').midcom_services_uimessage(options);
15
};
16
17
$.fn.midcom_services_uimessage = function(options) {
18
    var id = 'midcom_services_uimessages_' + MIDCOM_SERVICES_UIMESSAGES_INDEX;
19
20
    $('<div></div>')
21
        .attr('id', id)
22
        .addClass('midcom_services_uimessages')
23
        .addClass(options.type)
24
        .appendTo('#midcom_services_uimessages_wrapper');
25
26
    $('<h3></h3>')
27
        .html(options.title)
28
        .appendTo('#' + id);
29
30
    $('<div></div>')
31
        .html(options.message)
32
        .addClass('message')
33
        .appendTo('#' + id);
34
35
    $('<i class="close fa fa-times-circle"></i>')
36
        .click(function() {
37
            var message = $(this).parent();
38
            message.slideUp('fast');
39
            clearTimeout(message.data('timer'));
40
41
            // Return without removing the object
42
            if (!MIDCOM_SERVICES_UIMESSAGES_REMOVE) {
43
                return;
44
            }
45
46
            // Remove the element after some safety margin
47
            message.data('timer', setTimeout(function() {
48
                message.remove();
49
            }, MIDCOM_SERVICES_UIMESSAGES_SLIDE_DELAY));
50
        })
51
        .prependTo('#' + id);
52
53
    switch (options.type) {
54
        case MIDCOM_SERVICES_UIMESSAGES_TYPE_INFO:
55
            $('#' + id).data('timer', setTimeout(function() {
56
                $('#' + id).slideUp(MIDCOM_SERVICES_UIMESSAGES_SLIDE_SPEED);
57
58
                // Return without removing the object
59
                if (!MIDCOM_SERVICES_UIMESSAGES_REMOVE) {
60
                    return;
61
                }
62
63
                // Remove the element after some safety margin
64
                $('#' + id).data('timer', setTimeout(function() {
65
                    $('#' + id).remove();
66
                }, MIDCOM_SERVICES_UIMESSAGES_SLIDE_DELAY));
67
            }, MIDCOM_SERVICES_UIMESSAGES_SLIDE_DELAY));
68
69
            break;
70
71
        case MIDCOM_SERVICES_UIMESSAGES_TYPE_ERROR:
72
            if (MIDCOM_SERVICES_UIMESSAGES_ERROR_HIGHLIGHT) {
73
                $('#' + id).addClass('pulsate');
74
            }
75
            break;
76
77
        case MIDCOM_SERVICES_UIMESSAGES_TYPE_OK:
78
        case MIDCOM_SERVICES_UIMESSAGES_TYPE_WARNING:
79
        default:
80
            // Do nothing?
81
            break;
82
83
    }
84
85
    MIDCOM_SERVICES_UIMESSAGES_INDEX++;
86
}
87