Browse Source

Pull request #3320: KERNEL-14001 refactor: plguin、system等

Merge in VISUAL/fineui from ~ZHENFEI.LI/fineui:es6 to es6

* commit '7ae79f12e34d873a2f4c058cf2fe3d880c8eb2a0':
  KERNEL-14001 refactor: plguin、system等
es6
Zhenfei.Li-李振飞 2 years ago
parent
commit
62fe9b5f31
  1. 7
      src/core/0.foundation.js
  2. 212
      src/core/6.plugin.js
  3. 14
      src/core/decorator.js
  4. 49
      src/core/h.js
  5. 17
      src/core/index.js
  6. 232
      src/core/system.js
  7. 101
      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 = [];
} }

212
src/core/6.plugin.js

@ -1,123 +1,127 @@
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) {
if (_GlobalWidgetConfigFns.length > 0) {
getWidget: function (type, options) { const fns = _GlobalWidgetConfigFns.slice(0);
if (_GlobalWidgetConfigFns.length > 0) { for (let i = fns.length - 1; i >= 0; i--) {
var fns = _GlobalWidgetConfigFns.slice(0); fns[i](type, options);
for (var i = fns.length - 1; i >= 0; i--) {
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);
options = res; if (res) {
} options = res;
} }
} }
// Deprecated }
if (_WidgetsPlugin[type]) { // Deprecated
for (var i = _WidgetsPlugin[type].length - 1; i >= 0; i--) { if (_WidgetsPlugin[type]) {
if (res = _WidgetsPlugin[type][i](options)) { for (let i = _WidgetsPlugin[type].length - 1; i >= 0; i--) {
return res; 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)) { configWidget (type, fn, opt) {
_ConfigPlugin[type] = []; // opt.single: true 最后一次注册有效
} if (!_ConfigPlugin[type] || (opt && opt.single)) {
_ConfigPlugin[type].push(fn); _ConfigPlugin[type] = [];
}, }
_ConfigPlugin[type].push(fn);
getRender: function (type, rendered) { },
var res;
if (_ConfigRenderPlugin[type]) { getRender (type, rendered) {
for (var i = _ConfigRenderPlugin[type].length - 1; i >= 0; i--) { let res;
if (res = _ConfigRenderPlugin[type][i](rendered)) { if (_ConfigRenderPlugin[type]) {
rendered = res; for (let i = _ConfigRenderPlugin[type].length - 1; i >= 0; i--) {
} res = _ConfigRenderPlugin[type][i](rendered);
if (res) {
rendered = res;
} }
} }
return rendered; }
},
configRender: function (type, fn) { return rendered;
if (!_ConfigRenderPlugin[type]) { },
_ConfigRenderPlugin[type] = [];
}
_ConfigRenderPlugin[type].push(fn);
},
// Deprecated configRender (type, fn) {
registerWidget: function (type, fn) { if (!_ConfigRenderPlugin[type]) {
if (!_WidgetsPlugin[type]) { _ConfigRenderPlugin[type] = [];
_WidgetsPlugin[type] = []; }
} _ConfigRenderPlugin[type].push(fn);
if (_WidgetsPlugin[type].length > 0) { },
console.log("组件已经注册过了!");
}
_WidgetsPlugin[type].push(fn);
},
// Deprecated // Deprecated
relieveWidget: function (type) { registerWidget (type, fn) {
delete _WidgetsPlugin[type]; if (!_WidgetsPlugin[type]) {
}, _WidgetsPlugin[type] = [];
}
getObject: function (type, object) { if (_WidgetsPlugin[type].length > 0) {
if (__GlobalObjectConfigFns.length > 0) { console.log("组件已经注册过了!");
var fns = __GlobalObjectConfigFns.slice(0); }
for (var i = fns.length - 1; i >= 0; i--) { _WidgetsPlugin[type].push(fn);
fns[i](type, object); },
}
// Deprecated
relieveWidget (type) {
delete _WidgetsPlugin[type];
},
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]) { let res;
var res; if (_ObjectPlugin[type]) {
for (var i = 0, len = _ObjectPlugin[type].length; i < len; i++) { for (let i = 0, len = _ObjectPlugin[type].length; i < len; i++) {
if (res = _ObjectPlugin[type][i](object)) { res = _ObjectPlugin[type][i](object);
object = res; if (res) {
} object = res;
} }
} }
return res || object; }
},
hasObject: function (type) { return res || object;
return __GlobalObjectConfigFns.length > 0 || !!_ObjectPlugin[type]; },
},
registerObject: function (type, fn) { hasObject (type) {
if (!_ObjectPlugin[type]) { return __GlobalObjectConfigFns.length > 0 || !!_ObjectPlugin[type];
_ObjectPlugin[type] = []; },
}
if (_ObjectPlugin[type].length > 0) {
console.log("对象已经注册过了!");
}
_ObjectPlugin[type].push(fn);
},
relieveObject: function (type) { registerObject (type, fn) {
delete _ObjectPlugin[type]; if (!_ObjectPlugin[type]) {
_ObjectPlugin[type] = [];
}
if (_ObjectPlugin[type].length > 0) {
console.log("对象已经注册过了!");
} }
}); _ObjectPlugin[type].push(fn);
})(); },
relieveObject (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,
}); });

