From 3e98c47b71f2b1bb53482963134cb3af19055df2 Mon Sep 17 00:00:00 2001 From: "Zhenfei.Li" Date: Tue, 3 Jan 2023 11:37:01 +0800 Subject: [PATCH] =?UTF-8?q?KERNEL-14001=20refactor:=20inject=E7=9A=84es6?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/base/1.pane.js | 19 +- src/core/3.ob.js | 12 +- src/core/4.widget.js | 53 ++- src/core/5.inject.js | 994 +++++++++++++++++++++--------------------- src/core/decorator.js | 3 +- src/core/index.js | 8 +- 6 files changed, 555 insertions(+), 534 deletions(-) diff --git a/src/base/1.pane.js b/src/base/1.pane.js index 1f7f1cc59..51ceecfce 100644 --- a/src/base/1.pane.js +++ b/src/base/1.pane.js @@ -6,7 +6,8 @@ * @extends BI.Widget * @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() export default class Pane extends Widget { @@ -27,7 +28,7 @@ export default class Pane extends Widget { _assertTip() { if (!this._tipText) { - BI.createWidget({ + createWidget({ type: "bi.absolute_center_adapt", element: this, items: [{ @@ -45,7 +46,7 @@ export default class Pane extends Widget { loading() { 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, context: this, })); @@ -53,18 +54,18 @@ export default class Pane extends Widget { // loading的异步情况下由loaded后对面板的populate的时机决定 this.setTipVisible(false); if (o.overlap === true) { - if (!BI.Layers.has(this.getName() + "-loading")) { - BI.createWidget({ + if (!Layers.has(this.getName() + "-loading")) { + createWidget({ type: "bi.center_adapt", cls: "loading-container", 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)) { loadingAnimation.element.css("zIndex", 1); - BI.createWidget({ + createWidget({ type: "bi.center_adapt", element: this, cls: "loading-container", @@ -101,7 +102,7 @@ export default class Pane extends Widget { } loaded() { - BI.Layers.remove(this.getName() + "-loading"); + Layers.remove(this.getName() + "-loading"); this._loading && this._loading.destroy(); this.options.onLoaded(); this.fireEvent(Pane.EVENT_LOADED); diff --git a/src/core/3.ob.js b/src/core/3.ob.js index 5d0e9f7fb..f51a68f06 100644 --- a/src/core/3.ob.js +++ b/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; for (; i < length; i++) { // Only deal with non-null/undefined values @@ -52,11 +52,11 @@ export default class OB { if (isFunction(this.props)) { 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) { - return extend(conf, value.fn(defaultProps, config, value.opt)); + return obExtend(conf, value.fn(defaultProps, config, value.opt)); }, {}) : null; - this.options = extend(defaultProps, modifiedDefaultProps, config); + this.options = obExtend(defaultProps, modifiedDefaultProps, config); } _init() { @@ -216,4 +216,4 @@ export default class OB { } } -BI.extend(BI, { OB }); +extend(BI, { OB }); diff --git a/src/core/4.widget.js b/src/core/4.widget.js index 728e70dfa..54e833b51 100644 --- a/src/core/4.widget.js +++ b/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 OB from "./3.ob"; +import { Providers, _lazyCreateWidget } from "./5.inject"; const cancelAnimationFrame = _global.cancelAnimationFrame || @@ -360,7 +361,7 @@ export default class Widget extends OB { _initElement() { this.__isMounting = true; // 当开启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); let els = render && render.call(this); els = this.options.configRender ? this.options.configRender.call(this, els) : els; @@ -372,7 +373,7 @@ export default class Widget extends OB { if (isArray(els)) { each(els, (i, el) => { if (el) { - BI._lazyCreateWidget(el, { + _lazyCreateWidget(el, { element: this }); } @@ -840,9 +841,6 @@ export default class Widget extends OB { } }; -extend(BI, { Widget }); - - let context = null, current = null; const contextStack = [], currentStack = []; @@ -875,7 +873,7 @@ function popTarget() { Widget.current = current = currentStack.pop(); } -BI.useStore = function (_store) { +export function useStore(_store) { if (current && current.store) { return current.store; } @@ -908,9 +906,9 @@ BI.useStore = function (_store) { }; return current.$storeDelegate = delegate; } -}; +} -BI.useContext = function (inject) { +export function useContext(inject) { // 通过组件找最近的store const vm = Widget.findStore(Widget.current || Widget.context); if (vm) { @@ -934,9 +932,9 @@ BI.useContext = function (inject) { } } return vm; -}; +} -BI.watch = function (vm, watch, handler) { +export function watch(vm, watch, handler) { // 必须要保证组件当前环境存在 if (Widget.current) { if (vm instanceof BI.Model) { @@ -970,9 +968,9 @@ BI.watch = function (vm, watch, handler) { Widget.current.$watchDelayCallbacks || (Widget.current.$watchDelayCallbacks = []); Widget.current.$watchDelayCallbacks.push([watch, handler]); } -}; +} -BI.onBeforeMount = function (beforeMount) { +export function onBeforeMount(beforeMount) { if (current) { if (current.__isMounting) { beforeMount(); @@ -985,8 +983,9 @@ BI.onBeforeMount = function (beforeMount) { } current.beforeMount.push(beforeMount); } -}; -BI.onMounted = function (mounted) { +} + +export function onMounted(mounted) { if (current) { if (current._isMounted && !this.__async) { mounted(); @@ -1000,7 +999,8 @@ BI.onMounted = function (mounted) { current.mounted.push(mounted); } }; -BI.onBeforeUnmount = function (beforeDestroy) { + +export function onBeforeUnmount(beforeDestroy) { if (current) { if (!current.beforeDestroy) { current.beforeDestroy = []; @@ -1009,8 +1009,9 @@ BI.onBeforeUnmount = function (beforeDestroy) { } current.beforeDestroy.push(beforeDestroy); } -}; -BI.onUnmounted = function (destroyed) { +} + +export function onUnmounted(destroyed) { if (current) { if (!current.destroyed) { current.destroyed = []; @@ -1019,7 +1020,7 @@ BI.onUnmounted = function (destroyed) { } current.destroyed.push(destroyed); } -}; +} Widget.registerRenderEngine = function (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) { // 将widget的element元素都挂载好,并建立相互关系 widget.element.data("__widgets", [widget]); @@ -1077,4 +1078,16 @@ BI.mount = function (widget, container, predicate, hydrate) { Widget._renderEngine.createElement(container).append(widget.element); } return widget._mount(true, false, false, predicate); -}; +} + +extend(BI, { + Widget, + useStore, + useContext, + watch, + onBeforeMount, + onMounted, + onBeforeUnmount, + onUnmounted, + mount, +}); diff --git a/src/core/5.inject.js b/src/core/5.inject.js index 9602deed9..a8ec6786d 100644 --- a/src/core/5.inject.js +++ b/src/core/5.inject.js @@ -1,526 +1,530 @@ -(function () { - var moduleInjection = {}, moduleInjectionMap = { - components: {}, - constants: {}, - stores: {}, - services: {}, - models: {}, - providers: {} - }; - BI.module = BI.module || function (xtype, cls) { - if (moduleInjection[xtype] != null) { - _global.console && console.error("module: [" + xtype + "] 已经注册过了"); - } else { - if (BI.isFunction(cls)) { - cls = cls(); - } - for (var k in moduleInjectionMap) { - if (cls[k]) { - for (var key in cls[k]) { - if (!moduleInjectionMap[k]) { - continue; - } - if (!moduleInjectionMap[k][key]) { - moduleInjectionMap[k][key] = []; - } - moduleInjectionMap[k][key].push({ - version: cls[k][key], - moduleId: xtype - }); +import { isFunction, isNull, isNotNull, isArray, each, isWidget, extend, init, isEmpty, remove } from "./2.base"; +import OB from "./3.ob"; +import Widget from "./4.widget" + +let moduleInjection = {}, moduleInjectionMap = { + components: {}, + constants: {}, + stores: {}, + services: {}, + models: {}, + providers: {} +}; + +export function module(xtype, cls) { + if (moduleInjection[xtype] != null) { + _global.console && console.error("module: [" + xtype + "] 已经注册过了"); + } else { + if (isFunction(cls)) { + cls = cls(); + } + for (const k in moduleInjectionMap) { + if (cls[k]) { + for (const key in cls[k]) { + if (!moduleInjectionMap[k]) { + continue; + } + if (!moduleInjectionMap[k][key]) { + moduleInjectionMap[k][key] = []; } + moduleInjectionMap[k][key].push({ + version: cls[k][key], + moduleId: xtype + }); } } - moduleInjection[xtype] = cls; - } - - return function () { - return BI.Modules.getModule(xtype); - }; - }; - - var constantInjection = {}; - BI.constant = BI.constant || function (xtype, cls) { - if (constantInjection[xtype] != null) { - _global.console && console.error("constant: [" + xtype + "]已经注册过了"); - } else { - constantInjection[xtype] = cls; - } - - return function () { - return BI.Constants.getConstant(xtype); - }; - }; - - var modelInjection = {}; - BI.model = BI.model || function (xtype, cls) { - if (modelInjection[xtype] != null) { - _global.console && console.error("model: [" + xtype + "] 已经注册过了"); - } else { - modelInjection[xtype] = cls; - } - - return function (config) { - return BI.Models.getModel(xtype, config); - }; - }; - - var storeInjection = {}; - BI.store = BI.store || function (xtype, cls) { - if (storeInjection[xtype] != null) { - _global.console && console.error("store: [" + xtype + "] 已经注册过了"); - } else { - storeInjection[xtype] = cls; - } - - return function (config) { - return BI.Stores.getStore(xtype, config); - }; - }; - - var serviceInjection = {}; - BI.service = BI.service || function (xtype, cls) { - if (serviceInjection[xtype] != null) { - _global.console && console.error("service: [" + xtype + "] 已经注册过了"); - } - - serviceInjection[xtype] = cls; - - return function (config) { - return BI.Services.getService(xtype, config); - }; - }; - - var providerInjection = {}; - BI.provider = BI.provider || function (xtype, cls) { - if (providerInjection[xtype] != null) { - _global.console && console.error("provider: [" + xtype + "] 已经注册过了"); - } else { - providerInjection[xtype] = cls; - } - - return function (config) { - return BI.Providers.getProvider(xtype, config); - }; - }; - - var configFunctions = BI.OB.configFunctions = {}; - var runConfigFunction = function (type, configFn) { - if (!type || !configFunctions[type]) { - return false; - } - - var queue = []; - if (configFn) { - queue = configFunctions[type].filter(function (conf) { - return conf.fn === configFn; - }); - configFunctions[type] = configFunctions[type].filter(function (conf) { - return conf.fn !== configFn; - }); - } else { - queue = configFunctions[type]; - delete configFunctions[type]; } - - var dependencies = BI.Providers.getProvider("bi.provider.system").getDependencies(); - var modules = moduleInjectionMap.components[type] - || moduleInjectionMap.constants[type] - || moduleInjectionMap.services[type] - || moduleInjectionMap.stores[type] - || moduleInjectionMap.models[type] - || moduleInjectionMap.providers[type]; - for (var i = 0; i < queue.length; i++) { - var conf = queue[i]; - var version = conf.opt.version; - var fn = conf.fn; - if (modules && version) { - var findVersion = false; - for (var j = 0; j < modules.length; j++) { - var module = modules[j]; - if (module && dependencies[module.moduleId] && module.version === version) { - var minVersion = dependencies[module.moduleId].minVersion, - maxVersion = dependencies[module.moduleId].maxVersion; - if (minVersion && (moduleInjection[module.moduleId].version || version) < minVersion) { - findVersion = true; - break; - } - if (maxVersion && (moduleInjection[module.moduleId].version || version) > maxVersion) { - findVersion = true; - break; - } + moduleInjection[xtype] = cls; + } + + return () => Modules.getModule(xtype); +} +BI.module = BI.module || module; + +let constantInjection = {}; +export function constant(xtype, cls) { + if (constantInjection[xtype] != null) { + _global.console && console.error("constant: [" + xtype + "]已经注册过了"); + } else { + constantInjection[xtype] = cls; + } + + return () => Constants.getConstant(xtype); +} +BI.constant = BI.constant || constant; + +let modelInjection = {}; +export function model(xtype, cls) { + if (modelInjection[xtype] != null) { + _global.console && console.error("model: [" + xtype + "] 已经注册过了"); + } else { + modelInjection[xtype] = cls; + } + + return (config) => Models.getModel(xtype, config); +} +BI.model = BI.model || model; + +let storeInjection = {}; +export function store(xtype, cls) { + if (storeInjection[xtype] != null) { + _global.console && console.error("store: [" + xtype + "] 已经注册过了"); + } else { + storeInjection[xtype] = cls; + } + + return (config) => Stores.getStore(xtype, config); +} +BI.store = BI.store || store; + +let serviceInjection = {}; +export function service(xtype, cls) { + if (serviceInjection[xtype] != null) { + _global.console && console.error("service: [" + xtype + "] 已经注册过了"); + } + + serviceInjection[xtype] = cls; + + return (config) => Services.getService(xtype, config); +} +BI.service = BI.service || service; + +let providerInjection = {}; +export function provider(xtype, cls) { + if (providerInjection[xtype] != null) { + _global.console && console.error("provider: [" + xtype + "] 已经注册过了"); + } else { + providerInjection[xtype] = cls; + } + + return (config) => Providers.getProvider(xtype, config); +} +BI.provider = BI.provider || provider; + +let configFunctions = OB.configFunctions = {}; +const runConfigFunction = function (type, configFn) { + if (!type || !configFunctions[type]) { + return false; + } + + let queue = []; + if (configFn) { + queue = configFunctions[type].filter((conf) => conf.fn === configFn); + configFunctions[type] = configFunctions[type].filter((conf) => conf.fn !== configFn); + } else { + queue = configFunctions[type]; + delete configFunctions[type]; + } + + const dependencies = Providers.getProvider("bi.provider.system").getDependencies(); + const modules = moduleInjectionMap.components[type] + || moduleInjectionMap.constants[type] + || moduleInjectionMap.services[type] + || moduleInjectionMap.stores[type] + || moduleInjectionMap.models[type] + || moduleInjectionMap.providers[type]; + for (let i = 0; i < queue.length; i++) { + const conf = queue[i]; + const version = conf.opt.version; + const fn = conf.fn; + if (modules && version) { + let findVersion = false; + let module; + for (let j = 0; j < modules.length; j++) { + module = modules[j]; + if (module && dependencies[module.moduleId] && module.version === version) { + const minVersion = dependencies[module.moduleId].minVersion, + maxVersion = dependencies[module.moduleId].maxVersion; + if (minVersion && (moduleInjection[module.moduleId].version || version) < minVersion) { + findVersion = true; + break; + } + if (maxVersion && (moduleInjection[module.moduleId].version || version) > maxVersion) { + findVersion = true; + break; } - } - if (findVersion === true) { - _global.console && console.error("moduleId: [" + module.moduleId + "] 接口: [" + type + "] 接口版本: [" + version + "] 已过期,版本要求为:", dependencies[module.moduleId], "=>", moduleInjection[module.moduleId]); - continue; } } - if (constantInjection[type]) { - constantInjection[type] = fn(constantInjection[type]); + if (findVersion === true) { + _global.console && console.error("moduleId: [" + module.moduleId + "] 接口: [" + type + "] 接口版本: [" + version + "] 已过期,版本要求为:", dependencies[module.moduleId], "=>", moduleInjection[module.moduleId]); continue; } - if (providerInjection[type]) { - if (!providers[type]) { - providers[type] = new providerInjection[type](); - } - if (providerInstance[type]) { - delete providerInstance[type]; - } - fn(providers[type]); - continue; - } - BI.Plugin.configWidget(type, fn, conf.opt); } - }; - BI.config = BI.config || function (type, configFn, opt) { - if (BI.isFunction(type)) { - opt = configFn; - configFn = type; - type = "bi.provider.system"; + if (constantInjection[type]) { + constantInjection[type] = fn(constantInjection[type]); + continue; } - opt = opt || {}; - - // 系统配置直接执行 - if ("bi.provider.system" === type) { + if (providerInjection[type]) { if (!providers[type]) { providers[type] = new providerInjection[type](); } - // 如果config被重新配置的话,需要删除掉之前的实例 if (providerInstance[type]) { delete providerInstance[type]; } - return configFn(providers[type]); - } - - if (!configFunctions[type]) { - configFunctions[type] = []; - } - configFunctions[type].push({ - fn: configFn, - opt: opt - }); - - if (opt.immediately) { - return runConfigFunction(type, configFn); - } - }; - - BI.getReference = BI.getReference || function (type, fn) { - return BI.Plugin.registerObject(type, fn); - }; - - var actions = {}; - var globalAction = []; - BI.action = BI.action || function (type, actionFn) { - if (BI.isFunction(type)) { - globalAction.push(type); - return function () { - BI.remove(globalAction, function (idx) { - return globalAction.indexOf(actionFn) === idx; - }); - }; - } - if (!actions[type]) { - actions[type] = []; - } - actions[type].push(actionFn); - return function () { - BI.remove(actions[type], function (idx) { - return actions[type].indexOf(actionFn) === idx; - }); - if (actions[type].length === 0) { - delete actions[type]; - } + fn(providers[type]); + continue; + } + BI.Plugin.configWidget(type, fn, conf.opt); + } +}; + +export function config(type, configFn, opt) { + if (isFunction(type)) { + opt = configFn; + configFn = type; + type = "bi.provider.system"; + } + opt = opt || {}; + + // 系统配置直接执行 + if ("bi.provider.system" === type) { + if (!providers[type]) { + providers[type] = new providerInjection[type](); + } + // 如果config被重新配置的话,需要删除掉之前的实例 + if (providerInstance[type]) { + delete providerInstance[type]; + } + return configFn(providers[type]); + } + + if (!configFunctions[type]) { + configFunctions[type] = []; + } + configFunctions[type].push({ + fn: configFn, + opt: opt + }); + + if (opt.immediately) { + return runConfigFunction(type, configFn); + } +} +BI.config = BI.config || config; + +export function getReference(type, fn) { + return BI.Plugin.registerObject(type, fn); +} +BI.getReference = BI.getReference || getReference; + +let actions = {}; +let globalAction = []; +export function action(type, actionFn) { + if (isFunction(type)) { + globalAction.push(type); + return () => { + remove(globalAction, (idx) => globalAction.indexOf(actionFn) === idx); }; - }; - - var points = {}; - BI.point = BI.point || function (type, action, pointFn, after) { - if (!points[type]) { - points[type] = {}; - } - if (!points[type][action]) { - points[type][action] = {}; - } - if (!points[type][action][after ? "after" : "before"]) { - points[type][action][after ? "after" : "before"] = []; + } + if (!actions[type]) { + actions[type] = []; + } + actions[type].push(actionFn); + return () => { + remove(actions[type], (idx) => actions[type].indexOf(actionFn) === idx); + if (actions[type].length === 0) { + delete actions[type]; } - points[type][action][after ? "after" : "before"].push(pointFn); }; - - BI.Modules = BI.Modules || { - getModule: function (type) { - if (!moduleInjection[type]) { - _global.console && console.error("module: [" + type + "] 未定义"); - } - return moduleInjection[type]; - }, - getAllModules: function () { - return moduleInjection; - } - }; - - BI.Constants = BI.Constants || { - getConstant: function (type) { - if (BI.isNull(constantInjection[type])) { - _global.console && console.error("constant: [" + type + "] 未定义"); - } - runConfigFunction(type); - return BI.isFunction(constantInjection[type]) ? constantInjection[type]() : constantInjection[type]; - } - }; - - var callPoint = function (inst, types) { - types = BI.isArray(types) ? types : [types]; - BI.each(types, function (idx, type) { - if (points[type]) { - for (var action in points[type]) { - var bfns = points[type][action].before; - if (bfns) { - BI.aspect.before(inst, action, function (bfns) { - return function () { - for (var i = 0, len = bfns.length; i < len; i++) { - try { - bfns[i].apply(inst, arguments); - } catch (e) { - _global.console && console.error(e); - } +} +BI.action = BI.action || action; + +let points = {}; +export function point(type, action, pointFn, after) { + if (!points[type]) { + points[type] = {}; + } + if (!points[type][action]) { + points[type][action] = {}; + } + if (!points[type][action][after ? "after" : "before"]) { + points[type][action][after ? "after" : "before"] = []; + } + points[type][action][after ? "after" : "before"].push(pointFn); +} +BI.point = BI.point || point; + +export const Modules = { + getModule: function (type) { + if (!moduleInjection[type]) { + _global.console && console.error("module: [" + type + "] 未定义"); + } + return moduleInjection[type]; + }, + getAllModules: function () { + return moduleInjection; + } +} +BI.Modules = BI.Modules || Modules; + +export const Constants = { + getConstant: function (type) { + if (isNull(constantInjection[type])) { + _global.console && console.error("constant: [" + type + "] 未定义"); + } + runConfigFunction(type); + return isFunction(constantInjection[type]) ? constantInjection[type]() : constantInjection[type]; + } +} +BI.Constants = BI.Constants || Constants; + +const callPoint = function (inst, types) { + types = isArray(types) ? types : [types]; + each(types, (idx, type) => { + if (points[type]) { + for (const action in points[type]) { + let bfns = points[type][action].before; + if (bfns) { + BI.aspect.before(inst, action, function (bfns) { + return function () { + for (let i = 0, len = bfns.length; i < len; i++) { + try { + bfns[i].apply(inst, arguments); + } catch (e) { + _global.console && console.error(e); } - }; - }(bfns)); - } - var afns = points[type][action].after; - if (afns) { - BI.aspect.after(inst, action, function (afns) { - return function () { - for (var i = 0, len = afns.length; i < len; i++) { - try { - afns[i].apply(inst, arguments); - } catch (e) { - _global.console && console.error(e); - } + } + }; + }(bfns)); + } + let afns = points[type][action].after; + if (afns) { + BI.aspect.after(inst, action, function (afns) { + return function () { + for (let i = 0, len = afns.length; i < len; i++) { + try { + afns[i].apply(inst, arguments); + } catch (e) { + _global.console && console.error(e); } - }; - }(afns)); - } + } + }; + }(afns)); } } - }); - }; - - BI.Models = BI.Models || { - getModel: function (type, config) { - if (!modelInjection[type]) { - _global.console && console.error("model: [" + type + "] 未定义"); - } - runConfigFunction(type); - var inst = new modelInjection[type](config); - inst._constructor && inst._constructor(config); - inst.mixins && callPoint(inst, inst.mixins); - callPoint(inst, type); - return inst; } - }; - - var stores = {}; - - BI.Stores = BI.Stores || { - getStore: function (type, config) { - if (!storeInjection[type]) { - _global.console && console.error("store: [" + type + "] 未定义"); - } - if (stores[type]) { - return stores[type]; - } - var inst = stores[type] = new storeInjection[type](config); - inst._constructor && inst._constructor(config, function () { - delete stores[type]; - }); - callPoint(inst, type); - return inst; - } - }; - - var services = {}; - - BI.Services = BI.Services || { - getService: function (type, config) { - if (!serviceInjection[type]) { - _global.console && console.error("service: [" + type + "] 未定义"); - } - if (services[type]) { - return services[type]; - } - services[type] = new serviceInjection[type](config); - callPoint(services[type], type); + }); +}; + +export const Models = { + getModel: function (type, config) { + if (!modelInjection[type]) { + _global.console && console.error("model: [" + type + "] 未定义"); + } + runConfigFunction(type); + var inst = new modelInjection[type](config); + inst._constructor && inst._constructor(config); + inst.mixins && callPoint(inst, inst.mixins); + callPoint(inst, type); + return inst; + } +} +BI.Models = BI.Models || Models; + +let stores = {}; +export const Stores = { + getStore: function (type, config) { + if (!storeInjection[type]) { + _global.console && console.error("store: [" + type + "] 未定义"); + } + if (stores[type]) { + return stores[type]; + } + const inst = stores[type] = new storeInjection[type](config); + inst._constructor && inst._constructor(config, function () { + delete stores[type]; + }); + callPoint(inst, type); + return inst; + } +} +BI.Stores = BI.Stores || Stores; + +let services = {}; +export const Services = { + getService: (type, config) => { + if (!serviceInjection[type]) { + _global.console && console.error("service: [" + type + "] 未定义"); + } + if (services[type]) { return services[type]; } - }; - - var providers = {}, - providerInstance = {}; - - BI.Providers = BI.Providers || { - getProvider: function (type, config) { - if (!providerInjection[type]) { - _global.console && console.error("provider: [" + type + "] 未定义"); + services[type] = new serviceInjection[type](config); + callPoint(services[type], type); + return services[type]; + } +} +BI.Services = BI.Services || Services; + +let providers = {}, + providerInstance = {}; +export const Providers = { + getProvider: (type, config) => { + if (!providerInjection[type]) { + _global.console && console.error("provider: [" + type + "] 未定义"); + } + runConfigFunction(type); + if (!providers[type]) { + providers[type] = new providerInjection[type](); + } + if (!providerInstance[type] && providers[type].$get) { + providerInstance[type] = new (providers[type].$get())(config); + } + return providerInstance[type]; + } +} +BI.Providers = BI.Providers || Providers; + +export const Actions = { + runAction: function (type, event, config) { + each(actions[type], (i, act) => { + try { + act(event, config); + } catch (e) { + _global.console && console.error(e); } - runConfigFunction(type); - if (!providers[type]) { - providers[type] = new providerInjection[type](); - } - if (!providerInstance[type] && providers[type].$get) { - providerInstance[type] = new (providers[type].$get())(config); + }); + }, + runGlobalAction: function () { + var args = [].slice.call(arguments); + each(globalAction, (i, act) => { + try { + act.apply(null, args); + } catch (e) { + _global.console && console.error(e); } - return providerInstance[type]; - } - }; - - BI.Actions = BI.Actions || { - runAction: function (type, event, config) { - BI.each(actions[type], function (i, act) { - try { - act(event, config); - } catch (e) { - _global.console && console.error(e); - } - }); - }, - runGlobalAction: function () { - var args = [].slice.call(arguments); - BI.each(globalAction, function (i, act) { - try { - act.apply(null, args); - } catch (e) { - _global.console && console.error(e); - } - }); - } - }; - - var kv = {}; - BI.shortcut = BI.component = BI.shortcut || function (xtype, cls) { - if (kv[xtype] != null) { - _global.console && console.error("组件: [" + xtype + "] 已经注册过了"); - } - if (cls) { - cls["xtype"] = xtype; - } - kv[xtype] = cls; - }; - - // 根据配置属性生成widget - var createWidget = function (config, context, lazy) { - var cls = BI.isFunction(config.type) ? config.type : kv[config.type]; - - if (!cls) { - throw new Error("组件: [" + config.type + "] 未定义"); - } - var pushed = false; - var widget = new cls(); - widget._context = BI.Widget.context || context; - if (!BI.Widget.context && context) { - pushed = true; - BI.Widget.pushContext(context); - } - callPoint(widget, config.type); - widget._initProps(config); - widget._initRoot(); - widget._constructed(); - // if (!lazy || config.element || config.root) { - widget._lazyConstructor(); - // } - pushed && BI.Widget.popContext(); - return widget; - }; - - BI.createWidget = BI.createWidget || function (item, options, context, lazy) { - item || (item = {}); - if (BI.isWidget(options)) { - context = options; - options = {}; - } else { - options || (options = {}); - } - - var el, w; - if (item.type || options.type) { - el = BI.extend({}, options, item); - } else if (item.el && (item.el.type || options.type)) { - el = BI.extend({}, options, item.el); - } - - if (el) { - var elType = (el.type && el.type.xtype) || el.type; - runConfigFunction(elType); - } - - // 先把准备环境准备好 - BI.init(); - - if (BI.isEmpty(item) && BI.isEmpty(options)) { - return BI.createWidget({ - type: "bi.layout" - }); - } - if (BI.isWidget(item)) { - return item; - } - if (el) { - w = BI.Plugin.getWidget(elType, el); - var wType = (w.type && w.type.xtype) || w.type; - if (wType === elType) { - if (BI.Plugin.hasObject(elType)) { - if (!w.listeners || BI.isArray(w.listeners)) { - w.listeners = (w.listeners || []).concat([{ - eventName: BI.Events.MOUNT, - action: function () { - BI.Plugin.getObject(elType, this); - } - }]); - } else { - w.listeners[BI.Events.MOUNT] = [ - function () { - BI.Plugin.getObject(elType, this); - } - ].concat(w.listeners[BI.Events.MOUNT] || []); - } + }); + } +} +BI.Actions = BI.Actions || Actions; + +let kv = {}; +export function shortcut(xtype, cls) { + if (kv[xtype] != null) { + _global.console && console.error("组件: [" + xtype + "] 已经注册过了"); + } + if (cls) { + cls["xtype"] = xtype; + } + kv[xtype] = cls; +} +BI.shortcut = BI.component = BI.shortcut || shortcut; + +// 根据配置属性生成widget +const createRealWidget = function (config, context, lazy) { + const cls = isFunction(config.type) ? config.type : kv[config.type]; + + if (!cls) { + throw new Error("组件: [" + config.type + "] 未定义"); + } + let pushed = false; + const widget = new cls(); + widget._context = Widget.context || context; + if (!Widget.context && context) { + pushed = true; + Widget.pushContext(context); + } + callPoint(widget, config.type); + widget._initProps(config); + widget._initRoot(); + widget._constructed(); + // if (!lazy || config.element || config.root) { + widget._lazyConstructor(); + // } + pushed && Widget.popContext(); + return widget; +}; + +export function createWidget(item, options, context, lazy) { + item || (item = {}); + if (isWidget(options)) { + context = options; + options = {}; + } else { + options || (options = {}); + } + + var el, w; + if (item.type || options.type) { + el = extend({}, options, item); + } else if (item.el && (item.el.type || options.type)) { + el = extend({}, options, item.el); + } + + if (el) { + var elType = (el.type && el.type.xtype) || el.type; + runConfigFunction(elType); + } + + // 先把准备环境准备好 + init(); + + if (isEmpty(item) && isEmpty(options)) { + return createWidget({ + type: "bi.layout" + }); + } + if (isWidget(item)) { + return item; + } + if (el) { + w = BI.Plugin.getWidget(elType, el); + var wType = (w.type && w.type.xtype) || w.type; + if (wType === elType) { + if (BI.Plugin.hasObject(elType)) { + if (!w.listeners || isArray(w.listeners)) { + w.listeners = (w.listeners || []).concat([{ + eventName: BI.Events.MOUNT, + action: () => { + BI.Plugin.getObject(elType, this); + } + }]); + } else { + w.listeners[BI.Events.MOUNT] = [ + () => { + BI.Plugin.getObject(elType, this); + } + ].concat(w.listeners[BI.Events.MOUNT] || []); } - return createWidget(w, context, lazy); } - return BI.createWidget(w, options, context, lazy); - } - if (BI.isWidget(item.el)) { - return item.el; - } - throw new Error("组件:无法根据item创建组件", item); - }; - - BI._lazyCreateWidget = BI._lazyCreateWidget || function (item, options, context) { - return BI.createWidget(item, options, context, true); - }; - - BI.createElement = BI.createElement || function () { - var widget = BI.createWidget.apply(this, arguments); - return widget.element; - }; - - BI.getResource = BI.getResource || function (type, config) { - if (BI.isNotNull(constantInjection[type])) { - return BI.Constants.getConstant(type); - } - if (modelInjection[type]) { - return BI.Models.getModel(type, config); - } - if (storeInjection[type]) { - return BI.Stores.getStore(type, config); - } - if (serviceInjection[type]) { - return BI.Services.getService(type, config); - } - if (providerInjection[type]) { - return BI.Providers.getProvider(type, config); - } - throw new Error("未知类型: [" + type + "] 未定义"); - }; -})(); + return createRealWidget(w, context, lazy); + } + return createWidget(w, options, context, lazy); + } + if (isWidget(item.el)) { + return item.el; + } + throw new Error("组件:无法根据item创建组件", item); +} +BI.createWidget = BI.createWidget || createWidget; + +export function _lazyCreateWidget (item, options, context) { + return createWidget(item, options, context, true); +} +BI._lazyCreateWidget = BI._lazyCreateWidget || _lazyCreateWidget; + +export function createElement() { + const widget = createWidget.apply(this, arguments); + return widget.element; +} +BI.createElement = BI.createElement || createElement; + +export function getResource(type, config) { + if (isNotNull(constantInjection[type])) { + return Constants.getConstant(type); + } + if (modelInjection[type]) { + return Models.getModel(type, config); + } + if (storeInjection[type]) { + return Stores.getStore(type, config); + } + if (serviceInjection[type]) { + return Services.getService(type, config); + } + if (providerInjection[type]) { + return Providers.getProvider(type, config); + } + throw new Error("未知类型: [" + type + "] 未定义"); +} +BI.getResource = BI.getResource || getResource; diff --git a/src/core/decorator.js b/src/core/decorator.js index 23215d701..af7a6e005 100644 --- a/src/core/decorator.js +++ b/src/core/decorator.js @@ -1,8 +1,9 @@ /** * 注册widget */ +import { shortcut as biShortcut } from "./5.inject"; export function shortcut() { return function decorator(Target) { - BI.shortcut(Target.xtype, Target); + biShortcut(Target.xtype, Target); }; } diff --git a/src/core/index.js b/src/core/index.js index 0b0f014c0..18b15a0f2 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -37,6 +37,10 @@ BI.extend(BI, { StyleLoaderManager, }); +export * from './2.base'; +export * from './4.widget'; +export * from './5.inject'; + export { shortcut, OB, @@ -56,6 +60,4 @@ export { ResizeController, TooltipsController, StyleLoaderManager, -} - -export * from './2.base'; \ No newline at end of file +} \ No newline at end of file