# all files / Core/Components/Notifier/ NotifierService.js

57.14% Statements 12/21
100% Branches 0/0
12.5% Functions 1/8
57.14% Lines 12/21

    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

    1

    1

    1

    16

    16

    16

    1

    1

    1

    1

    1

    1
     'use strict';

    // Utilities:
    var _ = require('lodash');

    // Module:
    var Core = require('../../Core');

    var NotifierService = function (
        $interval
    ) {
        var NotificationTypes = {
            SUCCESS: 'success',
            INFO: 'info',
            ERROR: 'error'
        };

        var notifications = [];

        return {
            success: success,
            info: info,
            error: error,
            dismiss: dismiss,
            notifications: notifications
        };

        function addNotification (notification) {
            notifications.push(notification);
            $interval(_.noop, 0, 1);
            $interval(function () {
                dismiss(notification);
            }, 10000, 1);
        }

        function success (message) {
            addNotification({
                message: message,
                type: NotificationTypes.SUCCESS
            });
        }

        function info (message) {
            addNotification({
                message: message,
                type: NotificationTypes.INFO
            });
        }

        function error (message) {
            addNotification({
                message: message,
                type: NotificationTypes.ERROR
            });
        }

        function dismiss (toRemove) {
            _.remove(notifications, function (notification) {
                return notification === toRemove;
            });
        }
    };

    Core.service('notifierService', NotifierService);