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.

271 lines
7.6 KiB

2 years ago
import {
shortcut,
Widget,
extend,
emptyFn,
Controller,
createWidget,
Events,
isNotNull,
isEmptyString,
isEmptyArray,
Direction,
get,
LogicFactory,
each,
pixFormat
} from "@/core";
import { ButtonGroup } from "@/base";
2 years ago
import { MultiSelectBar } from "../toolbar/toolbar.multiselect";
import { ListPane } from "../layer/pane.list";
/* eslint-disable no-mixed-spaces-and-tabs */
@shortcut()
export class SelectList extends Widget {
static xtype = "bi.select_list";
static EVENT_CHANGE = "EVENT_CHANGE";
_defaultConfig() {
return extend(super._defaultConfig(...arguments), {
8 years ago
baseCls: "bi-select-list",
direction: Direction.Top, // toolbar的位置
8 years ago
logic: {
dynamic: true,
8 years ago
},
items: [],
itemsCreator: emptyFn,
hasNext: emptyFn,
onLoaded: emptyFn,
8 years ago
toolbar: {
2 years ago
type: MultiSelectBar.xtype,
iconWrapperWidth: 36,
8 years ago
},
el: {
2 years ago
type: ListPane.xtype,
},
7 years ago
});
}
_init() {
super._init(...arguments);
const o = this.options;
8 years ago
7 years ago
// 全选
this.toolbar = createWidget(o.toolbar);
7 years ago
this.allSelected = false;
this.toolbar.on(Controller.EVENT_CHANGE, (...args) => {
const [type, value, obj] = args;
this.allSelected = this.toolbar.isSelected();
if (type === Events.CLICK) {
this.setAllSelected(this.allSelected);
this.fireEvent(SelectList.EVENT_CHANGE, value, obj);
8 years ago
}
this.fireEvent(Controller.EVENT_CHANGE, ...args);
8 years ago
});
this.list = createWidget(o.el, {
2 years ago
type: ListPane.xtype,
8 years ago
items: o.items,
2 years ago
itemsCreator: (op, callback) => {
op.times === 1 && this.toolbar.setVisible(false);
o.itemsCreator(op, (items, keywords, context, ...args) => {
callback(items, keywords, context, ...args);
8 years ago
if (op.times === 1) {
const tipText = get(context, "tipText", "");
const visible = isEmptyString(tipText) && items && items.length > 0;
this.toolbar.setVisible(visible);
this.toolbar.setEnable(this.isEnabled() && visible);
8 years ago
}
this._checkAllSelected();
8 years ago
});
},
onLoaded: o.onLoaded,
hasNext: o.hasNext,
8 years ago
});
this.list.on(Controller.EVENT_CHANGE, (...args) => {
const [type, value, obj] = args;
if (type === Events.CLICK) {
this._checkAllSelected();
this.fireEvent(SelectList.EVENT_CHANGE, value, obj);
8 years ago
}
this.fireEvent(Controller.EVENT_CHANGE, ...args);
8 years ago
});
createWidget(
extend(
{
element: this,
},
LogicFactory.createLogic(
LogicFactory.createLogicTypeByDirection(o.direction),
extend(
{
scrolly: true,
},
o.logic,
{
items: LogicFactory.createLogicItemsByDirection(o.direction, this.toolbar, this.list),
}
)
)
)
);
8 years ago
if (o.items.length <= 0) {
this.toolbar.setVisible(false);
this.toolbar.setEnable(false);
}
if (isNotNull(o.value)) {
this.setValue(o.value);
}
}
8 years ago
_checkAllSelected() {
const selectLength = this.list.getValue().length;
const notSelectLength = this.getAllLeaves().length - selectLength;
const hasNext = this.list.hasNext();
const isAlreadyAllSelected = this.toolbar.isSelected();
let isHalf = selectLength > 0 && notSelectLength > 0;
let allSelected = selectLength > 0 && notSelectLength <= 0 && (!hasNext || isAlreadyAllSelected);
if (this.isAllSelected() === false) {
hasNext && (isHalf = selectLength > 0);
if (!isAlreadyAllSelected && notSelectLength === 0 && !hasNext) {
allSelected = true;
}
} else {
hasNext && (isHalf = notSelectLength > 0);
if (!isAlreadyAllSelected && notSelectLength === 0) {
allSelected = true;
}
}
8 years ago
this.toolbar.setHalfSelected(isHalf);
!isHalf && this.toolbar.setSelected(allSelected);
}
8 years ago
setAllSelected(v) {
if (this.list.setAllSelected) {
this.list.setAllSelected(v);
} else {
each(this.getAllButtons(), (i, btn) => {
(btn.setSelected || btn.setAllSelected).apply(btn, [v]);
});
}
7 years ago
this.allSelected = !!v;
8 years ago
this.toolbar.setSelected(v);
this.toolbar.setHalfSelected(false);
}
8 years ago
setToolBarVisible(b) {
8 years ago
this.toolbar.setVisible(b);
}
8 years ago
isAllSelected() {
7 years ago
return this.allSelected;
// return this.toolbar.isSelected();
}
8 years ago
hasPrev() {
8 years ago
return this.list.hasPrev();
}
8 years ago
hasNext() {
8 years ago
return this.list.hasNext();
}
8 years ago
prependItems(items) {
this.list.prependItems(...arguments);
}
8 years ago
addItems(items) {
this.list.addItems(...arguments);
}
8 years ago
setValue(data) {
const selectAll = data.type === ButtonGroup.CHOOSE_TYPE_ALL;
8 years ago
this.setAllSelected(selectAll);
this.list[selectAll ? "setNotSelectedValue" : "setValue"](data.value);
this._checkAllSelected();
}
8 years ago
getValue() {
8 years ago
if (this.isAllSelected() === false) {
return {
type: ButtonGroup.CHOOSE_TYPE_MULTI,
8 years ago
value: this.list.getValue(),
assist: this.list.getNotSelectedValue(),
8 years ago
};
}
7 years ago
return {
type: ButtonGroup.CHOOSE_TYPE_ALL,
7 years ago
value: this.list.getNotSelectedValue(),
assist: this.list.getValue(),
7 years ago
};
}
empty() {
8 years ago
this.list.empty();
}
8 years ago
populate(items) {
this.toolbar.setVisible(!isEmptyArray(items));
this.toolbar.setEnable(this.isEnabled() && !isEmptyArray(items));
this.list.populate(...arguments);
8 years ago
this._checkAllSelected();
}
8 years ago
_setEnable(enable) {
super._setEnable(...arguments);
8 years ago
this.toolbar.setEnable(enable);
}
resetHeight(h) {
const toolHeight = (this.toolbar.element.outerHeight() || 25) * (this.toolbar.isVisible() ? 1 : 0);
this.list.resetHeight
? this.list.resetHeight(h - toolHeight)
: this.list.element.css({ "max-height": pixFormat(h - toolHeight) });
}
8 years ago
setNotSelectedValue() {
this.list.setNotSelectedValue(...arguments);
8 years ago
this._checkAllSelected();
}
8 years ago
getNotSelectedValue() {
8 years ago
return this.list.getNotSelectedValue();
}
8 years ago
getAllButtons() {
8 years ago
return this.list.getAllButtons();
}
8 years ago
getAllLeaves() {
8 years ago
return this.list.getAllLeaves();
}
8 years ago
getSelectedButtons() {
8 years ago
return this.list.getSelectedButtons();
}
8 years ago
getNotSelectedButtons() {
8 years ago
return this.list.getNotSelectedButtons();
}
8 years ago
getIndexByValue(value) {
8 years ago
return this.list.getIndexByValue(value);
}
8 years ago
getNodeById(id) {
8 years ago
return this.list.getNodeById(id);
}
8 years ago
getNodeByValue(value) {
8 years ago
return this.list.getNodeByValue(value);
}
}