Debian Codegraph @

MiniZincIDE/server/index.html

raw
1 <!DOCTYPE html>
2 <html>
3
4 <head>
5 <meta charset="utf-8">
6 <title>%3 | MiniZincIDE</title>
7 <style>
8 html,
9 body {
10 margin: 0;
11 padding: 0;
12 font-family: sans-serif;
13 font-size: 14px;
14 }
15
16 * {
17 box-sizing: border-box;
18 }
19
20 #container {
21 display: flex;
22 flex-direction: column;
23 align-items: stretch;
24 width: 100vw;
25 height: 100vh;
26 }
27
28 #top {
29 display: none;
30 }
31
32 #top.has-solutions {
33 display: flex;
34 background: #F4F4F4;
35 padding: 5px 10px;
36 align-items: center;
37 gap: 5px;
38 }
39
40 #solutionSlider {
41 flex: 1 1 auto;
42 }
43
44 #wrapper {
45 flex: 1 0 auto;
46 }
47
48 #windows {
49 width: 100%;
50 height: 100%;
51 display: grid;
52 grid-auto-rows: auto;
53 gap: 1px;
54 background: #CCC;
55 }
56
57 #windows>* {
58 background: #FFF;
59 border: 0;
60 width: 100%;
61 height: 100%;
62 }
63
64 #notice {
65 width: 100vw;
66 position: absolute;
67 top: 20vw;
68 left: 0;
69 text-align: center;
70 }
71
72 #notice:empty {
73 display: none;
74 }
75
76 .hidden {
77 display: none !important;
78 }
79 </style>
80 </head>
81
82 <body>
83 <div id="container">
84 <div id="top">
85 <label for="solutionNumber">Solution:</label>
86 <input type="number" id="solutionNumber" min="0">
87 <input type="range" id="solutionSlider" min="0">
88 <input type="checkbox" id="followLatest">
89 <label for="followLatest">Follow latest solution</label>
90 </div>
91 <div id="wrapper">
92 <div id="windows"></div>
93 </div>
94 </div>
95 <div id="notice">Connecting...</div>
96 <script>
97 const layoutGrid = (container) => {
98 const numChildren = container.children.length;
99 const cols = Math.ceil(Math.sqrt(numChildren));
100 container.style = `grid-template-columns: repeat(${cols}, auto);`;
101 };
102 const windows = document.getElementById('windows');
103 const buffers = {};
104 const ws = new WebSocket('%1/%2');
105 const notice = document.getElementById('notice');
106
107 let numSolutions = 0;
108 let currentSolution = 0;
109
110 const topBar = document.getElementById('top');
111 const solNumberInput = document.getElementById('solutionNumber');
112 const solSliderInput = document.getElementById('solutionSlider');
113 const followLatestInput = document.getElementById('followLatest');
114
115 const updateControls = () => {
116 if (numSolutions > 0) {
117 topBar.classList.add('has-solutions');
118 }
119 if (followLatestInput.checked) {
120 currentSolution = numSolutions;
121 }
122 solNumberInput.min = numSolutions > 0 ? 1 : 0;
123 solNumberInput.max = numSolutions;
124 solNumberInput.value = currentSolution;
125 solSliderInput.min = numSolutions > 0 ? 1 : 0;
126 solSliderInput.max = numSolutions;
127 solSliderInput.value = currentSolution;
128 };
129
130 solNumberInput.addEventListener('change', () => {
131 if (solNumberInput.value !== currentSolution) {
132 currentSolution = solNumberInput.value;
133 followLatestInput.checked = false;
134 updateControls();
135 for (const buffer of Object.values(buffers)) {
136 buffer.queue.push({
137 event: 'goToSolution',
138 payload: currentSolution - 1
139 });
140 }
141 }
142 });
143 solSliderInput.addEventListener('change', () => {
144 if (solSliderInput.value !== currentSolution) {
145 currentSolution = solSliderInput.value;
146 followLatestInput.checked = false;
147 updateControls();
148 for (const buffer of Object.values(buffers)) {
149 buffer.queue.push({
150 event: 'goToSolution',
151 payload: currentSolution - 1
152 });
153 }
154 }
155 });
156 followLatestInput.addEventListener('change', () => {
157 currentSolution = numSolutions;
158 updateControls();
159 for (const buffer of Object.values(buffers)) {
160 buffer.queue.push({
161 event: 'goToSolution',
162 payload: followLatestInput.checked ? -1 : numSolutions - 1
163 });
164 }
165 });
166
167 const addWindow = (key, url, userData) => {
168 const frame = document.createElement("iframe");
169 const buffer = {
170 ready: false,
171 frame,
172 queue: [{ event: 'init', payload: userData }]
173 };
174 frame.addEventListener('load', () => {
175 buffer.ready = true;
176 });
177 frame.src = `%2/${url}`;
178 windows.appendChild(frame);
179 buffers[key] = buffer;
180 layoutGrid(windows);
181 }
182
183 ws.onopen = () => {
184 notice.textContent = '';
185 };
186 ws.onerror = e => {
187 notice.textContent = `A WebSocket error occurred (code ${e.code}).`;
188 console.log('WebSocket error: ', e);
189 };
190 ws.onclose = () => {
191 notice.textContent = 'Disconnected. Refresh the page to try again.';
192 };
193 ws.onmessage = e => {
194 const message = JSON.parse(e.data);
195 switch (message.event) {
196 case 'navigate':
197 window.location = message.url;
198 break;
199 case 'init':
200 for (const k in message.windows) {
201 addWindow(k, message.windows[k].url, message.windows[k].userData);
202 }
203 numSolutions = message.numSolutions;
204 followLatestInput.checked = true;
205 updateControls();
206 break;
207 case 'window':
208 addWindow(message.windowId, message.url, message.userData);
209 break;
210 case 'response':
211 // Pass response to child
212 buffers[message.window].queue.push({
213 event: 'response',
214 id: message.id,
215 payload: message.payload
216 });
217 break;
218 case 'error':
219 // Pass error to child
220 buffers[message.window].queue.push({
221 event: 'error',
222 id: message.id,
223 message: message.message
224 });
225 break;
226 case 'solution':
227 // Pass solution to children
228 for (const key in message.solution) {
229 buffers[key].queue.push({
230 event: 'solution',
231 payload: {
232 time: message.time,
233 data: message.solution[key]
234 }
235 });
236 }
237 numSolutions++;
238 updateControls();
239 break;
240 case 'status':
241 case 'finish':
242 // Pass on to all children
243 for (const buffer of Object.values(buffers)) {
244 buffer.queue.push(message);
245 }
246 break;
247 }
248 };
249 window.addEventListener('message', e => {
250 const message = e.data;
251 switch (message.event) {
252 case 'rebroadcast':
253 // Rebroadcast to all children
254 for (const buffer of Object.values(buffers)) {
255 buffer.queue.push(message.message);
256 }
257 if (message.message.event === 'goToSolution') {
258 followLatestInput.checked = message.message.payload === -1;
259 currentSolution = message.message.payload;
260 updateControls();
261 }
262 break;
263 default:
264 // Pass on to IDE
265 let windowId = null;
266 for (const key in buffers) {
267 if (buffers[key].frame.contentWindow === e.source) {
268 windowId = key;
269 break;
270 }
271 }
272 if (windowId === null) {
273 console.error('Could not determine source window for message');
274 e.source.postMessage({
275 event: 'error',
276 id: message.id,
277 message: 'Could not determine source window'
278 });
279 return;
280 }
281 ws.send(JSON.stringify({
282 ...message,
283 window: windowId
284 }));
285 break;
286 }
287 });
288 window.setInterval(() => {
289 for (const buffer of Object.values(buffers)) {
290 if (buffer.ready) {
291 for (const message of buffer.queue) {
292 buffer.frame.contentWindow.postMessage(message, '*');
293 }
294 buffer.queue = [];
295 }
296 }
297 }, 1);
298 </script>
299 </body>
300
301 </html>