📄 select.js
字号:
function uiHtml_Select(domSelect, optOptions) { this._super(domSelect); this.__domSelect = domSelect; this.__options = domSelect.options; this.__optionMap = new Object(); this.__valueMapping = false; this.__handler = uiHtml_SelectOptionWrapper.getInstance(); this.__group = new uiHtml_Group(this.__options, this.__handler); this.__scrollSupporter = new uiHtml_ScrollSupporter(this.__domSelect); if (optOptions != null) { this.addItems(optOptions); }}uiHtml_Select = uiUtil_Object.declareClass(uiHtml_Select, uiHtml_Element);uiHtml_Select.INVALID_INDEX = -1;uiHtml_Select.HISTORY_UPPER_LIMIT = 20;uiHtml_Select.HISTORY_REMOVE_RATE = 5;uiHtml_Select.__historyEnabled = new Array();uiHtml_Select.prototype.getItemHandler = function() { return this.__group.getItemHandler();};uiHtml_Select.prototype.size = function() { return this.__group.size();};uiHtml_Select.prototype.getItemAt = function(index) { return this.__group.getItemAt(index);};uiHtml_Select.prototype._getItems = function() { return this.__group._getItems();};uiHtml_Select.prototype.hasValue = function(value) { return this.__group.hasValue(value);};uiHtml_Select.prototype.getValues = function() { return this.__group.getValues();};uiHtml_Select.prototype.handleItemAtIndex = function(index, owner, handler) { return this.__group.handleItemAtIndex.apply(this.__group, arguments);};uiHtml_Select.prototype.handleItemWithValue = function(value, owner, handler) { return this.__group.handleItemWithValue.apply(this.__group, arguments);};uiHtml_Select.prototype.traverse = function(handlerOwner, handlerFunction, optArgN) { this.__group.traverse.apply(this.__group, arguments);};uiHtml_Select.prototype.setItemAt = function(index, item) { this.__options[index] = item;};uiHtml_Select.prototype.addItem = function(option) { this.__options.add(option); if (this.__valueMapping) { this.__optionMap[option.value] = option; }};uiHtml_Select.prototype.enableOptionValueMapping = function() { this.__valueMapping = true var listLength = this.size(); for (var i = 0; i < listLength; ++i) { var option = this.getItemAt(i); this.__optionMap[option.value] = option; }}uiHtml_Select.prototype.showScrollBars = function(always) { this.__scrollSupporter.showScrollBars(always);};uiHtml_Select.prototype.hideScrollBars = function() { this.__scrollSupporter.hideScrollBars();};uiHtml_Select.prototype.scrollToTop = function() { this.__scrollSupporter.scrollToTop();};uiHtml_Select.prototype.scrollToBottom = function() { if (uiHtml_Window.getInstance().isIe()) { var selectedItems = new Array(); var listLength = this.size(); for (var i = 0; i < listLength; ++i) { var item = this.getItemAt(i); if (this.__handler.isSelected(item)) { selectedItems.push(item); } } var domSelect = this.__domSelect; domSelect.multiple = false; window.setTimeout(function(e) { domSelect.selectedIndex = listLength - 1; }, 50); window.setTimeout(function(e) { domSelect.multiple = true; }, 10); var item = this.getItemAt(listLength - 1); item.selected = false; window.setTimeout(function(e) { for (var i = 0; i < selectedItems.length; ++i) { selectedItems[i].selected = true; } }, 100); } else { this.__scrollSupporter.scrollToBottom(); }};uiHtml_Select.prototype.getDomObject = function() { return this.__domSelect;};uiHtml_Select.prototype.getItemByValue = function(value) { if (!this.__valueMapping) { throw new uiUtil_IllegalStateException( "Option value mapping is not enabled"); } return this.__optionMap[value];};uiHtml_Select.prototype.addItems = function(options) { for (var i = 0; i < options.length; ++i) { this.addItem(options[i]); }};uiHtml_Select.prototype.sortItems = function(comparator, reverse) { var options = this.__domSelect.options; var tempArray = new Array(options.length); for(var i = 0; i < options.length; ++i) { tempArray[i] = options[i]; } tempArray.sort(function(a, b) { return comparator.compare(a,b); }) if (reverse) { tempArray.reverse(); } this.clearItems(); for (var i = 0; i < tempArray.length; ++i) { options.add(tempArray[i]); }};uiHtml_Select.prototype.setSelectedIndex = function(index, optDomEvent) { this.setSelectedItem(this.__options[index], optDomEvent);};uiHtml_Select.prototype.setSelectedItem = function(item, optDomEvent) { this.__handler.setSelected(item, true, optDomEvent);};uiHtml_Select.prototype.setSelectedValue = function(value, optDomEvent) { var option = this.getItemByValue(value); this.setSelectedItem(option, optDomEvent);};uiHtml_Select.prototype.clearItems = function() { var options = this.__domSelect.options; while (options.length > 0) { options[0] = null; }};uiHtml_Select.prototype.removeItemAt = function(index) { this.__options[index] = null;};uiHtml_Select.prototype.enableHistoryTracking = function() { var id = this.__domSelect.id; if (!uiHtml_Select.__historyEnabled[id]) { uiHtml_Select.__historyEnabled[id] = this; this.__changeHistory = new uiUtil_Vector(new Array()); this.__historyIndex = -1; this.__lastSelected = new Array(); this.__pushHistory(); var select = this; this.appendEventHandler("change", function(e) { select.__pushHistory(); }); this.appendEventHandler("keydown", function(e) { var event = new uiHtml_Event(e); if (!event.isAltPressed() && event.isCtrlPressed() && !event.isShiftPressed()) { var ch = event.getPressedChar(true); if (ch == "z") { select.__undoHistory(e); } else if (ch == "y") { select.__redoHistory(e); } } }); }};uiHtml_Select.prototype.__pushHistory = function() { var currSelected = new Array(); var options = this.__domSelect.options; for(var i = 0; i < options.length; ++i) { if (this.__handler.isSelected(options[i])) { currSelected[options[i].value] = options[i]; } } if (this.__historyIndex < this.__changeHistory.length - 1) { this.__changeHistory.removeAt( -1, this.__changeHistory.length - this.__historyIndex - 1); } var currChanges = this.__getChanges(currSelected, this.__lastSelected); this.__changeHistory.add(currChanges); if (this.__changeHistory.length >= uiHtml_Select.HISTORY_UPPER_LIMIT) { this.__changeHistory.removeAt(0, uiHtml_Select.HISTORY_REMOVE_RATE); } this.__historyIndex = this.__changeHistory.length - 1; this.__lastSelected = currSelected; this.__logger.debug("history size: " + this.__changeHistory.length);};uiHtml_Select.prototype.__getChanges = function(currSelected, lastSelected) { var changes = new Array(); for (var key in currSelected) { if (!lastSelected[key] && currSelected[key]) { changes.push(currSelected[key]); } } for (var key in lastSelected) { if (!currSelected[key] && lastSelected[key]) { changes.push(lastSelected[key]); } } return changes;};uiHtml_Select.prototype.__undoHistory = function(domEvent) { if (this.__historyIndex < 1) { return; } this.__playHistory(domEvent, this.__historyIndex--);};uiHtml_Select.prototype.__redoHistory = function(domEvent) { if (this.__historyIndex >= this.__changeHistory.length - 1) { return; } this.__playHistory(domEvent, ++this.__historyIndex);};uiHtml_Select.prototype.__playHistory = function(domEvent, historyIndex) { var changes = this.__changeHistory.get(historyIndex); for (var i = 0; i < changes.length; ++i) { var selected = this.__handler.isSelected(changes[i]); var key = changes[i].value; if (selected) { this.__handler.setSelected(changes[i], false, domEvent); this.__lastSelected[key] = null; } else { this.__handler.setSelected(changes[i], true, domEvent); this.__lastSelected[key] = changes[i]; } }};uiHtml_Select.createByEither = function(id, name) { return uiHtml_Element.createByEither(id, name, uiHtml_Select);};uiHtml_Select.create = function(options, optAppear) { return new uiHtml_Select( uiHtml_Document.getInstance().createDomObject("select", optAppear), options);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -