1ab5420facf06f0723508b7ed55ed1a40dab3848.svn-base 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.ToolTip
  8. * @extends Ext.Tip A standard tooltip implementation for providing additional
  9. * information when hovering over a target element.
  10. * @constructor Create a new Tooltip
  11. * @param {Object}
  12. * config The configuration options
  13. */
  14. Ext.ToolTip = Ext.extend(Ext.Tip, {
  15. /**
  16. * @cfg {Mixed} target The target HTMLElement, Ext.Element or id to
  17. * associate with this tooltip.
  18. */
  19. /**
  20. * @cfg {Boolean} autoHide True to automatically hide the tooltip
  21. * after the mouse exits the target element or after the
  22. * {@link #dismissDelay} has expired if set (defaults to true).
  23. * If {@link closable} = true a close tool button will be
  24. * rendered into the tooltip header.
  25. */
  26. /**
  27. * @cfg {Number} showDelay Delay in milliseconds before the tooltip
  28. * displays after the mouse enters the target element (defaults
  29. * to 500)
  30. */
  31. showDelay : 500,
  32. /**
  33. * @cfg {Number} hideDelay Delay in milliseconds after the mouse
  34. * exits the target element but before the tooltip actually
  35. * hides (defaults to 200). Set to 0 for the tooltip to hide
  36. * immediately.
  37. */
  38. hideDelay : 200,
  39. /**
  40. * @cfg {Number} dismissDelay Delay in milliseconds before the
  41. * tooltip automatically hides (defaults to 5000). To disable
  42. * automatic hiding, set dismissDelay = 0.
  43. */
  44. dismissDelay : 5000,
  45. /**
  46. * @cfg {Array} mouseOffset An XY offset from the mouse position
  47. * where the tooltip should be shown (defaults to [15,18]).
  48. */
  49. mouseOffset : [15, 18],
  50. /**
  51. * @cfg {Boolean} trackMouse True to have the tooltip follow the
  52. * mouse as it moves over the target element (defaults to
  53. * false).
  54. */
  55. trackMouse : false,
  56. constrainPosition : true,
  57. // private
  58. initComponent : function() {
  59. Ext.ToolTip.superclass.initComponent.call(this);
  60. this.lastActive = allGetServerTime();
  61. this.initTarget();
  62. },
  63. // private
  64. initTarget : function() {
  65. if (this.target) {
  66. this.target = Ext.get(this.target);
  67. this.target.on('mouseover', this.onTargetOver, this);
  68. this.target.on('mouseout', this.onTargetOut, this);
  69. this.target.on('mousemove', this.onMouseMove, this);
  70. }
  71. },
  72. // private
  73. onMouseMove : function(e) {
  74. this.targetXY = e.getXY();
  75. if (!this.hidden && this.trackMouse) {
  76. this.setPagePosition(this.getTargetXY());
  77. }
  78. },
  79. // private
  80. getTargetXY : function() {
  81. return [this.targetXY[0] + this.mouseOffset[0],
  82. this.targetXY[1] + this.mouseOffset[1]];
  83. },
  84. // private
  85. onTargetOver : function(e) {
  86. if (this.disabled || e.within(this.target.dom, true)) {
  87. return;
  88. }
  89. this.clearTimer('hide');
  90. this.targetXY = e.getXY();
  91. this.delayShow();
  92. },
  93. // private
  94. delayShow : function() {
  95. if (this.hidden && !this.showTimer) {
  96. if (this.lastActive.getElapsed() < this.quickShowInterval) {
  97. this.show();
  98. } else {
  99. this.showTimer = this.show.defer(this.showDelay, this);
  100. }
  101. } else if (!this.hidden && this.autoHide !== false) {
  102. this.show();
  103. }
  104. },
  105. // private
  106. onTargetOut : function(e) {
  107. if (this.disabled || e.within(this.target.dom, true)) {
  108. return;
  109. }
  110. this.clearTimer('show');
  111. if (this.autoHide !== false) {
  112. this.delayHide();
  113. }
  114. },
  115. // private
  116. delayHide : function() {
  117. if (!this.hidden && !this.hideTimer) {
  118. this.hideTimer = this.hide.defer(this.hideDelay, this);
  119. }
  120. },
  121. /**
  122. * Hides this tooltip if visible.
  123. */
  124. hide : function() {
  125. this.clearTimer('dismiss');
  126. this.lastActive = allGetServerTime();
  127. Ext.ToolTip.superclass.hide.call(this);
  128. },
  129. /**
  130. * Shows this tooltip at the current event target XY position.
  131. */
  132. show : function() {
  133. this.showAt(this.getTargetXY());
  134. },
  135. // inherit docs
  136. showAt : function(xy) {
  137. this.lastActive = allGetServerTime();
  138. this.clearTimers();
  139. Ext.ToolTip.superclass.showAt.call(this, xy);
  140. if (this.dismissDelay && this.autoHide !== false) {
  141. this.dismissTimer = this.hide
  142. .defer(this.dismissDelay, this);
  143. }
  144. },
  145. // private
  146. clearTimer : function(name) {
  147. name = name + 'Timer';
  148. clearTimeout(this[name]);
  149. delete this[name];
  150. },
  151. // private
  152. clearTimers : function() {
  153. this.clearTimer('show');
  154. this.clearTimer('dismiss');
  155. this.clearTimer('hide');
  156. },
  157. // private
  158. onShow : function() {
  159. Ext.ToolTip.superclass.onShow.call(this);
  160. Ext.getDoc().on('mousedown', this.onDocMouseDown, this);
  161. },
  162. // private
  163. onHide : function() {
  164. Ext.ToolTip.superclass.onHide.call(this);
  165. Ext.getDoc().un('mousedown', this.onDocMouseDown, this);
  166. },
  167. // private
  168. onDocMouseDown : function(e) {
  169. if (this.autoHide !== false && !e.within(this.el.dom)) {
  170. this.disable();
  171. this.enable.defer(100, this);
  172. }
  173. },
  174. // private
  175. onDisable : function() {
  176. this.clearTimers();
  177. this.hide();
  178. },
  179. // private
  180. adjustPosition : function(x, y) {
  181. // keep the position from being under the mouse
  182. var ay = this.targetXY[1], h = this.getSize().height;
  183. if (this.constrainPosition && y <= ay && (y + h) >= ay) {
  184. y = ay - h - 5;
  185. }
  186. return {
  187. x : x,
  188. y : y
  189. };
  190. },
  191. // private
  192. onDestroy : function() {
  193. Ext.ToolTip.superclass.onDestroy.call(this);
  194. if (this.target) {
  195. this.target.un('mouseover', this.onTargetOver, this);
  196. this.target.un('mouseout', this.onTargetOut, this);
  197. this.target.un('mousemove', this.onMouseMove, this);
  198. }
  199. }
  200. });