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

运行 ion-scroll 缩放边界、ion-infinite-scroll 分页与 $ionicScrollDelegate 离线实测

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-scroll.py
explore-scroll.py211 行
"""Offline Ionic 1 ion-scroll, ion-infinite-scroll & $ionicScrollDelegate verifier."""
import json
from lxml import html


class ScrollView:
    """Models an ion-scroll / ion-content viewport controlled by $ionicScrollDelegate."""

    def __init__(self, handle, content_height=2000, viewport_height=500, direction="y",
                 zooming=False, min_zoom=0.5, max_zoom=3.0):
        self.handle = handle
        self.content_height = content_height
        self.viewport_height = viewport_height
        self.direction = direction
        self.zooming = zooming
        self.min_zoom = min_zoom
        self.max_zoom = max_zoom
        self.zoom_level = 1.0
        self.scroll_y = 0
        self.resize_calls = 0
        self.last_animated = None

    def resize(self, new_content_height=None):
        self.resize_calls += 1
        if new_content_height is not None:
            self.content_height = new_content_height

    def scroll_top(self, should_animate=False):
        self.scroll_y = 0
        self.last_animated = bool(should_animate)

    def scroll_bottom(self, should_animate=False):
        self.scroll_y = max(0, self.content_height - self.viewport_height)
        self.last_animated = bool(should_animate)

    def zoom_to(self, factor):
        if not self.zooming:
            return self.zoom_level
        self.zoom_level = max(self.min_zoom, min(self.max_zoom, float(factor)))
        return self.zoom_level


class IonicScrollDelegate:
    def __init__(self, views):
        self.views = {v.handle: v for v in views}

    def get_by_handle(self, handle):
        return self.views[handle]

    def scroll_top(self, should_animate=False):
        for v in self.views.values():
            v.scroll_top(should_animate)


