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

followingpages.js « client « Overlay « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 860fbc2274a68a17aace27a0cac8f63df41259d0 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
var Piwik_Overlay_FollowingPages = (function () {

    /** jQuery */
    var $ = jQuery;

    /** Info about the following pages */
    var followingPages = [];

    /** List of excluded get parameters */
    var excludedParams = [];

    /** Index of the links on the page */
    var linksOnPage = {};

    /** Reference to create element function */
    var c;

    /** Load the following pages */
    function load(callback) {
        // normalize current location
        var location = window.location.href;
        location = Piwik_Overlay_UrlNormalizer.normalize(location);
        location = (("https:" == document.location.protocol) ? 'https' : 'http') + '://' + location;

        var excludedParamsLoaded = false;
        var followingPagesLoaded = false;

        // load excluded params
        Piwik_Overlay_Client.api('getExcludedQueryParameters', function (data) {
            for (var i = 0; i < data.length; i++) {
                if (typeof data[i] == 'object') {
                    data[i] = data[i][0];
                }
            }
            excludedParams = data;

            excludedParamsLoaded = true;
            if (followingPagesLoaded) {
                callback();
            }
        });

        // load following pages
        Piwik_Overlay_Client.api('getFollowingPages', function (data) {
            followingPages = data;
            processFollowingPages();

            followingPagesLoaded = true;
            if (excludedParamsLoaded) {
                callback();
            }
        }, 'url=' + encodeURIComponent(location));
    }

    /** Normalize the URLs of following pages and aggregate some stats */
    function processFollowingPages() {
        var totalClicks = 0;
        for (var i = 0; i < followingPages.length; i++) {
            var page = followingPages[i];
            // though the following pages are returned without the prefix, downloads
            // and outlinks still have it.
            page.label = Piwik_Overlay_UrlNormalizer.removeUrlPrefix(page.label);
            totalClicks += followingPages[i].referrals;
        }
        for (i = 0; i < followingPages.length; i++) {
            followingPages[i].clickRate = followingPages[i].referrals / totalClicks * 100;
        }
    }

    /**
     * Build an index of links on the page.
     * This function is passed to $('a').each()
     */
    var processLinkDelta = false;

    function processLink() {
        var a = $(this);
        a[0].piwikDiscovered = true;

        var href = a.attr('href');
        href = Piwik_Overlay_UrlNormalizer.normalize(href);

        if (href) {
            if (typeof linksOnPage[href] == 'undefined') {
                linksOnPage[href] = [a];
            }
            else {
                linksOnPage[href].push(a);
            }
        }

        if (href && processLinkDelta !== false) {
            if (typeof processLinkDelta[href] == 'undefined') {
                processLinkDelta[href] = [a];
            }
            else {
                processLinkDelta[href].push(a);
            }
        }
    }

    var repositionTimeout = false;
    var resizeTimeout = false;

    function build(callback) {
        // build an index of all links on the page
        $('a').each(processLink);

        // add tags to known following pages
        createLinkTags(linksOnPage);

        // position the tags
        positionLinkTags();

        callback();

        // check on a regular basis whether new links have appeared.
        // we use a timeout instead of an interval to make sure one call is done before
        // the next one is triggered
        var repositionAfterTimeout;
        repositionAfterTimeout = function () {
            repositionTimeout = window.setTimeout(function () {
                findNewLinks();
                positionLinkTags(repositionAfterTimeout);
            }, 1800);
        };
        repositionAfterTimeout();

        // reposition link tags on window resize
        $(window).resize(function () {
            if (repositionTimeout) {
                window.clearTimeout(repositionTimeout);
            }
            if (resizeTimeout) {
                window.clearTimeout(resizeTimeout);
            }
            resizeTimeout = window.setTimeout(function () {
                positionLinkTags();
                repositionAfterTimeout();
            }, 70);
        });
    }

    /** Create a batch of link tags */
    function createLinkTags(links) {
        var body = $('body');
        for (var i = 0; i < followingPages.length; i++) {
            var url = followingPages[i].label;
            if (typeof links[url] != 'undefined') {
                for (var j = 0; j < links[url].length; j++) {
                    createLinkTag(links[url][j], url, followingPages[i], body);
                }
            }
        }
    }

    /** Create the link tag element */
    function createLinkTag(linkTag, linkUrl, data, body) {
        if (typeof linkTag[0].piwikTagElement != 'undefined' && linkTag[0].piwikTagElement !== null) {
            // this link tag already has a tag element. happens in rare cases.
            return;
        }

        linkTag[0].piwikTagElement = true;

        var rate = data.clickRate;
        if (rate < 10) {
            rate = Math.round(rate * 10) / 10;
        } else {
            rate = Math.round(rate);
        }

        var span = c('span').html(rate + '%');
        var tagElement = c('div', 'LinkTag').append(span).hide();
        body.prepend(tagElement);

        linkTag.add(tagElement).hover(function () {
            highlightLink(linkTag, linkUrl, data);
        }, function () {
            unHighlightLink(linkTag, linkUrl);
        });

        // attach the tag element to the link element. we can't use .data() because jquery
        // would remove it when removing the link from the dom. but we still need to find
        // the tag element to remove it as well.
        linkTag[0].piwikTagElement = tagElement;
    }

    /** Position the link tags next to the links */
    function positionLinkTags(callback) {
        var url, linkTag, tagElement, offset, top, left, isRight, hasOneChild, inlineChild;
        var tagWidth = 36, tagHeight = 21;
        var tagsToRemove = [];

        for (var i = 0; i < followingPages.length; i++) {
            url = followingPages[i].label;
            if (typeof linksOnPage[url] != 'undefined') {
                for (var j = 0; j < linksOnPage[url].length; j++) {
                    linkTag = linksOnPage[url][j];
                    tagElement = linkTag[0].piwikTagElement;

                    if (linkTag.closest('html').length == 0 || !tagElement) {
                        // the link has been removed from the dom
                        if (tagElement) {
                            tagElement.hide();
                        }
                        // mark for deletion. don't delete it now because we
                        // are iterating of the array it's in. it will be deleted
                        // below this for loop.
                        tagsToRemove.push({
                            index1: url,
                            index2: j
                        });
                        continue;
                    }

                    hasOneChild = checkHasOneChild(linkTag);
                    inlineChild = false;
                    if (hasOneChild && linkTag.css('display') != 'block') {
                        inlineChild = linkTag.children().eq(0);
                    }

                    if (getVisibility(linkTag) == 'hidden' || (
                        // in case of hasOneChild: jquery always returns linkTag.is(':visible')=false
                        !linkTag.is(':visible') && !(hasOneChild && inlineChild && inlineChild.is(':visible'))
                        )) {
                        // link is not visible
                        tagElement.hide();
                        continue;
                    }

                    tagElement.attr('class', 'PIS_LinkTag'); // reset class
                    if (tagElement[0].piwikHighlighted) {
                        tagElement.addClass('PIS_Highlighted');
                    }

                    // see comment in highlightLink()
                    if (hasOneChild && linkTag.find('> img').size() == 1) {
                        offset = linkTag.find('> img').offset();
                        if (offset.left == 0 && offset.top == 0) {
                            offset = linkTag.offset();
                        }
                    } else if (inlineChild !== false) {
                        offset = inlineChild.offset();
                    } else {
                        offset = linkTag.offset();
                    }

                    top = offset.top - tagHeight + 6;
                    left = offset.left - tagWidth + 10;

                    if (isRight = (left < 2)) {
                        tagElement.addClass('PIS_Right');
                        left = offset.left + linkTag.outerWidth() - 10;
                    }

                    if (top < 2) {
                        tagElement.addClass(isRight ? 'PIS_BottomRight' : 'PIS_Bottom');
                        top = offset.top + linkTag.outerHeight() - 6;
                    }

                    tagElement.css({
                        top: top + 'px',
                        left: left + 'px'
                    }).show();

                }
            }
        }

        // walk tagsToRemove from back to front because it contains the indexes in ascending
        // order. removing something from the front will impact the indexes that come after-
        // wards. this can be avoided by starting in the back.
        for (var k = tagsToRemove.length - 1; k >= 0; k--) {
            var tagToRemove = tagsToRemove[k];
            linkTag = linksOnPage[tagToRemove.index1][tagToRemove.index2];
            // remove the tag element from the dom
            if (linkTag && linkTag[0] && linkTag[0].piwikTagElement) {
                tagElement = linkTag[0].piwikTagElement;
                if (tagElement[0].piwikHighlighted) {
                    unHighlightLink(linkTag, tagToRemove.index1);
                }
                tagElement.remove();
                linkTag[0].piwikTagElement = null;
            }
            // remove the link from the index
            linksOnPage[tagToRemove.index1].splice(tagToRemove.index2, 1);
            if (linksOnPage[tagToRemove.index1].length == 0) {
                delete linksOnPage[tagToRemove.index1];
            }
        }

        if (typeof callback == 'function') {
            callback();
        }
    }

    /** Get the visibility of an element */
    function getVisibility(el) {
        var visibility = el.css('visibility');
        if (visibility == 'inherit') {
            el = el.parent();
            if (el.size() > 0) {
                return getVisibility(el);
            }
        }
        return visibility;
    }

    /**
     * Find out whether a link has only one child. Using .children().size() == 1 doesn't work
     * because it doesn't take additional text nodes into account.
     */
    function checkHasOneChild(linkTag) {
        var hasOneChild = (linkTag.children().size() == 1);
        if (hasOneChild) {
            // if the element contains one tag and some text, hasOneChild is set incorrectly
            var contents = linkTag.contents();
            if (contents.size() > 1) {
                // find non-empty text nodes
                contents = contents.filter(function () {
                    return this.nodeType == 3 && // text node
                        $.trim(this.data).length > 0; // contains more than whitespaces
                });
                if (contents.size() > 0) {
                    hasOneChild = false;
                }
            }
        }
        return hasOneChild;
    }

    /** Check whether new links have been added to the dom */
    function findNewLinks() {
        var newLinks = $('a').filter(function () {
            return typeof this.piwikDiscovered == 'undefined' || this.piwikDiscovered === null;
        });

        if (newLinks.size() == 0) {
            return;
        }

        processLinkDelta = {};
        newLinks.each(processLink);
        createLinkTags(processLinkDelta);
        processLinkDelta = false;
    }

    /** Dom elements used for drawing a box around the link */
    var highlightElements = [];

    /** Highlight a link on hover */
    function highlightLink(linkTag, linkUrl, data) {
        if (highlightElements.length == 0) {
            highlightElements.push(c('div', 'LinkHighlightBoxTop'));
            highlightElements.push(c('div', 'LinkHighlightBoxRight'));
            highlightElements.push(c('div', 'LinkHighlightBoxLeft'));

            highlightElements.push(c('div', 'LinkHighlightBoxText'));

            var body = $('body');
            for (var i = 0; i < highlightElements.length; i++) {
                body.prepend(highlightElements[i].css({display: 'none'}));
            }
        }

        var width = linkTag.outerWidth();

        var offset, height;
        var hasOneChild = checkHasOneChild(linkTag);
        if (hasOneChild && linkTag.find('img').size() == 1) {
            // if the <a> tag contains only an <img>, the offset and height methods don't work properly.
            // as a result, the box around the image link would be wrong. we use the image to derive
            // the offset and height instead of the link to get correct values.
            var img = linkTag.find('img');
            offset = img.offset();
            height = img.outerHeight();
        }
        if (hasOneChild && linkTag.css('display') != 'block') {
            // if the <a> tag is not displayed as block and has only one child, using the child to
            // derive the offset and dimensions is more robust.
            var child = linkTag.children().eq(0);
            offset = child.offset();
            height = child.outerHeight();
            width = child.outerWidth();
        } else {
            offset = linkTag.offset();
            height = linkTag.outerHeight();
        }

        var numLinks = linksOnPage[linkUrl].length;

        putBoxAroundLink(offset, width, height, numLinks, data.referrals);

        // highlight tags
        for (var j = 0; j < numLinks; j++) {
            var tag = linksOnPage[linkUrl][j][0].piwikTagElement;
            tag.addClass('PIS_Highlighted');
            tag[0].piwikHighlighted = true;
        }

        // Sometimes it fails to remove the notification when the hovered element is removed.
        // To make sure we don't display more than one location at a time, we hide all before showing the new one.
        Piwik_Overlay_Client.hideNotifications('LinkLocation');

        // we don't use .data() because jquery would remove the callback when the link tag is removed
        linkTag[0].piwikHideNotification = Piwik_Overlay_Client.notification(
            Piwik_Overlay_Translations.get('link') + ': ' + linkUrl, 'LinkLocation');
    }

    function putBoxAroundLink(offset, width, height, numLinks, numReferrals) {
        var borderWidth = 2;
        var padding = 4; // the distance between the link and the border

        // top border
        highlightElements[0]
            .width(width + 2 * padding)
            .css({
                top: offset.top - borderWidth - padding,
                left: offset.left - padding
            }).show();

        // right border
        highlightElements[1]
            .height(height + 2 * borderWidth + 2 * padding)
            .css({
                top: offset.top - borderWidth - padding,
                left: offset.left + width + padding
            }).show();

        // left border
        highlightElements[2]
            .height(height + 2 * borderWidth + 2 * padding)
            .css({
                top: offset.top - borderWidth - padding,
                left: offset.left - borderWidth - padding
            }).show();

        // bottom box text
        var text;
        if (numLinks > 1) {
            text = Piwik_Overlay_Translations.get('clicksFromXLinks')
                .replace(/%1\$s/, numReferrals)
                .replace(/%2\$s/, numLinks);
        } else if (numReferrals == 1) {
            text = Piwik_Overlay_Translations.get('oneClick');
        } else {
            text = Piwik_Overlay_Translations.get('clicks')
                .replace(/%s/, numReferrals);
        }

        // bottom box position and dimension
        var textPadding = '&nbsp;&nbsp;&nbsp;';
        highlightElements[3].html(textPadding + text + textPadding).css({
            width: 'auto',
            top: offset.top + height + padding,
            left: offset.left - borderWidth - padding
        }).show();

        var minBoxWidth = width + 2 * borderWidth + 2 * padding;
        if (highlightElements[3].width() < minBoxWidth) {
            // we cannot use minWidth because of IE7
            highlightElements[3].width(minBoxWidth);
        }
    }

    /** Remove highlight from link */
    function unHighlightLink(linkTag, linkUrl) {
        for (var i = 0; i < highlightElements.length; i++) {
            highlightElements[i].hide();
        }

        var numLinks = linksOnPage[linkUrl].length;
        for (var j = 0; j < numLinks; j++) {
            var tag = linksOnPage[linkUrl][j][0].piwikTagElement;
            if (tag) {
                tag.removeClass('PIS_Highlighted');
                tag[0].piwikHighlighted = false;
            }
        }

        if ((typeof linkTag[0].piwikHideNotification) == 'function') {
            linkTag[0].piwikHideNotification();
            linkTag[0].piwikHideNotification = null;
        }
    }

    return {

        /**
         * The main method
         */
        initialize: function (finishCallback) {
            c = Piwik_Overlay_Client.createElement;
            Piwik_Overlay_Client.loadScript('plugins/Overlay/client/urlnormalizer.js', function () {
                Piwik_Overlay_UrlNormalizer.initialize();
                load(function () {
                    Piwik_Overlay_UrlNormalizer.setExcludedParameters(excludedParams);
                    build(function () {
                        finishCallback();
                    })
                });
            });
        },

        /**
         * Remove everything from the dom and terminate timeouts.
         * This can be used from the console in order to load a new implementation for debugging afterwards.
         * If you add `Piwik_Overlay_FollowingPages.remove();` to the beginning and
         * `Piwik_Overlay_FollowingPages.initialize(function(){});` to the end of this file, you can just
         * paste it into the console to inject the new implementation.
         */
        remove: function () {
            for (var i = 0; i < followingPages.length; i++) {
                var url = followingPages[i].label;
                if (typeof linksOnPage[url] != 'undefined') {
                    for (var j = 0; j < linksOnPage[url].length; j++) {
                        var linkTag = linksOnPage[url][j];
                        var tagElement = linkTag[0].piwikTagElement;
                        if (tagElement) {
                            tagElement.remove();
                        }
                        linkTag[0].piwikTagElement = null;

                        $(linkTag).unbind('mouseenter').unbind('mouseleave');
                    }
                }
            }
            for (i = 0; i < highlightElements.length; i++) {
                highlightElements[i].remove();
            }
            if (repositionTimeout) {
                window.clearTimeout(repositionTimeout);
            }
            if (resizeTimeout) {
                window.clearTimeout(resizeTimeout);
            }
            $(window).unbind('resize');
        }

    };

})();