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

live.js « scripts « templates « Live « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bad5c82ee572562ab4d7ba1dfaa23c19992680d5 (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
/*!
 * Piwik - Web Analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

/**
 * jQuery Plugin for Live visitors widget
 */

(function ($) {
    $.extend({
        liveWidget: new function () {

            /**
             * Default settings for widgetPreview
             */
            var settings = {
                // Maximum numbers of rows to display in widget
                maxRows: 10,
                // minimal time in microseconds to wait between updates
                interval: 3000,
                // maximum time to wait between requests
                maxInterval: 300000,
                // url params to use for data request
                dataUrlParams: null,
                // callback triggered on a successfull update (content of widget changed)
                onUpdate: null,
                // speed for fade animation
                fadeInSpeed: 'slow'
            };

            var currentInterval, updated, updateInterval, liveWidget;
            var isStarted = true;

            /**
             * Update the widget
             */
            function update() {

                // is content updated (eg new visits/views)
                updated = false;

                var ajaxRequest = new ajaxHelper();
                ajaxRequest.addParams(settings.dataUrlParams, 'GET');
                ajaxRequest.setFormat('html');
                ajaxRequest.setCallback(function (r) {
                    parseResponse(r);

                    // add default interval to last interval if not updated or reset to default if so
                    if (!updated) {
                        currentInterval += settings.interval;
                    } else {
                        currentInterval = settings.interval;
                        if (settings.onUpdate) settings.onUpdate();
                    }

                    // check new interval doesn't reach the defined maximum
                    if (settings.maxInterval < currentInterval) {
                        currentInterval = settings.maxInterval;
                    }

                    if (isStarted) {
                        window.clearTimeout(updateInterval);
                        if ($(liveWidget).closest('body').length) {
                            updateInterval = window.setTimeout(update, currentInterval);
                        }
                    }
                });
                ajaxRequest.send(false);
            };

            /**
             * Parses the given response and updates the widget if newer content is available
             */
            function parseResponse(data) {
                if (!data || !data.length) {
                    updated = false;
                    return;
                }

                var items = $('li', $(data));
                for (var i = items.length; i--;) {
                    parseItem(items[i]);
                }
            };

            /**
             * Parses the given item and updates or adds an entry to the list
             *
             * @param item to parse
             */
            function parseItem(item) {
                var visitId = $(item).attr('id');
                if ($('#' + visitId, liveWidget).length) {
                    if ($('#' + visitId, liveWidget).html() != $(item).html()) {
                        updated = true;
                    }
                    $('#' + visitId, liveWidget).remove();
                    $(liveWidget).prepend(item);
                } else {
                    updated = true;
                    $(item).hide();
                    $(liveWidget).prepend(item);
                    $(item).fadeIn(settings.fadeInSpeed);
                }
                // remove rows if there are more than the maximum
                $('li:gt(' + (settings.maxRows - 1) + ')', liveWidget).remove();
            };

            /**
             * Constructor
             *
             * @param object userSettings Settings to be used
             * @return void
             */
            this.construct = function (userSettings) {
                settings = jQuery.extend(settings, userSettings);

                if (!settings.dataUrlParams) {
                    console && console.error('liveWidget error: dataUrlParams needs to be defined in settings.');
                    return;
                }

                liveWidget = this;

                currentInterval = settings.interval;

                updateInterval = window.setTimeout(update, currentInterval);
            };

            /**
             * Triggers an update for the widget
             *
             * @return void
             */
            this.update = function () {
                update();
            };

            /**
             * Starts the automatic update cycle
             */
            this.start = function () {
                isStarted = true;
                currentInterval = 0;
                update();
            };

            /**
             * Stops the automatic update cycle
             */
            this.stop = function () {
                isStarted = false;
                window.clearTimeout(updateInterval);
            };

            /**
             * Set the interval for refresh
             */
            this.setInterval = function (interval) {
                currentInterval = interval;
            };
        }
    });

    /**
     * Makes liveWidget available with $().liveWidget()
     */
    $.fn.extend({
        liveWidget: $.liveWidget.construct
    });
})(jQuery);


var pauseImage = "plugins/Live/templates/images/pause.gif";
var pauseDisabledImage = "plugins/Live/templates/images/pause_disabled.gif";
var playImage = "plugins/Live/templates/images/play.gif";
var playDisabledImage = "plugins/Live/templates/images/play_disabled.gif";
function onClickPause() {
    $('#pauseImage').attr('src', pauseImage);
    $('#playImage').attr('src', playDisabledImage);
    return $.liveWidget.stop();
}
function onClickPlay() {
    $('#playImage').attr('src', playImage);
    $('#pauseImage').attr('src', pauseDisabledImage);
    return $.liveWidget.start();
}