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.

348 lines
12 KiB

8 years ago
/**
8 years ago
* Created by zcf_1 on 2017/5/2.
8 years ago
*/
8 years ago
BI.MultiSelectList = BI.inherit(BI.Widget, {
6 years ago
_constant: {
EDITOR_HEIGHT: 24
},
8 years ago
_defaultConfig: function () {
8 years ago
return BI.extend(BI.MultiSelectList.superclass._defaultConfig.apply(this, arguments), {
7 years ago
baseCls: "bi-multi-select-list",
8 years ago
itemsCreator: BI.emptyFn,
valueFormatter: BI.emptyFn
7 years ago
});
8 years ago
},
_init: function () {
8 years ago
BI.MultiSelectList.superclass._init.apply(this, arguments);
8 years ago
var self = this, o = this.options;
8 years ago
this.storeValue = {};
8 years ago
var assertShowValue = function () {
BI.isKey(self._startValue) && (self.storeValue.type === BI.Selection.All ? BI.remove(self.storeValue.value, self._startValue) : BI.pushDistinct(self.storeValue.value, self._startValue));
8 years ago
// self.trigger.setValue(self.storeValue);
8 years ago
};
8 years ago
this.adapter = BI.createWidget({
8 years ago
type: "bi.multi_select_loader",
8 years ago
cls: "popup-multi-select-list bi-border-left bi-border-right bi-border-bottom",
8 years ago
itemsCreator: o.itemsCreator,
valueFormatter: o.valueFormatter,
7 years ago
logic: {
dynamic: false
},
8 years ago
// onLoaded: o.onLoaded,
7 years ago
el: {}
8 years ago
});
8 years ago
this.adapter.on(BI.MultiSelectLoader.EVENT_CHANGE, function () {
8 years ago
self.storeValue = this.getValue();
self._adjust(function () {
assertShowValue();
8 years ago
self.fireEvent(BI.MultiSelectList.EVENT_CHANGE);
8 years ago
});
});
8 years ago
this.searcherPane = BI.createWidget({
type: "bi.multi_select_search_pane",
cls: "bi-border-left bi-border-right bi-border-bottom",
8 years ago
valueFormatter: o.valueFormatter,
8 years ago
keywordGetter: function () {
return self.trigger.getKeyword();
},
8 years ago
itemsCreator: function (op, callback) {
var keyword = self.trigger.getKeyword();
if (BI.isNotEmptyString(keyword)) {
op.keywords = [keyword];
this.setKeyword(op.keywords[0]);
o.itemsCreator(op, callback);
}
8 years ago
}
});
8 years ago
this.searcherPane.setVisible(false);
8 years ago
8 years ago
this.trigger = BI.createWidget({
type: "bi.searcher",
isAutoSearch: false,
isAutoSync: false,
onSearch: function (op, callback) {
callback();
},
adapter: this.adapter,
popup: this.searcherPane,
height: 200,
masker: false,
listeners: [{
eventName: BI.Searcher.EVENT_START,
action: function () {
self._showSearcherPane();
8 years ago
self._setStartValue("");
8 years ago
this.setValue(BI.deepClone(self.storeValue));
8 years ago
}
}, {
eventName: BI.Searcher.EVENT_STOP,
action: function () {
self._showAdapter();
self._setStartValue("");
self.adapter.setValue(self.storeValue);
7 years ago
// 需要刷新回到初始界面,否则搜索的结果不能放在最前面
8 years ago
self.adapter.populate();
8 years ago
}
}, {
eventName: BI.Searcher.EVENT_PAUSE,
action: function () {
7 years ago
var keyword = this.getKeyword();
8 years ago
if (this.hasMatched()) {
self._join({
type: BI.Selection.Multi,
value: [keyword]
}, function () {
self._showAdapter();
self.adapter.setValue(self.storeValue);
self._setStartValue(keyword);
assertShowValue();
7 years ago
self.adapter.populate();
8 years ago
self._setStartValue("");
8 years ago
self.fireEvent(BI.MultiSelectList.EVENT_CHANGE);
7 years ago
});
8 years ago
}
8 years ago
}
}, {
eventName: BI.Searcher.EVENT_SEARCHING,
action: function () {
var keywords = this.getKeyword();
var last = BI.last(keywords);
keywords = BI.initial(keywords || []);
if (keywords.length > 0) {
self._joinKeywords(keywords, function () {
if (BI.isEndWithBlank(last)) {
self.adapter.setValue(self.storeValue);
assertShowValue();
self.adapter.populate();
self._setStartValue("");
} else {
self.adapter.setValue(self.storeValue);
assertShowValue();
}
self.fireEvent(BI.MultiSelectList.EVENT_CHANGE);
8 years ago
});
}
}
}, {
eventName: BI.Searcher.EVENT_CHANGE,
action: function (value, obj) {
if (obj instanceof BI.MultiSelectBar) {
self._joinAll(this.getValue(), function () {
assertShowValue();
self.fireEvent(BI.MultiSelectList.EVENT_CHANGE);
8 years ago
});
} else {
7 years ago
self._join(this.getValue(), function () {
8 years ago
assertShowValue();
self.fireEvent(BI.MultiSelectList.EVENT_CHANGE);
8 years ago
});
}
}
}]
8 years ago
});
BI.createWidget({
type: "bi.vtape",
8 years ago
element: this,
8 years ago
items: [{
el: this.trigger,
6 years ago
height: this._constant.EDITOR_HEIGHT
8 years ago
}, {
8 years ago
el: this.adapter,
8 years ago
height: "fill"
}]
});
8 years ago
BI.createWidget({
type: "bi.absolute",
element: this,
items: [{
el: this.searcherPane,
6 years ago
top: this._constant.EDITOR_HEIGHT,
8 years ago
bottom: 0,
left: 0,
right: 0
}]
7 years ago
});
8 years ago
},
8 years ago
_showAdapter: function () {
this.adapter.setVisible(true);
this.searcherPane.setVisible(false);
},
_showSearcherPane: function () {
this.searcherPane.setVisible(true);
this.adapter.setVisible(false);
},
8 years ago
_defaultState: function () {
this.trigger.stopEditing();
},
_assertValue: function (val) {
val || (val = {});
val.type || (val.type = BI.Selection.Multi);
val.value || (val.value = []);
},
_makeMap: function (values) {
return BI.makeObject(values || []);
},
_joinKeywords: function (keywords, callback) {
var self = this, o = this.options;
this._assertValue(this.storeValue);
// 和复选下拉框同步,allData做缓存是会爆炸的
o.itemsCreator({
type: BI.MultiSelectList.REQ_GET_ALL_DATA,
keywords: keywords
}, function (ob) {
var values = BI.map(ob.items, "value");
digest(values);
});
8 years ago
7 years ago
function digest (items) {
8 years ago
var selectedMap = self._makeMap(items);
BI.each(keywords, function (i, val) {
if (BI.isNotNull(selectedMap[val])) {
self.storeValue.type === BI.Selection.Multi ? BI.pushDistinct(self.storeValue.value, val) : BI.remove(self.storeValue.value, val);
8 years ago
}
});
self._adjust(callback);
}
},
_joinAll: function (res, callback) {
var self = this, o = this.options;
this._assertValue(res);
o.itemsCreator({
8 years ago
type: BI.MultiSelectList.REQ_GET_ALL_DATA,
7 years ago
keywords: [this.trigger.getKey()]
8 years ago
}, function (ob) {
var items = BI.map(ob.items, "value");
8 years ago
if (self.storeValue.type === res.type) {
var change = false;
var map = self._makeMap(self.storeValue.value);
BI.each(items, function (i, v) {
if (BI.isNotNull(map[v])) {
change = true;
delete map[v];
}
});
change && (self.storeValue.value = BI.values(map));
self._adjust(callback);
return;
}
var selectedMap = self._makeMap(self.storeValue.value);
var notSelectedMap = self._makeMap(res.value);
var newItems = [];
BI.each(items, function (i, item) {
if (BI.isNotNull(selectedMap[items[i]])) {
delete selectedMap[items[i]];
}
if (BI.isNull(notSelectedMap[items[i]])) {
newItems.push(item);
}
});
self.storeValue.value = newItems.concat(BI.values(selectedMap));
self._adjust(callback);
7 years ago
});
8 years ago
},
_adjust: function (callback) {
var self = this, o = this.options;
7 years ago
if (!this._count) {
o.itemsCreator({
type: BI.MultiSelectList.REQ_GET_DATA_LENGTH
}, function (res) {
self._count = res.count;
adjust();
callback();
});
} else {
adjust();
callback();
}
7 years ago
7 years ago
function adjust () {
7 years ago
if (self.storeValue.type === BI.Selection.All && self.storeValue.value.length >= self._count) {
self.storeValue = {
type: BI.Selection.Multi,
value: []
7 years ago
};
7 years ago
} else if (self.storeValue.type === BI.Selection.Multi && self.storeValue.value.length >= self._count) {
self.storeValue = {
type: BI.Selection.All,
value: []
7 years ago
};
7 years ago
}
8 years ago
}
},
_join: function (res, callback) {
var self = this, o = this.options;
this._assertValue(res);
this._assertValue(this.storeValue);
if (this.storeValue.type === res.type) {
var map = this._makeMap(this.storeValue.value);
BI.each(res.value, function (i, v) {
if (!map[v]) {
self.storeValue.value.push(v);
map[v] = v;
}
});
var change = false;
BI.each(res.assist, function (i, v) {
if (BI.isNotNull(map[v])) {
change = true;
delete map[v];
}
});
change && (this.storeValue.value = BI.values(map));
self._adjust(callback);
return;
}
this._joinAll(res, callback);
},
_setStartValue: function (value) {
this._startValue = value;
8 years ago
this.adapter.setStartValue(value);
8 years ago
},
8 years ago
isAllSelected: function () {
return this.adapter.isAllSelected();
},
8 years ago
resize: function () {
8 years ago
// this.trigger.getCounter().adjustView();
// this.trigger.adjustView();
8 years ago
},
setValue: function (v) {
this.storeValue = v || {};
this._assertValue(this.storeValue);
8 years ago
this.adapter.setValue(this.storeValue);
8 years ago
this.trigger.setValue(this.storeValue);
},
getValue: function () {
return BI.deepClone(this.storeValue);
8 years ago
},
populate: function () {
8 years ago
this.adapter.populate.apply(this.adapter, arguments);
8 years ago
this.trigger.populate.apply(this.trigger, arguments);
}
});
8 years ago
BI.extend(BI.MultiSelectList, {
7 years ago
REQ_GET_DATA_LENGTH: 1,
8 years ago
REQ_GET_ALL_DATA: -1
});
BI.MultiSelectList.EVENT_CHANGE = "EVENT_CHANGE";
8 years ago
BI.shortcut("bi.multi_select_list", BI.MultiSelectList);