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.

204 lines
5.5 KiB

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