summaryrefslogtreecommitdiff
path: root/htdocs/js/common/20-dynlogo.js
blob: 2d62a27a9073efdeb1b8a3f126cdd251b8a41336 (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
var DynamicLogo = {
    dynLink: null,
    afr: null,
    afrUrl: null,

    init: function() {
        this.dynLink = ge('head_dyn_link');
        this.cdText = ge('head_cd_text');

        if (!this.dynLink) {
            return console.warn('DynamicLogo.init: !this.dynLink');
        }

        var spans = this.dynLink.querySelectorAll('span.head-logo-path');
        for (var i = 0; i < spans.length; i++) {
            var span = spans[i];
            addEvent(span, 'mouseover', this.onSpanOver);
            addEvent(span, 'mouseout', this.onSpanOut);
        }
    },

    setUrl: function(url) {
        if (this.afr !== null) {
            cancelAnimationFrame(this.afr);
        }
        this.afrUrl = url;
        this.afr = requestAnimationFrame(this.onAnimationFrame);
    },

    onAnimationFrame: function() {
        var url = this.afrUrl;

        // update link
        this.dynLink.setAttribute('href', url);

        // update console text
        if (this.afrUrl === '/') {
            url = '~';
        } else {
            url = '~'+url.replace(/\/$/, '');
        }
        this.cdText.innerHTML = escape(url);

        this.afr = null;
    },

    onSpanOver: function() {
        var span = event.target;
        this.setUrl(span.getAttribute('data-url'));
        cancelEvent(event);
    },

    onSpanOut: function() {
        var span = event.target;
        this.setUrl('/');
        cancelEvent(event);
    }
};

bindEventHandlers(DynamicLogo);