Browse Source

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

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

10
src/base/1.pane.js

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

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

160
src/core/4.widget.js

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

4
src/core/index.js

@ -56,4 +56,6 @@ export {
ResizeController, ResizeController,
TooltipsController, TooltipsController,
StyleLoaderManager, StyleLoaderManager,
} }
export * from './2.base';
Loading…
Cancel
Save