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.

338 lines
7.8 KiB

7 years ago
/**
* Widget超类
* @class BI.Widget
* @extends BI.OB
*
* @cfg {JSON} options 配置属性
*/
BI.Widget = BI.inherit(BI.OB, {
_defaultConfig: function () {
7 years ago
return BI.extend({
7 years ago
tagName: "div",
7 years ago
attributes: null,
data: null,
7 years ago
tag: null,
disabled: false,
invisible: false,
invalid: false,
baseCls: "",
cls: ""
7 years ago
}, BI.Widget.superclass._defaultConfig.call(this))
7 years ago
},
7 years ago
//生命周期函数
beforeCreated: function () {
},
created: function () {
},
render: function () {
},
beforeMounted: function () {
},
mounted: function () {
},
destroyed: function () {
7 years ago
},
_init: function () {
7 years ago
BI.Widget.superclass._init.apply(this, arguments);
this.beforeCreated();
7 years ago
this._initRoot();
this._initElementWidth();
this._initElementHeight();
this._initVisualEffects();
7 years ago
this._initState();
this._initElement();
this.created();
7 years ago
},
/**
* 初始化根节点
* @private
*/
_initRoot: function () {
7 years ago
var o = this.options;
this.widgetName = o.widgetName || BI.uniqueId("widget");
if (BI.isWidget(o.element)) {
this._parent = o.element;
this._parent.addWidget(this.widgetName, this);
this.element = this.options.element.element;
} else if (BI.isString(o.element)) {
this.element = $(o.element);
this._isRoot = true;
7 years ago
} else {
7 years ago
this.element = $(document.createElement(o.tagName));
}
if (o.baseCls) {
this.element.addClass(o.baseCls);
}
if (o.cls) {
this.element.addClass(o.cls);
7 years ago
}
7 years ago
if (o.attributes) {
this.element.attr(o.attributes);
7 years ago
}
7 years ago
if (o.data) {
this.element.data(o.data);
}
this._children = {};
7 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);
}
},
_initVisualEffects: function () {
7 years ago
var o = this.options;
if (o.invisible) {
this.element.hide();
}
if (o.disabled) {
this.element.addClass("base-disabled disabled");
}
if (o.invalid) {
this.element.addClass("base-invalid invalid");
}
},
_initState: function () {
this._isMounted = false;
},
_initElement: function () {
var self = this;
var els = this.render();
if (BI.isPlainObject(els)) {
els = [els];
7 years ago
}
7 years ago
if (BI.isArray(els)) {
BI.each(els, function (i, el) {
BI.createWidget(el, {
element: self
})
})
}
if (this._isRoot === true) {
this._mount();
}
},
7 years ago
7 years ago
_setParent: function (parent) {
this._parent = parent;
},
_mount: function () {
var self = this;
var isMounted = this._isMounted;
if (isMounted) {
return;
}
if (this._isRoot === true) {
isMounted = true;
} else if (this._parent && this._parent._isMounted === true) {
isMounted = true;
7 years ago
}
7 years ago
if (!isMounted) {
return;
}
this.beforeMounted();
this._isMounted = true;
this._mountChildren();
BI.each(this._children, function (i, widget) {
widget._mount();
});
this.mounted();
7 years ago
},
7 years ago
_mountChildren: function () {
var self = this;
var frag = document.createDocumentFragment();
var hasChild = false;
BI.each(this._children, function (i, widget) {
if (widget.element !== self.element) {
frag.appendChild(widget.element[0]);
hasChild = true;
7 years ago
}
7 years ago
});
if (hasChild === true) {
this.element.append(frag);
7 years ago
}
7 years ago
},
_unMount: function () {
BI.each(this._children, function (i, widget) {
widget._unMount();
});
this._children = [];
this._parent = null;
this._isMounted = false;
this.destroyed();
7 years ago
},
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;
this.element.removeClass("base-disabled disabled");
} else if (enable === false) {
this.options.disabled = true;
this.element.addClass("base-disabled disabled");
}
},
setVisible: function (visible) {
if (visible === true) {
this.options.invisible = false;
this.element.show();
} else if (visible === false) {
this.options.invisible = true;
this.element.hide();
}
},
setValid: function (valid) {
this.options.invalid = !valid;
if (valid === true) {
this.element.removeClass("base-invalid invalid");
} else if (valid === false) {
this.element.addClass("base-invalid invalid");
}
},
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();
}
7 years ago
name = name || widget.getName() || BI.uniqueId("widget");
if (this._children[name]) {
7 years ago
throw new Error("name has already been existed");
}
7 years ago
widget._setParent(this);
7 years ago
widget.on(BI.Events.DESTROY, function () {
7 years ago
delete self._children[name]
7 years ago
});
7 years ago
return (this._children[name] = widget);
7 years ago
},
getWidgetByName: function (name) {
if (!BI.isKey(name) || name == this.getName()) {
return this;
}
name = name + "";
var widget = void 0, other = {};
7 years ago
BI.any(this._children, function (i, wi) {
7 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;
},
7 years ago
removeWidget: function (name) {
delete this._children[name];
7 years ago
},
7 years ago
hasWidget: function (name) {
return this._children[name] != null;
7 years ago
},
getName: function () {
7 years ago
return this.widgetName;
7 years ago
},
setTag: function (tag) {
this.options.tag = tag;
},
getTag: function () {
return this.options.tag;
},
attr: function (key, value) {
if (BI.isNotNull(value)) {
return this.options[key] = value;
}
return this.options[key];
},
7 years ago
getText: function () {
7 years ago
},
7 years ago
setText: function (text) {
7 years ago
},
getValue: function () {
},
7 years ago
setValue: function (value) {
7 years ago
},
isEnabled: function () {
return !this.options.disabled;
},
isVisible: function () {
return !this.options.invisible;
},
destroy: function () {
7 years ago
this._unMount();
this.element.destroy();
7 years ago
this.fireEvent(BI.Events.DESTROY);
}
});