232
src/core/system.js

@ -3,142 +3,146 @@
* @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, workerMode: false,
workerMode: false, size: {
size: { // 尺寸
// 尺寸 // 通用尺寸
// 通用尺寸 TOOL_BAR_HEIGHT: 24,
TOOL_BAR_HEIGHT: 24, LIST_ITEM_HEIGHT: 24,
LIST_ITEM_HEIGHT: 24, 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(config) {
loadingCreator: function(config) { const loadingSize = (config ? config.loadingSize : "small") || "small";
var loadingSize = (config ? config.loadingSize : "small") || "small";
const isIE = BI.isIE();
var 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));
}
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)
}]
};
} }
};
// 具体尺寸还没定,先写着 return {
var sizeMap = { type: "bi.horizontal",
"S": 10, cls: `bi-loading-widget${isIE ? " wave-loading hack" : ""}`,
"M": 20, height: getSize(60),
"L": 24 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) { @provider()
BI.deepExtend(system, { size: opt }); export class SystemProvider {
}; static xtype = "bi.provider.system";
this.setResponsiveMode = function (mode) { SYSTEM = system;
system.responsiveMode = !!mode;
};
this.setWorkerMode = function (mode) { setSize(opt) {
system.workerMode = !!mode; deepExtend(system, { size: opt });
}; }
this.setLayoutOptimize = function (layoutOptimize) { setResponsiveMode(mode) {
system.layoutOptimize = layoutOptimize; system.responsiveMode = !!mode;
}; }
this.addDependency = function (moduleId, minVersion, maxVersion) { setWorkerMode(mode) {
system.dependencies[moduleId] = { system.workerMode = !!mode;
min: minVersion, }
max: maxVersion
};
};
this.addDependencies = function (moduleConfig) { setLayoutOptimize(layoutOptimize) {
BI.extend(system.dependencies, moduleConfig); system.layoutOptimize = layoutOptimize;
}; }
this.setLoadingCreator = function(creator) { addDependency(moduleId, minVersion, maxVersion) {
system.loadingCreator = creator; system.dependencies[moduleId] = {
min: minVersion,
max: maxVersion,
}; };
}
addDependencies(moduleConfig) {
extend(system.dependencies, moduleConfig);
}
this.$get = function () { setLoadingCreator = function(creator) {
return BI.inherit(BI.OB, { system.loadingCreator = creator;
};
getSize: function () { $get() {
var size = system.size; return inherit(OB, {
var H_GAP_SIZE = sizeMap[size.H_GAP_SIZE];
var V_GAP_SIZE = sizeMap[size.V_GAP_SIZE];
return BI.extend({}, size, { getSize () {
H_GAP_SIZE: H_GAP_SIZE, const size = system.size;
V_GAP_SIZE: V_GAP_SIZE const H_GAP_SIZE = sizeMap[size.H_GAP_SIZE];
}); const V_GAP_SIZE = sizeMap[size.V_GAP_SIZE];
},
getResponsiveMode: function () { return extend({}, size, {
return system.responsiveMode; H_GAP_SIZE,
}, V_GAP_SIZE,
});
},
getWorkerMode: function () { getResponsiveMode () {
return system.workerMode; return system.responsiveMode;
}, },
getLayoutOptimize: function () { getWorkerMode () {
return system.layoutOptimize; return system.workerMode;
}, },
getDependencies: function () { getLayoutOptimize () {
return system.dependencies; return system.layoutOptimize;
}, },
getLoading: function(config) { getDependencies () {
return system.loadingCreator(config); return system.dependencies;
} },
});
};
}
BI.provider("bi.provider.system", provider); getLoading(config) {
}()); return system.loadingCreator(config);
},
});
}
}
BI.prepares.push(function () { BI.prepares.push(() => {
BI.SIZE_CONSANTS = BI.Providers.getProvider("bi.provider.system").getSize(); BI.SIZE_CONSANTS = 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";

101
src/core/worker.js

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

Loading…
Cancel
Save