summaryrefslogtreecommitdiff
path: root/expandvideo.js
blob: 4ecddd60aa749fb24853469376efc9b82e0b4f21 (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
function setupVideo(thumb, url) {
    var video = null;
    var videoContainer, videoHide;
    var expanded = false;
    var hovering = false;

    function unexpand() {
        if (expanded) {
            expanded = false;
            if (video.pause) video.pause();
            videoContainer.style.display = "none";
            thumb.style.display = "inline";
        }
    }

    function unhover() {
        if (hovering) {
            hovering = false;
            if (video.pause) video.pause();
            video.style.display = "none";
        }
    }

    function getVideo() {
        if (video == null) {
            video = document.createElement("video");
            video.src = url;
            video.loop = true;
            video.innerText = "Your browser does not support HTML5 video.";
            video.onclick = function(e) {
                if (e.shiftKey) {
                    unexpand();
                    e.preventDefault();
                }
            };

            videoHide = document.createElement("img");
            videoHide.src = configRoot + "cc/collapse.gif";
            videoHide.alt = "[ - ]";
            videoHide.title = "Collapse to thumbnail";
            videoHide.style.verticalAlign = "top";
            videoHide.style.marginRight = "2px";
            videoHide.onclick = unexpand;

            videoContainer = document.createElement("div");
            videoContainer.style.whiteSpace = "nowrap";
            videoContainer.appendChild(videoHide);
            videoContainer.appendChild(video);
            thumb.parentNode.insertBefore(videoContainer, thumb.nextSibling);
        }
    }

    thumb.onclick = function(e) {
        if (setting("videoexpand") && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
            getVideo();
            expanded = true;
            hovering = false;

            video.style.position = "static";
            video.style.maxWidth = "";
            video.style.maxHeight = "";

            video.style.display = "inline";
            videoHide.style.display = "inline";
            videoContainer.style.display = "block";
            thumb.style.display = "none";

            video.muted = setting("videomuted");
            video.controls = true;
            video.play();
            return false;
        }
    };

    thumb.onmouseover = function(e) {
        if (setting("videohover")) {
            getVideo();
            expanded = false;
            hovering = true;

            video.style.position = "fixed";
            video.style.right = "0px";
            video.style.top = "0px";
            video.style.maxWidth = (document.body.parentNode.getBoundingClientRect().right - thumb.getBoundingClientRect().right) + "px";
            video.style.maxHeight = "100%";

            video.style.display = "inline";
            videoHide.style.display = "none";
            videoContainer.style.display = "inline";

            video.muted = setting("videomuted");
            video.controls = false;
            video.play();
        }
    };

    thumb.onmouseout = unhover;
}

window.onload = function() {
    settingsPanel.style.position = "absolute";
    settingsPanel.style.top = "1em";
    settingsPanel.style.right = "1em";
    document.body.insertBefore(settingsPanel, document.body.firstChild);

    var thumbs = document.querySelectorAll("a.file");
    for (var i = 0; i < thumbs.length; i++) {
        if (/\.webm$/.test(thumbs[i].pathname)) {
            setupVideo(thumbs[i], thumbs[i].href);
        } else {
            var m = thumbs[i].search.match(/\bv=([^&]*)/);
            if (m != null) {
                var url = decodeURIComponent(m[1]);
                if (/\.webm$/.test(url)) setupVideo(thumbs[i], url);
            }
        }
    }
};