-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.js
157 lines (137 loc) · 2.45 KB
/
url.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
function resetUrlOptions()
{
var path = location.pathname;
history.replaceState(null, "", path);
}
function getUrlOptionString(isGraphDrawn)
{
getPageOptions();
var path = location.pathname;
var hash = "#";
var i = 0;
for (var key in pageOpts)
{
if (i != 0)
{
hash += "&"
}
hash += key + "=" + pageOpts[key].value;
i++;
}
if (isGraphDrawn)
{
hash += "&calc=true";
}
return path+hash;
}
function setUrlOptions(isGraphDrawn)
{
state = {id:'iv_chances_calc_state',options:pageOpts}
history.pushState(state, "", getUrlOptionString(isGraphDrawn));
}
function getUrlOptions()
{
clearErrors();
var err = 0;
var errortext = "";
var hash = location.hash.substr(1);
if (hash)
{
var keyval = hash.split('&');
var key, val;
var opts = {};
for (var i in keyval)
{
key = keyval[i].split('=')[0];
val = keyval[i].split('=')[1];
opts[key] = val;
}
}
var calc = false;
var optionsSet = 0;
for (var k in opts)
{
if (pageOpts[k])
{
try
{
if (pageOpts[k].optionType === "bool")
{
pageOpts[k].setValue(opts[k] === "true");
}
else if (pageOpts[k].optionType === "int")
{
if (opts[k] === "any")
{
pageOpts[k].setValue("any");
}
else
{
pageOpts[k].setValue(parseFloat(opts[k])); //Parse as a float so we can throw an error if it's not an integer
}
}
else if (pageOpts[k].optionType === "float")
{
if (opts[k] === "any")
{
pageOpts[k].setValue("any");
}
else
{
pageOpts[k].setValue(parseFloat(opts[k]));
}
}
else if (pageOpts[k].optionType === "string")
{
pageOpts[k].setValue(opts[k].toString());
}
optionsSet++;
}
catch(e)
{
err++;
addError(e.message,pageOpts[k]);
}
}
else if (k === "calc")
{
if (opts["calc"] === "true")
{
calc = true;
}
optionsSet++;
}
else
{
err++;
addError("Invalid option " + k + ".");;
}
}
if (err)
{
processErrors("url");
}
else if (optionsSet > 0)
{
err = validateOptions("url");
if (!err)
{
setPageOptions();
processOptions();
if (calc)
{
// Start the loading spinner
document.getElementById("loadingoverlay").style.display = "flex";
// Set a timeout of 0 to push the calculation into the queue and let the spinner update on the page
setTimeout(function(){
calculate();
}, 1000);
}
}
return err;
}
else
{
return;
}
}