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. 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.
*/
/**
* 初始化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 = [];
}

82
src/core/6.plugin.js

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

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) {
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);
};
}

17
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,
});

120
src/core/system.js

@ -3,9 +3,13 @@
* @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 = {
const system = {
dependencies: {},
layoutOptimize: false,
responsiveMode: false,
@ -18,12 +22,12 @@
TRIGGER_HEIGHT: 24,
TOAST_TOP: 10,
H_GAP_SIZE: "M",
V_GAP_SIZE: "S"
V_GAP_SIZE: "S",
},
loadingCreator: function(config) {
var loadingSize = (config ? config.loadingSize : "small") || "small";
loadingCreator(config) {
const loadingSize = (config ? config.loadingSize : "small") || "small";
var isIE = BI.isIE();
const isIE = BI.isIE();
function getSize(v) {
return Math.ceil(v / (loadingSize === "small" ? 2 : 1));
@ -31,7 +35,7 @@
return {
type: "bi.horizontal",
cls: "bi-loading-widget" + (isIE ? " wave-loading hack" : ""),
cls: `bi-loading-widget${isIE ? " wave-loading hack" : ""}`,
height: getSize(60),
width: getSize(60),
hgap: getSize(10),
@ -40,105 +44,105 @@
type: "bi.layout",
cls: "animate-rect rect1",
height: getSize(50),
width: getSize(5)
width: getSize(5),
}, {
type: "bi.layout",
cls: "animate-rect rect2",
height: getSize(50),
width: getSize(5)
width: getSize(5),
}, {
type: "bi.layout",
cls: "animate-rect rect3",
height: getSize(50),
width: getSize(5)
}]
};
}
width: getSize(5),
}],
};
},
};
// 具体尺寸还没定,先写着
var sizeMap = {
"S": 10,
"M": 20,
"L": 24
};
// 具体尺寸还没定,先写着
const sizeMap = {
S: 10,
M: 20,
L: 24,
};
function provider () {
this.SYSTEM = system;
@provider()
export class SystemProvider {
static xtype = "bi.provider.system";
this.setSize = function (opt) {
BI.deepExtend(system, { size: opt });
};
SYSTEM = system;
this.setResponsiveMode = function (mode) {
setSize(opt) {
deepExtend(system, { size: opt });
}
setResponsiveMode(mode) {
system.responsiveMode = !!mode;
};
}
this.setWorkerMode = function (mode) {
setWorkerMode(mode) {
system.workerMode = !!mode;
};
}
this.setLayoutOptimize = function (layoutOptimize) {
setLayoutOptimize(layoutOptimize) {
system.layoutOptimize = layoutOptimize;
};
}
this.addDependency = function (moduleId, minVersion, maxVersion) {
addDependency(moduleId, minVersion, maxVersion) {
system.dependencies[moduleId] = {
min: minVersion,
max: maxVersion
};
max: maxVersion,
};
}
this.addDependencies = function (moduleConfig) {
BI.extend(system.dependencies, moduleConfig);
};
addDependencies(moduleConfig) {
extend(system.dependencies, moduleConfig);
}
this.setLoadingCreator = function(creator) {
setLoadingCreator = function(creator) {
system.loadingCreator = creator;
};
this.$get = function () {
return BI.inherit(BI.OB, {
$get() {
return inherit(OB, {
getSize: function () {
var size = system.size;
var H_GAP_SIZE = sizeMap[size.H_GAP_SIZE];
var V_GAP_SIZE = sizeMap[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];
return BI.extend({}, size, {
H_GAP_SIZE: H_GAP_SIZE,
V_GAP_SIZE: V_GAP_SIZE
return extend({}, size, {
H_GAP_SIZE,
V_GAP_SIZE,
});
},
getResponsiveMode: function () {
getResponsiveMode () {
return system.responsiveMode;
},
getWorkerMode: function () {
getWorkerMode () {
return system.workerMode;
},
getLayoutOptimize: function () {
getLayoutOptimize () {
return system.layoutOptimize;
},
getDependencies: function () {
getDependencies () {
return system.dependencies;
},
getLoading: function(config) {
getLoading(config) {
return system.loadingCreator(config);
}
},
});
};
}
}
BI.provider("bi.provider.system", provider);
}());
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";

49
src/core/worker.js

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

Loading…
Cancel
Save