-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
161 lines (134 loc) · 3.5 KB
/
main.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
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
/* global $ */
'use strict';
console.log('loaded');
var DEFAULT_CHANNEL_DURATION = 60 * 2;
var CHANNELS = {
algoviz: {
author: 'Neil',
},
beatprocessing: {
author: 'Mick',
},
mosaic: {
author: 'Grieve',
},
dirtygif: {
author: 'Neil',
},
reactivelogo: {
author: 'Pedro',
},
timetraveller: {
author: 'Grieve',
},
};
var CHANNEL_IDS = Object.keys(CHANNELS),
OSD_HANG_TIME = 2000;
var currentChannel = null,
nextChannel = null,
autoRandom = true;
function getURLParam(name, url) {
if (!url) url = location.href;
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regexS = '[\\?&]' + name + '=([^&#]*)';
var regex = new RegExp(regexS);
var results = regex.exec(url);
return results == null ? null : results[1];
}
function getRndInt(min, max) {
return ~~(Math.random() * (max - min + 1)) + min;
}
function getEl(id) {
return document.getElementById(id);
}
var els = {
osd: getEl('osd'),
channelName: getEl('channel-name'),
channelAuthor: getEl('channel-author'),
mainframe: getEl('mainframe'),
instructions: getEl('instructions'),
channelList: getEl('channel-list'),
};
CHANNEL_IDS.forEach(function (name, idx) {
var item = document.createElement('p');
item.textContent = idx + 1 + ' - ' + name;
els.channelList.appendChild(item);
});
var _ti;
function changeChannel(id) {
console.log('change channel', id);
var ipt = getURLParam('input');
console.log('URL PARAM', ipt);
var inputAppend = ipt !== null ? '?input=' + ipt : '';
var src = id === null ? '' : '/channels/' + id + inputAppend;
els.mainframe.setAttribute('src', src);
currentChannel = id;
var name;
var author;
if (currentChannel) {
name = id;
author = CHANNELS[currentChannel].author;
} else {
name = 'RHB';
author = '...';
}
clearTimeout(_ti);
els.channelName.textContent = name;
els.channelAuthor.textContent = author;
els.osd.classList.add('visible');
setTimeout(function () {
els.osd.classList.remove('visible');
}, OSD_HANG_TIME);
}
function changeRandomChannel() {
var cid = CHANNEL_IDS[getRndInt(0, CHANNEL_IDS.length - 1)];
changeChannel(cid);
}
var _ti;
function timedChangeRandom() {
if (autoRandom == false) return false;
var nid = currentChannel;
while (nid === currentChannel) {
nid = CHANNEL_IDS[getRndInt(0, CHANNEL_IDS.length - 1)];
}
_ti = setTimeout(
function () {
changeChannel(nid);
timedChangeRandom();
},
(CHANNELS[nid].duration || DEFAULT_CHANNEL_DURATION) * 1000,
);
}
function enableAutoRandom() {
changeRandomChannel();
timedChangeRandom();
autoRandom = true;
console.log('autoRandom on');
}
function disableAutoRandom() {
clearInterval(_ti);
autoRandom = false;
console.log('autoRandom off');
}
$(document).on('keypress', function (e) {
var k = e.which || e.keyCode;
var nk = k - 48;
console.log('keypress', k, nk);
if (nk >= 0 && nk <= CHANNEL_IDS.length) {
disableAutoRandom();
var cid = nk ? CHANNEL_IDS[nk - 1] : null;
changeChannel(cid);
}
if (k == 82) {
// R
if (autoRandom) {
disableAutoRandom();
} else {
enableAutoRandom();
}
}
});
setTimeout(function () {
els.instructions.classList.remove('visible');
enableAutoRandom();
}, 6000);