fineui是帆软报表和BI产品线所使用的前端框架。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

661 lines
20 KiB

8 years ago
/**
* Widget超类
* @class BI.Widget
* @extends BI.OB
*
* @cfg {JSON} options 配置属性
*/
!(function () {
4 years ago
function callLifeHook (self, life) {
if (self[life]) {
var hooks = BI.isArray(self[life]) ? self[life] : [self[life]];
BI.each(hooks, function (i, hook) {
4 years ago
hook.call(self);
4 years ago
});
}
}
BI.Widget = BI.Widget || BI.inherit(BI.OB, {
_defaultConfig: function () {
return BI.extend(BI.Widget.superclass._defaultConfig.apply(this), {
root: false,
tagName: "div",
attributes: null,
data: null,
tag: null,
disabled: false,
invisible: false,
invalid: false,
baseCls: "",
extraCls: "",
5 years ago
cls: "",
css: null
});
},
// 覆盖父类的_constructor方法,widget不走ob的生命周期
5 years ago
_constructor: function () {
4 years ago
if (this.setup) {
pushTarget(this);
this.render = this.setup();
popTarget();
}
},
_lazyConstructor: function () {
if (!this._constructed) {
this._constructed = true;
this._init();
4 years ago
this._initRef();
}
5 years ago
},
beforeInit: null,
// 生命周期函数
beforeCreate: null,
created: null,
render: null,
beforeMount: null,
mounted: null,
shouldUpdate: null,
update: function () {
},
beforeDestroy: null,
destroyed: null,
_init: function () {
BI.Widget.superclass._init.apply(this, arguments);
this._initElementWidth();
this._initElementHeight();
this._initVisual();
this._initState();
this._initRender();
},
_initRender: function () {
6 years ago
if (this.beforeInit) {
this.__asking = true;
this.beforeInit(BI.bind(this._render, this));
if (this.__asking === true) {
this.__async = true;
}
6 years ago
} else {
this._render();
}
},
_render: function () {
this.__asking = false;
4 years ago
callLifeHook(this, "beforeCreate");
this._initElement();
this._initEffects();
4 years ago
callLifeHook(this, "created");
},
/**
* 初始化根节点
* @private
*/
_initRoot: function () {
var o = this.options;
this.widgetName = o.widgetName || BI.uniqueId("widget");
this._isRoot = o.root;
this._children = {};
if (BI.isWidget(o.element)) {
this.element = this.options.element.element;
if (o.element instanceof BI.Widget) {
this._parent = o.element;
this._parent.addWidget(this.widgetName, this);
} else {
this._isRoot = true;
}
} else if (o.element) {
// if (o.root !== true) {
// throw new Error("root is a required property");
// }
this.element = BI.Widget._renderEngine.createElement(this);
8 years ago
this._isRoot = true;
} else {
this.element = BI.Widget._renderEngine.createElement(this);
8 years ago
}
this.element._isWidget = true;
if (o._baseCls || o.baseCls || o.extraCls || o.cls) {
this.element.addClass((o._baseCls || "") + " " + (o.baseCls || "") + " " + (o.extraCls || "") + " " + (o.cls || ""));
}
if (o.attributes) {
this.element.attr(o.attributes);
}
if (o.data) {
this.element.data(o.data);
}
5 years ago
if (o.css) {
this.element.css(o.css);
}
},
8 years ago
_initElementWidth: function () {
var o = this.options;
if (BI.isWidthOrHeight(o.width)) {
this.element.css("width", o.width);
}
},
8 years ago
_initElementHeight: function () {
var o = this.options;
if (BI.isWidthOrHeight(o.height)) {
this.element.css("height", o.height);
}
},
8 years ago
_initVisual: function () {
var o = this.options;
if (o.invisible) {
// 用display属性做显示和隐藏,否则jquery会在显示时将display设为block会覆盖掉display:flex属性
this.element.css("display", "none");
8 years ago
}
},
_initEffects: function () {
var o = this.options;
if (o.disabled || o.invalid) {
if (this.options.disabled) {
this.setEnable(false);
}
if (this.options.invalid) {
this.setValid(false);
}
8 years ago
}
},
8 years ago
_initState: function () {
this._isMounted = false;
},
8 years ago
_initElement: function () {
var self = this;
var els = this.render && this.render();
if (BI.isPlainObject(els)) {
els = [els];
}
if (BI.isArray(els)) {
BI.each(els, function (i, el) {
4 years ago
BI._lazyCreateWidget(el, {
element: self
});
7 years ago
});
}
// if (this._isRoot === true || !(this instanceof BI.Layout)) {
this._mount();
// }
},
_setParent: function (parent) {
this._parent = parent;
},
/**
*
* @param force 是否强制挂载子节点
* @param deep 子节点是否也是按照当前force处理
* @param lifeHook 生命周期钩子触不触发默认触发
6 years ago
* @param predicate 递归每个widget的回调
* @returns {boolean}
* @private
*/
6 years ago
_mount: function (force, deep, lifeHook, predicate) {
var self = this;
if (!force && (this._isMounted || !this.isVisible() || this.__asking === true || !(this._isRoot === true || (this._parent && this._parent._isMounted === true)))) {
return false;
}
4 years ago
lifeHook !== false && callLifeHook(this, "beforeMount");
this._isMounted = true;
6 years ago
this._mountChildren && this._mountChildren();
BI.each(this._children, function (i, widget) {
!self.isEnabled() && widget._setEnable(false);
!self.isValid() && widget._setValid(false);
6 years ago
widget._mount && widget._mount(deep ? force : false, deep, lifeHook, predicate);
7 years ago
});
4 years ago
lifeHook !== false && callLifeHook(this, "mounted");
6 years ago
this.fireEvent(BI.Events.MOUNT);
6 years ago
predicate && predicate(this);
6 years ago
return true;
},
_mountChildren: null,
isMounted: function () {
return this._isMounted;
},
setWidth: function (w) {
this.options.width = w;
this._initElementWidth();
},
setHeight: function (h) {
this.options.height = h;
this._initElementHeight();
},
_setEnable: function (enable) {
if (enable === true) {
this.options.disabled = false;
} else if (enable === false) {
this.options.disabled = true;
}
// 递归将所有子组件使能
BI.each(this._children, function (i, child) {
!child._manualSetEnable && child._setEnable && child._setEnable(enable);
});
},
8 years ago
_setValid: function (valid) {
if (valid === true) {
this.options.invalid = false;
} else if (valid === false) {
this.options.invalid = true;
8 years ago
}
// 递归将所有子组件使有效
BI.each(this._children, function (i, child) {
!child._manualSetValid && child._setValid && child._setValid(valid);
8 years ago
});
},
_setVisible: function (visible) {
if (visible === true) {
this.options.invisible = false;
} else if (visible === false) {
this.options.invisible = true;
}
},
setEnable: function (enable) {
this._manualSetEnable = true;
this._setEnable(enable);
if (enable === true) {
this.element.removeClass("base-disabled disabled");
} else if (enable === false) {
this.element.addClass("base-disabled disabled");
}
},
setVisible: function (visible) {
this._setVisible(visible);
if (visible === true) {
// 用this.element.show()会把display属性改成block
this.element.css("display", "");
this._mount();
} else if (visible === false) {
this.element.css("display", "none");
}
this.fireEvent(BI.Events.VIEW, visible);
},
setValid: function (valid) {
this._manualSetValid = true;
this._setValid(valid);
if (valid === true) {
this.element.removeClass("base-invalid invalid");
} else if (valid === false) {
this.element.addClass("base-invalid invalid");
}
},
8 years ago
doBehavior: function () {
var args = arguments;
// 递归将所有子组件使有效
BI.each(this._children, function (i, child) {
child.doBehavior && child.doBehavior.apply(child, args);
});
},
8 years ago
getWidth: function () {
return this.options.width;
},
8 years ago
getHeight: function () {
return this.options.height;
},
8 years ago
isValid: function () {
return !this.options.invalid;
},
8 years ago
addWidget: function (name, widget) {
var self = this;
if (name instanceof BI.Widget) {
widget = name;
name = widget.getName();
}
if (BI.isKey(name)) {
name = name + "";
}
name = name || widget.getName() || BI.uniqueId("widget");
if (this._children[name]) {
throw new Error("name has already been existed");
}
widget._setParent && widget._setParent(this);
widget.on(BI.Events.DESTROY, function () {
BI.remove(self._children, this);
7 years ago
});
return (this._children[name] = widget);
},
8 years ago
getWidgetByName: function (name) {
if (!BI.isKey(name) || name === this.getName()) {
return this;
}
name = name + "";
var widget = void 0, other = {};
BI.any(this._children, function (i, wi) {
if (i === name) {
widget = wi;
return true;
}
other[i] = wi;
});
if (!widget) {
BI.any(other, function (i, wi) {
return (widget = wi.getWidgetByName(i));
});
}
return widget;
},
8 years ago
removeWidget: function (nameOrWidget) {
var self = this;
if (BI.isWidget(nameOrWidget)) {
BI.remove(this._children, nameOrWidget);
} else {
delete this._children[nameOrWidget];
}
},
8 years ago
hasWidget: function (name) {
return this._children[name] != null;
},
8 years ago
getName: function () {
return this.widgetName;
},
8 years ago
setTag: function (tag) {
this.options.tag = tag;
},
8 years ago
getTag: function () {
return this.options.tag;
},
8 years ago
attr: function (key, value) {
var self = this;
if (BI.isPlainObject(key)) {
BI.each(key, function (k, v) {
self.attr(k, v);
});
return;
}
if (BI.isNotNull(value)) {
return this.options[key] = value;
}
return this.options[key];
},
5 years ago
css: function (name, value) {
return this.element.css(name, value);
},
getText: function () {
},
setText: function (text) {
},
8 years ago
getValue: function () {
8 years ago
},
8 years ago
setValue: function (value) {
8 years ago
},
8 years ago
isEnabled: function () {
return !this.options.disabled;
},
8 years ago
isVisible: function () {
return !this.options.invisible;
},
8 years ago
disable: function () {
this.setEnable(false);
},
8 years ago
enable: function () {
this.setEnable(true);
},
8 years ago
valid: function () {
this.setValid(true);
},
8 years ago
invalid: function () {
this.setValid(false);
},
8 years ago
invisible: function () {
this.setVisible(false);
},
8 years ago
visible: function () {
this.setVisible(true);
},
__d: function () {
4 years ago
callLifeHook(this, "beforeDestroy");
this.beforeDestroy = null;
BI.each(this._children, function (i, widget) {
widget && widget._unMount && widget._unMount();
});
this._children = {};
this._parent = null;
this._isMounted = false;
4 years ago
callLifeHook(this, "destroyed");
this.destroyed = null;
},
_unMount: function () {
this.__d();
this.fireEvent(BI.Events.UNMOUNT);
this.purgeListeners();
},
isolate: function () {
if (this._parent) {
this._parent.removeWidget(this);
}
BI.DOM.hang([this]);
},
empty: function () {
BI.each(this._children, function (i, widget) {
widget && widget._unMount && widget._unMount();
});
this._children = {};
this.element.empty();
},
_destroy: function () {
this.__d();
this.element.destroy();
this.purgeListeners();
},
destroy: function () {
this.__d();
this.element.destroy();
this.fireEvent(BI.Events.UNMOUNT);
this.fireEvent(BI.Events.DESTROY);
this._purgeRef();
this.purgeListeners();
8 years ago
}
});
4 years ago
var context = null, current = null;
var contextStack = [], currentStack = [];
4 years ago
4 years ago
BI.Widget.pushContext = function (_context) {
4 years ago
if (context) contextStack.push(context);
4 years ago
BI.Widget.context = context = _context;
};
BI.Widget.popContext = function () {
4 years ago
BI.Widget.context = context = contextStack.pop();
4 years ago
};
function pushTarget (_current) {
if (current) currentStack.push(current);
BI.Widget.current = current = _current;
4 years ago
}
function popTarget () {
4 years ago
BI.Widget.current = current = currentStack.pop();
4 years ago
}
4 years ago
BI.useStore = function (_store) {
if (current && current.store) {
return current.store;
}
if (current && current.$storeDelegate) {
return current.$storeDelegate;
}
if (current) {
var delegate = {};
current._store = function () {
var st = _store.apply(this, arguments);
BI.extend(delegate, st);
return st;
};
return current.$storeDelegate = delegate;
}
};
BI.watch = function (watch, handler) {
if (BI.Widget.current) {
BI.Widget.current.$watchDelayCallbacks || (BI.Widget.current.$watchDelayCallbacks = []);
BI.Widget.current.$watchDelayCallbacks.push([watch, handler]);
}
};
4 years ago
BI.onBeforeMount = function (beforeMount) {
4 years ago
if (current) {
4 years ago
if (!current.beforeMount) {
current.beforeMount = [];
} else if (!BI.isArray(current.beforeMount)) {
current.beforeMount = [current.beforeMount];
}
current.beforeMount.push(beforeMount);
4 years ago
}
};
BI.onMounted = function (mounted) {
4 years ago
if (current) {
4 years ago
if (!current.mounted) {
current.mounted = [];
} else if (!BI.isArray(current.mounted)) {
current.mounted = [current.mounted];
}
current.mounted.push(mounted);
4 years ago
}
};
BI.onBeforeUnmount = function (beforeDestroy) {
4 years ago
if (current) {
4 years ago
if (!current.beforeDestroy) {
current.beforeDestroy = [];
} else if (!BI.isArray(current.beforeDestroy)) {
current.beforeDestroy = [current.beforeDestroy];
}
current.beforeDestroy.push(beforeDestroy);
4 years ago
}
};
BI.onUnmounted = function (destroyed) {
4 years ago
if (current) {
4 years ago
if (!current.destroyed) {
current.destroyed = [];
} else if (!BI.isArray(current.destroyed)) {
current.destroyed = [current.destroyed];
}
current.destroyed.push(destroyed);
4 years ago
}
};
BI.Widget.registerRenderEngine = function (engine) {
BI.Widget._renderEngine = engine;
};
BI.Widget.registerRenderEngine({
createElement: function (widget) {
6 years ago
if (BI.isWidget(widget)) {
var o = widget.options;
if (o.element) {
5 years ago
return BI.$(o.element);
}
if (o.tagName) {
return BI.$(document.createElement(o.tagName));
}
return BI.$(document.createDocumentFragment());
}
5 years ago
return BI.$(widget);
6 years ago
},
createFragment: function () {
return document.createDocumentFragment();
}
});
6 years ago
BI.mount = function (widget, container, predicate, hydrate) {
6 years ago
if (hydrate === true) {
6 years ago
// 将widget的element元素都挂载好,并建立相互关系
6 years ago
widget.element.data("__widgets", [widget]);
var res = widget._mount(true, false, false, function (w) {
BI.each(w._children, function (i, child) {
var ws = child.element.data("__widgets");
if (!ws) {
ws = [];
}
6 years ago
ws.push(child);
6 years ago
child.element.data("__widgets", ws);
});
6 years ago
predicate && predicate.apply(this, arguments);
6 years ago
});
// 将新的dom树属性(事件等)patch到已存在的dom上
var c = BI.Widget._renderEngine.createElement;
BI.DOM.patchProps(widget.element, c(c(container).children()[0]));
var triggerLifeHook = function (w) {
w.beforeMount && w.beforeMount();
w.mounted && w.mounted();
BI.each(w._children, function (i, child) {
triggerLifeHook(child);
});
};
6 years ago
// 最后触发组件树生命周期函数
6 years ago
triggerLifeHook(widget);
return res;
}
if (container) {
BI.Widget._renderEngine.createElement(container).append(widget.element);
}
6 years ago
return widget._mount(true, false, false, predicate);
};
})();