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.

220 lines
6.1 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. Ext.ns('Ext.ux.form');
  16. /**
  17. * @class Ext.ux.form.SelectBox
  18. * @extends Ext.form.ComboBox
  19. * <p>Makes a ComboBox more closely mimic an HTML SELECT. Supports clicking and dragging
  20. * through the list, with item selection occurring when the mouse button is released.
  21. * When used will automatically set {@link #editable} to false and call {@link Ext.Element#unselectable}
  22. * on inner elements. Re-enabling editable after calling this will NOT work.</p>
  23. * @author Corey Gilmore http://extjs.com/forum/showthread.php?t=6392
  24. * @history 2007-07-08 jvs
  25. * Slight mods for Ext 2.0
  26. * @xtype selectbox
  27. */
  28. Ext.ux.form.SelectBox = Ext.extend(Ext.form.ComboBox, {
  29. constructor: function(config){
  30. this.searchResetDelay = 1000;
  31. config = config || {};
  32. config = Ext.apply(config || {}, {
  33. editable: false,
  34. forceSelection: true,
  35. rowHeight: false,
  36. lastSearchTerm: false,
  37. triggerAction: 'all',
  38. mode: 'local'
  39. });
  40. Ext.ux.form.SelectBox.superclass.constructor.apply(this, arguments);
  41. this.lastSelectedIndex = this.selectedIndex || 0;
  42. },
  43. initEvents : function(){
  44. Ext.ux.form.SelectBox.superclass.initEvents.apply(this, arguments);
  45. // you need to use keypress to capture upper/lower case and shift+key, but it doesn't work in IE
  46. this.el.on('keydown', this.keySearch, this, true);
  47. this.cshTask = new Ext.util.DelayedTask(this.clearSearchHistory, this);
  48. },
  49. keySearch : function(e, target, options) {
  50. var raw = e.getKey();
  51. var key = String.fromCharCode(raw);
  52. var startIndex = 0;
  53. if( !this.store.getCount() ) {
  54. return;
  55. }
  56. switch(raw) {
  57. case Ext.EventObject.HOME:
  58. e.stopEvent();
  59. this.selectFirst();
  60. return;
  61. case Ext.EventObject.END:
  62. e.stopEvent();
  63. this.selectLast();
  64. return;
  65. case Ext.EventObject.PAGEDOWN:
  66. this.selectNextPage();
  67. e.stopEvent();
  68. return;
  69. case Ext.EventObject.PAGEUP:
  70. this.selectPrevPage();
  71. e.stopEvent();
  72. return;
  73. }
  74. // skip special keys other than the shift key
  75. if( (e.hasModifier() && !e.shiftKey) || e.isNavKeyPress() || e.isSpecialKey() ) {
  76. return;
  77. }
  78. if( this.lastSearchTerm == key ) {
  79. startIndex = this.lastSelectedIndex;
  80. }
  81. this.search(this.displayField, key, startIndex);
  82. this.cshTask.delay(this.searchResetDelay);
  83. },
  84. onRender : function(ct, position) {
  85. this.store.on('load', this.calcRowsPerPage, this);
  86. Ext.ux.form.SelectBox.superclass.onRender.apply(this, arguments);
  87. if( this.mode == 'local' ) {
  88. this.initList();
  89. this.calcRowsPerPage();
  90. }
  91. },
  92. onSelect : function(record, index, skipCollapse){
  93. if(this.fireEvent('beforeselect', this, record, index) !== false){
  94. this.setValue(record.data[this.valueField || this.displayField]);
  95. if( !skipCollapse ) {
  96. this.collapse();
  97. }
  98. this.lastSelectedIndex = index + 1;
  99. this.fireEvent('select', this, record, index);
  100. }
  101. },
  102. afterRender : function() {
  103. Ext.ux.form.SelectBox.superclass.afterRender.apply(this, arguments);
  104. if(Ext.isWebKit) {
  105. this.el.swallowEvent('mousedown', true);
  106. }
  107. this.el.unselectable();
  108. this.innerList.unselectable();
  109. this.trigger.unselectable();
  110. this.innerList.on('mouseup', function(e, target, options) {
  111. if( target.id && target.id == this.innerList.id ) {
  112. return;
  113. }
  114. this.onViewClick();
  115. }, this);
  116. this.mun(this.view, 'containerclick', this.onViewClick, this);
  117. this.mun(this.view, 'click', this.onViewClick, this);
  118. this.innerList.on('mouseover', function(e, target, options) {
  119. if( target.id && target.id == this.innerList.id ) {
  120. return;
  121. }
  122. this.lastSelectedIndex = this.view.getSelectedIndexes()[0] + 1;
  123. this.cshTask.delay(this.searchResetDelay);
  124. }, this);
  125. this.trigger.un('click', this.onTriggerClick, this);
  126. this.trigger.on('mousedown', function(e, target, options) {
  127. e.preventDefault();
  128. this.onTriggerClick();
  129. }, this);
  130. this.on('collapse', function(e, target, options) {
  131. Ext.getDoc().un('mouseup', this.collapseIf, this);
  132. }, this, true);
  133. this.on('expand', function(e, target, options) {
  134. Ext.getDoc().on('mouseup', this.collapseIf, this);
  135. }, this, true);
  136. },
  137. clearSearchHistory : function() {
  138. this.lastSelectedIndex = 0;
  139. this.lastSearchTerm = false;
  140. },
  141. selectFirst : function() {
  142. this.focusAndSelect(this.store.data.first());
  143. },
  144. selectLast : function() {
  145. this.focusAndSelect(this.store.data.last());
  146. },
  147. selectPrevPage : function() {
  148. if( !this.rowHeight ) {
  149. return;
  150. }
  151. var index = Math.max(this.selectedIndex-this.rowsPerPage, 0);
  152. this.focusAndSelect(this.store.getAt(index));
  153. },
  154. selectNextPage : function() {
  155. if( !this.rowHeight ) {
  156. return;
  157. }
  158. var index = Math.min(this.selectedIndex+this.rowsPerPage, this.store.getCount() - 1);
  159. this.focusAndSelect(this.store.getAt(index));
  160. },
  161. search : function(field, value, startIndex) {
  162. field = field || this.displayField;
  163. this.lastSearchTerm = value;
  164. var index = this.store.find.apply(this.store, arguments);
  165. if( index !== -1 ) {
  166. this.focusAndSelect(index);
  167. }
  168. },
  169. focusAndSelect : function(record) {
  170. var index = Ext.isNumber(record) ? record : this.store.indexOf(record);
  171. this.select(index, this.isExpanded());
  172. this.onSelect(this.store.getAt(index), index, this.isExpanded());
  173. },
  174. calcRowsPerPage : function() {
  175. if( this.store.getCount() ) {
  176. this.rowHeight = Ext.fly(this.view.getNode(0)).getHeight();
  177. this.rowsPerPage = this.maxHeight / this.rowHeight;
  178. } else {
  179. this.rowHeight = false;
  180. }
  181. }
  182. });
  183. Ext.reg('selectbox', Ext.ux.form.SelectBox);
  184. //backwards compat
  185. Ext.ux.SelectBox = Ext.ux.form.SelectBox;