agentgui 1.0.67 → 1.0.68
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prd +214 -0
- package/.prd-browser +607 -0
- package/CLAUDE.md +1532 -125
- package/browser-test-harness.js +371 -0
- package/browser-test.js +409 -0
- package/execute-tests.js +164 -0
- package/lib/claude-runner.js +41 -12
- package/lib/database-service.ts +252 -0
- package/lib/sync-service.ts +275 -0
- package/lib/types.ts +168 -0
- package/package.json +1 -1
- package/readme.md +586 -0
- package/run-e2e-test.sh +88 -0
- package/server.js +274 -8
- package/static/index.html +487 -180
- package/static/js/client.js +558 -0
- package/static/js/event-filter.js +311 -0
- package/static/js/event-processor.js +454 -0
- package/static/js/streaming-renderer.js +813 -0
- package/static/js/syntax-highlighter.js +271 -0
- package/static/js/ui-components.js +433 -0
- package/static/js/websocket-manager.js +482 -0
- package/static/templates/INDEX.html +465 -0
- package/static/templates/README.md +190 -0
- package/static/templates/agent-capabilities.html +56 -0
- package/static/templates/agent-metadata-panel.html +44 -0
- package/static/templates/agent-status-badge.html +30 -0
- package/static/templates/code-annotation-panel.html +155 -0
- package/static/templates/code-suggestion-panel.html +184 -0
- package/static/templates/command-header.html +77 -0
- package/static/templates/command-output-scrollable.html +118 -0
- package/static/templates/elapsed-time.html +54 -0
- package/static/templates/error-alert.html +106 -0
- package/static/templates/error-history-timeline.html +160 -0
- package/static/templates/error-recovery-options.html +109 -0
- package/static/templates/error-stack-trace.html +95 -0
- package/static/templates/error-summary.html +80 -0
- package/static/templates/event-counter.html +48 -0
- package/static/templates/execution-actions.html +97 -0
- package/static/templates/execution-progress-bar.html +80 -0
- package/static/templates/execution-stepper.html +120 -0
- package/static/templates/file-breadcrumb.html +118 -0
- package/static/templates/file-diff-viewer.html +121 -0
- package/static/templates/file-metadata.html +133 -0
- package/static/templates/file-read-panel.html +66 -0
- package/static/templates/file-write-panel.html +120 -0
- package/static/templates/git-branch-remote.html +107 -0
- package/static/templates/git-diff-list.html +101 -0
- package/static/templates/git-log-visualization.html +153 -0
- package/static/templates/git-status-panel.html +115 -0
- package/static/templates/quality-metrics-display.html +170 -0
- package/static/templates/terminal-output-panel.html +87 -0
- package/static/templates/test-results-display.html +144 -0
- package/test-browser.js +457 -0
- package/test-runner.js +182 -0
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Syntax Highlighter Integration
|
|
3
|
+
* Handles lazy-loading and caching of Prism.js for code highlighting
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
class SyntaxHighlighter {
|
|
7
|
+
constructor(config = {}) {
|
|
8
|
+
this.config = {
|
|
9
|
+
cdnUrl: config.cdnUrl || 'https://unpkg.com/prism@1.29.0',
|
|
10
|
+
lazyLoad: config.lazyLoad !== false,
|
|
11
|
+
enableCache: config.enableCache !== false,
|
|
12
|
+
maxCacheSize: config.maxCacheSize || 500,
|
|
13
|
+
supportedLanguages: config.supportedLanguages || [
|
|
14
|
+
'javascript', 'typescript', 'python', 'java', 'cpp', 'c', 'csharp', 'go', 'rust',
|
|
15
|
+
'php', 'ruby', 'swift', 'kotlin', 'sql', 'html', 'css', 'scss', 'bash', 'shell',
|
|
16
|
+
'json', 'xml', 'yaml', 'markdown', 'plaintext'
|
|
17
|
+
],
|
|
18
|
+
...config
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
this.isLoaded = false;
|
|
22
|
+
this.isLoading = false;
|
|
23
|
+
this.highlightCache = new Map();
|
|
24
|
+
this.loadPromise = null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Ensure Prism is loaded
|
|
29
|
+
*/
|
|
30
|
+
async ensureLoaded() {
|
|
31
|
+
// Already loaded
|
|
32
|
+
if (typeof Prism !== 'undefined' && this.isLoaded) {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Currently loading
|
|
37
|
+
if (this.isLoading && this.loadPromise) {
|
|
38
|
+
return this.loadPromise;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Start loading
|
|
42
|
+
this.isLoading = true;
|
|
43
|
+
this.loadPromise = this.loadPrism();
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const result = await this.loadPromise;
|
|
47
|
+
this.isLoaded = true;
|
|
48
|
+
return result;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.error('Failed to load Prism:', error);
|
|
51
|
+
this.isLoading = false;
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Load Prism library
|
|
58
|
+
*/
|
|
59
|
+
async loadPrism() {
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
try {
|
|
62
|
+
// Load main Prism JS
|
|
63
|
+
const script = document.createElement('script');
|
|
64
|
+
script.src = `${this.config.cdnUrl}/prism.js`;
|
|
65
|
+
script.async = true;
|
|
66
|
+
|
|
67
|
+
script.onload = () => {
|
|
68
|
+
// Load common language files
|
|
69
|
+
this.loadLanguages();
|
|
70
|
+
this.isLoading = false;
|
|
71
|
+
resolve(true);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
script.onerror = () => {
|
|
75
|
+
this.isLoading = false;
|
|
76
|
+
reject(new Error('Failed to load Prism.js'));
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
document.head.appendChild(script);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
this.isLoading = false;
|
|
82
|
+
reject(error);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Load language files
|
|
89
|
+
*/
|
|
90
|
+
loadLanguages() {
|
|
91
|
+
const languages = ['javascript', 'python', 'sql', 'bash', 'json'];
|
|
92
|
+
|
|
93
|
+
for (const lang of languages) {
|
|
94
|
+
const script = document.createElement('script');
|
|
95
|
+
script.src = `${this.config.cdnUrl}/components/prism-${lang}.js`;
|
|
96
|
+
script.async = true;
|
|
97
|
+
document.head.appendChild(script);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Highlight code
|
|
103
|
+
*/
|
|
104
|
+
async highlight(code, language = 'plaintext') {
|
|
105
|
+
if (!code) return '';
|
|
106
|
+
|
|
107
|
+
// Ensure Prism is loaded
|
|
108
|
+
if (this.config.lazyLoad) {
|
|
109
|
+
try {
|
|
110
|
+
await this.ensureLoaded();
|
|
111
|
+
} catch (error) {
|
|
112
|
+
console.warn('Prism loading failed, returning unformatted code');
|
|
113
|
+
return this.escapeHtml(code);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Check cache
|
|
118
|
+
const cacheKey = `${language}:${code}`;
|
|
119
|
+
if (this.config.enableCache && this.highlightCache.has(cacheKey)) {
|
|
120
|
+
return this.highlightCache.get(cacheKey);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Highlight code
|
|
124
|
+
let highlighted;
|
|
125
|
+
try {
|
|
126
|
+
if (typeof Prism !== 'undefined' && Prism.languages[language]) {
|
|
127
|
+
highlighted = Prism.highlight(code, Prism.languages[language], language);
|
|
128
|
+
} else {
|
|
129
|
+
// Fallback to escaped HTML if language not supported
|
|
130
|
+
highlighted = this.escapeHtml(code);
|
|
131
|
+
}
|
|
132
|
+
} catch (error) {
|
|
133
|
+
console.error('Highlight error:', error);
|
|
134
|
+
highlighted = this.escapeHtml(code);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Cache result
|
|
138
|
+
if (this.config.enableCache) {
|
|
139
|
+
this.highlightCache.set(cacheKey, highlighted);
|
|
140
|
+
|
|
141
|
+
// Trim cache if too large
|
|
142
|
+
if (this.highlightCache.size > this.config.maxCacheSize) {
|
|
143
|
+
const firstKey = this.highlightCache.keys().next().value;
|
|
144
|
+
this.highlightCache.delete(firstKey);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return highlighted;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Create highlighted code element
|
|
153
|
+
*/
|
|
154
|
+
async createHighlightedElement(code, language = 'plaintext') {
|
|
155
|
+
const pre = document.createElement('pre');
|
|
156
|
+
const code_el = document.createElement('code');
|
|
157
|
+
|
|
158
|
+
// Set language class
|
|
159
|
+
code_el.className = `language-${language}`;
|
|
160
|
+
|
|
161
|
+
if (this.config.lazyLoad) {
|
|
162
|
+
try {
|
|
163
|
+
await this.ensureLoaded();
|
|
164
|
+
const highlighted = await this.highlight(code, language);
|
|
165
|
+
code_el.innerHTML = highlighted;
|
|
166
|
+
} catch (error) {
|
|
167
|
+
code_el.textContent = code;
|
|
168
|
+
}
|
|
169
|
+
} else {
|
|
170
|
+
code_el.textContent = code;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
pre.appendChild(code_el);
|
|
174
|
+
return pre;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Highlight DOM element
|
|
179
|
+
*/
|
|
180
|
+
async highlightElement(element) {
|
|
181
|
+
if (!element || !element.querySelector('code')) return;
|
|
182
|
+
|
|
183
|
+
if (this.config.lazyLoad) {
|
|
184
|
+
try {
|
|
185
|
+
await this.ensureLoaded();
|
|
186
|
+
} catch (error) {
|
|
187
|
+
console.warn('Prism loading failed, skipping highlighting');
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (typeof Prism !== 'undefined') {
|
|
193
|
+
try {
|
|
194
|
+
Prism.highlightElement(element.querySelector('code'));
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error('Element highlighting error:', error);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Detect language from code content
|
|
203
|
+
*/
|
|
204
|
+
detectLanguage(code) {
|
|
205
|
+
if (!code) return 'plaintext';
|
|
206
|
+
|
|
207
|
+
// Shebang detection
|
|
208
|
+
if (code.startsWith('#!/')) {
|
|
209
|
+
if (code.includes('python')) return 'python';
|
|
210
|
+
if (code.includes('node') || code.includes('node.js')) return 'javascript';
|
|
211
|
+
if (code.includes('bash') || code.includes('sh')) return 'bash';
|
|
212
|
+
if (code.includes('ruby')) return 'ruby';
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Pattern detection
|
|
216
|
+
if (code.includes('def ') && code.includes(':')) return 'python';
|
|
217
|
+
if (code.includes('function') || code.includes('=>')) return 'javascript';
|
|
218
|
+
if (code.includes('fn ') && code.includes('->')) return 'rust';
|
|
219
|
+
if (code.includes('public static') || code.includes('class ')) return 'java';
|
|
220
|
+
if (code.includes('SELECT') || code.includes('INSERT')) return 'sql';
|
|
221
|
+
if (code.includes('<html') || code.includes('<div')) return 'html';
|
|
222
|
+
if (code.includes('::') && code.includes('use ')) return 'rust';
|
|
223
|
+
|
|
224
|
+
return 'plaintext';
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Get supported languages
|
|
229
|
+
*/
|
|
230
|
+
getSupportedLanguages() {
|
|
231
|
+
return [...this.config.supportedLanguages];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Check if language is supported
|
|
236
|
+
*/
|
|
237
|
+
isSupportedLanguage(language) {
|
|
238
|
+
return this.config.supportedLanguages.includes(language);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Clear cache
|
|
243
|
+
*/
|
|
244
|
+
clearCache() {
|
|
245
|
+
this.highlightCache.clear();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Get cache stats
|
|
250
|
+
*/
|
|
251
|
+
getCacheStats() {
|
|
252
|
+
return {
|
|
253
|
+
size: this.highlightCache.size,
|
|
254
|
+
maxSize: this.config.maxCacheSize
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* HTML escape utility
|
|
260
|
+
*/
|
|
261
|
+
escapeHtml(text) {
|
|
262
|
+
const div = document.createElement('div');
|
|
263
|
+
div.textContent = text;
|
|
264
|
+
return div.innerHTML;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Export for use in browser
|
|
269
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
270
|
+
module.exports = SyntaxHighlighter;
|
|
271
|
+
}
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UI Components
|
|
3
|
+
* Reusable UI building blocks for modals, tabs, buttons, and more
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
class UIComponents {
|
|
7
|
+
/**
|
|
8
|
+
* Create a modal dialog
|
|
9
|
+
*/
|
|
10
|
+
static createModal(config = {}) {
|
|
11
|
+
const {
|
|
12
|
+
title = 'Dialog',
|
|
13
|
+
content = '',
|
|
14
|
+
buttons = [],
|
|
15
|
+
onClose = null,
|
|
16
|
+
size = 'medium' // small, medium, large
|
|
17
|
+
} = config;
|
|
18
|
+
|
|
19
|
+
const modal = document.createElement('div');
|
|
20
|
+
modal.className = 'modal-overlay';
|
|
21
|
+
modal.dataset.modal = 'true';
|
|
22
|
+
|
|
23
|
+
const sizeClasses = {
|
|
24
|
+
'small': 'max-w-sm',
|
|
25
|
+
'medium': 'max-w-md',
|
|
26
|
+
'large': 'max-w-2xl'
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
modal.innerHTML = `
|
|
30
|
+
<div class="modal-content ${sizeClasses[size] || sizeClasses['medium']} bg-white dark:bg-gray-900 rounded-lg shadow-lg p-6">
|
|
31
|
+
<div class="modal-header flex justify-between items-center mb-4 pb-4 border-b">
|
|
32
|
+
<h2 class="text-xl font-bold">${UIComponents.escapeHtml(title)}</h2>
|
|
33
|
+
<button class="modal-close text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-300 text-2xl leading-none">×</button>
|
|
34
|
+
</div>
|
|
35
|
+
<div class="modal-body mb-4">
|
|
36
|
+
${typeof content === 'string' ? UIComponents.escapeHtml(content) : ''}
|
|
37
|
+
</div>
|
|
38
|
+
<div class="modal-footer flex gap-2 justify-end">
|
|
39
|
+
${buttons.map(btn => `
|
|
40
|
+
<button class="btn btn-${btn.variant || 'secondary'}" data-action="${btn.action || 'close'}">
|
|
41
|
+
${UIComponents.escapeHtml(btn.label)}
|
|
42
|
+
</button>
|
|
43
|
+
`).join('')}
|
|
44
|
+
</div>
|
|
45
|
+
</div>
|
|
46
|
+
`;
|
|
47
|
+
|
|
48
|
+
// Add close handler
|
|
49
|
+
const closeBtn = modal.querySelector('.modal-close');
|
|
50
|
+
closeBtn.addEventListener('click', () => {
|
|
51
|
+
modal.remove();
|
|
52
|
+
if (onClose) onClose();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Add button handlers
|
|
56
|
+
modal.querySelectorAll('[data-action]').forEach(btn => {
|
|
57
|
+
btn.addEventListener('click', (e) => {
|
|
58
|
+
const action = e.target.dataset.action;
|
|
59
|
+
if (action === 'close') {
|
|
60
|
+
modal.remove();
|
|
61
|
+
if (onClose) onClose();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Close on background click
|
|
67
|
+
modal.addEventListener('click', (e) => {
|
|
68
|
+
if (e.target === modal) {
|
|
69
|
+
modal.remove();
|
|
70
|
+
if (onClose) onClose();
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
return modal;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Create a tabbed interface
|
|
79
|
+
*/
|
|
80
|
+
static createTabs(config = {}) {
|
|
81
|
+
const {
|
|
82
|
+
tabs = [],
|
|
83
|
+
activeTab = 0,
|
|
84
|
+
onChange = null
|
|
85
|
+
} = config;
|
|
86
|
+
|
|
87
|
+
const container = document.createElement('div');
|
|
88
|
+
container.className = 'tabs';
|
|
89
|
+
|
|
90
|
+
// Tab buttons
|
|
91
|
+
const tabButtons = document.createElement('div');
|
|
92
|
+
tabButtons.className = 'tab-buttons flex border-b';
|
|
93
|
+
|
|
94
|
+
tabs.forEach((tab, index) => {
|
|
95
|
+
const btn = document.createElement('button');
|
|
96
|
+
btn.className = `tab-button px-4 py-2 font-medium transition-colors ${
|
|
97
|
+
index === activeTab
|
|
98
|
+
? 'border-b-2 border-blue-500 text-blue-600 dark:text-blue-400'
|
|
99
|
+
: 'text-gray-600 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200'
|
|
100
|
+
}`;
|
|
101
|
+
btn.textContent = tab.label;
|
|
102
|
+
btn.dataset.tabIndex = index;
|
|
103
|
+
|
|
104
|
+
btn.addEventListener('click', () => {
|
|
105
|
+
// Update active button
|
|
106
|
+
tabButtons.querySelectorAll('.tab-button').forEach((b, i) => {
|
|
107
|
+
if (i === index) {
|
|
108
|
+
b.classList.add('border-b-2', 'border-blue-500', 'text-blue-600', 'dark:text-blue-400');
|
|
109
|
+
b.classList.remove('text-gray-600', 'dark:text-gray-400');
|
|
110
|
+
} else {
|
|
111
|
+
b.classList.remove('border-b-2', 'border-blue-500', 'text-blue-600', 'dark:text-blue-400');
|
|
112
|
+
b.classList.add('text-gray-600', 'dark:text-gray-400');
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Update tab content
|
|
117
|
+
tabContent.querySelectorAll('.tab-pane').forEach((pane, i) => {
|
|
118
|
+
pane.style.display = i === index ? 'block' : 'none';
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
if (onChange) onChange(index);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
tabButtons.appendChild(btn);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
container.appendChild(tabButtons);
|
|
128
|
+
|
|
129
|
+
// Tab content
|
|
130
|
+
const tabContent = document.createElement('div');
|
|
131
|
+
tabContent.className = 'tab-content mt-4';
|
|
132
|
+
|
|
133
|
+
tabs.forEach((tab, index) => {
|
|
134
|
+
const pane = document.createElement('div');
|
|
135
|
+
pane.className = 'tab-pane';
|
|
136
|
+
pane.style.display = index === activeTab ? 'block' : 'none';
|
|
137
|
+
pane.innerHTML = typeof tab.content === 'string' ? tab.content : '';
|
|
138
|
+
tabContent.appendChild(pane);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
container.appendChild(tabContent);
|
|
142
|
+
return container;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Create an alert/notification
|
|
147
|
+
*/
|
|
148
|
+
static createAlert(config = {}) {
|
|
149
|
+
const {
|
|
150
|
+
message = '',
|
|
151
|
+
type = 'info', // info, success, warning, error
|
|
152
|
+
duration = 5000,
|
|
153
|
+
dismissible = true
|
|
154
|
+
} = config;
|
|
155
|
+
|
|
156
|
+
const alert = document.createElement('div');
|
|
157
|
+
const typeClasses = {
|
|
158
|
+
'info': 'bg-blue-50 border-blue-200 text-blue-800 dark:bg-blue-900 dark:border-blue-700 dark:text-blue-200',
|
|
159
|
+
'success': 'bg-green-50 border-green-200 text-green-800 dark:bg-green-900 dark:border-green-700 dark:text-green-200',
|
|
160
|
+
'warning': 'bg-yellow-50 border-yellow-200 text-yellow-800 dark:bg-yellow-900 dark:border-yellow-700 dark:text-yellow-200',
|
|
161
|
+
'error': 'bg-red-50 border-red-200 text-red-800 dark:bg-red-900 dark:border-red-700 dark:text-red-200'
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
alert.className = `alert border-l-4 p-4 mb-4 rounded ${typeClasses[type] || typeClasses['info']}`;
|
|
165
|
+
alert.innerHTML = `
|
|
166
|
+
<div class="flex justify-between items-center">
|
|
167
|
+
<span>${UIComponents.escapeHtml(message)}</span>
|
|
168
|
+
${dismissible ? '<button class="text-current hover:opacity-75">×</button>' : ''}
|
|
169
|
+
</div>
|
|
170
|
+
`;
|
|
171
|
+
|
|
172
|
+
if (dismissible) {
|
|
173
|
+
const closeBtn = alert.querySelector('button');
|
|
174
|
+
closeBtn.addEventListener('click', () => alert.remove());
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (duration > 0) {
|
|
178
|
+
setTimeout(() => alert.remove(), duration);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return alert;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Create a loading spinner
|
|
186
|
+
*/
|
|
187
|
+
static createSpinner(config = {}) {
|
|
188
|
+
const {
|
|
189
|
+
size = 'medium', // small, medium, large
|
|
190
|
+
text = 'Loading...'
|
|
191
|
+
} = config;
|
|
192
|
+
|
|
193
|
+
const sizeClasses = {
|
|
194
|
+
'small': 'w-4 h-4',
|
|
195
|
+
'medium': 'w-8 h-8',
|
|
196
|
+
'large': 'w-12 h-12'
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const container = document.createElement('div');
|
|
200
|
+
container.className = 'flex items-center gap-3 justify-center p-4';
|
|
201
|
+
container.innerHTML = `
|
|
202
|
+
<svg class="animate-spin ${sizeClasses[size] || sizeClasses['medium']} text-blue-600 dark:text-blue-400" fill="none" viewBox="0 0 24 24">
|
|
203
|
+
<circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2" opacity="0.25"></circle>
|
|
204
|
+
<path d="M4 12a8 8 0 018-8" stroke="currentColor" stroke-width="2" stroke-linecap="round"></path>
|
|
205
|
+
</svg>
|
|
206
|
+
<span class="text-gray-700 dark:text-gray-300">${UIComponents.escapeHtml(text)}</span>
|
|
207
|
+
`;
|
|
208
|
+
return container;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Create a progress bar
|
|
213
|
+
*/
|
|
214
|
+
static createProgressBar(config = {}) {
|
|
215
|
+
const {
|
|
216
|
+
percentage = 0,
|
|
217
|
+
label = '',
|
|
218
|
+
showLabel = true
|
|
219
|
+
} = config;
|
|
220
|
+
|
|
221
|
+
const container = document.createElement('div');
|
|
222
|
+
container.className = 'progress-container';
|
|
223
|
+
|
|
224
|
+
let html = '';
|
|
225
|
+
if (label && showLabel) {
|
|
226
|
+
html += `<div class="flex justify-between mb-2 text-sm"><span>${UIComponents.escapeHtml(label)}</span><span>${Math.round(percentage)}%</span></div>`;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
html += `
|
|
230
|
+
<div class="progress-bar bg-gray-200 dark:bg-gray-700 rounded-full h-2 overflow-hidden">
|
|
231
|
+
<div class="progress-fill bg-blue-500 h-full transition-all" style="width: ${Math.min(100, Math.max(0, percentage))}%"></div>
|
|
232
|
+
</div>
|
|
233
|
+
`;
|
|
234
|
+
|
|
235
|
+
container.innerHTML = html;
|
|
236
|
+
return container;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Create a collapsible section
|
|
241
|
+
*/
|
|
242
|
+
static createCollapsible(config = {}) {
|
|
243
|
+
const {
|
|
244
|
+
title = 'Details',
|
|
245
|
+
content = '',
|
|
246
|
+
isOpen = false
|
|
247
|
+
} = config;
|
|
248
|
+
|
|
249
|
+
const container = document.createElement('div');
|
|
250
|
+
container.className = 'collapsible';
|
|
251
|
+
|
|
252
|
+
container.innerHTML = `
|
|
253
|
+
<details ${isOpen ? 'open' : ''}>
|
|
254
|
+
<summary class="cursor-pointer font-semibold hover:bg-gray-100 dark:hover:bg-gray-800 px-2 py-1 rounded transition-colors">
|
|
255
|
+
${UIComponents.escapeHtml(title)}
|
|
256
|
+
</summary>
|
|
257
|
+
<div class="content mt-2 ml-4">
|
|
258
|
+
${typeof content === 'string' ? content : ''}
|
|
259
|
+
</div>
|
|
260
|
+
</details>
|
|
261
|
+
`;
|
|
262
|
+
|
|
263
|
+
return container;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Create a form input
|
|
268
|
+
*/
|
|
269
|
+
static createInput(config = {}) {
|
|
270
|
+
const {
|
|
271
|
+
type = 'text',
|
|
272
|
+
name = '',
|
|
273
|
+
label = '',
|
|
274
|
+
placeholder = '',
|
|
275
|
+
value = '',
|
|
276
|
+
required = false
|
|
277
|
+
} = config;
|
|
278
|
+
|
|
279
|
+
const container = document.createElement('div');
|
|
280
|
+
container.className = 'form-group mb-4';
|
|
281
|
+
|
|
282
|
+
let html = '';
|
|
283
|
+
if (label) {
|
|
284
|
+
html += `<label class="block text-sm font-medium mb-2">${UIComponents.escapeHtml(label)}</label>`;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
html += `
|
|
288
|
+
<input
|
|
289
|
+
type="${type}"
|
|
290
|
+
name="${name}"
|
|
291
|
+
placeholder="${UIComponents.escapeHtml(placeholder)}"
|
|
292
|
+
value="${UIComponents.escapeHtml(value)}"
|
|
293
|
+
${required ? 'required' : ''}
|
|
294
|
+
class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500"
|
|
295
|
+
/>
|
|
296
|
+
`;
|
|
297
|
+
|
|
298
|
+
container.innerHTML = html;
|
|
299
|
+
return container;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Create a select dropdown
|
|
304
|
+
*/
|
|
305
|
+
static createSelect(config = {}) {
|
|
306
|
+
const {
|
|
307
|
+
name = '',
|
|
308
|
+
label = '',
|
|
309
|
+
options = [],
|
|
310
|
+
value = '',
|
|
311
|
+
required = false
|
|
312
|
+
} = config;
|
|
313
|
+
|
|
314
|
+
const container = document.createElement('div');
|
|
315
|
+
container.className = 'form-group mb-4';
|
|
316
|
+
|
|
317
|
+
let html = '';
|
|
318
|
+
if (label) {
|
|
319
|
+
html += `<label class="block text-sm font-medium mb-2">${UIComponents.escapeHtml(label)}</label>`;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
html += `
|
|
323
|
+
<select
|
|
324
|
+
name="${name}"
|
|
325
|
+
${required ? 'required' : ''}
|
|
326
|
+
class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500"
|
|
327
|
+
>
|
|
328
|
+
${options.map(opt => `
|
|
329
|
+
<option value="${opt.value}" ${opt.value === value ? 'selected' : ''}>
|
|
330
|
+
${UIComponents.escapeHtml(opt.label)}
|
|
331
|
+
</option>
|
|
332
|
+
`).join('')}
|
|
333
|
+
</select>
|
|
334
|
+
`;
|
|
335
|
+
|
|
336
|
+
container.innerHTML = html;
|
|
337
|
+
return container;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Create a button group
|
|
342
|
+
*/
|
|
343
|
+
static createButtonGroup(config = {}) {
|
|
344
|
+
const {
|
|
345
|
+
buttons = [],
|
|
346
|
+
vertical = false
|
|
347
|
+
} = config;
|
|
348
|
+
|
|
349
|
+
const container = document.createElement('div');
|
|
350
|
+
container.className = `button-group flex gap-2 ${vertical ? 'flex-col' : 'flex-row'}`;
|
|
351
|
+
|
|
352
|
+
buttons.forEach(btn => {
|
|
353
|
+
const button = document.createElement('button');
|
|
354
|
+
button.className = `btn btn-${btn.variant || 'secondary'} flex-${vertical ? '1' : 'none'}`;
|
|
355
|
+
button.textContent = btn.label;
|
|
356
|
+
if (btn.onClick) {
|
|
357
|
+
button.addEventListener('click', btn.onClick);
|
|
358
|
+
}
|
|
359
|
+
container.appendChild(button);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
return container;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Create a badge/tag
|
|
367
|
+
*/
|
|
368
|
+
static createBadge(config = {}) {
|
|
369
|
+
const {
|
|
370
|
+
label = '',
|
|
371
|
+
variant = 'default', // default, primary, success, warning, error
|
|
372
|
+
size = 'medium' // small, medium, large
|
|
373
|
+
} = config;
|
|
374
|
+
|
|
375
|
+
const sizeClasses = {
|
|
376
|
+
'small': 'text-xs px-2 py-1',
|
|
377
|
+
'medium': 'text-sm px-3 py-1',
|
|
378
|
+
'large': 'text-base px-4 py-2'
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
const variantClasses = {
|
|
382
|
+
'default': 'bg-gray-200 text-gray-800 dark:bg-gray-700 dark:text-gray-200',
|
|
383
|
+
'primary': 'bg-blue-200 text-blue-800 dark:bg-blue-700 dark:text-blue-200',
|
|
384
|
+
'success': 'bg-green-200 text-green-800 dark:bg-green-700 dark:text-green-200',
|
|
385
|
+
'warning': 'bg-yellow-200 text-yellow-800 dark:bg-yellow-700 dark:text-yellow-200',
|
|
386
|
+
'error': 'bg-red-200 text-red-800 dark:bg-red-700 dark:text-red-200'
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
const badge = document.createElement('span');
|
|
390
|
+
badge.className = `badge rounded-full font-medium ${sizeClasses[size] || sizeClasses['medium']} ${variantClasses[variant] || variantClasses['default']}`;
|
|
391
|
+
badge.textContent = label;
|
|
392
|
+
return badge;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* HTML escape utility
|
|
397
|
+
*/
|
|
398
|
+
static escapeHtml(text) {
|
|
399
|
+
const div = document.createElement('div');
|
|
400
|
+
div.textContent = text;
|
|
401
|
+
return div.innerHTML;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Copy text to clipboard
|
|
406
|
+
*/
|
|
407
|
+
static copyToClipboard(text) {
|
|
408
|
+
return navigator.clipboard.writeText(text).catch(err => {
|
|
409
|
+
console.error('Failed to copy:', err);
|
|
410
|
+
return false;
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Download data as file
|
|
416
|
+
*/
|
|
417
|
+
static downloadFile(data, filename, mimeType = 'text/plain') {
|
|
418
|
+
const blob = new Blob([data], { type: mimeType });
|
|
419
|
+
const url = URL.createObjectURL(blob);
|
|
420
|
+
const link = document.createElement('a');
|
|
421
|
+
link.href = url;
|
|
422
|
+
link.download = filename;
|
|
423
|
+
document.body.appendChild(link);
|
|
424
|
+
link.click();
|
|
425
|
+
document.body.removeChild(link);
|
|
426
|
+
URL.revokeObjectURL(url);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// Export for use in browser
|
|
431
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
432
|
+
module.exports = UIComponents;
|
|
433
|
+
}
|