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

运行 XP 风格四任务面板折叠切换与 CSS 样式覆写离线实验

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-layout-xp.py
explore-layout-xp.py181 行
"""Offline Playwright Chromium lab for jQuery EasyUI Ch 20: XP Left Panel."""
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 XP Left Panel Offline Lab</title>
<style>
  body { font-family: sans-serif; margin: 12px; }
  .xp-sidebar { width: 200px; height: auto; background: #7190E0; padding: 5px; }
  .panel { width: 200px; box-sizing: border-box; margin-bottom: 10px; border: 1px solid #95b8e7; }
  .panel-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 5px 8px; font-size: 13px; font-weight: 600; color: #1d4ed8;
    background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20'%3E%3Crect width='20' height='20' fill='%23dbeafe'/%3E%3C/svg%3E") no-repeat top right;
  }
  .panel-body { background: #f0f0f0; padding: 10px; font-size: 12px; line-height: 1.6; }
  .panel-body.collapsed { display: none; }
  .panel-tool-btn { width: 16px; height: 16px; border: none; cursor: pointer; display: inline-block; }
  .panel-tool-collapse {
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16'%3E%3Cpath d='M3 10l5-5 5 5' stroke='%231d4ed8' fill='none'/%3E%3C/svg%3E") no-repeat 0px -3px;
  }
  .panel-tool-expand {
    background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16'%3E%3Cpath d='M3 6l5 5 5-5' stroke='%231d4ed8' fill='none'/%3E%3C/svg%3E") no-repeat 0px -3px;
  }
</style>
</head>
<body>
<div id="xp-wrap" class="xp-sidebar">
  <div class="easyui-panel" title="Picture Tasks" collapsible="true">
    View as a slide show<br/>Order prints online<br/>Print pictures
  </div>
  <div class="easyui-panel" title="File and Folder Tasks" collapsible="true">
    Make a new folder<br/>Publish this folder to the Web<br/>Share this folder
  </div>
  <div class="easyui-panel" title="Other Places" collapsible="true" collapsed="true">
    New York<br/>My Pictures<br/>My Computer<br/>My Network Places
  </div>
  <div class="easyui-panel" title="Details" collapsible="true">
    My documents<br/>File folder<br/><br/>Date modified: Oct.3rd 2010
  </div>
</div>
<script>
(function(){
  const wrap = document.getElementById('xp-wrap');
  const rawPanels = Array.from(wrap.querySelectorAll('.easyui-panel'));
  rawPanels.forEach((src, idx) => {
    const title = src.getAttribute('title');
    const collapsible = src.getAttribute('collapsible') === 'true';
    const collapsed = src.getAttribute('collapsed') === 'true';
    const panel = document.createElement('div');
    panel.className = 'panel';
    panel.dataset.panelTitle = title;
    const header = document.createElement('div');
    header.className = 'panel-header';
    const titleSpan = document.createElement('span');
    titleSpan.textContent = title;
    header.appendChild(titleSpan);
    const body = document.createElement('div');
    body.className = 'panel-body' + (collapsed ? ' collapsed' : '');
    body.innerHTML = src.innerHTML;
    if (collapsible) {
      const btn = document.createElement('button');
      btn.type = 'button';
      btn.id = 'toggle-' + idx;
      btn.className = 'panel-tool-btn ' + (collapsed ? 'panel-tool-expand' : 'panel-tool-collapse');
      btn.addEventListener('click', () => {
        const nowCollapsed = body.classList.toggle('collapsed');
        btn.classList.toggle('panel-tool-expand', nowCollapsed);
        btn.classList.toggle('panel-tool-collapse', !nowCollapsed);
      });
      header.appendChild(btn);
    }
    panel.appendChild(header);
    panel.appendChild(body);
    src.replaceWith(panel);
  });
})();
</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(viewport={"width": 500, "height": 600})
        page.set_content(HTML_DOC)

        # Case 1: Four XP panels initial collapsed/expanded state & sidebar container
        res1 = page.evaluate("""() => {
            const wrapStyle = getComputedStyle(document.getElementById('xp-wrap'));
            const panels = Array.from(document.querySelectorAll('.panel')).map(el => ({
                title: el.dataset.panelTitle,
                bodyVisible: getComputedStyle(el.querySelector('.panel-body')).display !== 'none',
                toolClass: el.querySelector('.panel-tool-btn').classList.contains('panel-tool-expand') ? 'panel-tool-expand' : 'panel-tool-collapse',
            }));
            return {
                sidebarBg: wrapStyle.backgroundColor,
                sidebarWidth: wrapStyle.width,
                panelCount: panels.length,
                panels,
            };
        }""")
        exp1 = {
            "sidebarBg": "rgb(113, 144, 224)",
            "sidebarWidth": "200px",
            "panelCount": 4,
            "panels": [
                {"title": "Picture Tasks", "bodyVisible": True, "toolClass": "panel-tool-collapse"},
                {"title": "File and Folder Tasks", "bodyVisible": True, "toolClass": "panel-tool-collapse"},
                {"title": "Other Places", "bodyVisible": False, "toolClass": "panel-tool-expand"},
                {"title": "Details", "bodyVisible": True, "toolClass": "panel-tool-collapse"},
            ],
        }
        cases.append(dict(name="four_xp_panels_initial_state", expected=exp1, actual=res1, passed=(res1 == exp1)))

        # Case 2: Toggle Picture Tasks collapse & Other Places expand
        page.click("#toggle-0")
        page.click("#toggle-2")
        res2 = page.evaluate("""() => {
            const p0 = document.querySelector('.panel[data-panel-title="Picture Tasks"]');
            const p2 = document.querySelector('.panel[data-panel-title="Other Places"]');
            return {
                pictureTasksVisible: getComputedStyle(p0.querySelector('.panel-body')).display !== 'none',
                pictureTasksIcon: p0.querySelector('.panel-tool-btn').classList.contains('panel-tool-expand') ? 'panel-tool-expand' : 'panel-tool-collapse',
                otherPlacesVisible: getComputedStyle(p2.querySelector('.panel-body')).display !== 'none',
                otherPlacesIcon: p2.querySelector('.panel-tool-btn').classList.contains('panel-tool-collapse') ? 'panel-tool-collapse' : 'panel-tool-expand',
                otherPlacesItems: p2.querySelector('.panel-body').innerText.split('\\n').map(s => s.trim()).filter(Boolean),
            };
        }""")
        exp2 = {
            "pictureTasksVisible": False,
            "pictureTasksIcon": "panel-tool-expand",
            "otherPlacesVisible": True,
            "otherPlacesIcon": "panel-tool-collapse",
            "otherPlacesItems": ["New York", "My Pictures", "My Computer", "My Network Places"],
        }
        cases.append(dict(name="toggle_collapse_and_expand_interaction", expected=exp2, actual=res2, passed=(res2 == exp2)))

        # Case 3: Custom XP CSS overrides on panel-body, panel-header, tool icons
        res3 = page.evaluate("""() => {
            const bodyStyle = getComputedStyle(document.querySelector('.panel-body:not(.collapsed)'));
            const headerStyle = getComputedStyle(document.querySelector('.panel-header'));
            const collapseStyle = getComputedStyle(document.querySelector('.panel-tool-collapse'));
            const expandStyle = getComputedStyle(document.querySelector('.panel-tool-expand'));
            return {
                bodyBg: bodyStyle.backgroundColor,
                headerBgColor: headerStyle.backgroundColor,
                headerBgRepeat: headerStyle.backgroundRepeat,
                collapsePos: collapseStyle.backgroundPosition,
                expandPos: expandStyle.backgroundPosition,
            };
        }""")
        exp3 = {
            "bodyBg": "rgb(240, 240, 240)",
            "headerBgColor": "rgb(255, 255, 255)",
            "headerBgRepeat": "no-repeat",
            "collapsePos": "0px -3px",
            "expandPos": "0px -3px",
        }
        cases.append(dict(name="custom_xp_css_theme_override", 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-layout-xp-lab/explore-layout-xp.py

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

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

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

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

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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