-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcode.js
118 lines (102 loc) · 2.41 KB
/
code.js
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
var list = [];
var current = -1;
var preloaders = [];
var max_preload = 10;
// we load list.json which lists all images so far
function updateCurrent(preload_forward) {
$.History.setHash(""+current);
var im = document.getElementById("image");
if (current >= 0) {
var element = document.getElementById("current_id");
if (element.textContent != undefined)
element.textContent = current+" ("+list[current]+")";
else
element.innerText = current+" ("+list[current]+")";
im.src = "img/"+list[current];
}
var new_preloaders = [];
var i;
for (i = 1 ; i <= max_preload ; i++) {
var preload_idx = preload_forward ? (current + i) : (current - i);
var preload = list[preload_idx];
if (!preload) {
break;
}
var image = new Image();
image.src = "img/" + preload;
new_preloaders.push(image);
}
preloaders = new_preloaders;
}
function update_list(play_sound,first_run) {
$.getJSON( "list.json?r="+Math.random(), function(data) {
var do_go_last = false;
if (current == list.length - 1) do_go_last = true;
list = data;
if (first_run) {
var pos = $.History.getHash();
var pos_int = pos || 0;
if ((pos_int) || (pos == '0')) {
go_to(pos_int);
return;
}
}
if ((do_go_last) && (current != list.length - 1)) {
go_last();
if ((document.hasFocus) && (!document.hasFocus())) {
document.title = "[!] xkcd 1446";
}
if (play_sound)
$.playSound('sound');
}
});
// Disable auto loading since it's over now
// setTimeout(function() { update_list(true,false); }, 60000);
}
function go_to(frame) {
frame = frame || 0;
if (frame == current) return;
if (frame < 0) return;
if (frame > (list.length-1)) return;
current = frame;
updateCurrent(true); // assume direction will be forward
}
function go_prev() {
if (current <= 0) return;
current--;
updateCurrent(false);
}
function go_next() {
if (current >= (list.length-1)) return;
current++;
updateCurrent(true);
}
function go_first() {
current = 0;
updateCurrent(true);
}
function go_last() {
current = list.length-1;
updateCurrent(false);
}
$(document).keydown(function(e) {
switch(e.which) {
case 37: // left
go_prev();
break;
case 38: // up
go_first();
break;
case 39: // right
go_next();
break;
case 40: // down
go_last();
break;
default: return;
}
e.preventDefault();
});
update_list(false,true);
$.History.bind(go_to);
$(window).focus(function(){document.title = "xkcd 1446";});