From 7ae79f12e34d873a2f4c058cf2fe3d880c8eb2a0 Mon Sep 17 00:00:00 2001 From: "Zhenfei.Li" Date: Thu, 5 Jan 2023 15:19:10 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-14001=20refactor:=20plguin=E3=80=81syste?= =?UTF-8?q?m=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/0.foundation.js | 9 +- src/core/6.plugin.js | 204 +++++++++++++++++----------------- src/core/decorator.js | 14 ++- src/core/h.js | 49 +++++---- src/core/index.js | 17 ++- src/core/system.js | 232 ++++++++++++++++++++------------------- src/core/version.js | 2 +- src/core/worker.js | 101 ++++++++--------- 8 files changed, 330 insertions(+), 298 deletions(-) diff --git a/src/core/0.foundation.js b/src/core/0.foundation.js index ea69e97a9..96c6e59ba 100644 --- a/src/core/0.foundation.js +++ b/src/core/0.foundation.js @@ -1,10 +1,11 @@ +/* eslint-disable eqeqeq */ /** * Created by richie on 15/7/8. */ /** * 初始化BI对象 */ -var _global = undefined; +let _global = undefined; if (typeof window !== "undefined") { _global = window; } else if (typeof global !== "undefined") { @@ -20,8 +21,8 @@ if (_global) { } if (_global.BI == null) { - _global.BI = {prepares: []}; + _global.BI = { prepares: [] }; } -if(_global.BI.prepares == null) { +if (_global.BI.prepares == null) { _global.BI.prepares = []; -} \ No newline at end of file +} diff --git a/src/core/6.plugin.js b/src/core/6.plugin.js index bbfd5e5ac..e0561b4fd 100644 --- a/src/core/6.plugin.js +++ b/src/core/6.plugin.js @@ -1,123 +1,127 @@ -BI.Plugin = BI.Plugin || {}; -!(function () { - var _WidgetsPlugin = {}; - var _ObjectPlugin = {}; - var _ConfigPlugin = {}; - var _ConfigRenderPlugin = {}; - var _GlobalWidgetConfigFns = []; - var __GlobalObjectConfigFns = []; - BI.defaults(BI.Plugin, { +const _WidgetsPlugin = {}; +const _ObjectPlugin = {}; +const _ConfigPlugin = {}; +const _ConfigRenderPlugin = {}; +let _GlobalWidgetConfigFns = []; +let __GlobalObjectConfigFns = []; - getWidget: function (type, options) { - if (_GlobalWidgetConfigFns.length > 0) { - var fns = _GlobalWidgetConfigFns.slice(0); - for (var i = fns.length - 1; i >= 0; i--) { - fns[i](type, options); - } +export const Plugin = { + getWidget (type, options) { + if (_GlobalWidgetConfigFns.length > 0) { + const fns = _GlobalWidgetConfigFns.slice(0); + for (let i = fns.length - 1; i >= 0; i--) { + fns[i](type, options); } + } - var res; - if (_ConfigPlugin[type]) { - for (var i = _ConfigPlugin[type].length - 1; i >= 0; i--) { - if (res = _ConfigPlugin[type][i](options)) { - options = res; - } + let res; + if (_ConfigPlugin[type]) { + for (let i = _ConfigPlugin[type].length - 1; i >= 0; i--) { + res = _ConfigPlugin[type][i](options); + if (res) { + options = res; } } - // Deprecated - if (_WidgetsPlugin[type]) { - for (var i = _WidgetsPlugin[type].length - 1; i >= 0; i--) { - if (res = _WidgetsPlugin[type][i](options)) { - return res; - } + } + // Deprecated + if (_WidgetsPlugin[type]) { + for (let i = _WidgetsPlugin[type].length - 1; i >= 0; i--) { + res = _WidgetsPlugin[type][i](options); + if (res) { + return res; } } - return options; - }, + } + + return options; + }, - config: function (widgetConfigFn, objectConfigFn) { - _GlobalWidgetConfigFns = _GlobalWidgetConfigFns.concat(BI._.isArray(widgetConfigFn) ? widgetConfigFn : [widgetConfigFn]); - __GlobalObjectConfigFns = __GlobalObjectConfigFns.concat(BI._.isArray(objectConfigFn) ? objectConfigFn : [objectConfigFn]); - }, + config (widgetConfigFn, objectConfigFn) { + _GlobalWidgetConfigFns = _GlobalWidgetConfigFns.concat(BI._.isArray(widgetConfigFn) ? widgetConfigFn : [widgetConfigFn]); + __GlobalObjectConfigFns = __GlobalObjectConfigFns.concat(BI._.isArray(objectConfigFn) ? objectConfigFn : [objectConfigFn]); + }, - configWidget: function (type, fn, opt) { - // opt.single: true 最后一次注册有效 - if (!_ConfigPlugin[type] || (opt && opt.single)) { - _ConfigPlugin[type] = []; - } - _ConfigPlugin[type].push(fn); - }, + configWidget (type, fn, opt) { + // opt.single: true 最后一次注册有效 + if (!_ConfigPlugin[type] || (opt && opt.single)) { + _ConfigPlugin[type] = []; + } + _ConfigPlugin[type].push(fn); + }, - getRender: function (type, rendered) { - var res; - if (_ConfigRenderPlugin[type]) { - for (var i = _ConfigRenderPlugin[type].length - 1; i >= 0; i--) { - if (res = _ConfigRenderPlugin[type][i](rendered)) { - rendered = res; - } + getRender (type, rendered) { + let res; + if (_ConfigRenderPlugin[type]) { + for (let i = _ConfigRenderPlugin[type].length - 1; i >= 0; i--) { + res = _ConfigRenderPlugin[type][i](rendered); + if (res) { + rendered = res; } } - return rendered; - }, + } + + return rendered; + }, - configRender: function (type, fn) { - if (!_ConfigRenderPlugin[type]) { - _ConfigRenderPlugin[type] = []; - } - _ConfigRenderPlugin[type].push(fn); - }, + configRender (type, fn) { + if (!_ConfigRenderPlugin[type]) { + _ConfigRenderPlugin[type] = []; + } + _ConfigRenderPlugin[type].push(fn); + }, - // Deprecated - registerWidget: function (type, fn) { - if (!_WidgetsPlugin[type]) { - _WidgetsPlugin[type] = []; - } - if (_WidgetsPlugin[type].length > 0) { - console.log("组件已经注册过了!"); - } - _WidgetsPlugin[type].push(fn); - }, + // Deprecated + registerWidget (type, fn) { + if (!_WidgetsPlugin[type]) { + _WidgetsPlugin[type] = []; + } + if (_WidgetsPlugin[type].length > 0) { + console.log("组件已经注册过了!"); + } + _WidgetsPlugin[type].push(fn); + }, - // Deprecated - relieveWidget: function (type) { - delete _WidgetsPlugin[type]; - }, + // Deprecated + relieveWidget (type) { + delete _WidgetsPlugin[type]; + }, - getObject: function (type, object) { - if (__GlobalObjectConfigFns.length > 0) { - var fns = __GlobalObjectConfigFns.slice(0); - for (var i = fns.length - 1; i >= 0; i--) { - fns[i](type, object); - } + getObject (type, object) { + if (__GlobalObjectConfigFns.length > 0) { + const fns = __GlobalObjectConfigFns.slice(0); + for (let i = fns.length - 1; i >= 0; i--) { + fns[i](type, object); } + } - if (_ObjectPlugin[type]) { - var res; - for (var i = 0, len = _ObjectPlugin[type].length; i < len; i++) { - if (res = _ObjectPlugin[type][i](object)) { - object = res; - } + let res; + if (_ObjectPlugin[type]) { + for (let i = 0, len = _ObjectPlugin[type].length; i < len; i++) { + res = _ObjectPlugin[type][i](object); + if (res) { + object = res; } } - return res || object; - }, - - hasObject: function (type) { - return __GlobalObjectConfigFns.length > 0 || !!_ObjectPlugin[type]; - }, + } + + return res || object; + }, - registerObject: function (type, fn) { - if (!_ObjectPlugin[type]) { - _ObjectPlugin[type] = []; - } - if (_ObjectPlugin[type].length > 0) { - console.log("对象已经注册过了!"); - } - _ObjectPlugin[type].push(fn); - }, + hasObject (type) { + return __GlobalObjectConfigFns.length > 0 || !!_ObjectPlugin[type]; + }, - relieveObject: function (type) { - delete _ObjectPlugin[type]; + registerObject (type, fn) { + if (!_ObjectPlugin[type]) { + _ObjectPlugin[type] = []; + } + if (_ObjectPlugin[type].length > 0) { + console.log("对象已经注册过了!"); } - }); -})(); + _ObjectPlugin[type].push(fn); + }, + + relieveObject (type) { + delete _ObjectPlugin[type]; + }, +}; diff --git a/src/core/decorator.js b/src/core/decorator.js index af7a6e005..0e5d5e097 100644 --- a/src/core/decorator.js +++ b/src/core/decorator.js @@ -1,9 +1,21 @@ +// export * from "../../typescript/core/decorator/decorator.ts"; + +import { shortcut as biShortcut, provider as biProvider } from "./5.inject"; + /** * 注册widget */ -import { shortcut as biShortcut } from "./5.inject"; export function shortcut() { return function decorator(Target) { biShortcut(Target.xtype, Target); }; } + +/** + * 注册provider + */ +export function provider() { + return function decorator(Target) { + biProvider(Target.xtype, Target); + }; +} diff --git a/src/core/h.js b/src/core/h.js index b768b1415..c413788c2 100644 --- a/src/core/h.js +++ b/src/core/h.js @@ -1,58 +1,59 @@ -BI.Fragment = function () { -}; +import { isNotNull, isArray, isFunction, isKey, extend } from "./2.base"; -BI.h = function (type, props, children) { - if (children != null) { - if (!BI.isArray(children)) { +export function Fragment () {} + +export function h (type, props, children) { + if (isNotNull(children)) { + if (!isArray(children)) { children = [children]; } } else { children = []; } if (arguments.length > 3) { - for (var i = 3; i < arguments.length; i++) { - if (BI.isArray(arguments[i])) { + for (let i = 3; i < arguments.length; i++) { + if (isArray(arguments[i])) { children = children.concat(arguments[i]); } else { children.push(arguments[i]); } } } - if (type === BI.Fragment) { + if (type === Fragment) { return children; } - if (BI.isFunction(type)) { + if (isFunction(type)) { type = type.xtype || type; } if (type === "el") { - return BI.extend({ - el: children[0] + return extend({ + el: children[0], }, props); } if (type === "left") { - return BI.extend({ - left: children + return extend({ + left: children, }, props); } if (type === "right") { - return BI.extend({ - right: children + return extend({ + right: children, }, props); } if (children.length === 1) { - if (BI.isKey(children[0])) { - return BI.extend({ - type: type + if (isKey(children[0])) { + return extend({ + type, }, { text: children[0] }, props); } - if (BI.isFunction(children[0])) { - return BI.extend({ - type: type + if (isFunction(children[0])) { + return extend({ + type, }, { items: children[0] }, props); } } - return BI.extend({ - type: type + return extend({ + type, }, children.length > 0 ? { items: children } : {}, props); -}; +} diff --git a/src/core/index.js b/src/core/index.js index b7cf86b47..37c581af2 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -2,16 +2,18 @@ import * as base from "./2.base"; import * as ob from "./3.ob"; import * as widget from "./4.widget"; import * as inject from "./5.inject"; +import { Plugin } from "./6.plugin"; +import * as h from "./h"; import * as action from "./action"; import * as behavior from "./behavior"; import * as controllers from "./controller"; import * as func from "./func"; import * as structure from "./structure"; -import {StyleLoaderManager} from "./loader/loader.style"; -import "./h"; -import {ShowListener} from "./listener/listener.show"; -import {shortcut} from "./decorator"; +import { StyleLoaderManager } from "./loader/loader.style"; +import { ShowListener } from "./listener/listener.show"; +import { useInWorker } from "./worker"; +export * from "./decorator"; export * from "./2.base"; export * from "./3.ob"; export * from "./4.widget"; @@ -21,6 +23,7 @@ export * from "./behavior"; export * from "./controller"; export * from "./func"; export * from "./structure"; +export * from "./h"; // 有了后删掉 export const emptyFn = () => { }; @@ -28,7 +31,8 @@ export const emptyFn = () => { }; export { StyleLoaderManager, ShowListener, - shortcut + Plugin, + useInWorker }; Object.assign(BI, { @@ -36,6 +40,7 @@ Object.assign(BI, { ...ob, ...widget, ...inject, + Plugin, ...behavior, component: inject.shortcut, ...action, @@ -44,4 +49,6 @@ Object.assign(BI, { StyleLoaderManager, ShowListener, ...structure, + useInWorker, + ...h, }); diff --git a/src/core/system.js b/src/core/system.js index 545057e49..93be82dc4 100644 --- a/src/core/system.js +++ b/src/core/system.js @@ -3,142 +3,146 @@ * @version 2.0 * Created by windy on 2021/6/30 */ +import { deepExtend, extend, inherit } from "./2.base"; +import { OB } from "./3.ob"; +import { Providers } from "./5.inject"; +import { provider } from "./decorator"; + // 系统参数常量 -!(function () { - var system = { - dependencies: {}, - layoutOptimize: false, - responsiveMode: false, - workerMode: false, - size: { - // 尺寸 - // 通用尺寸 - TOOL_BAR_HEIGHT: 24, - LIST_ITEM_HEIGHT: 24, - TRIGGER_HEIGHT: 24, - TOAST_TOP: 10, - H_GAP_SIZE: "M", - V_GAP_SIZE: "S" - }, - loadingCreator: function(config) { - var loadingSize = (config ? config.loadingSize : "small") || "small"; - - var isIE = BI.isIE(); - - function getSize(v) { - return Math.ceil(v / (loadingSize === "small" ? 2 : 1)); - } - - return { - type: "bi.horizontal", - cls: "bi-loading-widget" + (isIE ? " wave-loading hack" : ""), - height: getSize(60), - width: getSize(60), - hgap: getSize(10), - vgap: 2.5, - items: isIE ? [] : [{ - type: "bi.layout", - cls: "animate-rect rect1", - height: getSize(50), - width: getSize(5) - }, { - type: "bi.layout", - cls: "animate-rect rect2", - height: getSize(50), - width: getSize(5) - }, { - type: "bi.layout", - cls: "animate-rect rect3", - height: getSize(50), - width: getSize(5) - }] - }; +const system = { + dependencies: {}, + layoutOptimize: false, + responsiveMode: false, + workerMode: false, + size: { + // 尺寸 + // 通用尺寸 + TOOL_BAR_HEIGHT: 24, + LIST_ITEM_HEIGHT: 24, + TRIGGER_HEIGHT: 24, + TOAST_TOP: 10, + H_GAP_SIZE: "M", + V_GAP_SIZE: "S", + }, + loadingCreator(config) { + const loadingSize = (config ? config.loadingSize : "small") || "small"; + + const isIE = BI.isIE(); + + function getSize(v) { + return Math.ceil(v / (loadingSize === "small" ? 2 : 1)); } - }; - // 具体尺寸还没定,先写着 - var sizeMap = { - "S": 10, - "M": 20, - "L": 24 - }; + return { + type: "bi.horizontal", + cls: `bi-loading-widget${isIE ? " wave-loading hack" : ""}`, + height: getSize(60), + width: getSize(60), + hgap: getSize(10), + vgap: 2.5, + items: isIE ? [] : [{ + type: "bi.layout", + cls: "animate-rect rect1", + height: getSize(50), + width: getSize(5), + }, { + type: "bi.layout", + cls: "animate-rect rect2", + height: getSize(50), + width: getSize(5), + }, { + type: "bi.layout", + cls: "animate-rect rect3", + height: getSize(50), + width: getSize(5), + }], + }; + }, +}; - function provider () { - this.SYSTEM = system; +// 具体尺寸还没定,先写着 +const sizeMap = { + S: 10, + M: 20, + L: 24, +}; - this.setSize = function (opt) { - BI.deepExtend(system, { size: opt }); - }; +@provider() +export class SystemProvider { + static xtype = "bi.provider.system"; - this.setResponsiveMode = function (mode) { - system.responsiveMode = !!mode; - }; + SYSTEM = system; - this.setWorkerMode = function (mode) { - system.workerMode = !!mode; - }; + setSize(opt) { + deepExtend(system, { size: opt }); + } - this.setLayoutOptimize = function (layoutOptimize) { - system.layoutOptimize = layoutOptimize; - }; + setResponsiveMode(mode) { + system.responsiveMode = !!mode; + } - this.addDependency = function (moduleId, minVersion, maxVersion) { - system.dependencies[moduleId] = { - min: minVersion, - max: maxVersion - }; - }; + setWorkerMode(mode) { + system.workerMode = !!mode; + } - this.addDependencies = function (moduleConfig) { - BI.extend(system.dependencies, moduleConfig); - }; + setLayoutOptimize(layoutOptimize) { + system.layoutOptimize = layoutOptimize; + } - this.setLoadingCreator = function(creator) { - system.loadingCreator = creator; + addDependency(moduleId, minVersion, maxVersion) { + system.dependencies[moduleId] = { + min: minVersion, + max: maxVersion, }; + } + + addDependencies(moduleConfig) { + extend(system.dependencies, moduleConfig); + } - this.$get = function () { - return BI.inherit(BI.OB, { + setLoadingCreator = function(creator) { + system.loadingCreator = creator; + }; - getSize: function () { - var size = system.size; - var H_GAP_SIZE = sizeMap[size.H_GAP_SIZE]; - var V_GAP_SIZE = sizeMap[size.V_GAP_SIZE]; + $get() { + return inherit(OB, { - return BI.extend({}, size, { - H_GAP_SIZE: H_GAP_SIZE, - V_GAP_SIZE: V_GAP_SIZE - }); - }, + getSize () { + const size = system.size; + const H_GAP_SIZE = sizeMap[size.H_GAP_SIZE]; + const V_GAP_SIZE = sizeMap[size.V_GAP_SIZE]; - getResponsiveMode: function () { - return system.responsiveMode; - }, + return extend({}, size, { + H_GAP_SIZE, + V_GAP_SIZE, + }); + }, - getWorkerMode: function () { - return system.workerMode; - }, + getResponsiveMode () { + return system.responsiveMode; + }, - getLayoutOptimize: function () { - return system.layoutOptimize; - }, + getWorkerMode () { + return system.workerMode; + }, - getDependencies: function () { - return system.dependencies; - }, + getLayoutOptimize () { + return system.layoutOptimize; + }, - getLoading: function(config) { - return system.loadingCreator(config); - } - }); - }; - } + getDependencies () { + return system.dependencies; + }, - BI.provider("bi.provider.system", provider); -}()); + getLoading(config) { + return system.loadingCreator(config); + }, + }); + } +} -BI.prepares.push(function () { - BI.SIZE_CONSANTS = BI.Providers.getProvider("bi.provider.system").getSize(); +BI.prepares.push(() => { + BI.SIZE_CONSANTS = Providers.getProvider("bi.provider.system").getSize(); // 不再增加线型的配置了,之后不维护前置版本直接删掉,都用实线连接线 BI.STYLE_CONSTANTS = {}; BI.STYLE_CONSTANTS.LINK_LINE_TYPE = BI.SIZE_CONSANTS.LIST_ITEM_HEIGHT === 24 ? "dashed" : "solid"; diff --git a/src/core/version.js b/src/core/version.js index a850da743..88665e6e3 100644 --- a/src/core/version.js +++ b/src/core/version.js @@ -1 +1 @@ -BI.version = "2.0"; \ No newline at end of file +BI.version = "2.0"; diff --git a/src/core/worker.js b/src/core/worker.js index d3ba08ee4..643e77e1a 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -1,51 +1,54 @@ -!(function () { - BI.useInWorker = function () { - function createWatcher (model, keyOrFn, cb, options) { - if (BI.isPlainObject(cb)) { - options = cb; - cb = cb.handler; - } - options = options || {}; - return Fix.watch(model, keyOrFn, cb, BI.extend(options, { - store: model - })); +import { isPlainObject, extend, each, isArray } from "./2.base"; +import { Models } from "./5.inject"; + +export function useInWorker () { + function createWatcher (model, keyOrFn, cb, options) { + if (isPlainObject(cb)) { + options = cb; + cb = cb.handler; } + options = options || {}; + + return Fix.watch(model, keyOrFn, cb, extend(options, { + store: model, + })); + } - var models = {}, watches = {}; - addEventListener("message", function (e) { - var data = e.data; - switch (data.eventType) { - case "action": - models[data.name][data.action].apply(models[data.name], data.args); - break; - case "destroy": - BI.each(watches[data.name], function (i, unwatches) { - unwatches = BI.isArray(unwatches) ? unwatches : [unwatches]; - BI.each(unwatches, function (j, unwatch) { - unwatch(); - }); - }); - delete models[data.name]; - delete watches[data.name]; - break; - case "create": - var store = models[data.name] = BI.Models.getModel(data.type, data.options); - watches[data.name] = []; - BI.each(data.watches, function (i, key) { - watches[data.name].push(createWatcher(store.model, key, function (newValue, oldValue) { - postMessage(BI.extend({}, data, { - eventType: "watch", - currentWatchType: key - }, {args: [newValue, oldValue]})); - })); - }); - postMessage(BI.extend({}, data, { - eventType: "create" - }, {msg: store.model})); - break; - default: - break; - } - }, false); - }; -}()); + const models = {}, watches = {}; + addEventListener("message", e => { + const data = e.data; + let store; + switch (data.eventType) { + case "action": + models[data.name][data.action](...data.args); + break; + case "destroy": + each(watches[data.name], (i, unwatches) => { + unwatches = isArray(unwatches) ? unwatches : [unwatches]; + each(unwatches, (j, unwatch) => { + unwatch(); + }); + }); + delete models[data.name]; + delete watches[data.name]; + break; + case "create": + store = models[data.name] = Models.getModel(data.type, data.options); + watches[data.name] = []; + each(data.watches, (i, key) => { + watches[data.name].push(createWatcher(store.model, key, (newValue, oldValue) => { + postMessage(extend({}, data, { + eventType: "watch", + currentWatchType: key, + }, { args: [newValue, oldValue] })); + })); + }); + postMessage(extend({}, data, { + eventType: "create", + }, { msg: store.model })); + break; + default: + break; + } + }, false); +}