RangeMenu.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Ext JS Library 3.2.1
  3. * Copyright(c) 2006-2010 Ext JS, Inc.
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. Ext.ns('Ext.ux.menu');
  8. /**
  9. * @class Ext.ux.menu.RangeMenu
  10. * @extends Ext.menu.Menu
  11. * Custom implementation of Ext.menu.Menu that has preconfigured
  12. * items for gt, lt, eq.
  13. * <p><b><u>Example Usage:</u></b></p>
  14. * <pre><code>
  15. * </code></pre>
  16. */
  17. Ext.ux.menu.RangeMenu = Ext.extend(Ext.menu.Menu, {
  18. constructor : function (config) {
  19. Ext.ux.menu.RangeMenu.superclass.constructor.call(this, config);
  20. this.addEvents(
  21. /**
  22. * @event update
  23. * Fires when a filter configuration has changed
  24. * @param {Ext.ux.grid.filter.Filter} this The filter object.
  25. */
  26. 'update'
  27. );
  28. this.updateTask = new Ext.util.DelayedTask(this.fireUpdate, this);
  29. var i, len, item, cfg, Cls;
  30. for (i = 0, len = this.menuItems.length; i < len; i++) {
  31. item = this.menuItems[i];
  32. if (item !== '-') {
  33. // defaults
  34. cfg = {
  35. itemId: 'range-' + item,
  36. enableKeyEvents: true,
  37. iconCls: this.iconCls[item] || 'no-icon',
  38. listeners: {
  39. scope: this,
  40. keyup: this.onInputKeyUp
  41. }
  42. };
  43. Ext.apply(
  44. cfg,
  45. // custom configs
  46. Ext.applyIf(this.fields[item] || {}, this.fieldCfg[item]),
  47. // configurable defaults
  48. this.menuItemCfgs
  49. );
  50. Cls = cfg.fieldCls || this.fieldCls;
  51. item = this.fields[item] = new Cls(cfg);
  52. }
  53. this.add(item);
  54. }
  55. },
  56. /**
  57. * @private
  58. * called by this.updateTask
  59. */
  60. fireUpdate : function () {
  61. this.fireEvent('update', this);
  62. },
  63. /**
  64. * Get and return the value of the filter.
  65. * @return {String} The value of this filter
  66. */
  67. getValue : function () {
  68. var result = {}, key, field;
  69. for (key in this.fields) {
  70. field = this.fields[key];
  71. if (field.isValid() && String(field.getValue()).length > 0) {
  72. result[key] = field.getValue();
  73. }
  74. }
  75. return result;
  76. },
  77. /**
  78. * Set the value of this menu and fires the 'update' event.
  79. * @param {Object} data The data to assign to this menu
  80. */
  81. setValue : function (data) {
  82. var key;
  83. for (key in this.fields) {
  84. this.fields[key].setValue(data[key] !== undefined ? data[key] : '');
  85. }
  86. this.fireEvent('update', this);
  87. },
  88. /**
  89. * @private
  90. * Handler method called when there is a keyup event on an input
  91. * item of this menu.
  92. */
  93. onInputKeyUp : function (field, e) {
  94. var k = e.getKey();
  95. if (k == e.RETURN && field.isValid()) {
  96. e.stopEvent();
  97. this.hide(true);
  98. return;
  99. }
  100. if (field == this.fields.eq) {
  101. if (this.fields.gt) {
  102. this.fields.gt.setValue(null);
  103. }
  104. if (this.fields.lt) {
  105. this.fields.lt.setValue(null);
  106. }
  107. }
  108. else {
  109. this.fields.eq.setValue(null);
  110. }
  111. // restart the timer
  112. this.updateTask.delay(this.updateBuffer);
  113. }
  114. });