"""Offline Playwright Chromium lab for jQuery EasyUI Ch 49: Create Dialog."""
import json
from playwright.sync_api import sync_playwright
HTML_DOC = r"""<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>EasyUI Dialog Offline Lab</title>
<style>
body { font-family: sans-serif; margin: 16px; font-size: 13px; }
.dialog-wrap { border: 1px solid #95b8e7; background: #e0ecff; padding: 4px; box-sizing: border-box; margin-bottom: 14px; }
.dialog-wrap.closed { display: none; }
.dialog-header { padding: 4px 6px; font-weight: bold; }
.dialog-toolbar { background: #f4f4f4; border: 1px solid #95b8e7; border-bottom: 0; padding: 4px 6px; }
.dialog-body { background: #fff; border: 1px solid #95b8e7; box-sizing: border-box; }
.dialog-button { background: #f4f4f4; border: 1px solid #95b8e7; border-top: 0; padding: 5px 6px; text-align: right; }
.l-btn { display: inline-block; padding: 2px 8px; border: 1px solid #bbb; background: #fff; text-decoration: none; color: #333; margin-left: 4px; }
.l-btn.plain { border-color: transparent; background: transparent; }
.resize-handle { height: 6px; cursor: nwse-resize; background: #cfe0f5; }
</style>
</head>
<body>
<div id="dd" class="easyui-dialog" style="padding:5px;width:400px;height:200px;"
title="My Dialog" iconCls="icon-ok" toolbar="#dlg-toolbar" buttons="#dlg-buttons">
Dialog Content.
</div>
<div id="dlg-toolbar">
<a href="#" id="tb-add" class="easyui-linkbutton" iconCls="icon-add" plain="true">Add</a>
<a href="#" id="tb-save" class="easyui-linkbutton" iconCls="icon-save" plain="true">Save</a>
</div>
<div id="dlg-buttons">
<a href="#" id="bt-ok" class="easyui-linkbutton" iconCls="icon-ok">Ok</a>
<a href="#" id="bt-cancel" class="easyui-linkbutton" iconCls="icon-cancel">Cancel</a>
</div>
<div id="dd-resize" class="easyui-dialog" style="padding:5px;width:400px;height:200px;"
title="Resizable Dialog" resizable="true">
Resizable Body.
</div>
<script>
const actionLog = [];
const dialogs = new Map();
function initDialogs() {
document.querySelectorAll(".easyui-dialog").forEach(el => {
const title = el.getAttribute("title") || "";
const iconCls = el.getAttribute("iconCls") || "";
const tbSel = el.getAttribute("toolbar");
const btSel = el.getAttribute("buttons");
const resizable = el.getAttribute("resizable") === "true";
const width = el.style.width || "400px";
const height = el.style.height || "200px";
const pad = el.style.padding || "0px";
const wrap = document.createElement("div");
wrap.className = "dialog-wrap";
wrap.dataset.for = el.id;
wrap.style.width = width;
const header = document.createElement("div");
header.className = "dialog-header";
header.dataset.iconCls = iconCls;
header.textContent = title;
el.parentNode.insertBefore(wrap, el);
wrap.appendChild(header);
let tbEl = null;
if (tbSel) {
tbEl = document.querySelector(tbSel);
if (tbEl) {
tbEl.classList.add("dialog-toolbar");
wrap.appendChild(tbEl);
}
}
el.classList.add("dialog-body");
el.style.width = "100%";
el.style.height = height;
el.style.padding = pad;
wrap.appendChild(el);
let btEl = null;
if (btSel) {
btEl = document.querySelector(btSel);
if (btEl) {
btEl.classList.add("dialog-button");
wrap.appendChild(btEl);
}
}
let handle = null;
if (resizable) {
handle = document.createElement("div");
handle.className = "resize-handle";
wrap.appendChild(handle);
}
dialogs.set(el.id, {
resizable,
wrap,
header,
tbEl,
body: el,
btEl,
handle,
close() { wrap.classList.add("closed"); },
open() { wrap.classList.remove("closed"); },
resize(w, h) {
if (!resizable) return false;
wrap.style.width = w + "px";
el.style.height = h + "px";
return true;
}
});
});
document.querySelectorAll("a.easyui-linkbutton").forEach(a => {
a.classList.add("l-btn");
if (a.getAttribute("plain") === "true") a.classList.add("plain");
});
}
initDialogs();
document.getElementById("tb-add").addEventListener("click", (e) => { e.preventDefault(); actionLog.push("Add"); });
document.getElementById("tb-save").addEventListener("click", (e) => { e.preventDefault(); actionLog.push("Save"); });
document.getElementById("bt-ok").addEventListener("click", (e) => { e.preventDefault(); actionLog.push("Ok"); });
document.getElementById("bt-cancel").addEventListener("click", (e) => { e.preventDefault(); dialogs.get("dd").close(); });
window.lab = { dialogs, actionLog };
</script>
</body>
</html>
"""
def launch_browser(p):
try:
return p.chromium.launch(headless=True)
except Exception:
return p.chromium.launch(headless=True, channel="chrome")
def main():
cases = []
with sync_playwright() as p:
browser = launch_browser(p)
page = browser.new_page()
page.set_content(HTML_DOC)
# Case 1: Standard dialog anatomy: header < toolbar < body < buttons
res1 = page.evaluate("""() => {
const d = window.lab.dialogs.get("dd");
const hTop = d.header.getBoundingClientRect().top;
const tbTop = d.tbEl.getBoundingClientRect().top;
const bTop = d.body.getBoundingClientRect().top;
const btTop = d.btEl.getBoundingClientRect().top;
return {
title: d.header.textContent,
iconCls: d.header.dataset.iconCls,
bodyText: d.body.textContent.trim(),
toolbarInsideDialog: d.wrap.contains(d.tbEl),
buttonsInsideDialog: d.wrap.contains(d.btEl),
strictTopOrder: (hTop < tbTop) && (tbTop < bTop) && (bTop < btTop)
};
}""")
exp1 = {
"title": "My Dialog",
"iconCls": "icon-ok",
"bodyText": "Dialog Content.",
"toolbarInsideDialog": True,
"buttonsInsideDialog": True,
"strictTopOrder": True,
}
cases.append(dict(name="dialog_standard_anatomy_and_selector_mount", expected=exp1, actual=res1, passed=(res1 == exp1)))
# Case 2: Toolbar & button actions fire and Cancel closes dialog
page.click("#tb-add")
page.click("#tb-save")
page.click("#bt-ok")
page.click("#bt-cancel")
res2 = page.evaluate("""() => ({
actionLog: window.lab.actionLog,
dialogHiddenAfterCancel: getComputedStyle(window.lab.dialogs.get("dd").wrap).display === "none"
})""")
exp2 = {"actionLog": ["Add", "Save", "Ok"], "dialogHiddenAfterCancel": True}
cases.append(dict(name="dialog_toolbar_and_button_actions", expected=exp2, actual=res2, passed=(res2 == exp2)))
# Case 3: Default resizable=false vs resizable=true
res3 = page.evaluate("""() => {
const d1 = window.lab.dialogs.get("dd");
const d2 = window.lab.dialogs.get("dd-resize");
d1.open();
const d1Attempt = d1.resize(480, 260);
const d2Attempt = d2.resize(480, 260);
return {
defaultResizable: d1.resizable,
defaultHasHandle: d1.handle !== null,
defaultResizeBlocked: !d1Attempt,
defaultWidthStayed: Math.round(d1.wrap.getBoundingClientRect().width),
resizableHasHandle: d2.handle !== null,
resizableUpdated: d2Attempt,
resizableNewWidth: Math.round(d2.wrap.getBoundingClientRect().width),
resizableNewBodyHeight: Math.round(d2.body.getBoundingClientRect().height)
};
}""")
exp3 = {
"defaultResizable": False,
"defaultHasHandle": False,
"defaultResizeBlocked": True,
"defaultWidthStayed": 400,
"resizableHasHandle": True,
"resizableUpdated": True,
"resizableNewWidth": 480,
"resizableNewBodyHeight": 260,
}
cases.append(dict(name="dialog_resizable_default_false_vs_true", expected=exp3, actual=res3, passed=(res3 == exp3)))
browser.close()
print(json.dumps({"cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/jeasyui-win-dlg1-lab/explore-win-dlg1.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-win-dlg1.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1024.zip