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.

225 lines
6.6 KiB

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