-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.js
More file actions
403 lines (366 loc) · 15.1 KB
/
Copy pathshell.js
File metadata and controls
403 lines (366 loc) · 15.1 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
/* ==========================================================================
desktop-os-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 `os-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-os-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 strip beside it is never touched)
7. injects the mobile launcher button
8. binds Escape to close the topmost openable
9. runs the taskbar clock
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("os-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 --os-z-window; stays below --os-z-chrome (30)
var ICON_MIN =
'<svg viewBox="0 0 14 14" aria-hidden="true" fill="none" ' +
'stroke="currentColor" stroke-width="2" stroke-linecap="round">' +
'<path d="M2 11h10"/></svg>';
var ICON_CLOSE =
'<svg viewBox="0 0 14 14" aria-hidden="true" fill="none" ' +
'stroke="currentColor" stroke-width="2" stroke-linecap="round">' +
'<path d="M3 3l8 8M11 3l-8 8"/></svg>';
var ICON_GRID =
'<svg viewBox="0 0 20 20" aria-hidden="true" fill="none" ' +
'stroke="currentColor" stroke-width="2" stroke-linejoin="round">' +
'<rect x="2.5" y="2.5" width="6" height="6" rx="1"/>' +
'<rect x="11.5" y="2.5" width="6" height="6" rx="1"/>' +
'<rect x="2.5" y="11.5" width="6" height="6" rx="1"/>' +
'<rect x="11.5" y="11.5" width="6" height="6" rx="1"/></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-os-window]");
Array.prototype.forEach.call(winEls, function (el) {
var title = el.querySelector(".os-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(".os-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-os-controls]");
var minBtn = null;
var closeBtn = null;
if (slot) {
var name = title ? title.textContent.trim() : el.id;
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);
});
});
function makeControl(label, svg) {
var b = doc.createElement("button");
b.type = "button";
b.className = "os-control";
b.setAttribute("aria-label", label);
b.innerHTML = svg;
return b;
}
/* ------------------------------------------------------------------
4. Anchors become openers. Any link to #<window-id>, wherever it is
(icon rail, launcher panel, window content), opens that window. The
same listener closes the launcher panel 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();
setLauncher(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 open(win, opener) {
if (opener) win.opener = opener;
if (openOrder.indexOf(win) === -1) {
openOrder.push(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("--os-dx", step * cascade() + "px");
win.el.style.setProperty("--os-dy", step * cascade() + "px");
openCount += 1;
}
win.minimized = false;
win.el.classList.add("os-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("os-open", "os-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("os-open", "os-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("os-top", w === tv);
});
syncTaskbar();
}
function isVisible(el) {
return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
}
function cascade() {
var v = getComputedStyle(root).getPropertyValue("--os-cascade");
return parseInt(v, 10) || 28;
}
/* ------------------------------------------------------------------
6. Taskbar. This fills the open-window list and only that list. The
shortcut strip 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: 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-os-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 = "os-task" + (win.minimized ? " os-task-min" : "");
var title = win.el.querySelector(".os-title");
b.textContent = title ? title.textContent : win.id;
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. Launcher. Injected because it only means something live. It
controls the icon nav, which shell.css re-hangs as a panel on narrow
screens.
------------------------------------------------------------------ */
var icons = doc.querySelector(".os-icons");
var launcher = null;
if (icons) {
if (!icons.id) icons.id = "os-icons";
launcher = doc.createElement("button");
launcher.type = "button";
launcher.className = "os-launcher";
launcher.setAttribute("aria-label", "Programs");
launcher.setAttribute("aria-expanded", "false");
launcher.setAttribute("aria-controls", icons.id);
launcher.innerHTML = ICON_GRID;
launcher.addEventListener("click", function () {
setLauncher(launcher.getAttribute("aria-expanded") !== "true");
});
doc.body.appendChild(launcher);
}
function setLauncher(openIt) {
if (!launcher) return;
var was = launcher.getAttribute("aria-expanded") === "true";
launcher.setAttribute("aria-expanded", openIt ? "true" : "false");
icons.classList.toggle("os-open", openIt);
if (!openIt && was) launcher.focus();
}
/* ------------------------------------------------------------------
8. Escape closes the topmost openable: the launcher panel if it is
open, otherwise the top window. One key, one rule, no exceptions.
------------------------------------------------------------------ */
doc.addEventListener("keydown", function (e) {
if (e.key !== "Escape") return;
if (launcher && launcher.getAttribute("aria-expanded") === "true") {
setLauncher(false);
return;
}
var t = topVisible();
if (t) close(t);
});
/* ------------------------------------------------------------------
8b. 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 scene; the no-JS document gets the
quiet field from plain CSS because this line never runs. Per-session
only, no storage: a demo desktop should greet everyone the same way.
------------------------------------------------------------------ */
var WALLPAPERS = ["scene", "quiet"];
var ICON_WALL =
'<svg viewBox="0 0 20 20" aria-hidden="true" fill="none" ' +
'stroke="currentColor" stroke-width="1.8" stroke-linejoin="round" ' +
'stroke-linecap="round">' +
'<rect x="2.5" y="3.5" width="15" height="13" rx="2"/>' +
'<circle cx="7.2" cy="8.2" r="1.4"/>' +
'<path d="M4.5 14.5l4-4.5 3 3 2.5-2.5 3.5 4"/></svg>';
doc.body.setAttribute("data-wallpaper", WALLPAPERS[0]);
var clockEl = doc.querySelector("[data-os-clock]");
if (clockEl && clockEl.parentNode) {
var wallBtn = doc.createElement("button");
wallBtn.type = "button";
wallBtn.className = "os-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();
});
clockEl.parentNode.insertBefore(wallBtn, clockEl);
}
/* ------------------------------------------------------------------
9. The clock. A desktop without a clock is a screenshot.
------------------------------------------------------------------ */
var clock = doc.querySelector("[data-os-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-os-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-os-open")) {
var icon = doc.querySelector('.os-icons a[href="#' + win.id + '"]');
if (icon) win.opener = icon;
if (openOrder.indexOf(win) === -1) {
openOrder.push(win);
var step = openCount % 5;
win.el.style.setProperty("--os-dx", step * cascade() + "px");
win.el.style.setProperty("--os-dy", step * cascade() + "px");
openCount += 1;
}
win.el.classList.add("os-open");
}
});
restack();
})();