-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-roulette.html
More file actions
113 lines (113 loc) · 4.76 KB
/
string-roulette.html
File metadata and controls
113 lines (113 loc) · 4.76 KB
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
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
:root {
--header-background-color: lightgray;
}
html, body { margin: 0; padding: 0; }
html, body, div#body { height: 100%; overflow: auto; }
div#header { text-align: center; padding-top: 0.25em; padding-bottom: 2em; background-color: var(--header-background-color, lightgray); }
div#body { padding-top: 3em; }
div#list_picker_container { text-align: center; }
span#list_picker button { margin: auto 0.25em; }
span#list_picker button.active { font-weight: bold; }
div#string_container, div#secondary_string_container { text-align: center; }
div#string_container { padding-bottom: 3em; }
span#the_string { font-size: 3em; font-weight: bold; }
span#secondary_string { font-style: italic; }
</style>
<title>String Roulette</title>
<script type="text/javascript">
var document_title = "String Roulette";
var lists = {"numbers":["0","1","2","3","4","5","6","7","8","9"]};
var list_name = "";
var list = "";
function update_string(list_name) {
if ( ! list_name ) {
// Get list name from URI query string
// .../string-roulette.html?list=numbers
var list_name = new URLSearchParams(window.location.search).get("list");
// Get list name from URI fragment
// .../string-roulette.html#numbers
//var list_name = window.location.hash.slice(1);
}
if ( lists[list_name] ) {
list = lists[list_name];
} else {
list_name = "numbers";
list = lists["numbers"];
}
var list_selection = Math.floor(Math.random() * list.length);
if ( typeof list[list_selection] === "string" ) {
var new_string = list[list_selection];
} else if ( typeof list[list_selection] === "object" ) {
var new_string = list[list_selection][0];
var new_secondary_string = list[list_selection][1];
} else {
//alert("Unknown data type: " + typeof(list[list_selection]));
console.error("Unknown data type: " + typeof(list[list_selection]));
exit;
}
document.getElementsByTagName("title")[0].innerText = document_title + " (" + list_name + ")";
document.getElementById("page_heading").innerText = document_title;
document.getElementById("the_string").innerText = new_string;
if ( typeof new_secondary_string === 'undefined' ) {
document.getElementById("secondary_string").innerText = "";
} else {
document.getElementById("secondary_string").innerText = new_secondary_string;
}
// Rewrite div#body onMouseUp handler
var body_element = document.getElementById("body")
body_element.removeAttribute("onmouseup");
body_element.setAttribute("onmouseup", "update_string('" + list_name + "')");
// Update location bar
window.history.pushState("{ list_name: " + list_name + "}", document.title, window.location.protocol + "/" + window.location.host + "/" + window.location.pathname + "?list=" + list_name);
// Update active list picker button
var list_picker_items = document.getElementsByClassName("list_picker_item");
for ( var i=0; i < list_picker_items.length; i++ ) {
if ( list_picker_items[i].innerText == list_name ) {
if ( ! list_picker_items[i].classList.contains("active") ) { list_picker_items[i].classList.add("active"); }
} else {
if ( list_picker_items[i].classList.contains("active") ) { list_picker_items[i].classList.remove("active"); }
}
}
}
function update_list_picker() {
var picker_contents = "";
for (var local_list_name in lists) {
picker_contents = picker_contents + '<button class="list_picker_item" onMouseUp="update_string(\'' + local_list_name + '\')">' + local_list_name + '</button>';
}
picker_contents = picker_contents.slice(0,-3);
document.getElementById("list_picker").innerHTML = picker_contents;
}
function do_onload() {
console.log("Loading scripts");
update_list_picker();
update_string();
}
// Run on page load
window.onload = do_onload;
</script>
<script src="lists.js"></script>
</head>
<body>
<div id="header">
<h1 id="page_heading"></h1>
<div id="list_picker_container">
<span id="list_picker"></span>
</div>
</div>
<div id="body" onMouseUp="update_string();">
<div id="string_container">
<span id="the_string"></span>
</div>
<div id="secondary_string_container">
<span id="secondary_string"></span>
</div>
</div>
<div id="footer">
</div>
</body>
</html>