jquery.ui.mouse.js 4.9 KB

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