Browse Source

KERNEL-14001 refactor: inject的es6化

es6
Zhenfei.Li 2 years ago
parent
commit
3e98c47b71
  1. 19
      src/base/1.pane.js
  2. 12
      src/core/3.ob.js
  3. 53
      src/core/4.widget.js
  4. 316
      src/core/5.inject.js
  5. 3
      src/core/decorator.js
  6. 6
      src/core/index.js

19
src/base/1.pane.js

@ -6,7 +6,8 @@
* @extends BI.Widget * @extends BI.Widget
* @abstract * @abstract
*/ */
import { Widget, shortcut, isNotEmptyString, extend, isNull, isEmpty } from "../core"; import { Widget, shortcut, isNotEmptyString, extend, isNull, isEmpty, createWidget, Providers } from "../core";
import { Layers } from "./0.base";
@shortcut() @shortcut()
export default class Pane extends Widget { export default class Pane extends Widget {
@ -27,7 +28,7 @@ export default class Pane extends Widget {
_assertTip() { _assertTip() {
if (!this._tipText) { if (!this._tipText) {
BI.createWidget({ createWidget({
type: "bi.absolute_center_adapt", type: "bi.absolute_center_adapt",
element: this, element: this,
items: [{ items: [{
@ -45,7 +46,7 @@ export default class Pane extends Widget {
loading() { loading() {
const o = this.options; const o = this.options;
const loadingAnimation = BI.createWidget(BI.Providers.getProvider("bi.provider.system").getLoading({ const loadingAnimation = createWidget(Providers.getProvider("bi.provider.system").getLoading({
loadingSize: o.loadingSize, loadingSize: o.loadingSize,
context: this, context: this,
})); }));
@ -53,18 +54,18 @@ export default class Pane extends Widget {
// loading的异步情况下由loaded后对面板的populate的时机决定 // loading的异步情况下由loaded后对面板的populate的时机决定
this.setTipVisible(false); this.setTipVisible(false);
if (o.overlap === true) { if (o.overlap === true) {
if (!BI.Layers.has(this.getName() + "-loading")) { if (!Layers.has(this.getName() + "-loading")) {
BI.createWidget({ createWidget({
type: "bi.center_adapt", type: "bi.center_adapt",
cls: "loading-container", cls: "loading-container",
items: this._getLoadingTipItems(loadingAnimation), items: this._getLoadingTipItems(loadingAnimation),
element: BI.Layers.make(this.getName() + "-loading", this), element: Layers.make(this.getName() + "-loading", this),
}); });
} }
BI.Layers.show(this.getName() + "-loading"); Layers.show(this.getName() + "-loading");
} else if (isNull(this._loading)) { } else if (isNull(this._loading)) {
loadingAnimation.element.css("zIndex", 1); loadingAnimation.element.css("zIndex", 1);
BI.createWidget({ createWidget({
type: "bi.center_adapt", type: "bi.center_adapt",
element: this, element: this,
cls: "loading-container", cls: "loading-container",
@ -101,7 +102,7 @@ export default class Pane extends Widget {
} }
loaded() { loaded() {
BI.Layers.remove(this.getName() + "-loading"); Layers.remove(this.getName() + "-loading");
this._loading && this._loading.destroy(); this._loading && this._loading.destroy();
this.options.onLoaded(); this.options.onLoaded();
this.fireEvent(Pane.EVENT_LOADED); this.fireEvent(Pane.EVENT_LOADED);

12
src/core/3.ob.js

@ -1,6 +1,6 @@
import { isFunction, isArray, isObject, isArguments, reduce, bind } from "./2.base"; import { isFunction, isArray, isObject, isArguments, reduce, bind, extend } from "./2.base";
function extend() { function obExtend() {
let target = arguments[0] || {}, length = arguments.length, i = 1, name, copy; let target = arguments[0] || {}, length = arguments.length, i = 1, name, copy;
for (; i < length; i++) { for (; i < length; i++) {
// Only deal with non-null/undefined values // Only deal with non-null/undefined values
@ -52,11 +52,11 @@ export default class OB {
if (isFunction(this.props)) { if (isFunction(this.props)) {
props = this.props(config); props = this.props(config);
} }
const defaultProps = extend(this._defaultConfig(config), props); const defaultProps = obExtend(this._defaultConfig(config), props);
const modifiedDefaultProps = (config && config.type && OB.configFunctions[config.type + ".props"]) ? reduce(OB.configFunctions[config.type + ".props"], function (value, conf, index) { const modifiedDefaultProps = (config && config.type && OB.configFunctions[config.type + ".props"]) ? reduce(OB.configFunctions[config.type + ".props"], function (value, conf, index) {
return extend(conf, value.fn(defaultProps, config, value.opt)); return obExtend(conf, value.fn(defaultProps, config, value.opt));
}, {}) : null; }, {}) : null;
this.options = extend(defaultProps, modifiedDefaultProps, config); this.options = obExtend(defaultProps, modifiedDefaultProps, config);
} }
_init() { _init() {
@ -216,4 +216,4 @@ export default class OB {
} }
} }
BI.extend(BI, { OB }); extend(BI, { OB });

53
src/core/4.widget.js

@ -8,6 +8,7 @@
import { isFunction, isArray, each, extend, isPlainObject, isNull, uniqueId, isWidget, isWidthOrHeight, isKey, remove, any, isNotNull } from "./2.base"; import { isFunction, isArray, each, extend, isPlainObject, isNull, uniqueId, isWidget, isWidthOrHeight, isKey, remove, any, isNotNull } from "./2.base";
import OB from "./3.ob"; import OB from "./3.ob";
import { Providers, _lazyCreateWidget } from "./5.inject";
const cancelAnimationFrame = const cancelAnimationFrame =
_global.cancelAnimationFrame || _global.cancelAnimationFrame ||
@ -360,7 +361,7 @@ export default class Widget extends OB {
_initElement() { _initElement() {
this.__isMounting = true; this.__isMounting = true;
// 当开启worker模式时,可以通过$render来实现另一种效果 // 当开启worker模式时,可以通过$render来实现另一种效果
const workerMode = BI.Providers.getProvider("bi.provider.system").getWorkerMode(); const workerMode = Providers.getProvider("bi.provider.system").getWorkerMode();
const render = isFunction(this.options.render) ? this.options.render : (workerMode ? (this.$render || this.render) : this.render); const render = isFunction(this.options.render) ? this.options.render : (workerMode ? (this.$render || this.render) : this.render);
let els = render && render.call(this); let els = render && render.call(this);
els = this.options.configRender ? this.options.configRender.call(this, els) : els; els = this.options.configRender ? this.options.configRender.call(this, els) : els;
@ -372,7 +373,7 @@ export default class Widget extends OB {
if (isArray(els)) { if (isArray(els)) {
each(els, (i, el) => { each(els, (i, el) => {
if (el) { if (el) {
BI._lazyCreateWidget(el, { _lazyCreateWidget(el, {
element: this element: this
}); });
} }
@ -840,9 +841,6 @@ export default class Widget extends OB {
} }
}; };
extend(BI, { Widget });
let context = null, current = null; let context = null, current = null;
const contextStack = [], currentStack = []; const contextStack = [], currentStack = [];
@ -875,7 +873,7 @@ function popTarget() {
Widget.current = current = currentStack.pop(); Widget.current = current = currentStack.pop();
} }
BI.useStore = function (_store) { export function useStore(_store) {
if (current && current.store) { if (current && current.store) {
return current.store; return current.store;
} }
@ -908,9 +906,9 @@ BI.useStore = function (_store) {
}; };
return current.$storeDelegate = delegate; return current.$storeDelegate = delegate;
} }
}; }
BI.useContext = function (inject) { export function useContext(inject) {
// 通过组件找最近的store // 通过组件找最近的store
const vm = Widget.findStore(Widget.current || Widget.context); const vm = Widget.findStore(Widget.current || Widget.context);
if (vm) { if (vm) {
@ -934,9 +932,9 @@ BI.useContext = function (inject) {
} }
} }
return vm; return vm;
}; }
BI.watch = function (vm, watch, handler) { export function watch(vm, watch, handler) {
// 必须要保证组件当前环境存在 // 必须要保证组件当前环境存在
if (Widget.current) { if (Widget.current) {
if (vm instanceof BI.Model) { if (vm instanceof BI.Model) {
@ -970,9 +968,9 @@ BI.watch = function (vm, watch, handler) {
Widget.current.$watchDelayCallbacks || (Widget.current.$watchDelayCallbacks = []); Widget.current.$watchDelayCallbacks || (Widget.current.$watchDelayCallbacks = []);
Widget.current.$watchDelayCallbacks.push([watch, handler]); Widget.current.$watchDelayCallbacks.push([watch, handler]);
} }
}; }
BI.onBeforeMount = function (beforeMount) { export function onBeforeMount(beforeMount) {
if (current) { if (current) {
if (current.__isMounting) { if (current.__isMounting) {
beforeMount(); beforeMount();
@ -985,8 +983,9 @@ BI.onBeforeMount = function (beforeMount) {
} }
current.beforeMount.push(beforeMount); current.beforeMount.push(beforeMount);
} }
}; }
BI.onMounted = function (mounted) {
export function onMounted(mounted) {
if (current) { if (current) {
if (current._isMounted && !this.__async) { if (current._isMounted && !this.__async) {
mounted(); mounted();
@ -1000,7 +999,8 @@ BI.onMounted = function (mounted) {
current.mounted.push(mounted); current.mounted.push(mounted);
} }
}; };
BI.onBeforeUnmount = function (beforeDestroy) {
export function onBeforeUnmount(beforeDestroy) {
if (current) { if (current) {
if (!current.beforeDestroy) { if (!current.beforeDestroy) {
current.beforeDestroy = []; current.beforeDestroy = [];
@ -1009,8 +1009,9 @@ BI.onBeforeUnmount = function (beforeDestroy) {
} }
current.beforeDestroy.push(beforeDestroy); current.beforeDestroy.push(beforeDestroy);
} }
}; }
BI.onUnmounted = function (destroyed) {
export function onUnmounted(destroyed) {
if (current) { if (current) {
if (!current.destroyed) { if (!current.destroyed) {
current.destroyed = []; current.destroyed = [];
@ -1019,7 +1020,7 @@ BI.onUnmounted = function (destroyed) {
} }
current.destroyed.push(destroyed); current.destroyed.push(destroyed);
} }
}; }
Widget.registerRenderEngine = function (engine) { Widget.registerRenderEngine = function (engine) {
Widget._renderEngine = engine; Widget._renderEngine = engine;
@ -1043,7 +1044,7 @@ Widget.registerRenderEngine({
} }
}); });
BI.mount = function (widget, container, predicate, hydrate) { export function mount(widget, container, predicate, hydrate) {
if (hydrate === true) { if (hydrate === true) {
// 将widget的element元素都挂载好,并建立相互关系 // 将widget的element元素都挂载好,并建立相互关系
widget.element.data("__widgets", [widget]); widget.element.data("__widgets", [widget]);
@ -1077,4 +1078,16 @@ BI.mount = function (widget, container, predicate, hydrate) {
Widget._renderEngine.createElement(container).append(widget.element); Widget._renderEngine.createElement(container).append(widget.element);
} }
return widget._mount(true, false, false, predicate); return widget._mount(true, false, false, predicate);
}; }
extend(BI, {
Widget,
useStore,
useContext,
watch,
onBeforeMount,
onMounted,
onBeforeUnmount,
onUnmounted,
mount,
});

316
src/core/5.inject.js

@ -1,5 +1,8 @@
(function () { import { isFunction, isNull, isNotNull, isArray, each, isWidget, extend, init, isEmpty, remove } from "./2.base";
var moduleInjection = {}, moduleInjectionMap = { import OB from "./3.ob";
import Widget from "./4.widget"
let moduleInjection = {}, moduleInjectionMap = {
components: {}, components: {},
constants: {}, constants: {},
stores: {}, stores: {},
@ -7,16 +10,17 @@
models: {}, models: {},
providers: {} providers: {}
}; };
BI.module = BI.module || function (xtype, cls) {
export function module(xtype, cls) {
if (moduleInjection[xtype] != null) { if (moduleInjection[xtype] != null) {
_global.console && console.error("module: [" + xtype + "] 已经注册过了"); _global.console && console.error("module: [" + xtype + "] 已经注册过了");
} else { } else {
if (BI.isFunction(cls)) { if (isFunction(cls)) {
cls = cls(); cls = cls();
} }
for (var k in moduleInjectionMap) { for (const k in moduleInjectionMap) {
if (cls[k]) { if (cls[k]) {
for (var key in cls[k]) { for (const key in cls[k]) {
if (!moduleInjectionMap[k]) { if (!moduleInjectionMap[k]) {
continue; continue;
} }
@ -33,112 +37,103 @@
moduleInjection[xtype] = cls; moduleInjection[xtype] = cls;
} }
return function () { return () => Modules.getModule(xtype);
return BI.Modules.getModule(xtype); }
}; BI.module = BI.module || module;
};
var constantInjection = {}; let constantInjection = {};
BI.constant = BI.constant || function (xtype, cls) { export function constant(xtype, cls) {
if (constantInjection[xtype] != null) { if (constantInjection[xtype] != null) {
_global.console && console.error("constant: [" + xtype + "]已经注册过了"); _global.console && console.error("constant: [" + xtype + "]已经注册过了");
} else { } else {
constantInjection[xtype] = cls; constantInjection[xtype] = cls;
} }
return function () { return () => Constants.getConstant(xtype);
return BI.Constants.getConstant(xtype); }
}; BI.constant = BI.constant || constant;
};
var modelInjection = {}; let modelInjection = {};
BI.model = BI.model || function (xtype, cls) { export function model(xtype, cls) {
if (modelInjection[xtype] != null) { if (modelInjection[xtype] != null) {
_global.console && console.error("model: [" + xtype + "] 已经注册过了"); _global.console && console.error("model: [" + xtype + "] 已经注册过了");
} else { } else {
modelInjection[xtype] = cls; modelInjection[xtype] = cls;
} }
return function (config) { return (config) => Models.getModel(xtype, config);
return BI.Models.getModel(xtype, config); }
}; BI.model = BI.model || model;
};
var storeInjection = {}; let storeInjection = {};
BI.store = BI.store || function (xtype, cls) { export function store(xtype, cls) {
if (storeInjection[xtype] != null) { if (storeInjection[xtype] != null) {
_global.console && console.error("store: [" + xtype + "] 已经注册过了"); _global.console && console.error("store: [" + xtype + "] 已经注册过了");
} else { } else {
storeInjection[xtype] = cls; storeInjection[xtype] = cls;
} }
return function (config) { return (config) => Stores.getStore(xtype, config);
return BI.Stores.getStore(xtype, config); }
}; BI.store = BI.store || store;
};
var serviceInjection = {}; let serviceInjection = {};
BI.service = BI.service || function (xtype, cls) { export function service(xtype, cls) {
if (serviceInjection[xtype] != null) { if (serviceInjection[xtype] != null) {
_global.console && console.error("service: [" + xtype + "] 已经注册过了"); _global.console && console.error("service: [" + xtype + "] 已经注册过了");
} }
serviceInjection[xtype] = cls; serviceInjection[xtype] = cls;
return function (config) { return (config) => Services.getService(xtype, config);
return BI.Services.getService(xtype, config); }
}; BI.service = BI.service || service;
};
var providerInjection = {}; let providerInjection = {};
BI.provider = BI.provider || function (xtype, cls) { export function provider(xtype, cls) {
if (providerInjection[xtype] != null) { if (providerInjection[xtype] != null) {
_global.console && console.error("provider: [" + xtype + "] 已经注册过了"); _global.console && console.error("provider: [" + xtype + "] 已经注册过了");
} else { } else {
providerInjection[xtype] = cls; providerInjection[xtype] = cls;
} }
return function (config) { return (config) => Providers.getProvider(xtype, config);
return BI.Providers.getProvider(xtype, config); }
}; BI.provider = BI.provider || provider;
};
var configFunctions = BI.OB.configFunctions = {}; let configFunctions = OB.configFunctions = {};
var runConfigFunction = function (type, configFn) { const runConfigFunction = function (type, configFn) {
if (!type || !configFunctions[type]) { if (!type || !configFunctions[type]) {
return false; return false;
} }
var queue = []; let queue = [];
if (configFn) { if (configFn) {
queue = configFunctions[type].filter(function (conf) { queue = configFunctions[type].filter((conf) => conf.fn === configFn);
return conf.fn === configFn; configFunctions[type] = configFunctions[type].filter((conf) => conf.fn !== configFn);
});
configFunctions[type] = configFunctions[type].filter(function (conf) {
return conf.fn !== configFn;
});
} else { } else {
queue = configFunctions[type]; queue = configFunctions[type];
delete configFunctions[type]; delete configFunctions[type];
} }
var dependencies = BI.Providers.getProvider("bi.provider.system").getDependencies(); const dependencies = Providers.getProvider("bi.provider.system").getDependencies();
var modules = moduleInjectionMap.components[type] const modules = moduleInjectionMap.components[type]
|| moduleInjectionMap.constants[type] || moduleInjectionMap.constants[type]
|| moduleInjectionMap.services[type] || moduleInjectionMap.services[type]
|| moduleInjectionMap.stores[type] || moduleInjectionMap.stores[type]
|| moduleInjectionMap.models[type] || moduleInjectionMap.models[type]
|| moduleInjectionMap.providers[type]; || moduleInjectionMap.providers[type];
for (var i = 0; i < queue.length; i++) { for (let i = 0; i < queue.length; i++) {
var conf = queue[i]; const conf = queue[i];
var version = conf.opt.version; const version = conf.opt.version;
var fn = conf.fn; const fn = conf.fn;
if (modules && version) { if (modules && version) {
var findVersion = false; let findVersion = false;
for (var j = 0; j < modules.length; j++) { let module;
var module = modules[j]; for (let j = 0; j < modules.length; j++) {
module = modules[j];
if (module && dependencies[module.moduleId] && module.version === version) { if (module && dependencies[module.moduleId] && module.version === version) {
var minVersion = dependencies[module.moduleId].minVersion, const minVersion = dependencies[module.moduleId].minVersion,
maxVersion = dependencies[module.moduleId].maxVersion; maxVersion = dependencies[module.moduleId].maxVersion;
if (minVersion && (moduleInjection[module.moduleId].version || version) < minVersion) { if (minVersion && (moduleInjection[module.moduleId].version || version) < minVersion) {
findVersion = true; findVersion = true;
@ -172,8 +167,9 @@
BI.Plugin.configWidget(type, fn, conf.opt); BI.Plugin.configWidget(type, fn, conf.opt);
} }
}; };
BI.config = BI.config || function (type, configFn, opt) {
if (BI.isFunction(type)) { export function config(type, configFn, opt) {
if (isFunction(type)) {
opt = configFn; opt = configFn;
configFn = type; configFn = type;
type = "bi.provider.system"; type = "bi.provider.system";
@ -203,39 +199,38 @@
if (opt.immediately) { if (opt.immediately) {
return runConfigFunction(type, configFn); return runConfigFunction(type, configFn);
} }
}; }
BI.config = BI.config || config;
BI.getReference = BI.getReference || function (type, fn) { export function getReference(type, fn) {
return BI.Plugin.registerObject(type, fn); return BI.Plugin.registerObject(type, fn);
}; }
BI.getReference = BI.getReference || getReference;
var actions = {}; let actions = {};
var globalAction = []; let globalAction = [];
BI.action = BI.action || function (type, actionFn) { export function action(type, actionFn) {
if (BI.isFunction(type)) { if (isFunction(type)) {
globalAction.push(type); globalAction.push(type);
return function () { return () => {
BI.remove(globalAction, function (idx) { remove(globalAction, (idx) => globalAction.indexOf(actionFn) === idx);
return globalAction.indexOf(actionFn) === idx;
});
}; };
} }
if (!actions[type]) { if (!actions[type]) {
actions[type] = []; actions[type] = [];
} }
actions[type].push(actionFn); actions[type].push(actionFn);
return function () { return () => {
BI.remove(actions[type], function (idx) { remove(actions[type], (idx) => actions[type].indexOf(actionFn) === idx);
return actions[type].indexOf(actionFn) === idx;
});
if (actions[type].length === 0) { if (actions[type].length === 0) {
delete actions[type]; delete actions[type];
} }
}; };
}; }
BI.action = BI.action || action;
var points = {}; let points = {};
BI.point = BI.point || function (type, action, pointFn, after) { export function point(type, action, pointFn, after) {
if (!points[type]) { if (!points[type]) {
points[type] = {}; points[type] = {};
} }
@ -246,9 +241,10 @@
points[type][action][after ? "after" : "before"] = []; points[type][action][after ? "after" : "before"] = [];
} }
points[type][action][after ? "after" : "before"].push(pointFn); points[type][action][after ? "after" : "before"].push(pointFn);
}; }
BI.point = BI.point || point;
BI.Modules = BI.Modules || { export const Modules = {
getModule: function (type) { getModule: function (type) {
if (!moduleInjection[type]) { if (!moduleInjection[type]) {
_global.console && console.error("module: [" + type + "] 未定义"); _global.console && console.error("module: [" + type + "] 未定义");
@ -258,28 +254,30 @@
getAllModules: function () { getAllModules: function () {
return moduleInjection; return moduleInjection;
} }
}; }
BI.Modules = BI.Modules || Modules;
BI.Constants = BI.Constants || { export const Constants = {
getConstant: function (type) { getConstant: function (type) {
if (BI.isNull(constantInjection[type])) { if (isNull(constantInjection[type])) {
_global.console && console.error("constant: [" + type + "] 未定义"); _global.console && console.error("constant: [" + type + "] 未定义");
} }
runConfigFunction(type); runConfigFunction(type);
return BI.isFunction(constantInjection[type]) ? constantInjection[type]() : constantInjection[type]; return isFunction(constantInjection[type]) ? constantInjection[type]() : constantInjection[type];
} }
}; }
BI.Constants = BI.Constants || Constants;
var callPoint = function (inst, types) { const callPoint = function (inst, types) {
types = BI.isArray(types) ? types : [types]; types = isArray(types) ? types : [types];
BI.each(types, function (idx, type) { each(types, (idx, type) => {
if (points[type]) { if (points[type]) {
for (var action in points[type]) { for (const action in points[type]) {
var bfns = points[type][action].before; let bfns = points[type][action].before;
if (bfns) { if (bfns) {
BI.aspect.before(inst, action, function (bfns) { BI.aspect.before(inst, action, function (bfns) {
return function () { return function () {
for (var i = 0, len = bfns.length; i < len; i++) { for (let i = 0, len = bfns.length; i < len; i++) {
try { try {
bfns[i].apply(inst, arguments); bfns[i].apply(inst, arguments);
} catch (e) { } catch (e) {
@ -289,11 +287,11 @@
}; };
}(bfns)); }(bfns));
} }
var afns = points[type][action].after; let afns = points[type][action].after;
if (afns) { if (afns) {
BI.aspect.after(inst, action, function (afns) { BI.aspect.after(inst, action, function (afns) {
return function () { return function () {
for (var i = 0, len = afns.length; i < len; i++) { for (let i = 0, len = afns.length; i < len; i++) {
try { try {
afns[i].apply(inst, arguments); afns[i].apply(inst, arguments);
} catch (e) { } catch (e) {
@ -308,7 +306,7 @@
}); });
}; };
BI.Models = BI.Models || { export const Models = {
getModel: function (type, config) { getModel: function (type, config) {
if (!modelInjection[type]) { if (!modelInjection[type]) {
_global.console && console.error("model: [" + type + "] 未定义"); _global.console && console.error("model: [" + type + "] 未定义");
@ -320,11 +318,11 @@
callPoint(inst, type); callPoint(inst, type);
return inst; return inst;
} }
}; }
BI.Models = BI.Models || Models;
var stores = {};
BI.Stores = BI.Stores || { let stores = {};
export const Stores = {
getStore: function (type, config) { getStore: function (type, config) {
if (!storeInjection[type]) { if (!storeInjection[type]) {
_global.console && console.error("store: [" + type + "] 未定义"); _global.console && console.error("store: [" + type + "] 未定义");
@ -332,19 +330,19 @@
if (stores[type]) { if (stores[type]) {
return stores[type]; return stores[type];
} }
var inst = stores[type] = new storeInjection[type](config); const inst = stores[type] = new storeInjection[type](config);
inst._constructor && inst._constructor(config, function () { inst._constructor && inst._constructor(config, function () {
delete stores[type]; delete stores[type];
}); });
callPoint(inst, type); callPoint(inst, type);
return inst; return inst;
} }
}; }
BI.Stores = BI.Stores || Stores;
var services = {};
BI.Services = BI.Services || { let services = {};
getService: function (type, config) { export const Services = {
getService: (type, config) => {
if (!serviceInjection[type]) { if (!serviceInjection[type]) {
_global.console && console.error("service: [" + type + "] 未定义"); _global.console && console.error("service: [" + type + "] 未定义");
} }
@ -355,13 +353,13 @@
callPoint(services[type], type); callPoint(services[type], type);
return services[type]; return services[type];
} }
}; }
BI.Services = BI.Services || Services;
var providers = {}, let providers = {},
providerInstance = {}; providerInstance = {};
export const Providers = {
BI.Providers = BI.Providers || { getProvider: (type, config) => {
getProvider: function (type, config) {
if (!providerInjection[type]) { if (!providerInjection[type]) {
_global.console && console.error("provider: [" + type + "] 未定义"); _global.console && console.error("provider: [" + type + "] 未定义");
} }
@ -374,11 +372,12 @@
} }
return providerInstance[type]; return providerInstance[type];
} }
}; }
BI.Providers = BI.Providers || Providers;
BI.Actions = BI.Actions || { export const Actions = {
runAction: function (type, event, config) { runAction: function (type, event, config) {
BI.each(actions[type], function (i, act) { each(actions[type], (i, act) => {
try { try {
act(event, config); act(event, config);
} catch (e) { } catch (e) {
@ -388,7 +387,7 @@
}, },
runGlobalAction: function () { runGlobalAction: function () {
var args = [].slice.call(arguments); var args = [].slice.call(arguments);
BI.each(globalAction, function (i, act) { each(globalAction, (i, act) => {
try { try {
act.apply(null, args); act.apply(null, args);
} catch (e) { } catch (e) {
@ -396,10 +395,11 @@
} }
}); });
} }
}; }
BI.Actions = BI.Actions || Actions;
var kv = {}; let kv = {};
BI.shortcut = BI.component = BI.shortcut || function (xtype, cls) { export function shortcut(xtype, cls) {
if (kv[xtype] != null) { if (kv[xtype] != null) {
_global.console && console.error("组件: [" + xtype + "] 已经注册过了"); _global.console && console.error("组件: [" + xtype + "] 已经注册过了");
} }
@ -407,21 +407,22 @@
cls["xtype"] = xtype; cls["xtype"] = xtype;
} }
kv[xtype] = cls; kv[xtype] = cls;
}; }
BI.shortcut = BI.component = BI.shortcut || shortcut;
// 根据配置属性生成widget // 根据配置属性生成widget
var createWidget = function (config, context, lazy) { const createRealWidget = function (config, context, lazy) {
var cls = BI.isFunction(config.type) ? config.type : kv[config.type]; const cls = isFunction(config.type) ? config.type : kv[config.type];
if (!cls) { if (!cls) {
throw new Error("组件: [" + config.type + "] 未定义"); throw new Error("组件: [" + config.type + "] 未定义");
} }
var pushed = false; let pushed = false;
var widget = new cls(); const widget = new cls();
widget._context = BI.Widget.context || context; widget._context = Widget.context || context;
if (!BI.Widget.context && context) { if (!Widget.context && context) {
pushed = true; pushed = true;
BI.Widget.pushContext(context); Widget.pushContext(context);
} }
callPoint(widget, config.type); callPoint(widget, config.type);
widget._initProps(config); widget._initProps(config);
@ -430,13 +431,13 @@
// if (!lazy || config.element || config.root) { // if (!lazy || config.element || config.root) {
widget._lazyConstructor(); widget._lazyConstructor();
// } // }
pushed && BI.Widget.popContext(); pushed && Widget.popContext();
return widget; return widget;
}; };
BI.createWidget = BI.createWidget || function (item, options, context, lazy) { export function createWidget(item, options, context, lazy) {
item || (item = {}); item || (item = {});
if (BI.isWidget(options)) { if (isWidget(options)) {
context = options; context = options;
options = {}; options = {};
} else { } else {
@ -445,9 +446,9 @@
var el, w; var el, w;
if (item.type || options.type) { if (item.type || options.type) {
el = BI.extend({}, options, item); el = extend({}, options, item);
} else if (item.el && (item.el.type || options.type)) { } else if (item.el && (item.el.type || options.type)) {
el = BI.extend({}, options, item.el); el = extend({}, options, item.el);
} }
if (el) { if (el) {
@ -456,14 +457,14 @@
} }
// 先把准备环境准备好 // 先把准备环境准备好
BI.init(); init();
if (BI.isEmpty(item) && BI.isEmpty(options)) { if (isEmpty(item) && isEmpty(options)) {
return BI.createWidget({ return createWidget({
type: "bi.layout" type: "bi.layout"
}); });
} }
if (BI.isWidget(item)) { if (isWidget(item)) {
return item; return item;
} }
if (el) { if (el) {
@ -471,56 +472,59 @@
var wType = (w.type && w.type.xtype) || w.type; var wType = (w.type && w.type.xtype) || w.type;
if (wType === elType) { if (wType === elType) {
if (BI.Plugin.hasObject(elType)) { if (BI.Plugin.hasObject(elType)) {
if (!w.listeners || BI.isArray(w.listeners)) { if (!w.listeners || isArray(w.listeners)) {
w.listeners = (w.listeners || []).concat([{ w.listeners = (w.listeners || []).concat([{
eventName: BI.Events.MOUNT, eventName: BI.Events.MOUNT,
action: function () { action: () => {
BI.Plugin.getObject(elType, this); BI.Plugin.getObject(elType, this);
} }
}]); }]);
} else { } else {
w.listeners[BI.Events.MOUNT] = [ w.listeners[BI.Events.MOUNT] = [
function () { () => {
BI.Plugin.getObject(elType, this); BI.Plugin.getObject(elType, this);
} }
].concat(w.listeners[BI.Events.MOUNT] || []); ].concat(w.listeners[BI.Events.MOUNT] || []);
} }
} }
return createWidget(w, context, lazy); return createRealWidget(w, context, lazy);
} }
return BI.createWidget(w, options, context, lazy); return createWidget(w, options, context, lazy);
} }
if (BI.isWidget(item.el)) { if (isWidget(item.el)) {
return item.el; return item.el;
} }
throw new Error("组件:无法根据item创建组件", item); throw new Error("组件:无法根据item创建组件", item);
}; }
BI.createWidget = BI.createWidget || createWidget;
BI._lazyCreateWidget = BI._lazyCreateWidget || function (item, options, context) { export function _lazyCreateWidget (item, options, context) {
return BI.createWidget(item, options, context, true); return createWidget(item, options, context, true);
}; }
BI._lazyCreateWidget = BI._lazyCreateWidget || _lazyCreateWidget;
BI.createElement = BI.createElement || function () { export function createElement() {
var widget = BI.createWidget.apply(this, arguments); const widget = createWidget.apply(this, arguments);
return widget.element; return widget.element;
}; }
BI.createElement = BI.createElement || createElement;
BI.getResource = BI.getResource || function (type, config) { export function getResource(type, config) {
if (BI.isNotNull(constantInjection[type])) { if (isNotNull(constantInjection[type])) {
return BI.Constants.getConstant(type); return Constants.getConstant(type);
} }
if (modelInjection[type]) { if (modelInjection[type]) {
return BI.Models.getModel(type, config); return Models.getModel(type, config);
} }
if (storeInjection[type]) { if (storeInjection[type]) {
return BI.Stores.getStore(type, config); return Stores.getStore(type, config);
} }
if (serviceInjection[type]) { if (serviceInjection[type]) {
return BI.Services.getService(type, config); return Services.getService(type, config);
} }
if (providerInjection[type]) { if (providerInjection[type]) {
return BI.Providers.getProvider(type, config); return Providers.getProvider(type, config);
} }
throw new Error("未知类型: [" + type + "] 未定义"); throw new Error("未知类型: [" + type + "] 未定义");
}; }
})(); BI.getResource = BI.getResource || getResource;

3
src/core/decorator.js

@ -1,8 +1,9 @@
/** /**
* 注册widget * 注册widget
*/ */
import { shortcut as biShortcut } from "./5.inject";
export function shortcut() { export function shortcut() {
return function decorator(Target) { return function decorator(Target) {
BI.shortcut(Target.xtype, Target); biShortcut(Target.xtype, Target);
}; };
} }

6
src/core/index.js

@ -37,6 +37,10 @@ BI.extend(BI, {
StyleLoaderManager, StyleLoaderManager,
}); });
export * from './2.base';
export * from './4.widget';
export * from './5.inject';
export { export {
shortcut, shortcut,
OB, OB,
@ -57,5 +61,3 @@ export {
TooltipsController, TooltipsController,
StyleLoaderManager, StyleLoaderManager,
} }
export * from './2.base';
Loading…
Cancel
Save