-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
275 lines (234 loc) · 10.1 KB
/
main.js
File metadata and controls
275 lines (234 loc) · 10.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
class BadgeGenerator {
constructor() {
this.baseUrl = 'https://img.shields.io';
this.currentCategory = 'build';
this.generatedBadges = new Set();
this.init();
}
init() {
this.setupTimeUpdate();
this.setupCategoryNavigation();
this.setupButtons();
this.setupCopyButton();
this.setupThemeToggle();
this.setupRepoLoader();
// Show initial category
this.showSection('build');
}
setupTimeUpdate() {
const updateTime = () => {
const now = new Date();
const timeString = now.toISOString().replace('T', ' ').substring(0, 19);
document.getElementById('current-time').textContent = timeString;
};
updateTime();
setInterval(updateTime, 1000);
}
setupCategoryNavigation() {
const navButtons = document.querySelectorAll('.category-btn');
navButtons.forEach(button => {
button.addEventListener('click', () => {
// Remove active class from all buttons
navButtons.forEach(btn => {
btn.classList.remove('bg-indigo-600', 'text-white');
btn.classList.add('bg-gray-200', 'hover:bg-gray-300');
});
// Add active class to clicked button
button.classList.remove('bg-gray-200', 'hover:bg-gray-300');
button.classList.add('bg-indigo-600', 'text-white');
// Show corresponding section
const category = button.getAttribute('data-category');
this.showSection(category);
});
});
}
setupButtons() {
// Generate Badge button
document.getElementById('generate-badge').addEventListener('click', () => {
this.generateBadge();
});
// Reset button
document.getElementById('reset-preview').addEventListener('click', () => {
this.resetPreview();
});
}
setupCopyButton() {
const copyButton = document.getElementById('copy-markdown');
copyButton.addEventListener('click', () => {
const markdown = document.getElementById('markdown-output');
navigator.clipboard.writeText(markdown.value)
.then(() => {
this.showToast('Copied to clipboard!');
})
.catch(() => {
this.showToast('Failed to copy!', 'error');
});
});
}
setupThemeToggle() {
const themeToggle = document.getElementById('theme-toggle');
themeToggle.addEventListener('click', () => {
document.documentElement.classList.toggle('dark');
const icon = themeToggle.querySelector('i');
icon.classList.toggle('fa-moon');
icon.classList.toggle('fa-sun');
});
}
setupRepoLoader() {
document.getElementById('load-repo').addEventListener('click', () => {
const repoInput = document.getElementById('repo-input');
if (repoInput.value) {
this.loadRepositoryInfo(repoInput.value);
} else {
this.showToast('Please enter a repository name!', 'error');
}
});
}
showSection(category) {
this.currentCategory = category;
// Hide all sections
document.querySelectorAll('.badge-section').forEach(section => {
section.style.display = 'none';
});
// Show selected section
const selectedSection = document.getElementById(`${category}-section`);
if (selectedSection) {
selectedSection.style.display = 'block';
}
}
generateBadge() {
const style = document.getElementById('badge-style').value;
const color = document.getElementById('badge-color').value;
const repo = document.getElementById('repo-input').value;
if (!repo) {
this.showToast('Please enter a repository name!', 'error');
return;
}
let badges = [];
switch (this.currentCategory) {
case 'build':
badges = this.generateBuildBadges(repo, style);
break;
case 'testing':
badges = this.generateTestingBadges(repo, style);
break;
case 'package':
badges = this.generatePackageBadges(style);
break;
// Add other categories here
}
this.updatePreview(badges);
}
generateBuildBadges(repo, style) {
const badges = [];
const ciPlatform = document.querySelector('select[name="ci-platform"]').value;
const workflow = document.querySelector('input[name="workflow"]').value;
const deployPlatform = document.querySelector('select[name="deploy-platform"]').value;
const environment = document.querySelector('select[name="environment"]').value;
// CI Badge
badges.push(`${this.baseUrl}/github/workflow/status/${repo}/${workflow || 'main.yml'}?style=${style}`);
// Deployment Badge
badges.push(`${this.baseUrl}/deployment/${deployPlatform}/${repo}/${environment}?style=${style}`);
return badges;
}
generateTestingBadges(repo, style) {
const badges = [];
const service = document.querySelector('select[name="coverage-service"]').value;
const threshold = document.querySelector('input[name="coverage-threshold"]').value;
const branch = document.querySelector('input[name="test-branch"]').value;
// Coverage Badge
if (service === 'codecov') {
badges.push(`${this.baseUrl}/codecov/c/github/${repo}?style=${style}`);
} else if (service === 'coveralls') {
badges.push(`${this.baseUrl}/coveralls/github/${repo}?style=${style}`);
}
// Tests Badge
badges.push(`${this.baseUrl}/github/workflow/status/${repo}/tests?label=tests&style=${style}`);
return badges;
}
generatePackageBadges(style) {
const badges = [];
const packageName = document.querySelector('input[name="package-name"]').value;
const registry = document.querySelector('select[name="package-registry"]').value;
const showDownloads = document.querySelector('input[name="show-downloads"]').checked;
const showVersion = document.querySelector('input[name="show-version"]').checked;
if (showVersion) {
badges.push(`${this.baseUrl}/${registry}/v/${packageName}?style=${style}`);
}
if (showDownloads) {
badges.push(`${this.baseUrl}/${registry}/dt/${packageName}?style=${style}`);
}
return badges;
}
updatePreview(badges) {
const preview = document.getElementById('badge-preview');
const markdownOutput = document.getElementById('markdown-output');
let markdown = '';
badges.forEach(badgeUrl => {
if (!this.generatedBadges.has(badgeUrl)) {
this.generatedBadges.add(badgeUrl);
// Create badge preview
const wrapper = document.createElement('div');
wrapper.className = 'relative group';
const img = document.createElement('img');
img.src = badgeUrl;
img.alt = 'Badge';
img.className = 'h-6';
wrapper.appendChild(img);
// Add remove button
const removeBtn = document.createElement('button');
removeBtn.className = 'absolute -top-2 -right-2 hidden group-hover:block bg-red-500 text-white rounded-full w-4 h-4 text-xs flex items-center justify-center';
removeBtn.innerHTML = '×';
removeBtn.onclick = () => {
wrapper.remove();
this.generatedBadges.delete(badgeUrl);
this.updateMarkdown();
};
wrapper.appendChild(removeBtn);
preview.appendChild(wrapper);
markdown += `\n`;
}
});
markdownOutput.value += markdown;
}
resetPreview() {
document.getElementById('badge-preview').innerHTML = '';
document.getElementById('markdown-output').value = '';
this.generatedBadges.clear();
}
showToast(message, type = 'success') {
const toast = document.getElementById('toast');
const toastMessage = document.getElementById('toast-message');
toastMessage.textContent = message;
toast.classList.remove('translate-y-full');
if (type === 'error') {
toast.querySelector('div').classList.remove('bg-green-500');
toast.querySelector('div').classList.add('bg-red-500');
} else {
toast.querySelector('div').classList.remove('bg-red-500');
toast.querySelector('div').classList.add('bg-green-500');
}
setTimeout(() => {
toast.classList.add('translate-y-full');
}, 3000);
}
async loadRepositoryInfo(repo) {
try {
const response = await fetch(`https://api.github.com/repos/${repo}`);
const data = await response.json();
if (response.ok) {
this.showToast('Repository loaded successfully!');
// You can automatically fill some fields based on the repository data
// For example, setting the default branch, etc.
} else {
this.showToast(data.message || 'Failed to load repository!', 'error');
}
} catch (error) {
this.showToast('Failed to load repository!', 'error');
}
}
}
// Initialize the badge generator when the document is loaded
document.addEventListener('DOMContentLoaded', () => {
new BadgeGenerator();
});