关于前端:SAP-UI5-sapuiDevicemediainitRangeSet-方法的单步调试

0次阅读

共计 1878 个字符,预计需要花费 5 分钟才能阅读完成。

每个 SAP UI5 利用在初始化时,都会触发这个办法的调用。

调用栈的上下文:

sap.ui.define(['sap/ui/Device'], function(Device) {

能够看到 Device.js 文件内,有这两行代码进行默认 RangeSet 的初始化:

    //Always initialize the default media range set
    Device.media.initRangeSet();
    Device.media.initRangeSet(RANGESETS["SAP_STANDARD_EXTENDED"]);

Device.media._predefinedRangeSets 就是 SAP UI5 官网里介绍的预约义的 Range Set:

默认的 RangeSet:

默认的 RangeSet 是 SAP_STANDARD, 如下图第 1041 行代码所示:

两个 point 决定三个 device type:

三个 query 和对应的 device type:

这里是通过 window 原生 API,matchMedia 来计算的。

注册 RangeSet 变更的事件监听函数:

if (oQuery.media.addEventListener) {oQuery.media.addEventListener("change", oConfig.listener);

sap.ui.Device.orientation

sap.ui.Device.os

这里设置 oReducedNavigator

var setDefaultNavigator = function () {
        oReducedNavigator = {
            userAgent: window.navigator.userAgent,
            platform: window.navigator.platform
        };
        // Only add property standalone in case navigator has this property
        if (window.navigator.hasOwnProperty("standalone")) {oReducedNavigator.standalone = window.navigator.standalone;}
    };

首先应用正则表达式试图匹配 iOS 零碎:

rPlatform = /\(([a-zA-Z]+);\s(?:[U]?[;]?)([\D]+)((?:[\d._]*))(?:.*[\)][^\d]*)([\d.]*)\s/;
        aMatches = userAgent.match(rPlatform);
        if (aMatches) {var rAppleDevices = /iPhone|iPad|iPod/;

如果匹配不胜利,再试图匹配 Android:

//Firefox on Android
        rPlatform = /\((Android)[\s]?([\d][.\d]*)?;.*Firefox\/[\d][.\d]*/;
        aMatches = userAgent.match(rPlatform);
        if (aMatches) {
            return ({
                "name": OS.ANDROID,
                "versionStr": aMatches.length == 3 ? aMatches[2] : ""
            });
        }

最初再匹配 DesktopOS:

userAgent 字段就能晓得是 Windows10,X64 架构:

最初设置到 Device 对象里,不便其余代码生产:

OS 大写,模仿其余编程语言里的枚举类型。

sap.ui.Device.resize

保留了以后窗口的宽度 (width) 和高度(height)。

在 SAP UI5 利用初始化时,会执行 setResizeInfo(Device.resize);

执行到这一步时,Device 对象明细如下图:

应用 windowsSize 获取以后 window 尺寸:

实际上就是应用的 windows 全局对象的 innerHeight 和 innerWidth 这两个属性。

Support Detect 的实现源代码:

var detectTouch = function () {return !!(('ontouchstart' in window)
            || (window.navigator.maxTouchPoints > 0)
            || (window.DocumentTouch && document instanceof window.DocumentTouch)
            || (window.TouchEvent && Device.browser.firefox));
    };

    Device.support.touch = detectTouch();
正文完
 0