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

client.js « client « Overlay « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 04b1f88f1390e5b9e8eaa8073d5fce79f61f75e0 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
var Piwik_Overlay_Client = (function () {

    /** jQuery */
    var $;

    /** Url of the Piwik root */
    var piwikRoot;

    /** Piwik idsite */
    var idSite;

    /** The current period and date */
    var period, date;

    /** Reference to the status bar DOM element */
    var statusBar;

    /** Load the client CSS */
    function loadCss() {
        var css = c('link').attr({
            rel: 'stylesheet',
            type: 'text/css',
            href: piwikRoot + 'plugins/Overlay/client/client.css'
        });
        $('head').append(css);
    }

    /**
     * This method loads jQuery, if it is not there yet.
     * The callback is triggered after jQuery is loaded.
     */
    function loadJQuery(callback) {
        if (typeof jQuery != 'undefined') {
            $ = jQuery;
            callback();
        }
        else {
            Piwik_Overlay_Client.loadScript('libs/bower_components/jquery/dist/jquery.min.js', function () {
                $ = jQuery;
                jQuery.noConflict();
                callback();
            });
        }
    }

    /**
     * Notify Piwik of the current iframe location.
     * This way, we can display additional metrics on the side of the iframe.
     */
    function notifyPiwikOfLocation() {
        // check whether the session has been opened in a new tab (instead of an iframe)
        if (window != window.top) {
            var iframe = c('iframe', false, {
                src: piwikRoot + 'index.php?module=Overlay&action=notifyParentIframe#' + window.location.href
            }).css({width: 0, height: 0, border: 0});

            // in some cases, calling append right away doesn't work in IE8
            $(document).ready(function () {
                $('body').append(iframe);
            });
        }
    }

    /** Create a jqueryfied DOM element */
    function c(tagName, className, attributes) {
        var el = $(document.createElement(tagName));

        if (className) {
            if (className.substring(0, 1) == '#') {
                var id = className.substring(1, className.length);
                id = 'PIS_' + id;
                el.attr('id', id);
            }
            else {
                className = 'PIS_' + className;
                el.addClass(className);
            }
        }

        if (attributes) {
            el.attr(attributes);
        }

        return el;
    }

    /** Special treatment for some internet explorers */
    var ieStatusBarEventsBound = false;

    function handleIEStatusBar() {
        if (navigator.appVersion.indexOf("MSIE 7.") == -1
            && navigator.appVersion.indexOf("MSIE 8.") == -1) {
            // this is not IE8 or lower
            return;
        }

        // IE7/8 can't handle position:fixed so we need to do it by hand
        statusBar.css({
            position: 'absolute',
            right: 'auto',
            bottom: 'auto',
            left: 0,
            top: 0
        });

        var position = function () {
            var scrollY = document.body.parentElement.scrollTop;
            var scrollX = document.body.parentElement.scrollLeft;
            statusBar.css({
                top: (scrollY + $(window).height() - statusBar.outerHeight()) + 'px',
                left: (scrollX + $(window).width() - statusBar.outerWidth()) + 'px'
            });
        };

        position();

        statusBar.css({width: 'auto'});
        if (statusBar.width() < 350) {
            statusBar.width(350);
        } else {
            statusBar.width(statusBar.width());
        }

        if (!ieStatusBarEventsBound) {
            ieStatusBarEventsBound = true;
            $(window).resize(position);
            $(window).scroll(position);
        }
    }

    return {

        /** Initialize in-site analytics */
        initialize: function (pPiwikRoot, pIdSite, pPeriod, pDate) {
            piwikRoot = pPiwikRoot;
            idSite = pIdSite;
            period = pPeriod;
            date = pDate;

            var load = this.loadScript;
            var loading = this.loadingNotification;

            loadJQuery(function () {
                notifyPiwikOfLocation();
                loadCss();

                // translations
                load('plugins/Overlay/client/translations.js', function () {
                    Piwik_Overlay_Translations.initialize(function () {
                        // following pages
                        var finishPages = loading('Loading following pages');
                        load('plugins/Overlay/client/followingpages.js', function () {
                            Piwik_Overlay_FollowingPages.initialize(finishPages);
                        });

                    });
                });
            });
        },

        /** Create a jqueryfied DOM element */
        createElement: function (tagName, className, attributes) {
            return c(tagName, className, attributes);
        },

        /** Load a script and wait for it to be loaded */
        loadScript: function (relativePath, callback) {
            var loaded = false;
            var onLoad = function () {
                if (!loaded) {
                    loaded = true;
                    callback();
                }
            };

            var head = document.getElementsByTagName('head')[0];
            var script = document.createElement('script');
            script.type = 'text/javascript';

            script.onreadystatechange = function () {
                if (this.readyState == 'loaded' || this.readyState == 'complete') {
                    onLoad();
                }
            };
            script.onload = onLoad;

            script.src = piwikRoot + relativePath + '?v=1';
            head.appendChild(script);
        },

        /** Piwik Overlay API Request */
        api: function (method, callback, additionalParams) {
            var url = piwikRoot + 'index.php?module=API&method=Overlay.' + method
                + '&idSite=' + idSite + '&period=' + period + '&date=' + date + '&format=JSON&filter_limit=-1';

            if (additionalParams) {
                url += '&' + additionalParams;
            }

            $.getJSON(url + "&jsoncallback=?", function (data) {
                if (typeof data.result != 'undefined' && data.result == 'error') {
                    alert('Error: ' + data.message);
                }
                else {
                    callback(data);
                }
            });
        },

        /**
         * Initialize a notification
         * To hide the notification use the returned callback
         */
        notification: function (message, addClass) {
            if (!statusBar) {
                statusBar = c('div', '#StatusBar').css('opacity', .8);
                $('body').prepend(statusBar);
            }

            var item = c('div', 'Item').html(message);

            if (addClass) {
                item.addClass('PIS_' + addClass);
            }

            statusBar.show().append(item);

            handleIEStatusBar();
            window.setTimeout(handleIEStatusBar, 100);

            return function () {
                item.remove();
                if (statusBar.children().size() == 0) {
                    statusBar.hide();
                } else {
                    handleIEStatusBar();
                }
            };
        },

        /** Hide all notifications with a certain class */
        hideNotifications: function (className) {
            statusBar.find('.PIS_' + className).remove();
            if (statusBar.children().size() == 0) {
                statusBar.hide();
            } else {
                handleIEStatusBar();
            }
        },

        /**
         * Initialize a loading notification
         * To hide the notification use the returned callback
         */
        loadingNotification: function (message) {
            return Piwik_Overlay_Client.notification(message, 'Loading');
        }

    };

})();