-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
299 lines (259 loc) · 8.76 KB
/
app.js
File metadata and controls
299 lines (259 loc) · 8.76 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
// Application Configuration Module for HEYMING-OS
// Application list is loaded from apps-registry.json (sync XHR; use http-server for local dev).
function loadAppRegistrySync() {
try {
const url =
typeof document !== 'undefined' && document.currentScript && document.currentScript.src
? new URL('apps-registry.json', document.currentScript.src).href
: '/apps-registry.json';
const xhr = new XMLHttpRequest();
xhr.open('GET', url, false);
xhr.send(null);
const ok = xhr.status === 200 || xhr.status === 304 || xhr.status === 0;
if (!ok) {
console.error('[AppModule] Failed to load apps-registry.json', xhr.status, url);
return [];
}
const data = JSON.parse(xhr.responseText);
return Array.isArray(data) ? data : [];
} catch (e) {
console.error('[AppModule] apps-registry.json load/parse error', e);
return [];
}
}
let appRegistry = loadAppRegistrySync();
// sort appRegistry by name
appRegistry = appRegistry.sort((a, b) => a.name.localeCompare(b.name));
// App categories for organization
const appCategories = {
game: {
name: 'Games',
icon: '🎮',
description: 'Interactive entertainment applications'
},
utility: {
name: 'Utilities',
icon: '🛠️',
description: 'Useful tools and utilities'
},
entertainment: {
name: 'Entertainment',
icon: '🎪',
description: 'Fun and entertaining applications'
}
};
// Shared App Filter utility
const AppFilter = {
/**
* Create a filterable app list manager
* @param {Object} config - Configuration object
* @param {HTMLElement} config.container - Container element for app items
* @param {HTMLElement} config.filterInput - Filter input element
* @param {HTMLElement} config.noResultsEl - "No results" message element (optional)
* @param {HTMLElement} config.clearButton - Clear filter button (optional)
* @param {Function} config.getSearchText - Function to extract search text from an element
* @param {Function} config.onFilter - Callback after filtering (optional)
* @returns {Object} - Filter controller with methods
*/
create(config) {
const { container, filterInput, noResultsEl, clearButton, getSearchText, onFilter } = config;
const controller = {
filter(searchTerm) {
const term = (searchTerm || '').toLowerCase().trim();
const items = container.querySelectorAll('[data-filterable="true"]');
let visibleCount = 0;
items.forEach((item) => {
const searchText = getSearchText ? getSearchText(item) : item.textContent.toLowerCase();
const matches = !term || searchText.includes(term);
if (matches) {
item.style.display = '';
visibleCount++;
} else {
item.style.display = 'none';
}
});
// Handle no results message
if (noResultsEl) {
noResultsEl.classList.toggle('hidden', visibleCount > 0 || !term);
}
// Handle clear button visibility
if (clearButton) {
clearButton.classList.toggle('hidden', !term);
}
// Call optional callback
if (onFilter) {
onFilter({ visibleCount, searchTerm: term });
}
return visibleCount;
},
clear() {
if (filterInput) {
filterInput.value = '';
}
this.filter('');
if (filterInput) {
filterInput.focus();
}
},
reset() {
if (filterInput) {
filterInput.value = '';
}
this.filter('');
},
getFirstVisible() {
return container.querySelector('[data-filterable="true"]:not([style*="display: none"])');
},
// Bind standard keyboard shortcuts
bindKeyboardShortcuts(options = {}) {
const { onEscape, onEnter } = options;
if (filterInput) {
filterInput.addEventListener('keydown', (e) => {
// Ignore bare Meta key press (used for OS-level shortcuts like opening start menu)
// But allow Meta+key combos like Cmd+A (select all), Cmd+C (copy), etc.
if (e.key === 'Meta') {
e.stopPropagation();
return;
}
if (e.key === 'Escape') {
if (filterInput.value) {
e.stopPropagation();
this.clear();
} else if (onEscape) {
onEscape();
}
} else if (e.key === 'Enter') {
const first = this.getFirstVisible();
if (first) {
if (onEnter) {
onEnter(first);
} else {
first.click();
}
}
} else if (e.key === 'ArrowDown') {
e.preventDefault();
const first = this.getFirstVisible();
if (first) {
first.focus();
}
}
});
// Bind input event
filterInput.addEventListener('input', (e) => {
this.filter(e.target.value);
});
}
// Bind clear button
if (clearButton) {
clearButton.addEventListener('click', (e) => {
e.stopPropagation();
this.clear();
});
}
}
};
return controller;
}
};
// Expose globally
window.AppFilter = AppFilter;
// App module namespace
const AppModule = {
/** Resolves when the registry and AppModule are safe to read (sync load today). */
ready: Promise.resolve(),
// Get all apps
getAllApps: () => appRegistry,
// Get app by ID
getApp: (appId) => appRegistry.find((app) => app.id === appId),
// Get apps by category
getAppsByCategory: (category) => {
return AppModule.getAllApps().filter((app) => app.category === category);
},
// Get app categories
getCategories: () => appCategories,
// Get app IDs
getAppIds: () => appRegistry.map((app) => app.id),
// Check if app exists
hasApp: (appId) => appRegistry.some((app) => app.id === appId),
// Get apps for taskbar (existing format for backward compatibility)
getTaskbarApps: () => {
const apps = {};
AppModule.getAllApps().forEach((app) => {
apps[app.id] = {
name: app.name,
description: app.description
};
});
return apps;
},
// Get app config for window system (existing format for backward compatibility)
getWindowConfig: () => {
const config = {};
AppModule.getAllApps().forEach((app) => {
config[app.id] = {
title: app.name,
icon: app.icon
};
});
return config;
},
// Generate hamburger menu items
generateHamburgerMenuItems: () => {
return AppModule.getAllApps()
.slice() // Create a copy to avoid modifying the original array
.sort((a, b) => a.shortName.localeCompare(b.shortName))
.map((app) => ({
id: app.id,
name: app.shortName,
description: app.detailedDescription,
icon: app.icon,
path: app.path,
gradient: app.gradient,
border: app.border
}));
},
// Get system apps (pinned to start menu, context menu, etc.)
getSystemApps: () => {
return AppModule.getAllApps().filter((app) => app.system === true);
},
// Get apps with desktop icons
getDesktopApps: () => {
return AppModule.getAllApps().filter((app) => app.desktopIcon === true);
},
// Get non-system apps (for launcher categories)
getNonSystemApps: () => {
return AppModule.getAllApps().filter((app) => app.system !== true);
},
/**
* All apps that handle a MIME type, with exact matches listed before wildcard matches.
* @param {string} mimeType
* @returns {Array<{ appId: string, appName: string, shortName: string, icon: string }>}
*/
getAppsForMimeType: (mimeType) => {
if (window.MimeHandlers && typeof window.MimeHandlers.getAppsForMimeType === 'function') {
return window.MimeHandlers.getAppsForMimeType(mimeType, appRegistry);
}
return [];
},
/**
* Get the app that handles a given MIME type
* Apps register their supported types via the 'handles' array
* Supports wildcards like 'image/*' and 'text/*'
* @param {string} mimeType - The MIME type to find a handler for
* @returns {{ appId: string, appName: string } | null} App info or null if no handler
*/
getAppForMimeType: (mimeType) => {
const apps = AppModule.getAppsForMimeType(mimeType);
if (!apps.length) return null;
return { appId: apps[0].appId, appName: apps[0].appName };
}
};
// Export for module usage (if using ES6 modules)
// export default AppModule;
// Global namespace for direct script inclusion
window.AppModule = AppModule;
window.__heymingAppRegistryReady = true;
// Backward compatibility exports
window.availableApps = AppModule.getTaskbarApps();
window.appConfig = AppModule.getWindowConfig();