蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 EasyUI 声明式窗口、closed 延迟打开与登录表单窗口离线实验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-win-win1.py
explore-win-win1.py185 行
"""Offline Playwright Chromium lab for jQuery EasyUI Ch 46: Simple Window."""
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 Simple Window Offline Lab</title>
<style>
  body { font-family: sans-serif; margin: 16px; font-size: 13px; }
  .window-wrap { border: 1px solid #95b8e7; background: #e0ecff; padding: 4px; box-sizing: border-box; margin-bottom: 12px; display: inline-block; vertical-align: top; margin-right: 12px; }
  .window-wrap.closed { display: none; }
  .window-header { display: flex; justify-content: space-between; align-items: center; padding: 2px 4px 4px; font-weight: bold; }
  .window-body { background: #fff; border: 1px solid #95b8e7; box-sizing: border-box; }
  .l-btn { display: inline-block; padding: 3px 10px; border: 1px solid #bbb; background: #f5f5f5; text-decoration: none; color: #333; border-radius: 3px; margin: 0 4px; }
</style>
</head>
<body>
<div id="win-basic" class="easyui-window" title="My Window" style="width:300px;height:100px;padding:5px;">
  Some Content.
</div>

<div id="win-closed" class="easyui-window" title="My Window" closed="true" style="width:300px;height:100px;padding:5px;">
  Some Content.
</div>

<div id="win-login" class="easyui-window" title="Login" style="width:300px;height:180px;">
  <form id="login-form" style="padding:10px 20px 10px 40px;">
    <p>Name: <input id="login-name" type="text"></p>
    <p>Pass: <input id="login-pass" type="password"></p>
    <div style="padding:5px;text-align:center;">
      <a href="#" id="btn-ok" class="easyui-linkbutton" icon="icon-ok">Ok</a>
      <a href="#" id="btn-cancel" class="easyui-linkbutton" icon="icon-cancel">Cancel</a>
    </div>
  </form>
</div>

<script>
const instances = new Map();
const submitted = [];

function parseEasyUI() {
  document.querySelectorAll(".easyui-window").forEach(el => {
    const title = el.getAttribute("title") || "";
    const isClosed = el.getAttribute("closed") === "true";
    const w = el.style.width || "300px";
    const h = el.style.height || "100px";
    const pad = el.style.padding || "0px";

    const wrap = document.createElement("div");
    wrap.className = "window-wrap" + (isClosed ? " closed" : "");
    wrap.dataset.for = el.id;
    wrap.style.width = w;

    const header = document.createElement("div");
    header.className = "window-header";
    header.innerHTML = `<span class="panel-title">${title}</span><button type="button" class="win-close-btn">x</button>`;

    el.parentNode.insertBefore(wrap, el);
    el.classList.add("window-body");
    el.style.width = "100%";
    el.style.height = h;
    el.style.padding = pad;
    wrap.append(header, el);

    const api = {
      open() { wrap.classList.remove("closed"); },
      close() { wrap.classList.add("closed"); },
      isVisible() { return getComputedStyle(wrap).display !== "none"; },
      wrap,
      header,
      body: el
    };
    header.querySelector(".win-close-btn").addEventListener("click", () => api.close());
    instances.set(el.id, api);
  });

  document.querySelectorAll("a.easyui-linkbutton").forEach(a => {
    a.classList.add("l-btn");
    a.dataset.icon = a.getAttribute("icon") || "";
  });
}

parseEasyUI();

document.getElementById("btn-ok").addEventListener("click", (ev) => {
  ev.preventDefault();
  submitted.push({
    name: document.getElementById("login-name").value,
    passLen: document.getElementById("login-pass").value.length
  });
});
document.getElementById("btn-cancel").addEventListener("click", (ev) => {
  ev.preventDefault();
  instances.get("win-login").close();
});

window.windowMethod = (id, method) => instances.get(id)[method]();
window.lab = { instances, submitted };
</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: Declarative easyui-window renders immediately without JS code
        res1 = page.evaluate("""() => {
            const inst = window.lab.instances.get("win-basic");
            const rect = inst.wrap.getBoundingClientRect();
            return {
                visible: inst.isVisible(),
                title: inst.header.querySelector(".panel-title").textContent,
                bodyText: inst.body.textContent.trim(),
                widthPx: Math.round(rect.width),
                bodyHeightPx: Math.round(inst.body.getBoundingClientRect().height)
            };
        }""")
        exp1 = {
            "visible": True,
            "title": "My Window",
            "bodyText": "Some Content.",
            "widthPx": 300,
            "bodyHeightPx": 100,
        }
        cases.append(dict(name="declarative_simple_window_render", expected=exp1, actual=res1, passed=(res1 == exp1)))

        # Case 2: closed="true" starts hidden and opens via window('open')
        res2 = page.evaluate("""() => {
            const beforeOpen = window.windowMethod("win-closed", "isVisible");
            window.windowMethod("win-closed", "open");
            const afterOpen = window.windowMethod("win-closed", "isVisible");
            window.windowMethod("win-closed", "close");
            const afterClose = window.windowMethod("win-closed", "isVisible");
            return { beforeOpen, afterOpen, afterClose };
        }""")
        exp2 = {"beforeOpen": False, "afterOpen": True, "afterClose": False}
        cases.append(dict(name="closed_window_and_open_method", expected=exp2, actual=res2, passed=(res2 == exp2)))

        # Case 3: Login window with form inputs and easyui-linkbutton Ok/Cancel
        page.fill("#login-name", "alice")
        page.fill("#login-pass", "secret123")
        page.click("#btn-ok")
        visible_before_cancel = page.evaluate('() => window.windowMethod("win-login", "isVisible")')
        page.click("#btn-cancel")
        res3 = page.evaluate("""(visibleBeforeCancel) => ({
            title: window.lab.instances.get("win-login").header.querySelector(".panel-title").textContent,
            buttonIcons: [
                document.getElementById("btn-ok").dataset.icon,
                document.getElementById("btn-cancel").dataset.icon
            ],
            submitted: window.lab.submitted,
            visibleBeforeCancel,
            visibleAfterCancel: window.windowMethod("win-login", "isVisible")
        })""", visible_before_cancel)
        exp3 = {
            "title": "Login",
            "buttonIcons": ["icon-ok", "icon-cancel"],
            "submitted": [{"name": "alice", "passLen": 9}],
            "visibleBeforeCancel": True,
            "visibleAfterCancel": False,
        }
        cases.append(dict(name="login_window_form_and_linkbuttons", 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-win1-lab/explore-win-win1.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-win-win1.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:jQuery EasyUI

课程关联工程包(登记关联68章) · 含当前章节或实例 · mbskycom202612a964.zip

下载课程关联工程包:jQuery EasyUI

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1026.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习