Welcome to mirror list, hosted at ThFree Co, Russian Federation.

notification.js « javascripts « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f0563c203e75cf0f192531a5319e46ab04bbaa1f (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

(function ($, require) {

    var exports = require('piwik/UI');

    /**
     * Creates a new notifications.
     *
     * Example:
     * var UI = require('piwik/UI');
     * var notification = new UI.Notification();
     * notification.show('My Notification Message', {title: 'Low space', context: 'warning'});
     */
    var Notification = function () {
        this.$node = null;
    };

    /**
     * Makes the notification visible.
     *
     * @param    {string}  message    The actual message that will be displayed. Must be set.
     * @param    {Object}  [options]
     * @param    {string}  [options.id]         Only needed for persistent notifications. The id will be sent to the
     *                                          frontend once the user closes the notifications. The notification has to
     *                                          be registered/notified under this name
     * @param    {string}  [options.title]      The title of the notification. For instance the plugin name.
     * @param    {bool}    [options.animate=true]     If enabled, the notification will be faded in.
     * @param    {string}  [options.context=warning]  Context of the notification: 'info', 'warning', 'success' or
     *                                                'error'
     * @param    {string}  [options.type=transient]   The type of the notification: Either 'toast' or 'transitent'
     * @param    {bool}    [options.noclear=false]    If set, the close icon is not displayed.
     * @param    {object}  [options.style]            Optional style/css dictionary. For instance {'display': 'inline-block'}
     * @param    {string}  [options.placeat]          By default, the notification will be displayed in the "stats bar".
     *                                                You can specify any other CSS selector to place the notifications
     *                                                wherever you want.
     */
    Notification.prototype.show = function (message, options) {
        checkMessage(message);
        options = checkOptions(options);

        var template = generateNotificationHtmlMarkup(options, message);
        this.$node   = placeNotification(template, options);
    };

    /**
     * Removes a previously shown notification having the given notification id.
     *
     *
     * @param {string}  notificationId   The id of a notification that was previously registered.
     */
    Notification.prototype.remove = function (notificationId) {
        $('[piwik-notification][notification-id=' + notificationId + ']').remove();
    };

    Notification.prototype.scrollToNotification = function () {
        if (this.$node) {
            piwikHelper.lazyScrollTo(this.$node, 250);
        }
    };

    /**
     * Shows a notification at a certain point with a quick upwards animation.
     *
     * TODO: if the materializecss version matomo uses is updated, should use their toasts.
     *
     * @type {Notification}
     * @param    {string}  message    The actual message that will be displayed. Must be set.
     * @param    {Object}  options
     * @param    {string}  options.placeat          Where to place the notification. Required.
     * @param    {string}  [options.id]         Only needed for persistent notifications. The id will be sent to the
     *                                          frontend once the user closes the notifications. The notification has to
     *                                          be registered/notified under this name
     * @param    {string}  [options.title]      The title of the notification. For instance the plugin name.
     * @param    {string}  [options.context=warning]  Context of the notification: 'info', 'warning', 'success' or
     *                                                'error'
     * @param    {string}  [options.type=transient]   The type of the notification: Either 'toast' or 'transitent'
     * @param    {bool}    [options.noclear=false]    If set, the close icon is not displayed.
     * @param    {object}  [options.style]            Optional style/css dictionary. For instance {'display': 'inline-block'}
     */
    Notification.prototype.toast = function (message, options) {
        checkMessage(message);
        options = checkOptions(options);

        var $placeat = $(options.placeat);
        if (!$placeat.length) {
            throw new Error("A valid selector is required for the placeat option when using Notification.toast().");
        }

        var $template = $(generateNotificationHtmlMarkup(options, message)).hide();
        $('body').append($template);

        compileNotification($template);

        $template.css({
            position: 'absolute',
            left: $placeat.offset().left,
            top: $placeat.offset().top
        });
        setTimeout(function () {
            $template.animate(
                {
                    top: $placeat.offset().top - $template.height()
                },
                {
                    duration: 300,
                    start: function () {
                        $template.show();
                    }
                }
            );
        });
    };

    exports.Notification = Notification;

    function generateNotificationHtmlMarkup(options, message) {
        var attributeMapping = {
                id: 'notification-id',
                title: 'notification-title',
                context: 'context',
                type: 'type',
                noclear: 'noclear',
                class: 'class',
                toastLength: 'toast-length'
            },
            html = '<div piwik-notification';

        for (var key in attributeMapping) {
            if (attributeMapping.hasOwnProperty(key)
                && options[key]
            ) {
                html += ' ' + attributeMapping[key] + '="' + options[key].toString().replace(/"/g, "&quot;") + '"';
            }
        }

        html += '>' + message + '</div>';

        return html;
    }

    function compileNotification($node) {
        angular.element(document).injector().invoke(function ($compile, $rootScope) {
            $compile($node)($rootScope.$new(true));
        });
    }

    function placeNotification(template, options) {
        var $notificationNode = $(template);

        // compile the template in angular
        compileNotification($notificationNode);

        if (options.style) {
            $notificationNode.css(options.style);
        }

        var notificationPosition = '#notificationContainer';
        var method = 'append';
        if (options.placeat) {
            notificationPosition = options.placeat;
        } else {
            // If a modal is open, we want to make sure the error message is visible and therefore show it within the opened modal
            var modalSelector = '.modal.open .modal-content';
            var modalOpen = $(modalSelector);
            if (modalOpen.length) {
                notificationPosition = modalSelector;
                method = 'prepend';
            }
        }

        $notificationNode = $notificationNode.hide();
        $(notificationPosition)[method]($notificationNode);

        if (false === options.animate) {
            $notificationNode.show();
        } else {
            $notificationNode.fadeIn(1000);
        }

        return $notificationNode;
    }

    function checkMessage(message) {
        if (!message) {
            throw new Error('No message given, cannot display notification');
        }
    }

    function checkOptions(options) {
        if (options && !$.isPlainObject(options)) {
            throw new Error('Options has the wrong format, cannot display notification');
        } else if (!options) {
            options = {};
        }
        return options;
    }

})(jQuery, require);