-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.js
More file actions
436 lines (395 loc) · 16.6 KB
/
Copy pathshell.js
File metadata and controls
436 lines (395 loc) · 16.6 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
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
/* ==========================================================================
retro-desktop-theme / shell.js
--------------------------------------------------------------------------
The whole shell in one file. Vanilla JS, zero dependencies, and the page
must remain fully readable without it: everything here is enhancement.
The contract with shell.css is the `rd-live` class on <html>; every
behavior and every piece of ARIA this script adds is added here, at
enhance time, so the no-JS document never claims semantics it cannot
honor.
What the script does, in order:
1. flips the document into live mode
2. registers every [data-rd-window] and gives it dialog semantics
3. injects the minimize and close controls (a control that does
nothing must not exist, so they cannot be static markup)
4. turns icon and in-content anchors into window openers
5. manages the pile: open, close, minimize, raise on click
6. fills the taskbar's open-window list (and only that list; the
shortcut bar beside it is never touched)
7. injects the launcher button and runs the programs menu
8. binds Escape to close the topmost openable
9. injects the wallpaper button and runs the tray clock
The window mechanics here are the pilot's, deliberately: the era changes
the skin and the chrome drawing, not the behavior. Focus is managed at the
three moments it can be lost: opening a window moves focus into it,
closing one returns focus to the element that opened it, minimizing one
moves focus to the window's taskbar entry. There is no focus trap
anywhere: Tab always walks the whole document, and closed windows leave
the tab order via display:none.
========================================================================== */
(function () {
"use strict";
var doc = document;
var root = doc.documentElement;
/* 1. Live mode. Everything shell.css does differently, it does under
this class. If the script fails to run, this line never happens and
the page stays a page. */
root.classList.add("rd-live");
/* ------------------------------------------------------------------
Window registry.
openOrder holds open (including minimized) windows, bottom to top.
------------------------------------------------------------------ */
var windows = [];
var byId = {};
var openOrder = [];
var openCount = 0; // total opens ever, drives the cascade offsets
var Z_BASE = 10; // matches --rd-z-window; stays below --rd-z-chrome (30)
/* The control glyphs. Square, chunky, drawn on a 10-unit grid because a
title-bar glyph in this era was a handful of pixels and a hairline
stroke would be the wrong century. Nothing here is traced from any
product's iconography. */
var ICON_MIN =
'<svg viewBox="0 0 10 10" aria-hidden="true" fill="currentColor">' +
'<rect x="1" y="7" width="7" height="2"/></svg>';
var ICON_CLOSE =
'<svg viewBox="0 0 10 10" aria-hidden="true" fill="none" ' +
'stroke="currentColor" stroke-width="1.8">' +
'<path d="M1.5 1.5l7 7M8.5 1.5l-7 7"/></svg>';
/* The launcher glyph: a two-drawer filing cabinet, which is what the menu
opens onto here. Original artwork; no flag, no logo, no lozenge. */
var ICON_LAUNCH =
'<svg viewBox="0 0 16 16" aria-hidden="true" fill="none" ' +
'stroke="currentColor" stroke-width="1.4" stroke-linecap="square">' +
'<rect x="2" y="2" width="12" height="12"/>' +
'<path d="M2 8h12"/><path d="M6 5h4M6 11h4"/></svg>';
/* The wallpaper glyph: slats. */
var ICON_WALL =
'<svg viewBox="0 0 16 16" aria-hidden="true" fill="none" ' +
'stroke="currentColor" stroke-width="1.4" stroke-linecap="square">' +
'<rect x="2" y="2" width="12" height="12"/>' +
'<path d="M2 6h12M2 10h12"/></svg>';
/* ------------------------------------------------------------------
2 + 3. Register windows, add dialog semantics, inject controls.
Non-modal dialogs: role="dialog" names the pattern, aria-modal is
deliberately absent because nothing is modal and nothing traps.
------------------------------------------------------------------ */
var winEls = doc.querySelectorAll("[data-rd-window]");
Array.prototype.forEach.call(winEls, function (el) {
var title = el.querySelector(".rd-title");
if (title && !title.id) title.id = el.id + "-title";
el.setAttribute("role", "dialog");
if (title) el.setAttribute("aria-labelledby", title.id);
el.tabIndex = -1;
var body = el.querySelector(".rd-window-body");
if (body) {
/* The scroll region has to be keyboard-scrollable, which means
focusable. It borrows the window's name so a screen reader says
what it is scrolling. */
body.tabIndex = 0;
if (title) body.setAttribute("aria-labelledby", title.id);
body.setAttribute("role", "region");
}
var slot = el.querySelector("[data-rd-controls]");
var minBtn = null;
var closeBtn = null;
if (slot) {
var name = taskName(el);
minBtn = makeControl("Minimize " + name, ICON_MIN);
closeBtn = makeControl("Close " + name, ICON_CLOSE);
slot.appendChild(minBtn);
slot.appendChild(closeBtn);
}
var win = {
el: el,
id: el.id,
minimized: false,
opener: null, // where focus returns on close
taskBtn: null,
};
windows.push(win);
byId[el.id] = win;
if (minBtn)
minBtn.addEventListener("click", function () {
minimize(win);
});
if (closeBtn)
closeBtn.addEventListener("click", function () {
close(win);
});
/* Clicking anywhere in a window raises it: z-order on click. */
el.addEventListener("pointerdown", function () {
if (top() !== win) raise(win);
});
});
/* Window titles read "Read me first - Tambour", the era's document-dash-
application convention. The taskbar and the control labels want the
document half only, so the markup carries it explicitly rather than the
script splitting a string it does not own. */
function taskName(el) {
var attr = el.getAttribute("data-rd-task");
if (attr) return attr;
var t = el.querySelector(".rd-title");
return t ? t.textContent.trim() : el.id;
}
function makeControl(label, svg) {
var b = doc.createElement("button");
b.type = "button";
b.className = "rd-control";
b.setAttribute("aria-label", label);
b.innerHTML = svg;
return b;
}
/* ------------------------------------------------------------------
4. Anchors become openers. Any link to #<window-id>, wherever it is
(desktop grid, programs menu, window content), opens that window. The
same listener closes the menu behind a successful open.
------------------------------------------------------------------ */
doc.addEventListener("click", function (e) {
var a = e.target.closest ? e.target.closest('a[href^="#"]') : null;
if (!a) return;
var win = byId[a.getAttribute("href").slice(1)];
if (!win) return;
e.preventDefault();
setMenu(false, false);
open(win, a);
});
/* ------------------------------------------------------------------
5. The pile.
------------------------------------------------------------------ */
function top() {
return openOrder.length ? openOrder[openOrder.length - 1] : null;
}
/* The topmost window a user can actually see. A minimized window can sit
high in the order, and Escape and focus-return must both look past it. */
function topVisible() {
for (var i = openOrder.length - 1; i >= 0; i--) {
if (!openOrder[i].minimized) return openOrder[i];
}
return null;
}
function place(win) {
/* The cascade: each opening lands a step down-right from the last,
cycling so the pile never walks off screen. */
var step = openCount % 5;
win.el.style.setProperty("--rd-dx", step * cascade() + "px");
win.el.style.setProperty("--rd-dy", step * cascade() + "px");
openCount += 1;
}
function open(win, opener) {
if (opener) win.opener = opener;
if (openOrder.indexOf(win) === -1) {
openOrder.push(win);
place(win);
}
win.minimized = false;
win.el.classList.add("rd-open");
raise(win);
win.el.focus({ preventScroll: true });
}
function close(win) {
var i = openOrder.indexOf(win);
if (i !== -1) openOrder.splice(i, 1);
win.minimized = false;
win.el.classList.remove("rd-open", "rd-top");
restack();
/* Never a dead end: focus returns to whatever opened the window, and
the desktop with its icons is what remains. */
if (win.opener && isVisible(win.opener)) {
win.opener.focus();
} else {
var t = topVisible();
if (t) t.el.focus({ preventScroll: true });
}
}
function minimize(win) {
win.minimized = true;
win.el.classList.remove("rd-open", "rd-top");
restack();
/* The window went to the taskbar, so focus honestly follows it. */
if (win.taskBtn) win.taskBtn.focus();
}
function raise(win) {
var i = openOrder.indexOf(win);
if (i !== -1) {
openOrder.splice(i, 1);
openOrder.push(win);
}
restack();
}
/* Reassign z-indexes bottom-to-top on every change, so the range stays
bounded and never creeps toward the chrome layer. */
function restack() {
var tv = topVisible();
openOrder.forEach(function (w, i) {
w.el.style.zIndex = String(Z_BASE + i);
w.el.classList.toggle("rd-top", w === tv);
});
syncTaskbar();
}
function isVisible(el) {
return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
}
function cascade() {
var v = getComputedStyle(root).getPropertyValue("--rd-cascade");
return parseInt(v, 10) || 24;
}
/* ------------------------------------------------------------------
6. Taskbar. This fills the open-window list and only that list. The
shortcut bar next to it is a fixed text index of every window and no
selector in this file reaches it, which is deliberate: it is the
navigation guarantee that has to read the same with the script and
without it (class decision 44). Rebuilding the taskbar used to
destroy it.
Click behavior follows the desktop convention this era set: raise it
if it is behind, restore it if it is minimized, minimize it if it is
already on top.
------------------------------------------------------------------ */
var tasklist = doc.querySelector("[data-rd-tasklist]");
function syncTaskbar() {
if (!tasklist) return;
tasklist.textContent = "";
openOrder.forEach(function (win) {
var li = doc.createElement("li");
var b = doc.createElement("button");
b.type = "button";
b.className = "rd-task" + (win.minimized ? " rd-task-min" : "");
b.textContent = taskName(win.el);
var isTop = topVisible() === win;
b.setAttribute("aria-pressed", isTop ? "true" : "false");
b.addEventListener("click", function () {
if (win.minimized) {
open(win);
} else if (topVisible() === win) {
minimize(win);
} else {
raise(win);
win.el.focus({ preventScroll: true });
}
});
li.appendChild(b);
tasklist.appendChild(li);
win.taskBtn = b;
});
}
/* ------------------------------------------------------------------
7. The launcher and the programs menu. The button is injected into the
taskbar's slot because it only means something live, and it exists at
every width: in this era the menu button was permanent chrome, not a
narrow-screen affordance. It controls the icon nav, which shell.css
re-hangs as a menu standing on the taskbar.
The label is "Programs", which is what the menu contains. It is
deliberately not the word the era's best-known desktop used.
------------------------------------------------------------------ */
var icons = doc.querySelector(".rd-icons");
var launchSlot = doc.querySelector("[data-rd-launcher]");
var launcher = null;
if (icons && launchSlot) {
if (!icons.id) icons.id = "rd-programs";
launcher = doc.createElement("button");
launcher.type = "button";
launcher.className = "rd-launcher";
launcher.setAttribute("aria-expanded", "false");
launcher.setAttribute("aria-controls", icons.id);
launcher.innerHTML = ICON_LAUNCH + "<span>Programs</span>";
launcher.addEventListener("click", function () {
setMenu(launcher.getAttribute("aria-expanded") !== "true", true);
});
launchSlot.appendChild(launcher);
}
function setMenu(openIt, returnFocus) {
if (!launcher) return;
var was = launcher.getAttribute("aria-expanded") === "true";
launcher.setAttribute("aria-expanded", openIt ? "true" : "false");
icons.classList.toggle("rd-open", openIt);
if (!openIt && was && returnFocus) launcher.focus();
}
/* A menu in this era closed when you clicked away from it. Focus is left
where the click put it; pulling it back to the launcher would fight the
user for it. */
doc.addEventListener("pointerdown", function (e) {
if (!launcher || launcher.getAttribute("aria-expanded") !== "true") return;
if (icons.contains(e.target) || launcher.contains(e.target)) return;
setMenu(false, false);
});
/* ------------------------------------------------------------------
8. Escape closes the topmost openable: the programs menu if it is
open, otherwise the top visible window. One key, one rule, no
exceptions. The "visible" part is load-bearing: a minimized window can
sit at the top of the order, and without topVisible() the key becomes
a no-op that looks like a bug.
------------------------------------------------------------------ */
doc.addEventListener("keydown", function (e) {
if (e.key !== "Escape") return;
if (launcher && launcher.getAttribute("aria-expanded") === "true") {
setMenu(false, true);
return;
}
var t = topVisible();
if (t) close(t);
});
/* ------------------------------------------------------------------
9a. Wallpaper. The ground is a wallpaper slot; this script's whole
involvement is one attribute on the body and one tray button. All the
styling lives in shell.css under body[data-wallpaper]. The enhanced
desktop defaults to the flat field, which is what the era's desktop
was, and it is also what the no-JS document gets from plain CSS.
Per-session only, no storage: a demo desktop should greet everyone the
same way.
------------------------------------------------------------------ */
var WALLPAPERS = ["quiet", "slats"];
doc.body.setAttribute("data-wallpaper", WALLPAPERS[0]);
var tray = doc.querySelector("[data-rd-tray]");
if (tray) {
var wallBtn = doc.createElement("button");
wallBtn.type = "button";
wallBtn.className = "rd-tray-btn";
wallBtn.innerHTML = ICON_WALL;
var labelWall = function () {
wallBtn.setAttribute(
"aria-label",
"Switch wallpaper, now " + doc.body.getAttribute("data-wallpaper")
);
};
labelWall();
wallBtn.addEventListener("click", function () {
var cur = doc.body.getAttribute("data-wallpaper");
var next = WALLPAPERS[(WALLPAPERS.indexOf(cur) + 1) % WALLPAPERS.length];
doc.body.setAttribute("data-wallpaper", next);
labelWall();
});
tray.insertBefore(wallBtn, tray.firstChild);
}
/* ------------------------------------------------------------------
9b. The clock. A desktop without a clock is a screenshot.
------------------------------------------------------------------ */
var clock = doc.querySelector("[data-rd-clock]");
if (clock) {
var tick = function () {
var d = new Date();
var h = String(d.getHours());
var m = String(d.getMinutes());
clock.textContent =
(h.length < 2 ? "0" + h : h) + ":" + (m.length < 2 ? "0" + m : m);
};
tick();
setInterval(tick, 30000);
}
/* ------------------------------------------------------------------
Boot: open whatever the page marked data-rd-open, in document order,
so the desktop greets rather than sitting empty. The last one marked
ends up on top. Focus is left alone at boot; stealing focus on page
load is rude to screen readers and to everyone else.
------------------------------------------------------------------ */
windows.forEach(function (win) {
if (win.el.hasAttribute("data-rd-open")) {
var icon = doc.querySelector('.rd-icons a[href="#' + win.id + '"]');
if (icon) win.opener = icon;
if (openOrder.indexOf(win) === -1) {
openOrder.push(win);
place(win);
}
win.el.classList.add("rd-open");
}
});
restack();
})();