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. 994
      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
* @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);

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

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

994
src/core/5.inject.js

File diff suppressed because it is too large Load Diff

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

6
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,
@ -57,5 +61,3 @@ export {
TooltipsController,
StyleLoaderManager,
}
export * from './2.base';
Loading…
Cancel
Save