Browse Source

Pull request #3298: KERNEL-14001 refactor: inject的es6化

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

* commit '3e98c47b71f2b1bb53482963134cb3af19055df2':
  KERNEL-14001 refactor: inject的es6化
es6
Zhenfei.Li-李振飞 2 years ago
parent
commit
5a768a5fae
  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. 8
      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,
});

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 * 注册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);
}; };
} }

8
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,
@ -56,6 +60,4 @@ export {
ResizeController, ResizeController,
TooltipsController, TooltipsController,
StyleLoaderManager, StyleLoaderManager,
} }
export * from './2.base';
Loading…
Cancel
Save