Browse Source

KERNEL-13947 refactor: base工具方法的es6化

es6
Zhenfei.Li 2 years ago
parent
commit
c04901c7bf
  1. 10
      src/base/1.pane.js
  2. 1116
      src/core/2.base.js
  3. 32
      src/core/3.ob.js
  4. 160
      src/core/4.widget.js
  5. 2
      src/core/index.js

10
src/base/1.pane.js

@ -6,7 +6,7 @@
* @extends BI.Widget
* @abstract
*/
import { Widget, shortcut } from "../core";
import { Widget, shortcut, isNotEmptyString, extend, isNull, isEmpty } from "../core";
@shortcut()
export default class Pane extends Widget {
@ -15,7 +15,7 @@ export default class Pane extends Widget {
static EVENT_LOADING = "EVENT_LOADING";
_defaultConfig() {
return BI.extend(super._defaultConfig(), {
return extend(super._defaultConfig(), {
_baseCls: "bi-pane",
tipText: BI.i18nText("BI-No_Selected_Item"),
loadingText: "",
@ -62,7 +62,7 @@ export default class Pane extends Widget {
});
}
BI.Layers.show(this.getName() + "-loading");
} else if (BI.isNull(this._loading)) {
} else if (isNull(this._loading)) {
loadingAnimation.element.css("zIndex", 1);
BI.createWidget({
type: "bi.center_adapt",
@ -85,7 +85,7 @@ export default class Pane extends Widget {
type: "bi.horizontal_adapt",
items: [loadingTip],
}];
BI.isNotEmptyString(o.loadingText) && loadingTipItems.push({
isNotEmptyString(o.loadingText) && loadingTipItems.push({
type: "bi.text",
text: o.loadingText,
tgap: this._getSize(10),
@ -109,7 +109,7 @@ export default class Pane extends Widget {
}
check() {
this.setTipVisible(BI.isEmpty(this.options.items));
this.setTipVisible(isEmpty(this.options.items));
}
setTipVisible(b) {

1116
src/core/2.base.js

File diff suppressed because it is too large Load Diff

32
src/core/3.ob.js

@ -1,4 +1,4 @@
import BI from "./2.base";
import { isFunction, isArray, isObject, isArguments, reduce, bind } from "./2.base";
function extend() {
let target = arguments[0] || {}, length = arguments.length, i = 1, name, copy;
@ -49,11 +49,11 @@ export default class OB {
_initProps(config) {
let props = this.props;
if (BI.isFunction(this.props)) {
if (isFunction(this.props)) {
props = this.props(config);
}
const defaultProps = extend(this._defaultConfig(config), props);
const modifiedDefaultProps = (config && config.type && OB.configFunctions[config.type + ".props"]) ? BI.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));
}, {}) : null;
this.options = extend(defaultProps, modifiedDefaultProps, config);
@ -67,19 +67,19 @@ export default class OB {
_initListeners() {
if (this.options.listeners !== null) {
BI._.each(this.options.listeners, (lis, eventName) => {
if (BI._.isFunction(lis)) {
if (isFunction(lis)) {
this.on(eventName, lis);
return;
}
if (BI._.isArray(lis)) {
if (isArray(lis)) {
BI._.each(lis, (l) => {
this.on(eventName, l);
});
return;
}
(lis.target ? lis.target : this)[lis.once ? "once" : "on"](lis.eventName, BI._.bind(lis.action, this));
(lis.target ? lis.target : this)[lis.once ? "once" : "on"](lis.eventName, bind(lis.action, this));
});
delete this.options.listeners;
}
@ -89,10 +89,10 @@ export default class OB {
_initRef() {
const o = this.options;
if (o.__ref) {
BI.isFunction(o.__ref) ? o.__ref.call(this, this) : o.__ref.current = this;
isFunction(o.__ref) ? o.__ref.call(this, this) : o.__ref.current = this;
}
if (o.ref) {
BI.isFunction(o.ref) ? o.ref.call(this, this) : o.ref.current = this;
isFunction(o.ref) ? o.ref.call(this, this) : o.ref.current = this;
}
}
@ -100,17 +100,17 @@ export default class OB {
_purgeRef() {
const o = this.options;
if (o.__ref) {
BI.isFunction(o.__ref) ? o.__ref.call(null, null) : o.__ref.current = null;
isFunction(o.__ref) ? o.__ref.call(null, null) : o.__ref.current = null;
o.__ref = null;
}
if (o.ref) {
BI.isFunction(o.ref) ? o.ref.call(null, null) : o.ref.current = null;
isFunction(o.ref) ? o.ref.call(null, null) : o.ref.current = null;
o.ref = null;
}
}
_getEvents() {
if (!BI._.isObject(this.events)) {
if (!isObject(this.events)) {
this.events = {};
}
@ -125,7 +125,7 @@ export default class OB {
on(eventName, fn) {
eventName = eventName.toLowerCase();
let fns = this._getEvents()[eventName];
if (!BI._.isArray(fns)) {
if (!isArray(fns)) {
fns = [];
this._getEvents()[eventName] = fns;
}
@ -160,7 +160,7 @@ export default class OB {
delete this._getEvents()[eventName];
} else {
const fns = this._getEvents()[eventName];
if (BI._.isArray(fns)) {
if (isArray(fns)) {
const newFns = [];
BI._.each(fns, function (ifn) {
if (ifn !== fn) {
@ -189,8 +189,8 @@ export default class OB {
fireEvent() {
const eventName = arguments[0].toLowerCase();
const fns = this._getEvents()[eventName];
if (BI.isArray(fns)) {
if (BI.isArguments(arguments[1])) {
if (isArray(fns)) {
if (isArguments(arguments[1])) {
for (let i = 0; i < fns.length; i++) {
if (fns[i].apply(this, arguments[1]) === false) {
return false;
@ -216,6 +216,4 @@ export default class OB {
}
}
// BI.OB = BI.OB || OB;
BI.extend(BI, { OB });

160
src/core/4.widget.js

@ -1,12 +1,12 @@
/**
* Widget超类
* @class BI.Widget
* @extends BI.OB
* @class Widget
* @extends OB
*
* @cfg {JSON} options 配置属性
*/
import BI 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";
const cancelAnimationFrame =
@ -23,20 +23,20 @@ function callLifeHook(self, life) {
let hooks = [], hook;
hook = self[life];
if (hook) {
hooks = hooks.concat(BI.isArray(hook) ? hook : [hook]);
hooks = hooks.concat(isArray(hook) ? hook : [hook]);
}
hook = self.options[life];
if (hook) {
hooks = hooks.concat(BI.isArray(hook) ? hook : [hook]);
hooks = hooks.concat(isArray(hook) ? hook : [hook]);
}
BI.each(hooks, function (i, hook) {
each(hooks, function (i, hook) {
hook.call(self);
});
}
export default class Widget extends OB {
_defaultConfig () {
return BI.extend(super._defaultConfig(), {
return extend(super._defaultConfig(), {
root: false,
tagName: "div",
attributes: null,
@ -67,9 +67,9 @@ export default class Widget extends OB {
if (this.setup) {
pushTarget(this);
const delegate = this.setup(this.options);
if (BI.isPlainObject(delegate)) {
if (isPlainObject(delegate)) {
// 如果setup返回一个json,即对外暴露的方法
BI.extend(this, delegate);
extend(this, delegate);
} else {
this.render = delegate;
}
@ -208,7 +208,7 @@ export default class Widget extends OB {
this.element.addClass((o._baseCls || "") + " " + (o.baseCls || "") + " " + (o.extraCls || ""));
}
if (o.cls) {
if (BI.isFunction(o.cls)) {
if (isFunction(o.cls)) {
const cls = this.__watch(o.cls, (context, newValue) => {
this.element.removeClass(cls).addClass(cls = newValue);
});
@ -227,10 +227,10 @@ export default class Widget extends OB {
this.element.data(o.data);
}
if (o.css) {
if (BI.isFunction(o.css)) {
if (isFunction(o.css)) {
const css = this.__watch(o.css, (context, newValue) => {
for (const k in css) {
if (BI.isNull(newValue[k])) {
if (isNull(newValue[k])) {
newValue[k] = "";
}
}
@ -250,7 +250,7 @@ export default class Widget extends OB {
return getter.call(this, this);
}, (handler && ((v) => {
handler.call(this, this, v);
})) || BI.emptyFn, BI.extend({ deep: true }, options));
})) || BI.emptyFn, extend({ deep: true }, options));
this._watchers.push(() => {
watcher.teardown();
});
@ -266,18 +266,18 @@ export default class Widget extends OB {
*/
_initRoot() {
const o = this.options;
this.widgetName = o.widgetName || BI.uniqueId("widget");
this.widgetName = o.widgetName || uniqueId("widget");
this._isRoot = o.root;
this._children = {};
if (BI.isWidget(o.element)) {
if (isWidget(o.element)) {
this.element = this.options.element.element;
this._parent = o.element;
this._parent._children && this._parent.addWidget(this.widgetName, this);
} else if (o.element) {
this.element = BI.Widget._renderEngine.createElement(this);
this.element = Widget._renderEngine.createElement(this);
this._isRoot = true;
} else {
this.element = BI.Widget._renderEngine.createElement(this);
this.element = Widget._renderEngine.createElement(this);
}
this.element._isWidget = true;
// const widgets = this.element.data("__widgets") || [];
@ -288,14 +288,14 @@ export default class Widget extends OB {
_initElementWidth() {
const o = this.options;
if (BI.isWidthOrHeight(o.width)) {
if (isWidthOrHeight(o.width)) {
this.element.css("width", BI.pixFormat(o.width));
}
}
_initElementHeight() {
const o = this.options;
if (BI.isWidthOrHeight(o.height)) {
if (isWidthOrHeight(o.height)) {
this.element.css("height", BI.pixFormat(o.height));
}
}
@ -303,7 +303,7 @@ export default class Widget extends OB {
_initVisual() {
const o = this.options;
if (o.invisible) {
const invisible = o.invisible = BI.isFunction(o.invisible) ? this.__watch(o.invisible, (context, newValue) => {
const invisible = o.invisible = isFunction(o.invisible) ? this.__watch(o.invisible, (context, newValue) => {
this.setVisible(!newValue);
}) : o.invisible;
if (invisible) {
@ -317,7 +317,7 @@ export default class Widget extends OB {
const o = this.options;
if (o.disabled || o.invalid) {
if (this.options.disabled) {
const disabled = o.disabled = BI.isFunction(o.disabled) ? this.__watch(o.disabled, (context, newValue) => {
const disabled = o.disabled = isFunction(o.disabled) ? this.__watch(o.disabled, (context, newValue) => {
this.setEnable(!newValue);
}) : o.disabled;
if (disabled) {
@ -325,7 +325,7 @@ export default class Widget extends OB {
}
}
if (this.options.invalid) {
const invalid = o.invalid = BI.isFunction(o.invalid) ? this.__watch(o.invalid, (context, newValue) => {
const invalid = o.invalid = isFunction(o.invalid) ? this.__watch(o.invalid, (context, newValue) => {
this.setValid(!newValue);
}) : o.invalid;
if (invalid) {
@ -334,9 +334,9 @@ export default class Widget extends OB {
}
}
if (o.effect) {
if (BI.isArray(o.effect)) {
if (BI.isArray(o.effect[0])) {
BI.each(o.effect, (i, effect) => {
if (isArray(o.effect)) {
if (isArray(o.effect[0])) {
each(o.effect, (i, effect) => {
this.__watch(effect[0], effect[1]);
});
} else {
@ -361,16 +361,16 @@ export default class Widget extends OB {
this.__isMounting = true;
// 当开启worker模式时,可以通过$render来实现另一种效果
const workerMode = BI.Providers.getProvider("bi.provider.system").getWorkerMode();
const render = BI.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);
els = this.options.configRender ? this.options.configRender.call(this, els) : els;
els = BI.Plugin.getRender(this.options.type, els);
if (BI.isPlainObject(els)) {
if (isPlainObject(els)) {
els = [els];
}
this.__initWatch();
if (BI.isArray(els)) {
BI.each(els, (i, el) => {
if (isArray(els)) {
each(els, (i, el) => {
if (el) {
BI._lazyCreateWidget(el, {
element: this
@ -475,7 +475,7 @@ export default class Widget extends OB {
this.options._disabled = true;
}
// 递归将所有子组件使能
BI.each(this._children, function (i, child) {
each(this._children, function (i, child) {
!child._manualSetEnable && child._setEnable && child._setEnable(enable);
});
}
@ -487,7 +487,7 @@ export default class Widget extends OB {
this.options._invalid = true;
}
// 递归将所有子组件使有效
BI.each(this._children, function (i, child) {
each(this._children, function (i, child) {
!child._manualSetValid && child._setValid && child._setValid(valid);
});
}
@ -581,7 +581,7 @@ export default class Widget extends OB {
doBehavior() {
const args = arguments;
// 递归将所有子组件使有效
BI.each(this._children, function (i, child) {
each(this._children, function (i, child) {
child.doBehavior && child.doBehavior.apply(child, args);
});
}
@ -596,32 +596,32 @@ export default class Widget extends OB {
addWidget(name, widget) {
const self = this;
if (name instanceof BI.Widget) {
if (name instanceof Widget) {
widget = name;
name = widget.getName();
}
if (BI.isKey(name)) {
if (isKey(name)) {
name = name + "";
}
name = name || widget.getName() || BI.uniqueId("widget");
name = name || widget.getName() || uniqueId("widget");
if (this._children[name]) {
throw new Error("组件:组件名已存在,不能进行添加");
}
widget._setParent && widget._setParent(this);
widget.on(BI.Events.DESTROY, function () {
// TODO: self待删
BI.remove(self._children, this);
remove(self._children, this);
});
return (this._children[name] = widget);
}
getWidgetByName(name) {
if (!BI.isKey(name) || name === this.getName()) {
if (!isKey(name) || name === this.getName()) {
return this;
}
name = name + "";
let widget = void 0, other = {};
BI.any(this._children, function (i, wi) {
any(this._children, function (i, wi) {
if (i === name) {
widget = wi;
return true;
@ -629,7 +629,7 @@ export default class Widget extends OB {
other[i] = wi;
});
if (!widget) {
BI.any(other, function (i, wi) {
any(other, function (i, wi) {
return (widget = wi.getWidgetByName(i));
});
}
@ -637,8 +637,8 @@ export default class Widget extends OB {
}
removeWidget(nameOrWidget) {
if (BI.isWidget(nameOrWidget)) {
BI.remove(this._children, nameOrWidget);
if (isWidget(nameOrWidget)) {
remove(this._children, nameOrWidget);
} else {
delete this._children[nameOrWidget];
}
@ -661,11 +661,11 @@ export default class Widget extends OB {
}
attr(key, value) {
if (BI.isPlainObject(key)) {
BI.each(key, (k, v) => this.attr(k, v));
if (isPlainObject(key)) {
each(key, (k, v) => this.attr(k, v));
return;
}
if (BI.isNotNull(value)) {
if (isNotNull(value)) {
return this.options[key] = value;
}
return this.options[key];
@ -728,7 +728,7 @@ export default class Widget extends OB {
}
__d() {
BI.each(this._children, function (i, widget) {
each(this._children, function (i, widget) {
widget && widget._unMount && widget._unMount();
});
this._children = {};
@ -766,7 +766,7 @@ export default class Widget extends OB {
_empty () {
this._assetMounted();
BI.each(this._children, function (i, widget) {
each(this._children, function (i, widget) {
widget && widget._unMount && widget._unMount();
});
this._children = {};
@ -800,9 +800,9 @@ export default class Widget extends OB {
// this.purgeListeners();
// 去掉组件绑定的watcher
BI.each(this._watchers, function (i, unwatches) {
unwatches = BI.isArray(unwatches) ? unwatches : [unwatches];
BI.each(unwatches, function (j, unwatch) {
each(this._watchers, function (i, unwatches) {
unwatches = isArray(unwatches) ? unwatches : [unwatches];
each(unwatches, function (j, unwatch) {
unwatch();
});
});
@ -840,39 +840,39 @@ export default class Widget extends OB {
}
};
BI.extend(BI, { Widget });
extend(BI, { Widget });
let context = null, current = null;
const contextStack = [], currentStack = [];
BI.Widget.pushContext = function (_context) {
Widget.pushContext = function (_context) {
if (context) contextStack.push(context);
BI.Widget.context = context = _context;
Widget.context = context = _context;
};
BI.Widget.popContext = function () {
BI.Widget.context = context = contextStack.pop();
Widget.popContext = function () {
Widget.context = context = contextStack.pop();
};
BI.Widget.execWithContext = function (context, execFunc) {
BI.Widget.pushContext(context);
Widget.execWithContext = function (context, execFunc) {
Widget.pushContext(context);
try {
execFunc();
} catch (e) {
throw e;
} finally {
BI.Widget.popContext();
Widget.popContext();
}
};
function pushTarget(_current) {
if (current) currentStack.push(current);
BI.Widget.current = current = _current;
Widget.current = current = _current;
}
function popTarget() {
BI.Widget.current = current = currentStack.pop();
Widget.current = current = currentStack.pop();
}
BI.useStore = function (_store) {
@ -903,7 +903,7 @@ BI.useStore = function (_store) {
}
current._store = function () {
const st = (_store || currentStore).apply(this, arguments);
BI.extend(delegate, st);
extend(delegate, st);
return st;
};
return current.$storeDelegate = delegate;
@ -912,7 +912,7 @@ BI.useStore = function (_store) {
BI.useContext = function (inject) {
// 通过组件找最近的store
const vm = BI.Widget.findStore(BI.Widget.current || BI.Widget.context);
const vm = Widget.findStore(Widget.current || Widget.context);
if (vm) {
if (inject) {
if (vm.$$computed && inject in vm.$$computed) {
@ -938,17 +938,17 @@ BI.useContext = function (inject) {
BI.watch = function (vm, watch, handler) {
// 必须要保证组件当前环境存在
if (BI.Widget.current) {
if (Widget.current) {
if (vm instanceof BI.Model) {
const watchers = [];
if (BI.isKey(watch)) {
if (isKey(watch)) {
const k = watch;
watch = {};
watch[k] = handler;
}
for (const key in watch) {
const innerHandler = watch[key];
if (BI.isArray(handler)) {
if (isArray(handler)) {
for (let i = 0; i < handler.length; i++) {
watchers.push(Fix.watch(vm.model, key, innerHandler, {
store: vm
@ -961,14 +961,14 @@ BI.watch = function (vm, watch, handler) {
}
}
// vm中一定有_widget
BI.Widget.current._watchers || (BI.Widget.current._watchers = []);
BI.Widget.current._watchers = BI.Widget.current._watchers.concat(watchers);
Widget.current._watchers || (Widget.current._watchers = []);
Widget.current._watchers = Widget.current._watchers.concat(watchers);
return;
}
handler = watch;
watch = vm;
BI.Widget.current.$watchDelayCallbacks || (BI.Widget.current.$watchDelayCallbacks = []);
BI.Widget.current.$watchDelayCallbacks.push([watch, handler]);
Widget.current.$watchDelayCallbacks || (Widget.current.$watchDelayCallbacks = []);
Widget.current.$watchDelayCallbacks.push([watch, handler]);
}
};
@ -980,7 +980,7 @@ BI.onBeforeMount = function (beforeMount) {
}
if (!current.beforeMount) {
current.beforeMount = [];
} else if (!BI.isArray(current.beforeMount)) {
} else if (!isArray(current.beforeMount)) {
current.beforeMount = [current.beforeMount];
}
current.beforeMount.push(beforeMount);
@ -994,7 +994,7 @@ BI.onMounted = function (mounted) {
}
if (!current.mounted) {
current.mounted = [];
} else if (!BI.isArray(current.mounted)) {
} else if (!isArray(current.mounted)) {
current.mounted = [current.mounted];
}
current.mounted.push(mounted);
@ -1004,7 +1004,7 @@ BI.onBeforeUnmount = function (beforeDestroy) {
if (current) {
if (!current.beforeDestroy) {
current.beforeDestroy = [];
} else if (!BI.isArray(current.beforeDestroy)) {
} else if (!isArray(current.beforeDestroy)) {
current.beforeDestroy = [current.beforeDestroy];
}
current.beforeDestroy.push(beforeDestroy);
@ -1014,19 +1014,19 @@ BI.onUnmounted = function (destroyed) {
if (current) {
if (!current.destroyed) {
current.destroyed = [];
} else if (!BI.isArray(current.destroyed)) {
} else if (!isArray(current.destroyed)) {
current.destroyed = [current.destroyed];
}
current.destroyed.push(destroyed);
}
};
BI.Widget.registerRenderEngine = function (engine) {
BI.Widget._renderEngine = engine;
Widget.registerRenderEngine = function (engine) {
Widget._renderEngine = engine;
};
BI.Widget.registerRenderEngine({
Widget.registerRenderEngine({
createElement: function (widget) {
if (BI.isWidget(widget)) {
if (isWidget(widget)) {
const o = widget.options;
if (o.element) {
return BI.$(o.element);
@ -1048,7 +1048,7 @@ BI.mount = function (widget, container, predicate, hydrate) {
// 将widget的element元素都挂载好,并建立相互关系
widget.element.data("__widgets", [widget]);
const res = widget._mount(true, false, false, function (w) {
BI.each(w._children, function (i, child) {
each(w._children, function (i, child) {
const ws = child.element.data("__widgets");
if (!ws) {
ws = [];
@ -1059,13 +1059,13 @@ BI.mount = function (widget, container, predicate, hydrate) {
predicate && predicate.apply(this, arguments);
});
// 将新的dom树属性(事件等)patch到已存在的dom上
const c = BI.Widget._renderEngine.createElement;
const c = Widget._renderEngine.createElement;
BI.DOM.patchProps(widget.element, c(c(container).children()[0]));
const triggerLifeHook = function (w) {
w.beforeMount && w.beforeMount();
w.mounted && w.mounted();
BI.each(w._children, function (i, child) {
each(w._children, function (i, child) {
triggerLifeHook(child);
});
};
@ -1074,7 +1074,7 @@ BI.mount = function (widget, container, predicate, hydrate) {
return res;
}
if (container) {
BI.Widget._renderEngine.createElement(container).append(widget.element);
Widget._renderEngine.createElement(container).append(widget.element);
}
return widget._mount(true, false, false, predicate);
};

2
src/core/index.js

@ -57,3 +57,5 @@ export {
TooltipsController,
StyleLoaderManager,
}
export * from './2.base';
Loading…
Cancel
Save