def main():
    cases = []

    # Case 1: Parse ion-scroll with zooming/direction/min-zoom/max-zoom & ion-infinite-scroll
    markup = """
    <div>
      <ion-scroll delegate-handle="mapScroll" zooming="true" direction="xy"
                  min-zoom="0.5" max-zoom="3" scrollbar-x="false" scrollbar-y="true"
                  paging="false" on-scroll="onScroll()" on-refresh="doRefresh()"
                  style="width: 500px; height: 500px">
        <div style="width: 5000px; height: 5000px;"></div>
      </ion-scroll>
      <ion-content>
        <ion-infinite-scroll ng-if="moreDataCanBeLoaded()" icon="ion-loading-c"
                             on-infinite="loadMoreData()" distance="1%">
        </ion-infinite-scroll>
      </ion-content>
    </div>
    """
    doc = html.fromstring(markup)
    sc = doc.xpath("//ion-scroll")[0]
    inf = doc.xpath("//ion-infinite-scroll")[0]
    actual_dom = {
        "handle": sc.get("delegate-handle"),
        "direction": sc.get("direction"),
        "zooming": sc.get("zooming") == "true",
        "min_zoom": float(sc.get("min-zoom")),
        "max_zoom": float(sc.get("max-zoom")),
        "scrollbar_x": sc.get("scrollbar-x") == "true",
        "scrollbar_y": sc.get("scrollbar-y") == "true",
        "inf_ng_if": inf.get("ng-if"),
        "inf_icon": inf.get("icon"),
        "inf_distance": inf.get("distance"),
        "inf_expr": inf.get("on-infinite"),
    }
    expected_dom = {
        "handle": "mapScroll",
        "direction": "xy",
        "zooming": True,
        "min_zoom": 0.5,
        "max_zoom": 3.0,
        "scrollbar_x": False,
        "scrollbar_y": True,
        "inf_ng_if": "moreDataCanBeLoaded()",
        "inf_icon": "ion-loading-c",
        "inf_distance": "1%",
        "inf_expr": "loadMoreData()",
    }
    cases.append({
        "name": "parse_scroll_and_infinite_directives",
        "summary": "解析 ion-scroll 全量属性及 ion-infinite-scroll 的 ng-if/distance/icon",
        "expected": expected_dom,
        "actual": actual_dom,
        "passed": actual_dom == expected_dom,
    })

    # Case 2: Zoom clamping [min-zoom=0.5, max-zoom=3.0]
    map_view = ScrollView("mapScroll", 5000, 500, direction="xy", zooming=True, min_zoom=0.5, max_zoom=3.0)
    actual_zoom = {
        "under_min": map_view.zoom_to(0.1),
        "within_range": map_view.zoom_to(2.0),
        "over_max": map_view.zoom_to(5.5),
    }
    expected_zoom = {"under_min": 0.5, "within_range": 2.0, "over_max": 3.0}
    cases.append({
        "name": "scroll_zoom_clamping",
        "summary": "验证 ion-scroll 双指缩放比例受 min-zoom(0.5) 与 max-zoom(3.0) 边界约束",
        "expected": expected_zoom,
        "actual": actual_zoom,
        "passed": actual_zoom == expected_zoom,
    })

    # Case 3: ion-infinite-scroll pagination with 1% threshold, scroll.infiniteScrollComplete & ng-if stop
    pages = [[1, 2], [3, 4]]
    loaded = []
    broadcasts = []
    has_more = True

    def try_infinite(scroll_y, content_h, view_h, distance_pct=1.0):
        nonlocal has_more
        if not has_more:
            return False
        remaining = content_h - (scroll_y + view_h)
        threshold = content_h * (distance_pct / 100.0)
        if remaining <= threshold:
            if pages:
                loaded.extend(pages.pop(0))
            if not pages:
                has_more = False
            broadcasts.append("scroll.infiniteScrollComplete")
            return True
        return False

    t1 = try_infinite(100, 1000, 500)      # remaining 400 > 10 -> not triggered
    t2 = try_infinite(495, 1000, 500)      # remaining 5 <= 10 -> page 1
    t3 = try_infinite(995, 1500, 500)      # remaining 5 <= 15 -> page 2, has_more becomes False
    t4 = try_infinite(1495, 2000, 500)     # ng-if false -> blocked
    actual_inf = {
        "triggers": [t1, t2, t3, t4],
        "loaded": loaded,
        "has_more": has_more,
        "broadcast_count": len(broadcasts),
    }
    expected_inf = {
        "triggers": [False, True, True, False],
        "loaded": [1, 2, 3, 4],
        "has_more": False,
        "broadcast_count": 2,
    }
    cases.append({
        "name": "infinite_scroll_pagination_and_guard",
        "summary": "验证 ion-infinite-scroll 的 1% 触底阈值、广播完成事件与 ng-if 终止",
        "expected": expected_inf,
        "actual": actual_inf,
        "passed": actual_inf == expected_inf,
    })

    # Case 4: $ionicScrollDelegate resize(), scrollBottom(true), scrollTop(true) & $getByHandle
    list_view = ScrollView("mainList", content_height=1800, viewport_height=600)
    delegate = IonicScrollDelegate([map_view, list_view])
    v = delegate.get_by_handle("mainList")
    v.resize(2400)
    v.scroll_bottom(should_animate=True)
    y_bottom = v.scroll_y
    anim_bottom = v.last_animated
    delegate.scroll_top(should_animate=False)
    actual_del = {
        "resize_calls": v.resize_calls,
        "y_bottom": y_bottom,
        "anim_bottom": anim_bottom,
        "y_top": v.scroll_y,
        "anim_top": v.last_animated,
    }
    expected_del = {
        "resize_calls": 1,
        "y_bottom": 1800,
        "anim_bottom": True,
        "y_top": 0,
        "anim_top": False,
    }
    cases.append({
        "name": "scroll_delegate_controls",
        "summary": "验证 $ionicScrollDelegate 的 $getByHandle、resize、scrollBottom 与 scrollTop",
        "expected": expected_del,
        "actual": actual_del,
        "passed": actual_del == expected_del,
    })

    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-scroll-lab/explore-scroll.py

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

下载课程关联工程包:Ionic

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

下载课程关联工程包:Ionic

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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