Browse Source

KERNEL-14001 refactor: plguin、system等

es6
Zhenfei.Li 2 years ago
parent
commit
7ae79f12e3
  1. 7
      src/core/0.foundation.js
  2. 82
      src/core/6.plugin.js
  3. 14
      src/core/decorator.js
  4. 49
      src/core/h.js
  5. 17
      src/core/index.js
  6. 120
      src/core/system.js
  7. 49
      src/core/worker.js

7
src/core/0.foundation.js vendored

@ -1,10 +1,11 @@
/* eslint-disable eqeqeq */
/** /**
* Created by richie on 15/7/8. * Created by richie on 15/7/8.
*/ */
/** /**
* 初始化BI对象 * 初始化BI对象
*/ */
var _global = undefined; let _global = undefined;
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
_global = window; _global = window;
} else if (typeof global !== "undefined") { } else if (typeof global !== "undefined") {
@ -20,8 +21,8 @@ if (_global) {
} }
if (_global.BI == null) { if (_global.BI == null) {
_global.BI = {prepares: []}; _global.BI = { prepares: [] };
} }
if(_global.BI.prepares == null) { if (_global.BI.prepares == null) {
_global.BI.prepares = []; _global.BI.prepares = [];
} }

82
src/core/6.plugin.js

@ -1,46 +1,47 @@
BI.Plugin = BI.Plugin || {}; const _WidgetsPlugin = {};
!(function () { const _ObjectPlugin = {};
var _WidgetsPlugin = {}; const _ConfigPlugin = {};
var _ObjectPlugin = {}; const _ConfigRenderPlugin = {};
var _ConfigPlugin = {}; let _GlobalWidgetConfigFns = [];
var _ConfigRenderPlugin = {}; let __GlobalObjectConfigFns = [];
var _GlobalWidgetConfigFns = [];
var __GlobalObjectConfigFns = []; export const Plugin = {
BI.defaults(BI.Plugin, { getWidget (type, options) {
getWidget: function (type, options) {
if (_GlobalWidgetConfigFns.length > 0) { if (_GlobalWidgetConfigFns.length > 0) {
var fns = _GlobalWidgetConfigFns.slice(0); const fns = _GlobalWidgetConfigFns.slice(0);
for (var i = fns.length - 1; i >= 0; i--) { for (let i = fns.length - 1; i >= 0; i--) {
fns[i](type, options); fns[i](type, options);
} }
} }
var res; let res;
if (_ConfigPlugin[type]) { if (_ConfigPlugin[type]) {
for (var i = _ConfigPlugin[type].length - 1; i >= 0; i--) { for (let i = _ConfigPlugin[type].length - 1; i >= 0; i--) {
if (res = _ConfigPlugin[type][i](options)) { res = _ConfigPlugin[type][i](options);
if (res) {
options = res; options = res;
} }
} }
} }
// Deprecated // Deprecated
if (_WidgetsPlugin[type]) { if (_WidgetsPlugin[type]) {
for (var i = _WidgetsPlugin[type].length - 1; i >= 0; i--) { for (let i = _WidgetsPlugin[type].length - 1; i >= 0; i--) {
if (res = _WidgetsPlugin[type][i](options)) { res = _WidgetsPlugin[type][i](options);
if (res) {
return res; return res;
} }
} }
} }
return options; return options;
}, },
config: function (widgetConfigFn, objectConfigFn) { config (widgetConfigFn, objectConfigFn) {
_GlobalWidgetConfigFns = _GlobalWidgetConfigFns.concat(BI._.isArray(widgetConfigFn) ? widgetConfigFn : [widgetConfigFn]); _GlobalWidgetConfigFns = _GlobalWidgetConfigFns.concat(BI._.isArray(widgetConfigFn) ? widgetConfigFn : [widgetConfigFn]);
__GlobalObjectConfigFns = __GlobalObjectConfigFns.concat(BI._.isArray(objectConfigFn) ? objectConfigFn : [objectConfigFn]); __GlobalObjectConfigFns = __GlobalObjectConfigFns.concat(BI._.isArray(objectConfigFn) ? objectConfigFn : [objectConfigFn]);
}, },
configWidget: function (type, fn, opt) { configWidget (type, fn, opt) {
// opt.single: true 最后一次注册有效 // opt.single: true 最后一次注册有效
if (!_ConfigPlugin[type] || (opt && opt.single)) { if (!_ConfigPlugin[type] || (opt && opt.single)) {
_ConfigPlugin[type] = []; _ConfigPlugin[type] = [];
@ -48,19 +49,21 @@ BI.Plugin = BI.Plugin || {};
_ConfigPlugin[type].push(fn); _ConfigPlugin[type].push(fn);
}, },
getRender: function (type, rendered) { getRender (type, rendered) {
var res; let res;
if (_ConfigRenderPlugin[type]) { if (_ConfigRenderPlugin[type]) {
for (var i = _ConfigRenderPlugin[type].length - 1; i >= 0; i--) { for (let i = _ConfigRenderPlugin[type].length - 1; i >= 0; i--) {
if (res = _ConfigRenderPlugin[type][i](rendered)) { res = _ConfigRenderPlugin[type][i](rendered);
if (res) {
rendered = res; rendered = res;
} }
} }
} }
return rendered; return rendered;
}, },
configRender: function (type, fn) { configRender (type, fn) {
if (!_ConfigRenderPlugin[type]) { if (!_ConfigRenderPlugin[type]) {
_ConfigRenderPlugin[type] = []; _ConfigRenderPlugin[type] = [];
} }
@ -68,7 +71,7 @@ BI.Plugin = BI.Plugin || {};
}, },
// Deprecated // Deprecated
registerWidget: function (type, fn) { registerWidget (type, fn) {
if (!_WidgetsPlugin[type]) { if (!_WidgetsPlugin[type]) {
_WidgetsPlugin[type] = []; _WidgetsPlugin[type] = [];
} }
@ -79,34 +82,36 @@ BI.Plugin = BI.Plugin || {};
}, },
// Deprecated // Deprecated
relieveWidget: function (type) { relieveWidget (type) {
delete _WidgetsPlugin[type]; delete _WidgetsPlugin[type];
}, },
getObject: function (type, object) { getObject (type, object) {
if (__GlobalObjectConfigFns.length > 0) { if (__GlobalObjectConfigFns.length > 0) {
var fns = __GlobalObjectConfigFns.slice(0); const fns = __GlobalObjectConfigFns.slice(0);
for (var i = fns.length - 1; i >= 0; i--) { for (let i = fns.length - 1; i >= 0; i--) {
fns[i](type, object); fns[i](type, object);
} }
} }
let res;
if (_ObjectPlugin[type]) { if (_ObjectPlugin[type]) {
var res; for (let i = 0, len = _ObjectPlugin[type].length; i < len; i++) {
for (var i = 0, len = _ObjectPlugin[type].length; i < len; i++) { res = _ObjectPlugin[type][i](object);
if (res = _ObjectPlugin[type][i](object)) { if (res) {
object = res; object = res;
} }
} }
} }
return res || object; return res || object;
}, },
hasObject: function (type) { hasObject (type) {
return __GlobalObjectConfigFns.length > 0 || !!_ObjectPlugin[type]; return __GlobalObjectConfigFns.length > 0 || !!_ObjectPlugin[type];
}, },
registerObject: function (type, fn) { registerObject (type, fn) {
if (!_ObjectPlugin[type]) { if (!_ObjectPlugin[type]) {
_ObjectPlugin[type] = []; _ObjectPlugin[type] = [];
} }
@ -116,8 +121,7 @@ BI.Plugin = BI.Plugin || {};
_ObjectPlugin[type].push(fn); _ObjectPlugin[type].push(fn);
}, },
relieveObject: function (type) { relieveObject (type) {
delete _ObjectPlugin[type]; delete _ObjectPlugin[type];
} },
}); };
})();

14
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 * 注册widget
*/ */
import { shortcut as biShortcut } from "./5.inject";
export function shortcut() { export function shortcut() {
return function decorator(Target) { return function decorator(Target) {
biShortcut(Target.xtype, Target); biShortcut(Target.xtype, Target);
}; };
} }
/**
* 注册provider
*/
export function provider() {
return function decorator(Target) {
biProvider(Target.xtype, Target);
};
}

49
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) { export function Fragment () {}
if (children != null) {
if (!BI.isArray(children)) { export function h (type, props, children) {
if (isNotNull(children)) {
if (!isArray(children)) {
children = [children]; children = [children];
} }
} else { } else {
children = []; children = [];
} }
if (arguments.length > 3) { if (arguments.length > 3) {
for (var i = 3; i < arguments.length; i++) { for (let i = 3; i < arguments.length; i++) {
if (BI.isArray(arguments[i])) { if (isArray(arguments[i])) {
children = children.concat(arguments[i]); children = children.concat(arguments[i]);
} else { } else {
children.push(arguments[i]); children.push(arguments[i]);
} }
} }
} }
if (type === BI.Fragment) { if (type === Fragment) {
return children; return children;
} }
if (BI.isFunction(type)) { if (isFunction(type)) {
type = type.xtype || type; type = type.xtype || type;
} }
if (type === "el") { if (type === "el") {
return BI.extend({ return extend({
el: children[0] el: children[0],
}, props); }, props);
} }
if (type === "left") { if (type === "left") {
return BI.extend({ return extend({
left: children left: children,
}, props); }, props);
} }
if (type === "right") { if (type === "right") {
return BI.extend({ return extend({
right: children right: children,
}, props); }, props);
} }
if (children.length === 1) { if (children.length === 1) {
if (BI.isKey(children[0])) { if (isKey(children[0])) {
return BI.extend({ return extend({
type: type type,
}, { text: children[0] }, props); }, { text: children[0] }, props);
} }
if (BI.isFunction(children[0])) { if (isFunction(children[0])) {
return BI.extend({ return extend({
type: type type,
}, { items: children[0] }, props); }, { items: children[0] }, props);
} }
} }
return BI.extend({ return extend({
type: type type,
}, children.length > 0 ? { items: children } : {}, props); }, children.length > 0 ? { items: children } : {}, props);
}; }

17
src/core/index.js

@ -2,16 +2,18 @@ import * as base from "./2.base";
import * as ob from "./3.ob"; import * as ob from "./3.ob";
import * as widget from "./4.widget"; import * as widget from "./4.widget";
import * as inject from "./5.inject"; import * as inject from "./5.inject";
import { Plugin } from "./6.plugin";
import * as h from "./h";
import * as action from "./action"; import * as action from "./action";
import * as behavior from "./behavior"; import * as behavior from "./behavior";
import * as controllers from "./controller"; import * as controllers from "./controller";
import * as func from "./func"; import * as func from "./func";
import * as structure from "./structure"; import * as structure from "./structure";
import {StyleLoaderManager} from "./loader/loader.style"; import { StyleLoaderManager } from "./loader/loader.style";
import "./h"; import { ShowListener } from "./listener/listener.show";
import {ShowListener} from "./listener/listener.show"; import { useInWorker } from "./worker";
import {shortcut} from "./decorator";
export * from "./decorator";
export * from "./2.base"; export * from "./2.base";
export * from "./3.ob"; export * from "./3.ob";
export * from "./4.widget"; export * from "./4.widget";
@ -21,6 +23,7 @@ export * from "./behavior";
export * from "./controller"; export * from "./controller";
export * from "./func"; export * from "./func";
export * from "./structure"; export * from "./structure";
export * from "./h";
// 有了后删掉 // 有了后删掉
export const emptyFn = () => { }; export const emptyFn = () => { };
@ -28,7 +31,8 @@ export const emptyFn = () => { };
export { export {
StyleLoaderManager, StyleLoaderManager,
ShowListener, ShowListener,
shortcut Plugin,
useInWorker
}; };
Object.assign(BI, { Object.assign(BI, {
@ -36,6 +40,7 @@ Object.assign(BI, {
...ob, ...ob,
...widget, ...widget,
...inject, ...inject,
Plugin,
...behavior, ...behavior,
component: inject.shortcut, component: inject.shortcut,
...action, ...action,
@ -44,4 +49,6 @@ Object.assign(BI, {
StyleLoaderManager, StyleLoaderManager,
ShowListener, ShowListener,
...structure, ...structure,
useInWorker,
...h,
}); });

120
src/core/system.js

@ -3,9 +3,13 @@
* @version 2.0 * @version 2.0
* Created by windy on 2021/6/30 * 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 () { const system = {
var system = {
dependencies: {}, dependencies: {},
layoutOptimize: false, layoutOptimize: false,
responsiveMode: false, responsiveMode: false,
@ -18,12 +22,12 @@
TRIGGER_HEIGHT: 24, TRIGGER_HEIGHT: 24,
TOAST_TOP: 10, TOAST_TOP: 10,
H_GAP_SIZE: "M", H_GAP_SIZE: "M",
V_GAP_SIZE: "S" V_GAP_SIZE: "S",
}, },
loadingCreator: function(config) { loadingCreator(config) {
var loadingSize = (config ? config.loadingSize : "small") || "small"; const loadingSize = (config ? config.loadingSize : "small") || "small";
var isIE = BI.isIE(); const isIE = BI.isIE();
function getSize(v) { function getSize(v) {
return Math.ceil(v / (loadingSize === "small" ? 2 : 1)); return Math.ceil(v / (loadingSize === "small" ? 2 : 1));
@ -31,7 +35,7 @@
return { return {
type: "bi.horizontal", type: "bi.horizontal",
cls: "bi-loading-widget" + (isIE ? " wave-loading hack" : ""), cls: `bi-loading-widget${isIE ? " wave-loading hack" : ""}`,
height: getSize(60), height: getSize(60),
width: getSize(60), width: getSize(60),
hgap: getSize(10), hgap: getSize(10),
@ -40,105 +44,105 @@
type: "bi.layout", type: "bi.layout",
cls: "animate-rect rect1", cls: "animate-rect rect1",
height: getSize(50), height: getSize(50),
width: getSize(5) width: getSize(5),
}, { }, {
type: "bi.layout", type: "bi.layout",
cls: "animate-rect rect2", cls: "animate-rect rect2",
height: getSize(50), height: getSize(50),
width: getSize(5) width: getSize(5),
}, { }, {
type: "bi.layout", type: "bi.layout",
cls: "animate-rect rect3", cls: "animate-rect rect3",
height: getSize(50), height: getSize(50),
width: getSize(5) width: getSize(5),
}] }],
};
}
}; };
},
};
// 具体尺寸还没定,先写着 // 具体尺寸还没定,先写着
var sizeMap = { const sizeMap = {
"S": 10, S: 10,
"M": 20, M: 20,
"L": 24 L: 24,
}; };
function provider () { @provider()
this.SYSTEM = system; export class SystemProvider {
static xtype = "bi.provider.system";
this.setSize = function (opt) { SYSTEM = system;
BI.deepExtend(system, { size: opt });
};
this.setResponsiveMode = function (mode) { setSize(opt) {
deepExtend(system, { size: opt });
}
setResponsiveMode(mode) {
system.responsiveMode = !!mode; system.responsiveMode = !!mode;
}; }
this.setWorkerMode = function (mode) { setWorkerMode(mode) {
system.workerMode = !!mode; system.workerMode = !!mode;
}; }
this.setLayoutOptimize = function (layoutOptimize) { setLayoutOptimize(layoutOptimize) {
system.layoutOptimize = layoutOptimize; system.layoutOptimize = layoutOptimize;
}; }
this.addDependency = function (moduleId, minVersion, maxVersion) { addDependency(moduleId, minVersion, maxVersion) {
system.dependencies[moduleId] = { system.dependencies[moduleId] = {
min: minVersion, min: minVersion,
max: maxVersion max: maxVersion,
};
}; };
}
this.addDependencies = function (moduleConfig) { addDependencies(moduleConfig) {
BI.extend(system.dependencies, moduleConfig); extend(system.dependencies, moduleConfig);
}; }
this.setLoadingCreator = function(creator) { setLoadingCreator = function(creator) {
system.loadingCreator = creator; system.loadingCreator = creator;
}; };
this.$get = function () { $get() {
return BI.inherit(BI.OB, { return inherit(OB, {
getSize: function () { getSize () {
var size = system.size; const size = system.size;
var H_GAP_SIZE = sizeMap[size.H_GAP_SIZE]; const H_GAP_SIZE = sizeMap[size.H_GAP_SIZE];
var V_GAP_SIZE = sizeMap[size.V_GAP_SIZE]; const V_GAP_SIZE = sizeMap[size.V_GAP_SIZE];
return BI.extend({}, size, { return extend({}, size, {
H_GAP_SIZE: H_GAP_SIZE, H_GAP_SIZE,
V_GAP_SIZE: V_GAP_SIZE V_GAP_SIZE,
}); });
}, },
getResponsiveMode: function () { getResponsiveMode () {
return system.responsiveMode; return system.responsiveMode;
}, },
getWorkerMode: function () { getWorkerMode () {
return system.workerMode; return system.workerMode;
}, },
getLayoutOptimize: function () { getLayoutOptimize () {
return system.layoutOptimize; return system.layoutOptimize;
}, },
getDependencies: function () { getDependencies () {
return system.dependencies; return system.dependencies;
}, },
getLoading: function(config) { getLoading(config) {
return system.loadingCreator(config); return system.loadingCreator(config);
} },
}); });
};
} }
}
BI.provider("bi.provider.system", provider); BI.prepares.push(() => {
}()); BI.SIZE_CONSANTS = Providers.getProvider("bi.provider.system").getSize();
BI.prepares.push(function () {
BI.SIZE_CONSANTS = BI.Providers.getProvider("bi.provider.system").getSize();
// 不再增加线型的配置了,之后不维护前置版本直接删掉,都用实线连接线 // 不再增加线型的配置了,之后不维护前置版本直接删掉,都用实线连接线
BI.STYLE_CONSTANTS = {}; BI.STYLE_CONSTANTS = {};
BI.STYLE_CONSTANTS.LINK_LINE_TYPE = BI.SIZE_CONSANTS.LIST_ITEM_HEIGHT === 24 ? "dashed" : "solid"; BI.STYLE_CONSTANTS.LINK_LINE_TYPE = BI.SIZE_CONSANTS.LIST_ITEM_HEIGHT === 24 ? "dashed" : "solid";

49
src/core/worker.js

@ -1,27 +1,31 @@
!(function () { import { isPlainObject, extend, each, isArray } from "./2.base";
BI.useInWorker = function () { import { Models } from "./5.inject";
export function useInWorker () {
function createWatcher (model, keyOrFn, cb, options) { function createWatcher (model, keyOrFn, cb, options) {
if (BI.isPlainObject(cb)) { if (isPlainObject(cb)) {
options = cb; options = cb;
cb = cb.handler; cb = cb.handler;
} }
options = options || {}; options = options || {};
return Fix.watch(model, keyOrFn, cb, BI.extend(options, {
store: model return Fix.watch(model, keyOrFn, cb, extend(options, {
store: model,
})); }));
} }
var models = {}, watches = {}; const models = {}, watches = {};
addEventListener("message", function (e) { addEventListener("message", e => {
var data = e.data; const data = e.data;
let store;
switch (data.eventType) { switch (data.eventType) {
case "action": case "action":
models[data.name][data.action].apply(models[data.name], data.args); models[data.name][data.action](...data.args);
break; break;
case "destroy": case "destroy":
BI.each(watches[data.name], function (i, unwatches) { each(watches[data.name], (i, unwatches) => {
unwatches = BI.isArray(unwatches) ? unwatches : [unwatches]; unwatches = isArray(unwatches) ? unwatches : [unwatches];
BI.each(unwatches, function (j, unwatch) { each(unwatches, (j, unwatch) => {
unwatch(); unwatch();
}); });
}); });
@ -29,23 +33,22 @@
delete watches[data.name]; delete watches[data.name];
break; break;
case "create": case "create":
var store = models[data.name] = BI.Models.getModel(data.type, data.options); store = models[data.name] = Models.getModel(data.type, data.options);
watches[data.name] = []; watches[data.name] = [];
BI.each(data.watches, function (i, key) { each(data.watches, (i, key) => {
watches[data.name].push(createWatcher(store.model, key, function (newValue, oldValue) { watches[data.name].push(createWatcher(store.model, key, (newValue, oldValue) => {
postMessage(BI.extend({}, data, { postMessage(extend({}, data, {
eventType: "watch", eventType: "watch",
currentWatchType: key currentWatchType: key,
}, {args: [newValue, oldValue]})); }, { args: [newValue, oldValue] }));
})); }));
}); });
postMessage(BI.extend({}, data, { postMessage(extend({}, data, {
eventType: "create" eventType: "create",
}, {msg: store.model})); }, { msg: store.model }));
break; break;
default: default:
break; break;
} }
}, false); }, false);
}; }
}());

Loading…
Cancel
Save