forked from KirkMcDonald/kirkmcdonald.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.js
453 lines (398 loc) · 11.8 KB
/
events.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
"use strict"
// build target events
// The "+" button to add a new target.
function plusHandler() {
addTarget()
itemUpdate()
}
// Triggered when mouse enters the dropdown box
function resetSearch(ev) {
ev.target.getElementsByClassName("search")[0].value = ""
// unhide all child nodes
var elems = ev.target.querySelectorAll("label, br, hr")
for (var i = 0; i < elems.length; i++) {
elems[i].style.display = ""
}
}
// Triggered when user is searching target
function searchTargets(ev) {
var search = ev.target
var search_text = search.value.toLowerCase().replace(/[^a-z0-9]+/g, "")
if (!search_text) {
resetSearch({target: search.parentNode})
return
}
// handle enter key press (select target if only one is visible)
if (ev.keyCode === 13) {
var visibleLabels = [];
var labels = search.parentNode.getElementsByTagName("label")
for (var i = 0; i < labels.length; i++) {
var label = labels[i]
if (label.style.display != "none") {
visibleLabels.push(label)
}
}
// don't do anything if more than one icon is visible
if (visibleLabels.length === 1) {
var item = visibleLabels[0].title
var selector = "input[value=" + item + "]"
var input = search.parentNode.querySelector(selector)
input.checked = true
input.dispatchEvent(new Event("change"))
}
return
}
var elems = search.parentNode.querySelectorAll("hr, br, label")
var currentHrHasContent, currentBrHasContent;
var lastHrWithContent;
// hide non-matching labels & icons
for (var i = 0; i < elems.length; i++) {
var elem = elems[i]
if (elem.tagName === "HR") {
if (currentHrHasContent) {
elem.style.display = ""
lastHrWithContent = elem
}
else {
elem.style.display = "none"
}
currentHrHasContent = false
currentBrHasContent = false
}
else if (elem.tagName === "BR") {
if (currentBrHasContent) {
elem.style.display = ""
}
else {
elem.style.display = "none"
}
currentBrHasContent = false
}
else {
var icon = elem.firstChild
var title = icon.title.replace("-", "")
// if title contains the search phrase
if (title.indexOf(search_text) != -1) {
elem.style.display = ""
currentHrHasContent = true
currentBrHasContent = true
}
else {
elem.style.display = "none"
}
}
}
// hide trailing horizonal rule
if (lastHrWithContent) {
lastHrWithContent.style.display = "none"
}
}
// Triggered when a build target's item is changed.
function ItemHandler(target) {
this.handleEvent = function(event) {
target.itemName = event.target.value
itemUpdate()
}
}
// The "x" button to remove a target.
function RemoveHandler(target) {
this.handleEvent = function(event) {
build_targets.splice(target.index, 1)
for (var i=target.index; i < build_targets.length; i++) {
build_targets[i].index--
}
target.element.remove()
itemUpdate()
}
}
// Triggered when a "Factories:" text box is changed.
function FactoryHandler(target) {
this.handleEvent = function(event) {
target.factoriesChanged()
itemUpdate()
}
}
// Triggered when a "Rate:" text box is changed.
function RateHandler(target) {
this.handleEvent = function(event) {
target.rateChanged()
itemUpdate()
}
}
// settings events
// Obtains current data set from UI element, and resets the world with the new
// data.
function changeMod() {
var modName = currentMod()
reset()
loadData(modName)
}
// Triggered when the display rate is changed.
function displayRateHandler(event) {
var value = event.target.value
displayRateFactor = displayRates[value]
rateName = value
display()
}
function changeRPrec(event) {
ratePrecision = Number(event.target.value)
display()
}
function changeFPrec(event) {
countPrecision = Number(event.target.value)
display()
}
// Triggered when the "minimum assembling machine" setting is changed.
function changeMin(event) {
setMinimumAssembler(event.target.value)
itemUpdate()
}
// Triggered when the furnace is changed.
function changeFurnace(event) {
spec.setFurnace(event.target.value)
itemUpdate()
}
// Triggered when the preferred belt is changed.
function changeBelt(event) {
setPreferredBelt(event.target.value)
display()
}
// Triggered when the minimum pipe length is changed.
function changePipeLength(event) {
setMinPipe(event.target.value)
display()
}
// Triggered when the mining productivity bonus is changed.
function changeMprod() {
spec.miningProd = getMprod()
itemUpdate()
}
// Triggered when the default module is changed.
function changeDefaultModule(event) {
var module
if (event.target.value === NO_MODULE) {
module = null
} else {
module = shortModules[event.target.value]
}
spec.setDefaultModule(module)
recipeTable.updateDisplayedModules()
itemUpdate()
}
// Triggered when the default beacon module is changed.
function changeDefaultBeacon(event) {
var module
if (event.target.value === NO_MODULE) {
module = null
} else {
module = shortModules[event.target.value]
}
spec.setDefaultBeacon(module, spec.defaultBeaconCount)
recipeTable.updateDisplayedModules()
itemUpdate()
}
// Triggered when the default beacon count is changed.
function changeDefaultBeaconCount(event) {
var count = RationalFromString(event.target.value)
spec.setDefaultBeacon(spec.defaultBeacon, count)
recipeTable.updateDisplayedModules()
itemUpdate()
}
// Triggered when the recipe sort order is changed.
function changeSortOrder(event) {
sortOrder = event.target.value
display()
}
// Triggered when the value format (decimal vs. rational) is changed.
function changeFormat(event) {
displayFormat = event.target.value
display()
}
// Triggered when the theme is changed.
function changeTheme(event) {
theme = getThemeByName(event.target.value)
renderTheme()
}
// recipe row events
function IgnoreHandler(row) {
this.handleEvent = function(event) {
if (spec.ignore[row.name]) {
delete spec.ignore[row.name]
row.setIgnore(false)
} else {
spec.ignore[row.name] = true
row.setIgnore(true)
}
itemUpdate()
}
}
// Triggered when a factory module is changed.
function ModuleHandler(row, index) {
this.handleEvent = function(event) {
var moduleName = event.target.value
var module = modules[moduleName]
if (spec.setModule(row.recipe, index, module) || isFactoryTarget(row.recipe.name)) {
itemUpdate()
} else {
display()
}
}
}
// Triggered when the right-arrow "copy module" button is pressed.
function ModuleCopyHandler(row) {
this.handleEvent = function(event) {
var moduleCount = spec.moduleCount(row.recipe)
var module = spec.getModule(row.recipe, 0)
var needRecalc = false
for (var i = 0; i < moduleCount; i++) {
needRecalc = spec.setModule(row.recipe, i, module) || needRecalc
row.setDisplayedModule(i, module)
}
if (needRecalc || isFactoryTarget(row.recipe.name)) {
itemUpdate()
} else {
display()
}
}
}
// Gets Factory object for a corresponding recipe name.
function getFactory(recipeName) {
var recipe = solver.recipes[recipeName]
return spec.getFactory(recipe)
}
// Triggered when a beacon module is changed.
function BeaconHandler(recipeName) {
this.handleEvent = function(event) {
var moduleName = event.target.value
var module = modules[moduleName]
var factory = getFactory(recipeName)
if (module === spec.defaultBeacon) {
module = null
}
factory.beaconModule = module
if (isFactoryTarget(recipeName) && !factory.beaconCount.isZero()) {
itemUpdate()
} else {
display()
}
}
}
// Triggered when a beacon module count is changed.
function BeaconCountHandler(recipeName) {
this.handleEvent = function(event) {
var moduleCount = RationalFromFloats(event.target.value, 1)
var factory = getFactory(recipeName)
factory.beaconCount = moduleCount
if (isFactoryTarget(recipeName) && (factory.beaconModule || spec.defaultModule)) {
itemUpdate()
} else {
display()
}
}
}
// Triggered when the up/down arrow "copy to all recipes" button is pressed.
function CopyAllHandler(name) {
this.handleEvent = function(event) {
var factory = spec.spec[name]
var needRecalc = false
for (var recipeName in spec.spec) {
if (recipeName == name) {
continue
}
var f = spec.spec[recipeName]
if (!f) {
continue
}
var recipe = solver.recipes[recipeName]
needRecalc = factory.copyModules(f, recipe) || needRecalc || isFactoryTarget(recipeName)
}
recipeTable.updateDisplayedModules()
if (needRecalc) {
itemUpdate()
} else {
display()
}
}
}
// items tab events
function PipeCountHandler(config) {
this.handleEvent = function(event) {
config.setPipes(event.target.value)
}
}
function PipeLengthHandler(config) {
this.handleEvent = function(event) {
config.setLength(event.target.value)
}
}
// graph hover events
function GraphMouseOverHandler(node) {
this.handleEvent = function(event) {
node.highlight()
}
}
function GraphMouseLeaveHandler(node) {
this.handleEvent = function(event) {
if (node !== clickedNode) {
node.unhighlight()
}
}
}
var clickedNode = null
function GraphClickHandler(node) {
this.handleEvent = function(event) {
if (node === clickedNode) {
node.unhighlight()
clickedNode = null
} else if (clickedNode) {
clickedNode.unhighlight()
clickedNode = node
} else {
clickedNode = node
}
}
}
// tab events
var DEFAULT_TAB = "totals_tab"
var currentTab = DEFAULT_TAB
var tabMap = {
"totals_tab": "totals_button",
"steps_tab": "steps_button",
"graph_tab": "graph_button",
"settings_tab": "settings_button",
"about_tab": "about_button",
"faq_tab": "faq_button",
"debug_tab": "debug_button",
}
// Triggered when a tab is clicked on.
function clickTab(tabName) {
currentTab = tabName
var tabs = document.getElementsByClassName("tab")
for (var i=0; i < tabs.length; i++) {
tabs[i].style.display = "none"
}
var buttons = document.getElementsByClassName("tab_button")
for (var i=0; i < buttons.length; i++) {
buttons[i].classList.remove("active")
}
document.getElementById(tabName).style.display = "block"
var button = document.getElementById(tabMap[tabName])
button.classList.add("active")
if (initDone) {
window.location.hash = "#" + formatSettings()
}
}
// Triggered when the "Visualize" tab is clicked on.
function clickVisualize(event, tabName) {
clickTab(event, tabName)
renderGraph(globalTotals, spec.ignore)
}
// utility events
function toggleVisible(targetID) {
var target = document.getElementById(targetID)
if (target.style.display == "none") {
target.style.display = "block"
} else {
target.style.display = "none"
}
}