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.

464 lines
12 KiB

8 years ago
/**
* Widget超类
* @class BI.Widget
* @extends BI.OB
*
* @cfg {JSON} options 配置属性
*/
BI.Widget = BI.inherit(BI.OB, {
_defaultConfig: function () {
return BI.extend(BI.Widget.superclass._defaultConfig.apply(this), {
8 years ago
root: false,
8 years ago
tagName: "div",
8 years ago
attributes: null,
data: null,
8 years ago
tag: null,
disabled: false,
invisible: false,
invalid: false,
baseCls: "",
extraCls: "",
8 years ago
cls: ""
7 years ago
});
8 years ago
},
7 years ago
beforeInit: null,
7 years ago
// 生命周期函数
8 years ago
beforeCreate: null,
8 years ago
8 years ago
created: null,
8 years ago
8 years ago
render: null,
8 years ago
7 years ago
beforeMount: null,
8 years ago
8 years ago
mounted: null,
8 years ago
7 years ago
shouldUpdate: null,
8 years ago
update: function () {
},
8 years ago
7 years ago
beforeDestroy: null,
8 years ago
8 years ago
destroyed: null,
8 years ago
_init: function () {
8 years ago
BI.Widget.superclass._init.apply(this, arguments);
7 years ago
this._initRoot();
this._initElementWidth();
this._initElementHeight();
this._initVisual();
this._initState();
7 years ago
if (this.beforeInit) {
7 years ago
this.beforeInit(BI.bind(this._render, this));
7 years ago
} else {
7 years ago
this._render();
7 years ago
}
8 years ago
},
7 years ago
_render: function(){
this._children = {};
7 years ago
this.beforeCreate && this.beforeCreate();
this._initElement();
this._initEffects();
this.created && this.created();
},
8 years ago
/**
* 初始化根节点
* @private
*/
_initRoot: function () {
8 years ago
var o = this.options;
this.widgetName = o.widgetName || BI.uniqueId("widget");
8 years ago
this._isRoot = o.root;
8 years ago
if (BI.isWidget(o.element)) {
8 years ago
if (o.element instanceof BI.Widget) {
this._parent = o.element;
this._parent.addWidget(this.widgetName, this);
} else {
this._isRoot = true;
}
8 years ago
this.element = this.options.element.element;
8 years ago
} else if (o.element) {
8 years ago
// if (o.root !== true) {
// throw new Error("root is a required property");
// }
8 years ago
this.element = $(o.element);
this._isRoot = true;
8 years ago
} else {
8 years ago
this.element = $(document.createElement(o.tagName));
}
7 years ago
this.element._isWidget = true;
if (o.baseCls || o.extraCls || o.cls) {
this.element.addClass((o.baseCls || "") + " " + (o.extraCls || "") + " " + (o.cls || ""));
8 years ago
}
8 years ago
if (o.attributes) {
this.element.attr(o.attributes);
8 years ago
}
8 years ago
if (o.data) {
this.element.data(o.data);
}
8 years ago
},
_initElementWidth: function () {
var o = this.options;
if (BI.isWidthOrHeight(o.width)) {
this.element.css("width", o.width);
}
},
_initElementHeight: function () {
var o = this.options;
if (BI.isWidthOrHeight(o.height)) {
this.element.css("height", o.height);
}
},
8 years ago
_initVisual: function () {
8 years ago
var o = this.options;
if (o.invisible) {
7 years ago
// 用display属性做显示和隐藏,否则jquery会在显示时将display设为block会覆盖掉display:flex属性
8 years ago
this.element.css("display", "none");
8 years ago
}
8 years ago
},
_initEffects: function () {
var o = this.options;
8 years ago
if (o.disabled || o.invalid) {
8 years ago
if (this.options.disabled) {
this.setEnable(false);
}
if (this.options.invalid) {
this.setValid(false);
}
8 years ago
}
},
_initState: function () {
this._isMounted = false;
},
_initElement: function () {
var self = this;
8 years ago
var els = this.render && this.render();
8 years ago
if (BI.isPlainObject(els)) {
els = [els];
8 years ago
}
8 years ago
if (BI.isArray(els)) {
BI.each(els, function (i, el) {
BI.createWidget(el, {
element: self
7 years ago
});
});
8 years ago
}
8 years ago
// if (this._isRoot === true || !(this instanceof BI.Layout)) {
this._mount();
// }
8 years ago
},
8 years ago
8 years ago
_setParent: function (parent) {
this._parent = parent;
},
_mount: function () {
var self = this;
var isMounted = this._isMounted;
8 years ago
if (isMounted || !this.isVisible()) {
8 years ago
return;
}
if (this._isRoot === true) {
isMounted = true;
} else if (this._parent && this._parent._isMounted === true) {
isMounted = true;
8 years ago
}
8 years ago
if (!isMounted) {
return;
}
7 years ago
this.beforeMount && this.beforeMount();
8 years ago
this._isMounted = true;
8 years ago
this._mountChildren && this._mountChildren();
8 years ago
BI.each(this._children, function (i, widget) {
8 years ago
!self.isEnabled() && widget._setEnable(false);
8 years ago
!self.isValid() && widget._setValid(false);
8 years ago
widget._mount && widget._mount();
8 years ago
});
8 years ago
this.mounted && this.mounted();
8 years ago
},
8 years ago
_mountChildren: null,
8 years ago
8 years ago
isMounted: function () {
return this._isMounted;
},
8 years ago
setWidth: function (w) {
this.options.width = w;
this._initElementWidth();
},
setHeight: function (h) {
this.options.height = h;
this._initElementHeight();
},
8 years ago
_setEnable: function (enable) {
8 years ago
if (enable === true) {
this.options.disabled = false;
} else if (enable === false) {
this.options.disabled = true;
8 years ago
}
7 years ago
// 递归将所有子组件使能
8 years ago
BI.each(this._children, function (i, child) {
8 years ago
!child._manualSetEnable && child._setEnable && child._setEnable(enable);
8 years ago
});
},
8 years ago
_setValid: function (valid) {
if (valid === true) {
this.options.invalid = false;
} else if (valid === false) {
this.options.invalid = true;
}
7 years ago
// 递归将所有子组件使有效
8 years ago
BI.each(this._children, function (i, child) {
8 years ago
!child._manualSetValid && child._setValid && child._setValid(valid);
8 years ago
});
},
8 years ago
_setVisible: function (visible) {
if (visible === true) {
this.options.invisible = false;
} else if (visible === false) {
this.options.invisible = true;
}
},
8 years ago
setEnable: function (enable) {
8 years ago
this._manualSetEnable = true;
8 years ago
this._setEnable(enable);
if (enable === true) {
this.element.removeClass("base-disabled disabled");
} else if (enable === false) {
8 years ago
this.element.addClass("base-disabled disabled");
}
},
setVisible: function (visible) {
8 years ago
this._setVisible(visible);
8 years ago
if (visible === true) {
7 years ago
// 用this.element.show()会把display属性改成block
8 years ago
this.element.css("display", "");
8 years ago
this._mount();
8 years ago
} else if (visible === false) {
8 years ago
this.element.css("display", "none");
8 years ago
}
8 years ago
this.fireEvent(BI.Events.VIEW, visible);
8 years ago
},
setValid: function (valid) {
8 years ago
this._manualSetValid = true;
8 years ago
this._setValid(valid);
8 years ago
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;
7 years ago
// 递归将所有子组件使有效
8 years ago
BI.each(this._children, function (i, child) {
child.doBehavior && child.doBehavior.apply(child, args);
});
},
8 years ago
getWidth: function () {
return this.options.width;
},
getHeight: function () {
return this.options.height;
},
isValid: function () {
return !this.options.invalid;
},
addWidget: function (name, widget) {
var self = this;
if (name instanceof BI.Widget) {
widget = name;
name = widget.getName();
}
8 years ago
if (BI.isKey(name)) {
8 years ago
name = name + "";
}
8 years ago
name = name || widget.getName() || BI.uniqueId("widget");
if (this._children[name]) {
8 years ago
throw new Error("name has already been existed");
}
8 years ago
widget._setParent && widget._setParent(this);
8 years ago
widget.on(BI.Events.DESTROY, function () {
8 years ago
BI.remove(self._children, this);
8 years ago
});
8 years ago
return (this._children[name] = widget);
8 years ago
},
getWidgetByName: function (name) {
if (!BI.isKey(name) || name === this.getName()) {
8 years ago
return this;
}
name = name + "";
var widget = void 0, other = {};
8 years ago
BI.any(this._children, function (i, wi) {
8 years ago
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)) {
8 years ago
BI.remove(this._children, nameOrWidget);
8 years ago
} else {
delete this._children[nameOrWidget];
}
8 years ago
},
8 years ago
hasWidget: function (name) {
return this._children[name] != null;
8 years ago
},
getName: function () {
8 years ago
return this.widgetName;
8 years ago
},
setTag: function (tag) {
this.options.tag = tag;
},
getTag: function () {
return this.options.tag;
},
attr: function (key, value) {
7 years ago
var self = this;
if (BI.isPlainObject(key)) {
BI.each(key, function (k, v) {
self.attr(k, v);
7 years ago
});
7 years ago
return;
}
8 years ago
if (BI.isNotNull(value)) {
return this.options[key] = value;
}
return this.options[key];
},
8 years ago
getText: function () {
8 years ago
},
8 years ago
setText: function (text) {
8 years ago
},
getValue: function () {
},
8 years ago
setValue: function (value) {
8 years ago
},
isEnabled: function () {
return !this.options.disabled;
},
isVisible: function () {
return !this.options.invisible;
},
8 years ago
disable: function () {
this.setEnable(false);
},
enable: function () {
this.setEnable(true);
},
valid: function () {
this.setValid(true);
},
invalid: function () {
this.setValid(false);
},
invisible: function () {
this.setVisible(false);
},
visible: function () {
this.setVisible(true);
},
8 years ago
__d: function () {
7 years ago
this.beforeDestroy && this.beforeDestroy();
8 years ago
BI.each(this._children, function (i, widget) {
7 years ago
widget && widget._unMount && widget._unMount();
8 years ago
});
this._children = {};
this._parent = null;
this._isMounted = false;
8 years ago
this.destroyed && this.destroyed();
8 years ago
},
_unMount: function () {
this.__d();
8 years ago
this.fireEvent(BI.Events.UNMOUNT);
8 years ago
this.purgeListeners();
},
8 years ago
isolate: function () {
if (this._parent) {
this._parent.removeWidget(this);
}
8 years ago
BI.DOM.hang([this]);
8 years ago
},
8 years ago
empty: function () {
BI.each(this._children, function (i, widget) {
7 years ago
widget && widget._unMount && widget._unMount();
8 years ago
});
this._children = {};
this.element.empty();
},
8 years ago
_destroy: function () {
this.__d();
8 years ago
this.element.destroy();
8 years ago
this.purgeListeners();
},
destroy: function () {
8 years ago
this.__d();
this.element.destroy();
8 years ago
this.fireEvent(BI.Events.DESTROY);
8 years ago
this.purgeListeners();
8 years ago
}
});