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.

447 lines
15 KiB

2 years ago
import {
shortcut,
extend,
emptyFn,
deepClone,
isKey,
Selection,
remove,
pushDistinct,
createWidget,
isNotEmptyString,
i18nText,
isEmptyArray,
last,
initial,
endWith,
AbsoluteLayout,
isEmptyString,
makeObject,
each,
Func,
map,
concat,
isNotNull,
values,
filter,
contains,
isNull, VerticalFillLayout
} from "@/core";
import { Single, Searcher } from "@/base";
import { MultiSelectBar } from "@/case";
import { SelectPatchEditor } from "../multiselect/trigger/editor/editor.patch";
import { MultiSelectLoader } from "../multiselect/multiselect.loader";
import { MultiSelectSearchInsertPane } from "../multiselect/search/multiselect.search.insert.pane";
import { SearchEditor } from "@/widget/editor/editor.search";
@shortcut()
export class MultiSelectInsertList extends Single {
static xtype = "bi.multi_select_insert_list";
static REQ_GET_DATA_LENGTH = "1";
static REQ_GET_ALL_DATA = "-1";
static EVENT_CHANGE = "EVENT_CHANGE";
_defaultConfig() {
return extend(super._defaultConfig(...arguments), {
7 years ago
baseCls: "bi-multi-select-insert-list",
2 years ago
itemsCreator: emptyFn,
valueFormatter: emptyFn,
searcherHeight: BI.SIZE_CONSANTS.TRIGGER_HEIGHT,
itemHeight: BI.SIZE_CONSANTS.LIST_ITEM_HEIGHT,
7 years ago
});
2 years ago
}
8 years ago
2 years ago
_init() {
super._init(...arguments);
8 years ago
2 years ago
const self = this,
o = this.options;
this.storeValue = this._assertValue(deepClone(o.value) || {});
function assertShowValue() {
isKey(self._startValue) &&
(self.storeValue.type === Selection.All
? remove(self.storeValue.value, self._startValue)
: pushDistinct(self.storeValue.value, self._startValue));
8 years ago
// self.trigger.setValue(self.storeValue);
2 years ago
}
8 years ago
2 years ago
this.adapter = createWidget({
type: MultiSelectLoader.xtype,
8 years ago
cls: "popup-multi-select-list bi-border-left bi-border-right bi-border-bottom",
itemsCreator: o.itemsCreator,
itemHeight: o.itemHeight,
8 years ago
valueFormatter: o.valueFormatter,
itemFormatter: o.itemFormatter,
8 years ago
logic: {
2 years ago
dynamic: false,
8 years ago
},
// onLoaded: o.onLoaded,
7 years ago
el: {},
isDefaultInit: true,
2 years ago
value: o.value,
8 years ago
});
2 years ago
this.adapter.on(MultiSelectLoader.EVENT_CHANGE, function () {
8 years ago
self.storeValue = this.getValue();
7 years ago
assertShowValue();
2 years ago
self.fireEvent(MultiSelectInsertList.EVENT_CHANGE);
8 years ago
});
2 years ago
this.searcherPane = createWidget({
type: MultiSelectSearchInsertPane.xtype,
8 years ago
cls: "bi-border-left bi-border-right bi-border-bottom",
valueFormatter: o.valueFormatter,
itemFormatter: o.itemFormatter,
2 years ago
keywordGetter() {
8 years ago
return self.trigger.getKeyword();
},
2 years ago
itemsCreator(op, callback) {
const keyword = self.trigger.getKeyword();
if (isNotEmptyString(keyword)) {
op.keywords = [keyword];
this.setKeyword(op.keywords[0]);
o.itemsCreator(op, callback);
}
7 years ago
},
itemHeight: o.itemHeight,
8 years ago
});
this.searcherPane.setVisible(false);
2 years ago
this.trigger = createWidget({
type: Searcher.xtype,
el: {
2 years ago
type: SelectPatchEditor.xtype,
el: {
2 years ago
type: SearchEditor.xtype,
watermark: i18nText("BI-Basic_Search_And_Patch_Paste"),
},
2 years ago
ref(ref) {
self.editor = ref;
},
},
8 years ago
isAutoSearch: false,
isAutoSync: false,
2 years ago
onSearch(op, callback) {
8 years ago
callback();
},
adapter: this.adapter,
popup: this.searcherPane,
masker: false,
2 years ago
listeners: [
{
eventName: Searcher.EVENT_START,
action() {
self._showSearcherPane();
self._setStartValue("");
this.setValue(deepClone(self.storeValue));
},
},
{
eventName: Searcher.EVENT_STOP,
action() {
self._showAdapter();
2 years ago
self._setStartValue("");
self.adapter.setValue(self.storeValue);
2 years ago
// 需要刷新回到初始界面,否则搜索的结果不能放在最前面
self.adapter.populate();
2 years ago
},
},
{
eventName: Searcher.EVENT_PAUSE,
action() {
let keywords = self._getKeywords();
if (
keywords[keywords.length - 1] === BI.BlankSplitChar
) {
keywords = keywords.slice(0, keywords.length - 1);
}
const keyword = isEmptyArray(keywords)
? ""
: keywords[keywords.length - 1];
self._join(
{
type: Selection.Multi,
value: [keyword],
},
() => {
if (self.storeValue.type === Selection.Multi) {
pushDistinct(
self.storeValue.value,
keyword
);
}
self._showAdapter();
8 years ago
self.adapter.setValue(self.storeValue);
2 years ago
self._setStartValue(keyword);
8 years ago
assertShowValue();
self.adapter.populate();
self._setStartValue("");
2 years ago
self.fireEvent(
MultiSelectInsertList.EVENT_CHANGE
);
8 years ago
}
2 years ago
);
self._showAdapter();
},
},
{
eventName: Searcher.EVENT_SEARCHING,
action() {
let keywords = self._getKeywords();
const lastKeyword = last(keywords);
keywords = initial(keywords || []);
if (keywords.length > 0) {
self._joinKeywords(keywords, () => {
if (endWith(lastKeyword, BI.BlankSplitChar)) {
self.adapter.setValue(self.storeValue);
assertShowValue();
self.adapter.populate();
self._setStartValue("");
} else {
self.adapter.setValue(self.storeValue);
assertShowValue();
}
self.fireEvent(
MultiSelectInsertList.EVENT_CHANGE
);
});
self._getKeywordsLength() > 2000 &&
BI.Msg.alert(
i18nText("BI-Basic_Prompt"),
i18nText(
"BI-Basic_Too_Much_Value_Get_Two_Thousand"
)
);
}
},
},
{
eventName: Searcher.EVENT_CHANGE,
action(value, obj) {
if (obj instanceof MultiSelectBar) {
self._joinAll(this.getValue(), () => {
assertShowValue();
self.fireEvent(
MultiSelectInsertList.EVENT_CHANGE
);
});
} else {
self._join(this.getValue(), () => {
assertShowValue();
self.fireEvent(
MultiSelectInsertList.EVENT_CHANGE
);
});
}
},
8 years ago
}
2 years ago
],
value: o.value,
8 years ago
});
2 years ago
createWidget({
type: VerticalFillLayout.xtype,
rowSize: ["", "fill"],
8 years ago
element: this,
2 years ago
items: [
{
el: this.trigger,
},
{
el: this.adapter,
}
],
8 years ago
});
2 years ago
createWidget({
type: AbsoluteLayout.xtype,
8 years ago
element: this,
2 years ago
items: [
{
el: this.searcherPane,
top: o.searcherHeight || BI.SIZE_CONSANTS.TRIGGER_HEIGHT,
bottom: 0,
left: 0,
right: 0,
}
],
7 years ago
});
2 years ago
}
8 years ago
2 years ago
_getKeywords() {
const val = this.editor.getValue();
let keywords = val.split(/\u200b\s\u200b/);
if (isEmptyString(keywords[keywords.length - 1])) {
keywords = keywords.slice(0, keywords.length - 1);
}
if (/\u200b\s\u200b$/.test(val)) {
keywords = keywords.concat([BI.BlankSplitChar]);
}
2 years ago
return keywords.length > 2000
? keywords.slice(0, 2000).concat([BI.BlankSplitChar])
: keywords.slice(0, 2000);
}
2 years ago
_getKeywordsLength() {
const val = this.editor.getValue();
const keywords = val.split(/\u200b\s\u200b/);
return keywords.length - 1;
2 years ago
}
2 years ago
_showAdapter() {
8 years ago
this.adapter.setVisible(true);
this.searcherPane.setVisible(false);
2 years ago
}
8 years ago
2 years ago
_showSearcherPane() {
8 years ago
this.searcherPane.setVisible(true);
this.adapter.setVisible(false);
2 years ago
}
8 years ago
2 years ago
_defaultState() {
8 years ago
this.trigger.stopEditing();
2 years ago
}
8 years ago
2 years ago
_assertValue(val) {
8 years ago
val || (val = {});
2 years ago
val.type || (val.type = Selection.Multi);
8 years ago
val.value || (val.value = []);
2 years ago
return val;
2 years ago
}
8 years ago
2 years ago
_makeMap(values) {
return makeObject(values || []);
}
8 years ago
2 years ago
_joinKeywords(keywords, callback) {
const self = this;
8 years ago
this._assertValue(this.storeValue);
// 和复选下拉框同步,allData做缓存是会爆炸的
8 years ago
digest();
function digest() {
2 years ago
each(keywords, (i, val) => {
self.storeValue.type === Selection.Multi
? pushDistinct(self.storeValue.value, val)
: remove(self.storeValue.value, val);
8 years ago
});
callback();
}
2 years ago
}
8 years ago
2 years ago
_joinAll(res, callback) {
const self = this,
o = this.options;
8 years ago
this._assertValue(res);
if (this.storeValue.type === res.type) {
2 years ago
const result = Func.getSearchResult(
map(this.storeValue.value, (_i, v) => {
return {
text: o.valueFormatter(v) || v,
value: v,
};
}),
this.trigger.getKeyword()
);
let change = false;
const tempMap = this._makeMap(this.storeValue.value);
each(concat(result.match, result.find), (i, obj) => {
const v = obj.value;
if (isNotNull(tempMap[v])) {
change = true;
2 years ago
delete tempMap[v];
}
});
2 years ago
change && (this.storeValue.value = values(tempMap));
callback();
2 years ago
return;
}
2 years ago
o.itemsCreator(
{
type: MultiSelectInsertList.REQ_GET_ALL_DATA,
keywords: [this.trigger.getKeyword()],
selectedValues: filter(this.storeValue.value, (_i, v) => !contains(res.value, v)),
},
ob => {
const items = map(ob.items, "value");
const selectedMap = self._makeMap(self.storeValue.value);
const notSelectedMap = self._makeMap(res.value);
const newItems = [];
each(items, (i, item) => {
if (isNotNull(selectedMap[items[i]])) {
delete selectedMap[items[i]];
}
if (isNull(notSelectedMap[items[i]])) {
newItems.push(item);
}
});
self.storeValue.value = newItems.concat(values(selectedMap));
callback();
}
);
}
8 years ago
2 years ago
_join(res, callback) {
const self = this;
8 years ago
this._assertValue(res);
this._assertValue(this.storeValue);
if (this.storeValue.type === res.type) {
2 years ago
const map = this._makeMap(this.storeValue.value);
each(res.value, (i, v) => {
8 years ago
if (!map[v]) {
2 years ago
pushDistinct(self.storeValue.value, v);
8 years ago
map[v] = v;
}
});
2 years ago
let change = false;
each(res.assist, (i, v) => {
if (isNotNull(map[v])) {
8 years ago
change = true;
delete map[v];
}
});
2 years ago
change && (this.storeValue.value = values(map));
8 years ago
callback();
2 years ago
8 years ago
return;
}
this._joinAll(res, callback);
2 years ago
}
8 years ago
2 years ago
_setStartValue(value) {
8 years ago
this._startValue = value;
this.adapter.setStartValue(value);
2 years ago
}
8 years ago
2 years ago
isAllSelected() {
8 years ago
return this.adapter.isAllSelected();
2 years ago
}
8 years ago
2 years ago
resize() {
8 years ago
// this.trigger.getCounter().adjustView();
// this.trigger.adjustView();
2 years ago
}
setValue(v) {
8 years ago
this.storeValue = v || {};
this._assertValue(this.storeValue);
this.adapter.setValue(this.storeValue);
this.trigger.setValue(this.storeValue);
}
2 years ago
getValue() {
return deepClone(this.storeValue);
}
8 years ago
2 years ago
populate() {
this.adapter.populate(...arguments);
this.trigger.populate(...arguments);
}
}