249 lines
10 KiB
JavaScript
249 lines
10 KiB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
/**
|
|
* Copyright (c) 2022 International Business Group, Ant Group. All rights reserved.
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), the rights to use, copy, modify, merge, and/or distribute the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
* 1. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE; and
|
|
* 2. If applicable, the use of the Software is also subject to the terms and conditions of any non-disclosure agreement signed by you and the relevant Ant Group entity.
|
|
*/
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
|
|
var getType = function getType(target) {
|
|
return Object.prototype.toString.call(target).slice(8, -1).toLowerCase();
|
|
};
|
|
var EventCenter = /*#__PURE__*/function () {
|
|
function EventCenter() {
|
|
_classCallCheck(this, EventCenter);
|
|
_defineProperty(this, "events", void 0);
|
|
_defineProperty(this, "eventsKey", void 0);
|
|
this.events = new Map();
|
|
this.eventsKey = new Map();
|
|
}
|
|
_createClass(EventCenter, [{
|
|
key: "on",
|
|
value: function on(name, func) {
|
|
var key = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
|
if (key) {
|
|
if (this.eventsKey.has("".concat(name, "-").concat(key))) {
|
|
// eslint-disable-next-line no-console
|
|
console.warn("".concat(key, "\uFF1AHas been registered. Please use another key or do not pass the key"));
|
|
return;
|
|
}
|
|
this.eventsKey.set("".concat(name, "-").concat(key), true);
|
|
}
|
|
var eventList = this.events.get(name) || [];
|
|
eventList.push({
|
|
func: func,
|
|
key: key || ''
|
|
});
|
|
this.events.set(name, eventList);
|
|
}
|
|
}, {
|
|
key: "off",
|
|
value: function off(name, func) {
|
|
var key = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
|
if (key && this.eventsKey.has("".concat(name, "-").concat(key))) {
|
|
this.eventsKey.delete("".concat(name, "-").concat(key));
|
|
}
|
|
if (this.events.has(name)) {
|
|
var eventList = this.events.get(name);
|
|
eventList = eventList.filter(function (item) {
|
|
if (func === item.func && item.key === key) return false;
|
|
if (item.key && item.key === key) return false;
|
|
return true;
|
|
});
|
|
this.events.set(name, eventList);
|
|
}
|
|
}
|
|
}, {
|
|
key: "emit",
|
|
value: function emit(name) {
|
|
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
rest[_key - 1] = arguments[_key];
|
|
}
|
|
if (this.events.has(name)) {
|
|
var eventList = this.events.get(name);
|
|
eventList.forEach(function (item) {
|
|
item.func.apply(item, rest);
|
|
});
|
|
}
|
|
}
|
|
}, {
|
|
key: "once",
|
|
value: function once(name, func, key) {
|
|
var _this = this;
|
|
var onceF = function onceF() {
|
|
func.apply(void 0, arguments);
|
|
_this.off(name, onceF, key);
|
|
};
|
|
this.on(name, onceF, key);
|
|
}
|
|
}]);
|
|
return EventCenter;
|
|
}();
|
|
var getViewPort = function getViewPort(key) {
|
|
var meta = document.getElementsByTagName('meta');
|
|
// HTMLMetaElement
|
|
var viewPort = meta.viewPort;
|
|
var _meta = {};
|
|
if (viewPort) {
|
|
var content = viewPort.content.split(',');
|
|
for (var j = 0; j < content.length; j++) {
|
|
var i = content[j].split('=');
|
|
_meta[i[0]] = i[1];
|
|
}
|
|
}
|
|
return _meta[key];
|
|
};
|
|
var getOrigin = function getOrigin() {
|
|
var _window$location = window.location,
|
|
origin = _window$location.origin,
|
|
protocol = _window$location.protocol,
|
|
hostname = _window$location.hostname,
|
|
port = _window$location.port;
|
|
return origin || "".concat(protocol, "//").concat(hostname).concat(port ? ":".concat(port) : '');
|
|
};
|
|
var serialize = /* istanbul ignore next */function serialize(obj) {
|
|
var s = [];
|
|
for (var item in obj) {
|
|
// eslint-disable-next-line no-prototype-builtins
|
|
if (obj.hasOwnProperty(item)) {
|
|
var k = encodeURIComponent(item);
|
|
var v = encodeURIComponent(obj[item] === null ? '' : obj[item]);
|
|
s.push("".concat(k, "=").concat(v));
|
|
}
|
|
}
|
|
return s.join('&');
|
|
};
|
|
var isJsonString = function isJsonString(str) {
|
|
try {
|
|
JSON.parse(str);
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
var isDom = function isDom(obj) {
|
|
if ((typeof HTMLElement === "undefined" ? "undefined" : _typeof(HTMLElement)) === 'object') {
|
|
return obj instanceof HTMLElement;
|
|
} else {
|
|
return obj && _typeof(obj) === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
|
|
}
|
|
};
|
|
var _window = window,
|
|
navigator = _window.navigator;
|
|
var UA = navigator.userAgent;
|
|
var isIOS = /iPhone|iPad|iPod/i.test(UA) || navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1;
|
|
var isAndroid = /Android/i.test(UA);
|
|
var isMacOS = /\bMacintosh\b/.test(UA);
|
|
var isWindowsNT = /Windows NT/.test(UA);
|
|
var isMobile = function isMobile() {
|
|
return /Android|iPhone|iPad|iPod|Mobile/.test(UA);
|
|
};
|
|
var device = {
|
|
iOS: isIOS,
|
|
Android: isAndroid,
|
|
macOS: isMacOS,
|
|
WindowsNT: isWindowsNT,
|
|
isMobile: !!isMobile()
|
|
};
|
|
var safeJson = function safeJson(data, obj) {
|
|
try {
|
|
return JSON.parse(data) || obj;
|
|
} catch (_unused) {
|
|
return obj;
|
|
}
|
|
};
|
|
var isPC = function isPC() {
|
|
var userAgentInfo = navigator.userAgent;
|
|
var agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPod'];
|
|
return !agents.some(function (agent) {
|
|
return userAgentInfo.includes(agent);
|
|
});
|
|
};
|
|
var queryParse = function queryParse(url) {
|
|
var qs = {};
|
|
if (!url) {
|
|
// eslint-disable-next-line no-param-reassign
|
|
url = window.location.href;
|
|
}
|
|
url.replace(/([^?=&#]+)(=([^&#]*))?/g, function (_$0, $1, _$2, $3) {
|
|
if ($3 === undefined) {
|
|
return '';
|
|
}
|
|
qs[$1] = decodeURIComponent($3);
|
|
return qs[$1];
|
|
});
|
|
return qs;
|
|
};
|
|
var designSize = 390;
|
|
var maxFontSize = 130;
|
|
var getDesignFontSize = function getDesignFontSize() {
|
|
var screenWidth = document.documentElement.clientWidth;
|
|
var currentFontSize = isPC() ? maxFontSize : screenWidth * 100 / designSize;
|
|
return Math.min(currentFontSize, maxFontSize);
|
|
};
|
|
var amsSetSize = function amsSetSize(event, dom) {
|
|
try {
|
|
var componentContainerId = 'ams-component-container';
|
|
var _dom = dom || document.getElementById(componentContainerId);
|
|
_dom.style.fontSize = "".concat(getDesignFontSize(), "px");
|
|
window.changingPageHeight = window.innerHeight;
|
|
// eslint-disable-next-line no-empty
|
|
} catch (error) {}
|
|
};
|
|
var addSetFontSizeEvent = function addSetFontSizeEvent() {
|
|
window.removeEventListener('resize', amsSetSize);
|
|
window.removeEventListener('pageShow', amsSetSize);
|
|
window.removeEventListener('DOMContentLoaded', amsSetSize);
|
|
window.addEventListener('resize', amsSetSize);
|
|
window.addEventListener('pageShow', amsSetSize);
|
|
window.addEventListener('DOMContentLoaded', amsSetSize);
|
|
};
|
|
|
|
/**
|
|
* @description get or set STORAGE
|
|
*/
|
|
var getOrSetStorageId = function getOrSetStorageId(key, value) {
|
|
try {
|
|
if (key && !value) {
|
|
var storageValue = window.localStorage.getItem(key);
|
|
if (storageValue) {
|
|
return storageValue;
|
|
} else return '';
|
|
}
|
|
if (key && value) {
|
|
window.localStorage.setItem(key, value);
|
|
}
|
|
} catch (error) {
|
|
return '';
|
|
}
|
|
};
|
|
var checkTimeElapsed = function checkTimeElapsed() {
|
|
var key = 'antomSDKConfiglastCallTime';
|
|
var currentTime = new Date().getTime();
|
|
try {
|
|
var lastCallTime = localStorage.getItem(key);
|
|
if (lastCallTime) {
|
|
var elapsedTime = currentTime - parseInt(lastCallTime);
|
|
if (elapsedTime >= 10 * 60 * 1000) {
|
|
// 记录当前调用时间
|
|
localStorage.setItem(key, currentTime.toString());
|
|
// 超过10分钟
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
/* empty */
|
|
}
|
|
// 记录当前调用时间
|
|
localStorage.setItem(key, currentTime.toString());
|
|
return true;
|
|
};
|
|
export { getType, EventCenter, getViewPort, getOrigin, serialize, isJsonString, isDom, device, safeJson, isPC, queryParse, getDesignFontSize, amsSetSize, addSetFontSizeEvent, getOrSetStorageId, checkTimeElapsed }; |