ewm
Version:
小程序原生插件
634 lines • 23.2 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _a, _Broker_instance, _Broker_eventRepository;
import { InstanceInject } from "./InstanceInject";
export var EInternalKey;
(function (EInternalKey) {
EInternalKey["urlData"] = "miss_annil";
EInternalKey["defUrl"] = "/xx&xx";
})(EInternalKey || (EInternalKey = {}));
let config = {
env: "development",
language: "ts",
};
try {
config = Object.assign(config, require("../../ewm.config"));
config = Object.assign(config, require("../../../../ewm.config"));
}
catch (error) {
void 0;
}
export function EmptyFunc() {
void 0;
}
export const IsDevelopment = config.env === "development";
export const IsTypescript = config.language === "ts";
export function _urlData(path, data) {
return `${path}?${EInternalKey.urlData}=${encodeURIComponent(JSON.stringify(data))}`;
}
export function pathCheck(_this, options) {
const path = options.path;
const route = _this.route;
if ((route && path === undefined)
|| (route && path !== "/" + route)) {
throw Error(`[ ${_this.route} ] defineComponent构建页面时,应书写path字段,且值应为 /${route}`);
}
if (path !== undefined && !route) {
throw Error(`[ ${_this.is} ] defineComponent构建组件时,不应该书写path字段`);
}
}
export function checkExtraAndType(receivedData, originalProperties, pageUrl) {
for (const [key, value] of Object.entries(receivedData)) {
if (!(key in originalProperties)) {
throw Error(`页面 ${pageUrl} 接收到的数据${key}未在properties中定义`);
}
else {
const typeArr = changePropertiesTypeToArr(originalProperties[key]);
let warnMessage = "";
typeArr.forEach((item) => {
if (warnMessage === "") {
warnMessage = item.name;
}
else {
warnMessage += " 或 " + item.name;
}
});
MatchCheck(typeArr, value, `页面${pageUrl}中,接收到的数据(${key})类型不正确,类型应该为 ${warnMessage.toLocaleLowerCase()}`);
}
}
}
export function checkRequiredField(requiredProperties, receivedData, path) {
requiredProperties.forEach((key) => {
if (!(key[0] in receivedData)) {
throw Error(`页面${path}接收到的数据缺少必传字段${key[0]}`);
}
});
}
export function mergeMainData(options) {
if (options.mainData) {
Object.assign(options, options.mainData);
delete options.mainData;
}
}
export function CheckDataReceivedByPage(_this, options, opt) {
const path = options.path;
const properties = options.properties && options.properties;
const urlData = opt[EInternalKey["urlData"]];
if (urlData === undefined) {
return;
}
const receivedData = urlData === "undefined"
? undefined
: JSON.parse(decodeURIComponent(urlData));
const requiredProperties = properties
&& Object.entries(properties).filter((item) => IsShortProperties(item[1]) || (IsFullProperties(item[1]) && item[1].value === undefined));
if (!receivedData && requiredProperties && requiredProperties.length > 0) {
throw Error(`页面 ${path} 未收到prperties字段中定义的必传数据 ${requiredProperties.map((item) => item[0])}`);
}
if (receivedData && requiredProperties && requiredProperties.length > 0) {
checkRequiredField(requiredProperties, receivedData, path);
}
if (receivedData && !properties) {
throw Error(`页面 ${path} 接收到的数据${Object.keys(receivedData)}未在properties中定义`);
}
if (receivedData && properties) {
checkExtraAndType(receivedData, properties, path);
}
receivedData && _this.setData(receivedData);
delete opt[EInternalKey["urlData"]];
Object.assign(opt, receivedData);
}
export function mergeInjectOption(options) {
var _b;
const injectOpt = Object.assign({}, InstanceInject.InjectOption);
injectOpt.options && (injectOpt.options = Object.assign({}, injectOpt.options));
const warnMessgesArr = [];
if (injectOpt && !isEmptyObj(injectOpt)) {
if (options.path && ((_b = injectOpt.options) === null || _b === void 0 ? void 0 : _b["virtualHost"])) {
delete injectOpt.options["virtualHost"];
}
_mergeInjectOption(warnMessgesArr, options, injectOpt);
}
options.__warnMessgesArr = warnMessgesArr;
}
export function methodsHandle(options) {
if (options.methods) {
for (const key in options.methods) {
options[key] = options.methods[key];
}
delete options.methods;
}
if (options.customEvent) {
for (const key in options.customEvent) {
options[key] = options.customEvent[key];
}
delete options.customEvent;
}
}
export function _mergeInjectOption(warnMessageArr, originOpt, injectOpt, nameSpace = "") {
function nameSpaceHandle(nameSpace, key) {
if (nameSpace === "") {
return key;
}
else {
return nameSpace + "." + key;
}
}
for (const key in injectOpt) {
const originVal = originOpt[key];
const injectVal = injectOpt[key];
if (originVal === undefined) {
originOpt[key] = injectVal;
}
else {
const originValType = TYPEOF(originVal);
const injectValType = TYPEOF(injectVal);
if (originValType === "Object" && injectValType === "Object") {
_mergeInjectOption(warnMessageArr, originVal, injectVal, nameSpaceHandle(nameSpace, key));
}
else {
warnMessageArr.push(`配置中${nameSpaceHandle(nameSpace, key)}已存在,注入无效`);
}
}
}
return originOpt;
}
export function TYPEOF(val) {
return Object.prototype.toString.call(val).slice(8, -1);
}
export function isArray(val) {
return Array.isArray(val);
}
export function isString(val) {
return TYPEOF(val) === "String";
}
export function isNumber(val) {
return TYPEOF(val) === "Number";
}
export function isBoolean(val) {
return TYPEOF(val) === "Boolean";
}
export function isSymbol(val) {
return TYPEOF(val) === "Symbol";
}
export function isFunction(val) {
return TYPEOF(val) === "Function";
}
export function isObject(val) {
return TYPEOF(val) === "Object";
}
export function isEmptyObj(O) {
return Object.keys(O).length === 0;
}
export function initResponseData(options) {
const data = options.data;
if (!data)
return;
const recordResponseData = {};
for (const key in data) {
if (isFunction(data[key])) {
recordResponseData[key] = data[key];
data[key] = data[key]();
}
}
options.__recordResponseData = recordResponseData;
}
export function createResponseData(_this, options) {
const responseData = options.__recordResponseData;
if (!responseData || isEmptyObj(responseData)) {
return;
}
const { reaction, comparer, toJS } = require("mobx");
let pendingSetData = (_this.pendingSetData || (_this.pendingSetData = null));
function applySetData(callback) {
if (pendingSetData === null)
return;
const data = pendingSetData;
pendingSetData = null;
_this.setData(data, () => {
if (callback) {
callback();
}
});
}
_this._applySetData = applySetData;
function scheduleSetData(field, value) {
if (!pendingSetData) {
pendingSetData = {};
wx.nextTick(applySetData);
}
pendingSetData[field] = toJS(value);
}
const reactionDisposer = (_this._disposer || (_this._disposer = {}));
for (const key in responseData) {
reactionDisposer[key] = reaction(responseData[key], (value) => {
scheduleSetData(key, value);
}, {
equals: comparer.default,
});
}
const pendingResponseData = {};
for (const key in responseData) {
const initData = _this.data[key];
const curData = toJS(responseData[key]());
if (initData && (JSON.stringify(initData) !== JSON.stringify(curData))) {
pendingResponseData[key] = curData;
}
}
if (!isEmptyObj(pendingResponseData)) {
_this.setData(pendingResponseData);
}
}
export function destroyResponsive(_this) {
const disposer = _this.disposer;
if (disposer && !isEmptyObj(disposer)) {
for (const [, func] of Object.entries(disposer)) {
func();
}
}
}
function isAuxType(value) {
return isFunction(value);
}
function isPropTypeArr(value) {
if (!isArray(value))
return false;
return value.every((item) => isAuxType(item));
}
function checkCustomEventParamsType(detail, description, key) {
let detailType;
if (isAuxType(description)) {
detailType = description.name;
}
else if (isPropTypeArr(description)) {
detailType = description.map((ele) => ele.name);
}
else {
if (isAuxType(description.detailType)) {
detailType = description.detailType.name;
}
else {
detailType = description.detailType.map((ele) => ele.name);
}
}
if (detail === null && (detailType === "Object" || detailType.includes("Object"))) {
return;
}
else if (TYPEOF(detail) !== detailType || !detailType.includes(TYPEOF(detail))) {
let typeStr = "";
if (isArray(detailType)) {
detailType.forEach((typeItem, i) => {
typeStr += i === 0 ? typeItem : "|" + typeItem;
});
}
else {
typeStr = detailType;
}
throw Error(`在${this.is}组件中,调用自定义事件${key}时,传入的参数类型不正确,应为${typeStr}`);
}
}
export function methodsCalledByMainHandle(options) {
const methodsCalledByMain = options.methodsCalledByMain;
if (!methodsCalledByMain)
return;
const methods = options.methods || (options.methods = {});
for (const [key, func] of Object.entries(methodsCalledByMain)) {
methods[key] = func;
}
delete options.methodsCalledByMain;
}
export function customEventHandle(options) {
const customEvents = options.customEvents;
if (customEvents) {
const methods = (options.methods || (options.methods = {}));
for (const [key, description] of Object.entries(customEvents)) {
methods[key] = function (detail) {
checkCustomEventParamsType.call(this, detail, description, key);
if (isAuxType(description) || isPropTypeArr(description)) {
return this.triggerEvent(key, detail, { bubbles: true });
}
else {
return this.triggerEvent(key, detail, Object.assign({ bubbles: true }, description.options));
}
};
}
}
}
export function eventsHandle(options) {
const events = options.events;
if (events) {
const methods = (options.methods || (options.methods = {}));
for (const [key, func] of Object.entries(events)) {
methods[key] = func;
}
delete options.events;
}
}
export function isPrimitiveDefaultData(value) {
return value === 0 || value === "" || value === false || value === [] || value === {};
}
export function SubComponentHandle(options) {
if (options.subComp) {
options.behaviors = (options.behaviors || []).concat(options.subComp);
}
}
function ExtractOptionalField(properties) {
const optionalProperties = {};
for (const [key, value] of Object.entries(properties)) {
if (IsFullProperties(value) && value.value !== undefined) {
optionalProperties[key] = value;
}
}
return optionalProperties;
}
function changePropertiesTypeToArr(option) {
if (IsShortProperties(option)) {
return [option];
}
else {
return option.optionalTypes ? [option.type, ...option.optionalTypes] : [option.type];
}
}
export function subMethodsHandle(options) {
const subMethods = options.subMethods;
if (!subMethods) {
return;
}
else {
options.methods = options.methods || {};
Object.assign(options.methods, subMethods);
delete options.subMethods;
}
}
export function deleteInheritAndMainMethods(options) {
delete options.mainMethods;
delete options.inherit;
}
export function checkPropertiesValueType(options) {
const properties = options.properties;
if (!properties)
return;
const optionalProperties = ExtractOptionalField(properties);
for (const [key, option] of Object.entries(optionalProperties)) {
const typeArr = changePropertiesTypeToArr(option);
let warnMessage = "";
typeArr.forEach((item) => {
if (warnMessage === "") {
warnMessage = item.name;
}
else {
warnMessage += "或" + item.name;
}
});
MatchCheck(typeArr, option.value, `'properties.${key}.value(${JSON.stringify(option.value)})' 类型非法, 应该为${warnMessage}`);
}
}
export function IsShortProperties(obj) {
return obj === String || obj === Number || obj === Boolean || obj === Array || obj === Object;
}
export function IsFullProperties(obj) {
return obj.type === String || obj.type === Number || obj.type === Boolean
|| obj.type === Array || obj.type === Object;
}
function MatchCheck(typeArr, value, errorMessage) {
for (const Constructor of typeArr) {
if (value.constructor === Constructor) {
return;
}
}
throw Error(errorMessage);
}
function CheckFields(key, item) {
const rules = {
type: "",
value: "",
optionalTypes: "",
};
for (const field in item) {
if (!(field in rules)) {
throw Error(`properties.${key}.${field} is illegal, ${field} Not in ["type,value,optionalTypes"]`);
}
}
}
export function deleteNameFiled(options) {
delete options.name;
}
export function checkPropertyField(options) {
const properties = options.properties;
if (!properties)
return;
for (const [key, item] of Object.entries(properties)) {
if (IsFullProperties(item)) {
CheckFields(key, item);
}
}
}
export function AddNavigateTo(_this) {
const nvigateTo = async function (obj) {
return wx.navigateTo({
url: _urlData(obj["url"], obj["data"]),
});
};
_this._navigateTo = nvigateTo;
}
export function insertSubComponentToBehaviors(options) {
if (options.subComponent) {
const mapSubComponent = options.subComponent.filter((ele) => ele !== "");
options.behaviors = (options.behaviors || []).concat(mapSubComponent);
}
}
export function pageLifetimesHandle(options) {
if (!options.path) {
if (options.pageLifetimes) {
for (const key in options.pageLifetimes) {
if (key === "onShow") {
options.pageLifetimes.show = options.pageLifetimes[key];
delete options.pageLifetimes.onShow;
}
else if (key === "onResize") {
options.pageLifetimes.resize = options.pageLifetimes[key];
delete options.pageLifetimes.onResize;
}
else if (key === "onHide") {
options.pageLifetimes.hide = options.pageLifetimes[key];
delete options.pageLifetimes.onHide;
}
}
}
}
else {
if (options.pageLifetimes) {
options.methods = options.methods || {};
for (const key in options.pageLifetimes) {
options.methods[key] = options.pageLifetimes[key];
}
delete options.pageLifetimes;
}
}
}
export function createdHijack(options, before, after) {
options.lifetimes = options.lifetimes || {};
const originalCreated = options.lifetimes.created;
options.lifetimes.created = function () {
before.forEach((func) => {
func(this, options);
});
originalCreated && originalCreated.call(this);
after.forEach((func) => {
func(this, options);
});
};
}
export function onloadHijack(options, before, after) {
options.methods = options.methods || {};
const originalOnLoad = options.methods.onLoad;
options.methods.onLoad = function (opt) {
before.forEach((func) => {
func(this, options, opt);
});
originalOnLoad && originalOnLoad.call(this, opt);
after.forEach((func) => {
func(this, options, opt);
});
};
}
export function onUnloadHijack(options, before, after) {
options.pageLifetimes = options.pageLifetimes || {};
const originalOnUnload = options.pageLifetimes.onUnload;
options.pageLifetimes.onUnload = function (opt) {
before.forEach((func) => {
func(this, options, opt);
});
originalOnUnload && originalOnUnload.call(this, opt);
after.forEach((func) => {
func(this, options, opt);
});
};
}
export function detachedHijack(options, before, after) {
options.lifetimes = options.lifetimes || {};
const originalDetached = options.lifetimes.detached;
options.lifetimes.detached = function (opt) {
before.forEach((func) => {
func(this, options, opt);
});
originalDetached && originalDetached.call(this, opt);
after.forEach((func) => {
func(this, options, opt);
});
};
}
export function attachedHijack(options, before, after) {
options.lifetimes = options.lifetimes || {};
const originalAttached = options.lifetimes.attached;
options.lifetimes.attached = function () {
before.forEach((func) => {
func(this, options);
});
originalAttached && originalAttached.call(this);
after.forEach((func) => {
func(this, options);
});
};
}
export function printInjectDataWarn(_this, options) {
const warnMessgesArr = options.__warnMessgesArr;
delete options.__warnMessgesArr;
warnMessgesArr
&& warnMessgesArr.forEach((item) => {
console.warn(`${_this.is} ${item}`);
});
}
export function deepClone(obj) {
const cloneObj = obj instanceof Array ? [] : {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === "object" && obj[key] !== null) {
cloneObj[key] = deepClone(obj[key]);
}
else {
cloneObj[key] = obj[key];
}
}
}
return cloneObj;
}
export function auxEvent(opt) {
return opt;
}
export function subscribeEventFieldHandle(_this, options) {
const subscribeEvents = options.subscribeEvents;
if (!subscribeEvents)
return;
const eventBroker = Broker.getInstance();
for (const [, obj] of Object.entries(subscribeEvents.call(_this, auxEvent))) {
if (!obj)
return;
for (const [, func] of Object.entries(obj)) {
eventBroker.addSubscribeEvent(func, _this.is);
}
}
}
export function publishEventFieldHandle(_this, options) {
const publishEventObj = options.publishEvents;
if (!publishEventObj)
return;
const eventBroker = Broker.getInstance();
for (const [key] of Object.entries(publishEventObj)) {
_this[key] = function (data) {
eventBroker.publishEvent(key, data);
};
}
}
export function destroySubscribeEventHandle(_this) {
const eventBroker = Broker.getInstance();
eventBroker.destroySubscribeEvent(_this.is);
}
class Broker {
constructor() {
_Broker_eventRepository.set(this, {});
}
static getInstance() {
return (__classPrivateFieldSet(this, _a, __classPrivateFieldGet(this, _a, "f", _Broker_instance) || new Broker(), "f", _Broker_instance));
}
addSubscribeEvent(func, path) {
var _b;
const eventRepository = __classPrivateFieldGet(this, _Broker_eventRepository, "f");
const eventName = func.name;
(_b = eventRepository[path]) !== null && _b !== void 0 ? _b : (eventRepository[path] = {});
eventRepository[path][eventName] = func;
}
destroySubscribeEvent(path) {
const eventRepository = __classPrivateFieldGet(this, _Broker_eventRepository, "f");
delete eventRepository[path];
}
publishEvent(eventName, data) {
const eventRepository = __classPrivateFieldGet(this, _Broker_eventRepository, "f");
for (const url in eventRepository) {
if (eventRepository[url][eventName]) {
const res = eventRepository[url][eventName](data);
if (res === false) {
delete eventRepository[url][eventName];
}
}
}
}
}
_a = Broker, _Broker_eventRepository = new WeakMap();
_Broker_instance = { value: void 0 };
export function fieldHandle(options, funcArr) {
funcArr.forEach((ele) => ele(options));
}
export function checkHandle(options, funcArr) {
funcArr.forEach((ele) => ele(options));
}
//# sourceMappingURL=_api.js.map