-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_lecture.js
More file actions
136 lines (116 loc) · 3 KB
/
Copy pathheap_lecture.js
File metadata and controls
136 lines (116 loc) · 3 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
DEFAULT_POSITIONS,
DELETE_SIMULATION,
INSERT_POSITIONS,
INSERT_SIMULATION,
PROBLEM_SIMULATION,
} from './heap_lecture_data.js';
import {
renderMap,
renderProblemStep,
renderTreeStep,
setupNavScrollSpy,
} from './heap_lecture_render.js';
function createStepController(config) {
let stepIndex = 0;
function render() {
config.render(stepIndex);
}
function next() {
if (stepIndex < config.lastStep) {
stepIndex += 1;
render();
}
}
function prev() {
if (stepIndex > 0) {
stepIndex -= 1;
render();
}
}
function reset() {
stepIndex = 0;
render();
}
document.getElementById(config.prevId).addEventListener('click', prev);
document.getElementById(config.nextId).addEventListener('click', next);
document.getElementById(config.resetId).addEventListener('click', reset);
render();
return { render };
}
function setupInsertSimulation() {
return createStepController({
prevId: 'ins-prev',
nextId: 'ins-next',
resetId: 'ins-reset',
lastStep: INSERT_SIMULATION.steps.length - 1,
render: (stepIndex) => renderTreeStep({
svgId: 'ins-svg',
logId: 'ins-log',
prevId: 'ins-prev',
nextId: 'ins-next',
badgeId: 'ins-badge',
stepContainerId: 'ins-steps',
positions: INSERT_POSITIONS,
simulation: INSERT_SIMULATION,
stepIndex,
}),
});
}
function setupDeleteSimulation() {
return createStepController({
prevId: 'del-prev',
nextId: 'del-next',
resetId: 'del-reset',
lastStep: DELETE_SIMULATION.steps.length - 1,
render: (stepIndex) => renderTreeStep({
svgId: 'del-svg',
logId: 'del-log',
prevId: 'del-prev',
nextId: 'del-next',
badgeId: 'del-badge',
stepContainerId: 'del-steps',
positions: DEFAULT_POSITIONS,
simulation: DELETE_SIMULATION,
stepIndex,
}),
});
}
function setupProblemSimulation() {
return createStepController({
prevId: 'prob-prev',
nextId: 'prob-next',
resetId: 'prob-reset',
lastStep: PROBLEM_SIMULATION.length - 1,
render: (stepIndex) => renderProblemStep(PROBLEM_SIMULATION, stepIndex),
});
}
function setupMapSimulation() {
let selectedIndex = -1;
const mapSvg = document.getElementById('map-svg');
function render() {
renderMap(selectedIndex);
}
mapSvg.addEventListener('click', (event) => {
const target = event.target.closest('[data-map-index]');
if (!target) {
return;
}
const nextIndex = Number(target.dataset.mapIndex);
selectedIndex = selectedIndex === nextIndex ? -1 : nextIndex;
render();
});
render();
return { render };
}
const insertSimulation = setupInsertSimulation();
const deleteSimulation = setupDeleteSimulation();
const mapSimulation = setupMapSimulation();
const problemSimulation = setupProblemSimulation();
document.addEventListener('themechange', () => {
insertSimulation.render();
deleteSimulation.render();
mapSimulation.render();
problemSimulation.render();
});
setupNavScrollSpy();