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.

209 lines
6.4 KiB

8 years ago
/**
* 卡片布局可以做到当前只显示一个组件其他的都隐藏
* @class BI.CardLayout
* @extends BI.Layout
*
* @cfg {JSON} options 配置属性
* @cfg {String} options.defaultShowName 默认展示的子组件名
*/
BI.CardLayout = BI.inherit(BI.Layout, {
8 years ago
props: function () {
return BI.extend(BI.CardLayout.superclass.props.apply(this, arguments), {
8 years ago
baseCls: "bi-card-layout",
items: []
});
},
8 years ago
render: function () {
BI.CardLayout.superclass.render.apply(this, arguments);
8 years ago
this.populate(this.options.items);
},
resize: function () {
// console.log("default布局不需要resize");
},
stroke: function (items) {
8 years ago
var self = this, o = this.options;
8 years ago
this.showIndex = void 0;
BI.each(items, function (i, item) {
7 years ago
if (item) {
8 years ago
if (!self.hasWidget(item.cardName)) {
7 years ago
var w = BI.createWidget(item);
8 years ago
w.on(BI.Events.DESTROY, function () {
8 years ago
var index = BI.findIndex(o.items, function (i, tItem) {
8 years ago
return tItem.cardName == item.cardName;
});
if (index > -1) {
o.items.splice(index, 1);
}
8 years ago
});
8 years ago
self.addWidget(item.cardName, w);
8 years ago
} else {
8 years ago
var w = self.getWidgetByName(item.cardName);
8 years ago
}
7 years ago
w.element.css({position: "absolute", top: "0", right: "0", bottom: "0", left: "0"});
8 years ago
w.setVisible(false);
8 years ago
}
});
},
8 years ago
update: function () {
},
8 years ago
empty: function () {
BI.CardLayout.superclass.empty.apply(this, arguments);
this.options.items = [];
},
8 years ago
populate: function (items) {
BI.CardLayout.superclass.populate.apply(this, arguments);
8 years ago
this._mount();
8 years ago
this.options.defaultShowName && this.showCardByName(this.options.defaultShowName);
},
isCardExisted: function (cardName) {
8 years ago
return BI.some(this.options.items, function (i, item) {
return item.cardName == cardName && item.el;
8 years ago
});
8 years ago
},
getCardByName: function (cardName) {
8 years ago
if (!this.isCardExisted(cardName)) {
8 years ago
throw new Error("cardName is not exist");
8 years ago
}
8 years ago
return this._children[cardName];
8 years ago
},
8 years ago
_deleteCardByName: function (cardName) {
delete this._children[cardName];
var index = BI.findIndex(this.options.items, function (i, item) {
8 years ago
return item.cardName == cardName;
});
8 years ago
if (index > -1) {
this.options.items.splice(index, 1);
}
8 years ago
},
deleteCardByName: function (cardName) {
if (!this.isCardExisted(cardName)) {
throw new Error("cardName is not exist");
}
8 years ago
var child = this._children[cardName];
8 years ago
this._deleteCardByName(cardName);
8 years ago
child && child._destroy();
8 years ago
},
addCardByName: function (cardName, cardItem) {
8 years ago
if (this.isCardExisted(cardName)) {
8 years ago
throw new Error("cardName is already exist");
8 years ago
}
7 years ago
var widget = BI.createWidget(cardItem, this);
8 years ago
widget.element.css({
7 years ago
position: "relative",
top: "0",
left: "0",
width: "100%",
height: "100%"
8 years ago
}).appendTo(this.element);
8 years ago
widget.invisible();
8 years ago
this.addWidget(cardName, widget);
this.options.items.push({el: cardItem, cardName: cardName});
8 years ago
return widget;
},
8 years ago
showCardByName: function (name, action, callback) {
8 years ago
var self = this;
7 years ago
// name不存在的时候全部隐藏
8 years ago
var exist = this.isCardExisted(name);
8 years ago
if (this.showIndex != null) {
this.lastShowIndex = this.showIndex;
}
8 years ago
this.showIndex = name;
8 years ago
var flag = false;
8 years ago
BI.each(this.options.items, function (i, item) {
var el = self._children[item.cardName];
8 years ago
if (el) {
if (name != item.cardName) {
7 years ago
// 动画效果只有在全部都隐藏的时候才有意义,且只要执行一次动画操作就够了
8 years ago
!flag && !exist && (BI.Action && action instanceof BI.Action) ? (action.actionBack(el), flag = true) : el.invisible();
} else {
7 years ago
(BI.Action && action instanceof BI.Action) ? action.actionPerformed(void 0, el, callback) : (el.visible(), callback && callback());
8 years ago
}
8 years ago
}
});
},
showLastCard: function () {
var self = this;
this.showIndex = this.lastShowIndex;
8 years ago
BI.each(this.options.items, function (i, item) {
self._children[item.cardName].setVisible(self.showIndex == i);
7 years ago
});
8 years ago
},
setDefaultShowName: function (name) {
this.options.defaultShowName = name;
return this;
},
getDefaultShowName: function () {
return this.options.defaultShowName;
},
getAllCardNames: function () {
return BI.map(this.options.items, function (i, item) {
return item.cardName;
7 years ago
});
8 years ago
},
getShowingCard: function () {
if (!BI.isKey(this.showIndex)) {
return void 0;
}
return this.getWidgetByName(this.showIndex);
},
deleteAllCard: function () {
var self = this;
BI.each(this.getAllCardNames(), function (i, name) {
self.deleteCardByName(name);
7 years ago
});
8 years ago
},
hideAllCard: function () {
8 years ago
var self = this;
BI.each(this.options.items, function (i, item) {
self._children[item.cardName].invisible();
8 years ago
});
},
isAllCardHide: function () {
8 years ago
var self = this;
8 years ago
var flag = true;
8 years ago
BI.some(this.options.items, function (i, item) {
if (self._children[item.cardName].isVisible()) {
8 years ago
flag = false;
8 years ago
return false;
}
});
return flag;
8 years ago
},
removeWidget: function (nameOrWidget) {
var removeName;
if (BI.isWidget(nameOrWidget)) {
BI.each(this._children, function (name, child) {
if (child === nameOrWidget) {
removeName = name;
}
7 years ago
});
8 years ago
} else {
removeName = nameOrWidget;
}
if (removeName) {
this._deleteCardByName(removeName);
}
8 years ago
}
8 years ago
});
7 years ago
BI.shortcut("bi.card", BI.CardLayout);