refactor(js): 提炼toast通用函数并保留toastSuccess封装

This commit is contained in:
萝卜
2026-03-17 17:17:27 +08:00
parent 0d3096d075
commit e93fda474b
2 changed files with 32 additions and 3 deletions

View File

@@ -546,7 +546,7 @@
});
}
function toastSuccess(text) {
function toast(type, text, ttlMs) {
try {
var container = qs('[data-role="toast-container"]');
if (!container) {
@@ -554,7 +554,7 @@
}
var t = document.createElement('div');
t.className = 'toast toast-success';
t.className = 'toast toast-' + String(type || 'info');
t.setAttribute('role', 'status');
var c = document.createElement('div');
@@ -564,11 +564,16 @@
t.appendChild(c);
container.appendChild(t);
var ttl = Number(ttlMs || 2500);
if (!isFinite(ttl) || ttl <= 0) {
ttl = 2500;
}
setTimeout(function () {
try {
container.removeChild(t);
} catch (e) {}
}, 2500);
}, ttl);
return true;
} catch (e) {
@@ -576,6 +581,10 @@
}
}
function toastSuccess(text) {
return toast('success', text, 2500);
}
// 通用:按钮短暂反馈(已复制/复制失败)并自动恢复
// 说明:用于复制 run_id / 复制治理链接,避免两套口径漂移。
function tempButtonFeedback(btn, ok, origAttr) {

View File

@@ -0,0 +1,20 @@
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class AdminJsToastHelperShouldExistTest extends TestCase
{
use RefreshDatabase;
public function test_admin_js_should_include_toast_helper_and_toast_success_wrapper(): void
{
$js = file_get_contents(public_path('js/admin.js'));
$this->assertIsString($js);
$this->assertStringContainsString('function toast(', $js);
$this->assertStringContainsString("toast('success'", $js);
}
}