jquery.ui.mouse.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*!
  2. * jQuery UI Mouse v1.9 stable
  3. * http://jqueryui.com
  4. *
  5. * Copyright 2012 jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. *
  9. * http://api.jqueryui.com/mouse/
  10. *
  11. * Depends:
  12. * jquery.ui.widget.js
  13. */
  14. (function( $, undefined ) {
  15. var mouseHandled = false;
  16. $( document ).mouseup( function( e ) {
  17. mouseHandled = false;
  18. });
  19. $.widget("ui.mouse", {
  20. version: "@VERSION",
  21. options: {
  22. cancel: 'input,textarea,button,select,option',
  23. distance: 1,
  24. delay: 0
  25. },
  26. _mouseInit: function() {
  27. var that = this;
  28. this.element
  29. .bind('mousedown.'+this.widgetName, function(event) {
  30. return that._mouseDown(event);
  31. })
  32. .bind('click.'+this.widgetName, function(event) {
  33. if (true === $.data(event.target, that.widgetName + '.preventClickEvent')) {
  34. $.removeData(event.target, that.widgetName + '.preventClickEvent');
  35. event.stopImmediatePropagation();
  36. return false;
  37. }
  38. });
  39. this.started = false;
  40. },
  41. // TODO: make sure destroying one instance of mouse doesn't mess with
  42. // other instances of mouse
  43. _mouseDestroy: function() {
  44. this.element.unbind('.'+this.widgetName);
  45. if ( this._mouseMoveDelegate ) {
  46. $(document)
  47. .unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
  48. .unbind('mouseup.'+this.widgetName, this._mouseUpDelegate);
  49. }
  50. },
  51. _mouseDown: function(event) {
  52. // don't let more than one widget handle mouseStart
  53. if( mouseHandled ) { return; }
  54. // we may have missed mouseup (out of window)
  55. (this._mouseStarted && this._mouseUp(event));
  56. this._mouseDownEvent = event;
  57. var that = this,
  58. btnIsLeft = (event.which === 1),
  59. // event.target.nodeName works around a bug in IE 8 with
  60. // disabled inputs (#7620)
  61. elIsCancel = (typeof this.options.cancel === "string" && event.target.nodeName ? $(event.target).closest(this.options.cancel).length : false);
  62. if (!btnIsLeft || elIsCancel || !this._mouseCapture(event)) {
  63. return true;
  64. }
  65. this.mouseDelayMet = !this.options.delay;
  66. if (!this.mouseDelayMet) {
  67. this._mouseDelayTimer = setTimeout(function() {
  68. that.mouseDelayMet = true;
  69. }, this.options.delay);
  70. }
  71. if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
  72. this._mouseStarted = (this._mouseStart(event) !== false);
  73. if (!this._mouseStarted) {
  74. event.preventDefault();
  75. return true;
  76. }
  77. }
  78. // Click event may never have fired (Gecko & Opera)
  79. if (true === $.data(event.target, this.widgetName + '.preventClickEvent')) {
  80. $.removeData(event.target, this.widgetName + '.preventClickEvent');
  81. }
  82. // these delegates are required to keep context
  83. this._mouseMoveDelegate = function(event) {
  84. return that._mouseMove(event);
  85. };
  86. this._mouseUpDelegate = function(event) {
  87. return that._mouseUp(event);
  88. };
  89. $(document)
  90. .bind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
  91. .bind('mouseup.'+this.widgetName, this._mouseUpDelegate);
  92. event.preventDefault();
  93. mouseHandled = true;
  94. return true;
  95. },
  96. _mouseMove: function(event) {
  97. // IE mouseup check - mouseup happened when mouse was out of window
  98. if ($.ui.ie && !(document.documentMode >= 9) && !event.button) {
  99. return this._mouseUp(event);
  100. }
  101. if (this._mouseStarted) {
  102. this._mouseDrag(event);
  103. return event.preventDefault();
  104. }
  105. if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
  106. this._mouseStarted =
  107. (this._mouseStart(this._mouseDownEvent, event) !== false);
  108. (this._mouseStarted ? this._mouseDrag(event) : this._mouseUp(event));
  109. }
  110. return !this._mouseStarted;
  111. },
  112. _mouseUp: function(event) {
  113. $(document)
  114. .unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
  115. .unbind('mouseup.'+this.widgetName, this._mouseUpDelegate);
  116. if (this._mouseStarted) {
  117. this._mouseStarted = false;
  118. if (event.target === this._mouseDownEvent.target) {
  119. $.data(event.target, this.widgetName + '.preventClickEvent', true);
  120. }
  121. this._mouseStop(event);
  122. }
  123. return false;
  124. },
  125. _mouseDistanceMet: function(event) {
  126. return (Math.max(
  127. Math.abs(this._mouseDownEvent.pageX - event.pageX),
  128. Math.abs(this._mouseDownEvent.pageY - event.pageY)
  129. ) >= this.options.distance
  130. );
  131. },
  132. _mouseDelayMet: function(event) {
  133. return this.mouseDelayMet;
  134. },
  135. // These are placeholder methods, to be overriden by extending plugin
  136. _mouseStart: function(event) {},
  137. _mouseDrag: function(event) {},
  138. _mouseStop: function(event) {},
  139. _mouseCapture: function(event) { return true; }
  140. });
  141. })(jQuery);