| 1 |
<!DOCTYPE html> |
| 2 |
<html lang="en"> |
| 3 |
|
| 4 |
<head> |
| 5 |
<meta charset="UTF-8"> |
| 6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 7 |
<title>Mithril.js Glaze Demo</title> |
| 8 |
|
| 9 |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css"> |
| 10 |
<script src="https://unpkg.com/mithril/mithril.js"></script> |
| 11 |
|
| 12 |
<style> |
| 13 |
.loading { |
| 14 |
opacity: 0.7; |
| 15 |
pointer-events: none; |
| 16 |
position: relative; |
| 17 |
} |
| 18 |
|
| 19 |
.loading::after { |
| 20 |
content: ""; |
| 21 |
box-sizing: border-box; |
| 22 |
position: absolute; |
| 23 |
top: 50%; |
| 24 |
left: 50%; |
| 25 |
width: 20px; |
| 26 |
height: 20px; |
| 27 |
margin-top: -10px; |
| 28 |
margin-left: -10px; |
| 29 |
border-radius: 50%; |
| 30 |
border: 2px solid #dbdbdb; |
| 31 |
border-top-color: #3273dc; |
| 32 |
animation: spin 0.6s linear infinite; |
| 33 |
} |
| 34 |
|
| 35 |
@keyframes spin { |
| 36 |
to { |
| 37 |
transform: rotate(360deg); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
.hero-gradient { |
| 42 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); |
| 43 |
} |
| 44 |
|
| 45 |
.user-avatar { |
| 46 |
font-size: 2rem; |
| 47 |
} |
| 48 |
|
| 49 |
.stats-grid { |
| 50 |
display: grid; |
| 51 |
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); |
| 52 |
gap: 1rem; |
| 53 |
} |
| 54 |
|
| 55 |
.error-display { |
| 56 |
margin-top: 1rem; |
| 57 |
} |
| 58 |
|
| 59 |
.error-content.notification { |
| 60 |
margin-bottom: 0; |
| 61 |
} |
| 62 |
</style> |
| 63 |
</head> |
| 64 |
|
| 65 |
<body> |
| 66 |
<div id="app"></div> |
| 67 |
|
| 68 |
<script> |
| 69 |
// Global State |
| 70 |
const State = { |
| 71 |
users: [], |
| 72 |
posts: [], |
| 73 |
requestCount: 0, |
| 74 |
loading: { |
| 75 |
users: false, |
| 76 |
posts: false, |
| 77 |
addUser: false, |
| 78 |
findUser: false, |
| 79 |
addPost: false |
| 80 |
}, |
| 81 |
errors: { |
| 82 |
users: null, |
| 83 |
addUser: null, |
| 84 |
findUser: null, |
| 85 |
posts: null, |
| 86 |
addPost: null |
| 87 |
}, |
| 88 |
foundUser: null, |
| 89 |
performance: {} |
| 90 |
}; |
| 91 |
|
| 92 |
// Utility functions |
| 93 |
const incrementRequestCount = () => { |
| 94 |
State.requestCount++; |
| 95 |
}; |
| 96 |
|
| 97 |
const setLoading = (key, value) => { |
| 98 |
State.loading[key] = value; |
| 99 |
m.redraw(); |
| 100 |
}; |
| 101 |
|
| 102 |
const setError = (key, error) => { |
| 103 |
State.errors[key] = error; |
| 104 |
m.redraw(); |
| 105 |
}; |
| 106 |
|
| 107 |
const clearError = (key) => { |
| 108 |
State.errors[key] = null; |
| 109 |
}; |
| 110 |
|
| 111 |
// API functions |
| 112 |
const api = { |
| 113 |
getAllUsers: () => { |
| 114 |
setLoading('users', true); |
| 115 |
clearError('users'); |
| 116 |
return m.request({ |
| 117 |
method: 'GET', |
| 118 |
url: '/api/getAllUsers' |
| 119 |
}).then(data => { |
| 120 |
State.users = data; |
| 121 |
incrementRequestCount(); |
| 122 |
setLoading('users', false); |
| 123 |
return data; |
| 124 |
}).catch(error => { |
| 125 |
setError('users', 'Failed to load users'); |
| 126 |
setLoading('users', false); |
| 127 |
incrementRequestCount(); |
| 128 |
throw error; |
| 129 |
}); |
| 130 |
}, |
| 131 |
|
| 132 |
createUser: (userData) => { |
| 133 |
setLoading('addUser', true); |
| 134 |
clearError('addUser'); |
| 135 |
return m.request({ |
| 136 |
method: 'POST', |
| 137 |
url: '/api/createUser', |
| 138 |
body: userData |
| 139 |
}).then(newUser => { |
| 140 |
State.users.unshift(newUser); |
| 141 |
incrementRequestCount(); |
| 142 |
setLoading('addUser', false); |
| 143 |
return newUser; |
| 144 |
}).catch(error => { |
| 145 |
setError('addUser', 'Could not add user. Please check inputs.'); |
| 146 |
setLoading('addUser', false); |
| 147 |
incrementRequestCount(); |
| 148 |
throw error; |
| 149 |
}); |
| 150 |
}, |
| 151 |
|
| 152 |
getUserById: (id) => { |
| 153 |
setLoading('findUser', true); |
| 154 |
clearError('findUser'); |
| 155 |
return m.request({ |
| 156 |
method: 'POST', |
| 157 |
url: '/api/getUserById', |
| 158 |
body: { id: parseInt(id) } |
| 159 |
}).then(user => { |
| 160 |
State.foundUser = user; |
| 161 |
incrementRequestCount(); |
| 162 |
setLoading('findUser', false); |
| 163 |
return user; |
| 164 |
}).catch(error => { |
| 165 |
setError('findUser', 'Error finding user.'); |
| 166 |
State.foundUser = null; |
| 167 |
setLoading('findUser', false); |
| 168 |
incrementRequestCount(); |
| 169 |
throw error; |
| 170 |
}); |
| 171 |
}, |
| 172 |
|
| 173 |
updateUser: (userId, updates) => { |
| 174 |
return m.request({ |
| 175 |
method: 'POST', |
| 176 |
url: '/api/updateUser', |
| 177 |
body: { |
| 178 |
id: parseInt(userId), |
| 179 |
...updates // Spread the updates into the body |
| 180 |
} |
| 181 |
}).then(updatedUser => { |
| 182 |
const index = State.users.findIndex(u => u.id == userId); |
| 183 |
if (index !== -1) { |
| 184 |
State.users[index] = updatedUser; |
| 185 |
} |
| 186 |
incrementRequestCount(); |
| 187 |
return updatedUser; |
| 188 |
}).catch(error => { |
| 189 |
incrementRequestCount(); |
| 190 |
throw error; |
| 191 |
}); |
| 192 |
}, |
| 193 |
|
| 194 |
deleteUser: (userId) => { |
| 195 |
return m.request({ |
| 196 |
method: 'POST', |
| 197 |
url: '/api/deleteUser', |
| 198 |
body: { id: parseInt(userId) } |
| 199 |
}).then(() => { |
| 200 |
State.users = State.users.filter(u => u.id != userId); |
| 201 |
incrementRequestCount(); |
| 202 |
}).catch(error => { |
| 203 |
incrementRequestCount(); |
| 204 |
throw error; |
| 205 |
}); |
| 206 |
}, |
| 207 |
|
| 208 |
getAllPosts: () => { |
| 209 |
setLoading('posts', true); |
| 210 |
clearError('posts'); |
| 211 |
return m.request({ |
| 212 |
method: 'GET', |
| 213 |
url: '/api/getAllPosts' |
| 214 |
}).then(data => { |
| 215 |
State.posts = data; |
| 216 |
incrementRequestCount(); |
| 217 |
setLoading('posts', false); |
| 218 |
return data; |
| 219 |
}).catch(error => { |
| 220 |
setError('posts', 'Could not load posts initially.'); |
| 221 |
setLoading('posts', false); |
| 222 |
incrementRequestCount(); |
| 223 |
throw error; |
| 224 |
}); |
| 225 |
}, |
| 226 |
|
| 227 |
createPost: (postData) => { |
| 228 |
setLoading('addPost', true); |
| 229 |
clearError('addPost'); |
| 230 |
return m.request({ |
| 231 |
method: 'POST', |
| 232 |
url: '/api/createPost', |
| 233 |
body: postData |
| 234 |
}).then(newPost => { |
| 235 |
State.posts.unshift(newPost); |
| 236 |
incrementRequestCount(); |
| 237 |
setLoading('addPost', false); |
| 238 |
return newPost; |
| 239 |
}).catch(error => { |
| 240 |
setError('addPost', 'Failed to publish post.'); |
| 241 |
setLoading('addPost', false); |
| 242 |
incrementRequestCount(); |
| 243 |
throw error; |
| 244 |
}); |
| 245 |
} |
| 246 |
}; |
| 247 |
|
| 248 |
// Components |
| 249 |
const HeroSection = { |
| 250 |
view: () => m('section.hero.is-primary.hero-gradient', |
| 251 |
m('.hero-body', |
| 252 |
m('.container', |
| 253 |
m('h1.title.is-1', '🚀 Mithril.js Demo'), |
| 254 |
m('p.subtitle.is-4', 'Interactive demo showcasing Mithril.js frontend with Glaze C++ REST backend') |
| 255 |
) |
| 256 |
) |
| 257 |
) |
| 258 |
}; |
| 259 |
|
| 260 |
const StatsSection = { |
| 261 |
view: () => m('section.section', |
| 262 |
m('.container', |
| 263 |
m('.stats-grid', [ |
| 264 |
m('.box.has-text-centered', [ |
| 265 |
m('p.title.is-2', State.users.length || '-'), |
| 266 |
m('p.subtitle', 'Users') |
| 267 |
]), |
| 268 |
m('.box.has-text-centered', [ |
| 269 |
m('p.title.is-2', State.posts.length || '-'), |
| 270 |
m('p.subtitle', 'Posts') |
| 271 |
]), |
| 272 |
m('.box.has-text-centered', [ |
| 273 |
m('p.title.is-2', State.requestCount), |
| 274 |
m('p.subtitle', 'Requests') |
| 275 |
]) |
| 276 |
]) |
| 277 |
) |
| 278 |
) |
| 279 |
}; |
| 280 |
|
| 281 |
const UserCard = { |
| 282 |
view: (vnode) => { |
| 283 |
const user = vnode.attrs.user; |
| 284 |
return m('.box.mb-3.user-card-item', [ |
| 285 |
m('.level', [ |
| 286 |
m('.level-left', [ |
| 287 |
m('.level-item', m('span.user-avatar', user.avatar || '👤')), |
| 288 |
m('.level-item', |
| 289 |
m('div', [ |
| 290 |
m('p.title.is-6', user.name || 'N/A'), |
| 291 |
m('p.subtitle.is-7', user.email || 'N/A'), |
| 292 |
m('p.is-size-7.has-text-grey', `ID: ${user.id || 'New'}`) |
| 293 |
]) |
| 294 |
) |
| 295 |
]), |
| 296 |
user.id ? m('.level-right', |
| 297 |
m('.level-item', |
| 298 |
m('.buttons', [ |
| 299 |
m('button.button.is-small.is-warning', { |
| 300 |
onclick: () => { |
| 301 |
api.updateUser(user.id, { avatar: '🎭', id: user.id }) |
| 302 |
.catch(() => { }); // Error handling could be added here |
| 303 |
} |
| 304 |
}, 'Update Avatar'), |
| 305 |
m('button.button.is-small.is-danger', { |
| 306 |
onclick: () => { |
| 307 |
api.deleteUser(user.id) |
| 308 |
.catch(() => { }); // Error handling could be added here |
| 309 |
} |
| 310 |
}, 'Delete') |
| 311 |
]) |
| 312 |
) |
| 313 |
) : null |
| 314 |
]) |
| 315 |
]); |
| 316 |
} |
| 317 |
}; |
| 318 |
|
| 319 |
const UsersSection = { |
| 320 |
view: () => m('.column.is-half', |
| 321 |
m('.box', [ |
| 322 |
m('h2.title.is-4', '👥 Users Management'), |
| 323 |
m('button.button.is-primary.mb-4', { |
| 324 |
class: State.loading.users ? 'loading' : '', |
| 325 |
onclick: () => api.getAllUsers() |
| 326 |
}, 'Load Users'), |
| 327 |
m('#usersList.content', |
| 328 |
State.users.length > 0 |
| 329 |
? State.users.map(user => m(UserCard, { user, key: user.id })) |
| 330 |
: m('p.has-text-grey', 'Users will load automatically...') |
| 331 |
), |
| 332 |
State.errors.users ? m('.error-display', |
| 333 |
m('.error-content.notification.is-danger', State.errors.users) |
| 334 |
) : null |
| 335 |
]) |
| 336 |
) |
| 337 |
}; |
| 338 |
|
| 339 |
const AddUserSection = { |
| 340 |
view: () => { |
| 341 |
let formData = { name: '', email: '', avatar: '👤' }; |
| 342 |
|
| 343 |
return m('.column.is-half', |
| 344 |
m('.box', [ |
| 345 |
m('h2.title.is-4', '➕ Add New User'), |
| 346 |
m('form', { |
| 347 |
onsubmit: (e) => { |
| 348 |
e.preventDefault(); |
| 349 |
const form = e.target; |
| 350 |
const data = { |
| 351 |
name: form.name.value, |
| 352 |
email: form.email.value, |
| 353 |
avatar: form.avatar.value |
| 354 |
}; |
| 355 |
api.createUser(data).then(() => { |
| 356 |
form.reset(); |
| 357 |
}); |
| 358 |
}, |
| 359 |
class: State.loading.addUser ? 'loading' : '' |
| 360 |
}, [ |
| 361 |
m('.field', [ |
| 362 |
m('label.label[for=newUserName]', 'Name:'), |
| 363 |
m('.control', |
| 364 |
m('input.input[type=text][name=name][required]') |
| 365 |
) |
| 366 |
]), |
| 367 |
m('.field', [ |
| 368 |
m('label.label[for=newUserEmail]', 'Email:'), |
| 369 |
m('.control', |
| 370 |
m('input.input[type=email][name=email][required]') |
| 371 |
) |
| 372 |
]), |
| 373 |
m('.field', [ |
| 374 |
m('label.label[for=newUserAvatar]', 'Avatar:'), |
| 375 |
m('.control', |
| 376 |
m('.select.is-fullwidth', |
| 377 |
m('select[name=avatar]', [ |
| 378 |
m('option[value=👤]', '👤 Default'), |
| 379 |
m('option[value=👨💻]', '👨💻 Developer'), |
| 380 |
m('option[value=👩💻]', '👩💻 Developer'), |
| 381 |
m('option[value=👨💼]', '👨💼 Business'), |
| 382 |
m('option[value=👩💼]', '👩💼 Business'), |
| 383 |
m('option[value=👨🎨]', '👨🎨 Artist'), |
| 384 |
m('option[value=👩🎨]', '👩🎨 Artist'), |
| 385 |
m('option[value=🧑🚀]', '🧑🚀 Astronaut') |
| 386 |
]) |
| 387 |
) |
| 388 |
) |
| 389 |
]), |
| 390 |
m('.field', |
| 391 |
m('.control', |
| 392 |
m('button.button.is-success.is-fullwidth[type=submit]', 'Add User') |
| 393 |
) |
| 394 |
) |
| 395 |
]), |
| 396 |
State.errors.addUser ? m('.error-display', |
| 397 |
m('.error-content.notification.is-danger', State.errors.addUser) |
| 398 |
) : null |
| 399 |
]) |
| 400 |
); |
| 401 |
} |
| 402 |
}; |
| 403 |
|
| 404 |
const FindUserSection = { |
| 405 |
view: () => m('.column.is-half', |
| 406 |
m('.box', [ |
| 407 |
m('h2.title.is-4', '🔍 Find User by ID'), |
| 408 |
m('form', { |
| 409 |
onsubmit: (e) => { |
| 410 |
e.preventDefault(); |
| 411 |
const id = e.target.id.value; |
| 412 |
if (id) { |
| 413 |
api.getUserById(id); |
| 414 |
} |
| 415 |
} |
| 416 |
}, [ |
| 417 |
m('.field', [ |
| 418 |
m('label.label[for=searchUserId]', 'User ID:'), |
| 419 |
m('.control', |
| 420 |
m('input.input[type=number][name=id][placeholder="Enter user ID (1, 2, 3...)"][required]') |
| 421 |
) |
| 422 |
]), |
| 423 |
m('.field', |
| 424 |
m('.control', |
| 425 |
m('button.button.is-info.is-fullwidth[type=submit]', { |
| 426 |
class: State.loading.findUser ? 'loading' : '' |
| 427 |
}, 'Search User') |
| 428 |
) |
| 429 |
) |
| 430 |
]), |
| 431 |
m('#userResult.mt-2', |
| 432 |
State.foundUser && State.foundUser.id ? |
| 433 |
m('.box.mt-3', |
| 434 |
m('.level', [ |
| 435 |
m('.level-left', [ |
| 436 |
m('.level-item', m('span.user-avatar', State.foundUser.avatar || '👤')), |
| 437 |
m('.level-item', |
| 438 |
m('div', [ |
| 439 |
m('p.title.is-6', State.foundUser.name), |
| 440 |
m('p.subtitle.is-7', State.foundUser.email), |
| 441 |
m('p.is-size-7.has-text-grey', `ID: ${State.foundUser.id}`) |
| 442 |
]) |
| 443 |
) |
| 444 |
]) |
| 445 |
]) |
| 446 |
) : |
| 447 |
State.foundUser && State.foundUser.message ? |
| 448 |
m('.notification.is-warning.mt-3', State.foundUser.message) : |
| 449 |
State.foundUser === null && !State.loading.findUser ? |
| 450 |
m('.notification.is-light.mt-3', 'User details will appear here if found.') : null |
| 451 |
), |
| 452 |
State.errors.findUser ? m('.error-display', |
| 453 |
m('.error-content.notification.is-danger', State.errors.findUser) |
| 454 |
) : null |
| 455 |
]) |
| 456 |
) |
| 457 |
}; |
| 458 |
|
| 459 |
const PostCard = { |
| 460 |
view: (vnode) => { |
| 461 |
const post = vnode.attrs.post; |
| 462 |
return m('article.box.mb-3.post-card-item', |
| 463 |
m('.content', [ |
| 464 |
m('p.is-size-7.has-text-grey.mb-2', [ |
| 465 |
'By ', m('strong', post.author || 'Unknown'), ' • ', |
| 466 |
m('em', post.createdAt || 'some time ago') |
| 467 |
]), |
| 468 |
m('h4.title.is-5', post.title || 'Untitled Post'), |
| 469 |
m('p', post.body || '[No content]') |
| 470 |
]) |
| 471 |
); |
| 472 |
} |
| 473 |
}; |
| 474 |
|
| 475 |
const PostsSection = { |
| 476 |
view: () => m('.column.is-half', |
| 477 |
m('.box', [ |
| 478 |
m('h2.title.is-4', '📝 Blog Posts'), |
| 479 |
m('button.button.is-primary.mb-4', { |
| 480 |
class: State.loading.posts ? 'loading' : '', |
| 481 |
onclick: () => api.getAllPosts() |
| 482 |
}, 'Load Posts'), |
| 483 |
m('#postsList.content', |
| 484 |
State.posts.length > 0 |
| 485 |
? State.posts.map(post => m(PostCard, { post, key: post.id })) |
| 486 |
: m('p.has-text-grey', 'Posts will load automatically...') |
| 487 |
), |
| 488 |
State.errors.posts ? m('.error-display', |
| 489 |
m('.error-content.notification.is-danger', State.errors.posts) |
| 490 |
) : null |
| 491 |
]) |
| 492 |
) |
| 493 |
}; |
| 494 |
|
| 495 |
const AddPostSection = { |
| 496 |
view: () => m('.column.is-half', |
| 497 |
m('.box', [ |
| 498 |
m('h2.title.is-4', '✍️ Write New Post'), |
| 499 |
m('form', { |
| 500 |
onsubmit: (e) => { |
| 501 |
e.preventDefault(); |
| 502 |
const form = e.target; |
| 503 |
const data = { |
| 504 |
title: form.title.value, |
| 505 |
body: form.body.value, |
| 506 |
author: form.author.value |
| 507 |
}; |
| 508 |
api.createPost(data).then(() => { |
| 509 |
form.reset(); |
| 510 |
}); |
| 511 |
}, |
| 512 |
class: State.loading.addPost ? 'loading' : '' |
| 513 |
}, [ |
| 514 |
m('.field', [ |
| 515 |
m('label.label[for=postTitle]', 'Title:'), |
| 516 |
m('.control', |
| 517 |
m('input.input[type=text][name=title][required]') |
| 518 |
) |
| 519 |
]), |
| 520 |
m('.field', [ |
| 521 |
m('label.label[for=postBody]', 'Content:'), |
| 522 |
m('.control', |
| 523 |
m('textarea.textarea[name=body][required]') |
| 524 |
) |
| 525 |
]), |
| 526 |
m('.field', [ |
| 527 |
m('label.label[for=postAuthor]', 'Author:'), |
| 528 |
m('.control', |
| 529 |
m('input.input[type=text][name=author][required]') |
| 530 |
) |
| 531 |
]), |
| 532 |
m('.field', |
| 533 |
m('.control', |
| 534 |
m('button.button.is-success.is-fullwidth[type=submit]', 'Publish Post') |
| 535 |
) |
| 536 |
) |
| 537 |
]), |
| 538 |
State.errors.addPost ? m('.error-display', |
| 539 |
m('.error-content.notification.is-danger', State.errors.addPost) |
| 540 |
) : null |
| 541 |
]) |
| 542 |
) |
| 543 |
}; |
| 544 |
|
| 545 |
const PerformanceSection = { |
| 546 |
view: () => m('.column.is-full', |
| 547 |
m('.box', [ |
| 548 |
m('h2.title.is-4', '📊 Performance Metrics'), |
| 549 |
m('button.button.is-link', { |
| 550 |
onclick: () => { |
| 551 |
// Simple performance display |
| 552 |
const results = document.getElementById('performanceResults'); |
| 553 |
if (results) { |
| 554 |
results.innerHTML = ` |
| 555 |
<div class="content has-text-left"> |
| 556 |
<h4 class="subtitle is-6">Request Statistics:</h4> |
| 557 |
<p>Total Requests Made: ${State.requestCount}</p> |
| 558 |
<p>Users Loaded: ${State.users.length}</p> |
| 559 |
<p>Posts Loaded: ${State.posts.length}</p> |
| 560 |
<p class="has-text-grey">Detailed performance metrics would be implemented with actual timing measurements.</p> |
| 561 |
</div> |
| 562 |
`; |
| 563 |
} |
| 564 |
} |
| 565 |
}, 'Show Performance'), |
| 566 |
m('#performanceResults.mt-3') |
| 567 |
]) |
| 568 |
) |
| 569 |
}; |
| 570 |
|
| 571 |
const MainSection = { |
| 572 |
view: () => m('section.section', |
| 573 |
m('.container', |
| 574 |
m('.columns.is-multiline', [ |
| 575 |
m(UsersSection), |
| 576 |
m(AddUserSection), |
| 577 |
m(FindUserSection), |
| 578 |
m(PostsSection), |
| 579 |
m(AddPostSection), |
| 580 |
m(PerformanceSection) |
| 581 |
]) |
| 582 |
) |
| 583 |
) |
| 584 |
}; |
| 585 |
|
| 586 |
const App = { |
| 587 |
oninit: () => { |
| 588 |
// Auto-load data on app initialization |
| 589 |
setTimeout(() => api.getAllUsers(), 200); |
| 590 |
setTimeout(() => api.getAllPosts(), 500); |
| 591 |
}, |
| 592 |
view: () => [ |
| 593 |
m(HeroSection), |
| 594 |
m(StatsSection), |
| 595 |
m(MainSection) |
| 596 |
] |
| 597 |
}; |
| 598 |
|
| 599 |
// Mount the app |
| 600 |
m.mount(document.getElementById('app'), App); |
| 601 |
</script> |
| 602 |
</body> |
| 603 |
|
| 604 |
</html> |