From e726abcddb733fe847d5f8de5f31c44c6dad6729 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 22 Apr 2026 17:04:54 +0800 Subject: [PATCH] =?UTF-8?q?Refactor=20UI=20files=20to=20use=20registerfunc?= =?UTF-8?q?tion=20instead=20of=20inline=20script,=20following=20bricks-fra?= =?UTF-8?q?mework=E7=90=86=E5=BF=B5:=20minimize=20JS=20usage=20and=20use?= =?UTF-8?q?=20existing=20controls=20with=20auxiliary=20validation=20functi?= =?UTF-8?q?ons?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wwwroot/add_service.ui | 8 ++++-- wwwroot/edit_service.ui | 9 +++++-- wwwroot/services.js | 57 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/wwwroot/add_service.ui b/wwwroot/add_service.ui index 4a222e6..3395f5c 100644 --- a/wwwroot/add_service.ui +++ b/wwwroot/add_service.ui @@ -68,8 +68,12 @@ { "wid": "self", "event": "click", - "actiontype": "script", - "script": "const formData = bricks.app.get_widget('add-service-form').get_data(); if (formData.name && formData.url) { fetch('/hermes-web-cli/services', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData) }).then(response => response.json()).then(data => { bricks.app.load_widget('{{entire_url(\"services.ui\")}}', 'app.main-content', 'replace'); }); }" + "actiontype": "registerfunction", + "target": "app.main-content", + "rfname": "add_service", + "params": { + "form_data": "{{add-service-form.data}}" + } } ] }, diff --git a/wwwroot/edit_service.ui b/wwwroot/edit_service.ui index a9e8e90..ecef95e 100644 --- a/wwwroot/edit_service.ui +++ b/wwwroot/edit_service.ui @@ -69,8 +69,13 @@ { "wid": "self", "event": "click", - "actiontype": "script", - "script": "const formData = bricks.app.get_widget('edit-service-form').get_data(); const serviceId = '{{query.service_id}}'; if (formData.name && formData.url) { fetch(`/hermes-web-cli/hermes_services/?id=${serviceId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(formData) }).then(() => { bricks.app.load_widget('{{entire_url(\"services.ui\")}}', 'app.main-content', 'replace'); }); }" + "actiontype": "registerfunction", + "target": "app.main-content", + "rfname": "update_service", + "params": { + "form_data": "{{edit-service-form.data}}", + "service_id": "{{query.service_id}}" + } } ] }, diff --git a/wwwroot/services.js b/wwwroot/services.js index ad15c80..2d05166 100644 --- a/wwwroot/services.js +++ b/wwwroot/services.js @@ -1,4 +1,61 @@ // Register functions for services management +bricks.RF.register('add_service', async function(params) { + try { + const formData = params.form_data || {}; + + const response = await fetch('/hermes-web-cli/services', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(formData) + }); + const result = await response.json(); + + if (result.success) { + // Navigate back to services list + const mainContent = bricks.getWidgetById('app.main-content'); + if (mainContent) { + mainContent.loadURL(bricks.entire_url('services.ui')); + } + bricks.showMessage('Service added successfully', 'success'); + } else { + bricks.showMessage(result.error || 'Failed to add service', 'error'); + } + } catch (error) { + bricks.showMessage('Network error: ' + error.message, 'error'); + } +}); + +bricks.RF.register('update_service', async function(params) { + try { + const formData = params.form_data || {}; + const serviceId = params.service_id; + + const response = await fetch(`/hermes-web-cli/hermes_services/?id=${serviceId}`, { + method: 'PUT', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify(formData) + }); + const result = await response.json(); + + if (result.success) { + // Navigate back to services list + const mainContent = bricks.getWidgetById('app.main-content'); + if (mainContent) { + mainContent.loadURL(bricks.entire_url('services.ui')); + } + bricks.showMessage('Service updated successfully', 'success'); + } else { + bricks.showMessage(result.error || 'Failed to update service', 'error'); + } + } catch (error) { + bricks.showMessage('Network error: ' + error.message, 'error'); + } +}); + bricks.RF.register('remove_service', async function(params) { try { const response = await fetch('/hermes-web-cli/services/remove/?id=' + params.service_id);