"""Offline Ionic 1 $ionicLoading & $ionicLoadingConfig service verifier."""
import json
from lxml import html
class IonicLoadingService:
"""Models $ionicLoading.show(opts) / hide() with $ionicLoadingConfig defaults."""
DEFAULTS = {
"template": "Loading...",
"templateUrl": None,
"noBackdrop": False,
"delay": 0,
"duration": None,
}
def __init__(self, config_defaults=None):
self.defaults = dict(self.DEFAULTS)
if config_defaults:
self.defaults.update(config_defaults)
self.visible = False
self.active_opts = None
self.history = []
def show(self, opts=None):
merged = dict(self.defaults)
raw = dict(opts or {})
# Map Ionic 1.0 beta legacy keys when encountered
if "content" in raw and "template" not in raw:
merged["template"] = raw["content"]
if "showBackdrop" in raw and "noBackdrop" not in raw:
merged["noBackdrop"] = not bool(raw["showBackdrop"])
if "showDelay" in raw and "delay" not in raw:
merged["delay"] = raw["showDelay"]
for key in ("template", "templateUrl", "noBackdrop", "delay", "duration"):
if key in raw:
merged[key] = raw[key]
self.active_opts = merged
self.visible = True
self.history.append(("show", merged["template"], merged["noBackdrop"], merged["delay"], merged["duration"]))
return merged
def hide(self):
self.visible = False
self.history.append(("hide",))
def main():
cases = []
# Case 1: Basic show(opts) / hide() with all documented option keys
svc = IonicLoadingService()
shown = svc.show({
"template": "<span>Loading...</span>",
"noBackdrop": False,
"delay": 100,
"duration": 2000,
})
was_visible = svc.visible
svc.hide()
actual_basic = {
"template": shown["template"],
"noBackdrop": shown["noBackdrop"],
"delay": shown["delay"],
"duration": shown["duration"],
"was_visible": was_visible,
"after_hide_visible": svc.visible,
}
expected_basic = {
"template": "<span>Loading...</span>",
"noBackdrop": False,
"delay": 100,
"duration": 2000,
"was_visible": True,
"after_hide_visible": False,
}
cases.append({
"name": "ionic_loading_show_hide_opts",
"summary": "验证 $ionicLoading.show(opts) 的 template/noBackdrop/delay/duration 与 hide()",
"expected": expected_basic,
"actual": actual_basic,
"passed": actual_basic == expected_basic,
})
# Case 2: $ionicLoadingConfig constant defaults & call-site override
cfg_svc = IonicLoadingService({"template": "默认加载模板……", "noBackdrop": True})
default_call = cfg_svc.show()
override_call = cfg_svc.show({"template": "提交中...", "noBackdrop": False})
actual_cfg = {
"default_template": default_call["template"],
"default_no_backdrop": default_call["noBackdrop"],
"override_template": override_call["template"],
"override_no_backdrop": override_call["noBackdrop"],
}
expected_cfg = {
"default_template": "默认加载模板……",
"default_no_backdrop": True,
"override_template": "提交中...",
"override_no_backdrop": False,
}
cases.append({
"name": "ionic_loading_config_defaults",
"summary": "验证 $ionicLoadingConfig 全局默认配置及单次 show() 覆盖行为",
"expected": expected_cfg,
"actual": actual_cfg,
"passed": actual_cfg == expected_cfg,
})
# Case 3: Legacy 1.0-beta option mapping + DOM template parse + timeout data load
markup = (
'<ion-view title="Home"><ion-header-bar><h1 class="title">The Stooges</h1></ion-header-bar>'
'<ion-content has-header="true"><ion-list>'
'<ion-item ng-repeat="stooge in stooges" href="#">{{stooge.name}}</ion-item>'
'</ion-list></ion-content></ion-view>'
)
doc = html.fromstring(markup)
beta_svc = IonicLoadingService()
mapped = beta_svc.show({"content": "Loading", "showBackdrop": True, "showDelay": 0})
beta_svc.hide()
stooges = [{"name": "Moe"}, {"name": "Larry"}, {"name": "Curly"}]
actual_ex = {
"view_title": doc.get("title"),
"ng_repeat": doc.xpath("//ion-item/@ng-repeat")[0],
"mapped_template": mapped["template"],
"mapped_no_backdrop": mapped["noBackdrop"],
"loaded_names": [s["name"] for s in stooges],
"hidden_after_timeout": not beta_svc.visible,
}
expected_ex = {
"view_title": "Home",
"ng_repeat": "stooge in stooges",
"mapped_template": "Loading",
"mapped_no_backdrop": False,
"loaded_names": ["Moe", "Larry", "Curly"],
"hidden_after_timeout": True,
}
cases.append({
"name": "loading_example_and_legacy_opts",
"summary": "验证示例视图模板、早期 content/showBackdrop 选项映射与异步隐藏加载数据",
"expected": expected_ex,
"actual": actual_ex,
"passed": actual_ex == expected_ex,
})
# Case 4: Verify source mispaste distinction (ion-list attrs vs $ionicLoading opts)
loading_opts = {"template", "templateUrl", "scope", "noBackdrop", "hideOnStateChange", "delay", "duration"}
list_attrs = {"delegate-handle", "show-delete", "show-reorder", "can-swipe"}
actual_dist = {
"disjoint_sets": loading_opts.isdisjoint(list_attrs),
"mispasted_list_attr_count": len(list_attrs),
}
expected_dist = {"disjoint_sets": True, "mispasted_list_attr_count": 4}
cases.append({
"name": "distinguish_loading_vs_list_api",
"summary": "核对 $ionicLoading 选项集与源页误贴的 ion-list 属性集完全互斥",
"expected": expected_dist,
"actual": actual_dist,
"passed": actual_dist == expected_dist,
})
print(json.dumps({
"version": "CPython 3.12.14 / lxml offline Ionic JS directive & service verifier",
"cases": cases,
}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/ionic-ionicloading-lab/explore-ionicloading.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-ionicloading.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a2013.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2038.zip