mouse.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*!
  2. * jQuery UI Mouse 1.11.4
  3. * http://jqueryui.com
  4. *
  5. * Copyright 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. (function( factory ) {
  12. if ( typeof define === "function" && define.amd ) {
  13. // AMD. Register as an anonymous module.
  14. define([
  15. "jquery",
  16. "./widget"
  17. ], factory );
  18. } else {
  19. // Browser globals
  20. factory( jQuery );
  21. }
  22. }(function( $ ) {
  23. var mouseHandled = false;
  24. $( document ).mouseup( function() {
  25. mouseHandled = false;
  26. });
  27. return $.widget("ui.mouse", {
  28. version: "1.11.4",
  29. options: {
  30. cancel: "input,textarea,button,select,option",
  31. distance: 1,
  32. delay: 0
  33. },
  34. _mouseInit: function() {
  35. var that = this;
  36. this.element
  37. .bind("mousedown." + this.widgetName, function(event) {
  38. return that._mouseDown(event);
  39. })
  40. .bind("click." + this.widgetName, function(event) {
  41. if (true === $.data(event.target, that.widgetName + ".preventClickEvent")) {
  42. $.removeData(event.target, that.widgetName + ".preventClickEvent");
  43. event.stopImmediatePropagation();
  44. return false;
  45. }
  46. });
  47. this.started = false;
  48. },
  49. // TODO: make sure destroying one instance of mouse doesn't mess with
  50. // other instances of mouse
  51. _mouseDestroy: function() {
  52. this.element.unbind("." + this.widgetName);
  53. if ( this._mouseMoveDelegate ) {
  54. this.document
  55. .unbind("mousemove." + this.widgetName, this._mouseMoveDelegate)
  56. .unbind("mouseup." + this.widgetName, this._mouseUpDelegate);
  57. }
  58. },
  59. _mouseDown: function(event) {
  60. // don't let more than one widget handle mouseStart
  61. if ( mouseHandled ) {
  62. return;
  63. }
  64. this._mouseMoved = false;
  65. // we may have missed mouseup (out of window)
  66. (this._mouseStarted && this._mouseUp(event));
  67. this._mouseDownEvent = event;
  68. var that = this,
  69. btnIsLeft = (event.which === 1),
  70. // event.target.nodeName works around a bug in IE 8 with
  71. // disabled inputs (#7620)
  72. elIsCancel = (typeof this.options.cancel === "string" && event.target.nodeName ? $(event.target).closest(this.options.cancel).length : false);
  73. if (!btnIsLeft || elIsCancel || !this._mouseCapture(event)) {
  74. return true;
  75. }
  76. this.mouseDelayMet = !this.options.delay;
  77. if (!this.mouseDelayMet) {
  78. this._mouseDelayTimer = setTimeout(function() {
  79. that.mouseDelayMet = true;
  80. }, this.options.delay);
  81. }
  82. if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
  83. this._mouseStarted = (this._mouseStart(event) !== false);
  84. if (!this._mouseStarted) {
  85. event.preventDefault();
  86. return true;
  87. }
  88. }
  89. // Click event may never have fired (Gecko & Opera)
  90. if (true === $.data(event.target, this.widgetName + ".preventClickEvent")) {
  91. $.removeData(event.target, this.widgetName + ".preventClickEvent");
  92. }
  93. // these delegates are required to keep context
  94. this._mouseMoveDelegate = function(event) {
  95. return that._mouseMove(event);
  96. };
  97. this._mouseUpDelegate = function(event) {
  98. return that._mouseUp(event);
  99. };
  100. this.document
  101. .bind( "mousemove." + this.widgetName, this._mouseMoveDelegate )
  102. .bind( "mouseup." + this.widgetName, this._mouseUpDelegate );
  103. event.preventDefault();
  104. mouseHandled = true;
  105. return true;
  106. },
  107. _mouseMove: function(event) {
  108. // Only check for mouseups outside the document if you've moved inside the document
  109. // at least once. This prevents the firing of mouseup in the case of IE<9, which will
  110. // fire a mousemove event if content is placed under the cursor. See #7778
  111. // Support: IE <9
  112. if ( this._mouseMoved ) {
  113. // IE mouseup check - mouseup happened when mouse was out of window
  114. if ($.ui.ie && ( !document.documentMode || document.documentMode < 9 ) && !event.button) {
  115. return this._mouseUp(event);
  116. // Iframe mouseup check - mouseup occurred in another document
  117. } else if ( !event.which ) {
  118. return this._mouseUp( event );
  119. }
  120. }
  121. if ( event.which || event.button ) {
  122. this._mouseMoved = true;
  123. }
  124. if (this._mouseStarted) {
  125. this._mouseDrag(event);
  126. return event.preventDefault();
  127. }
  128. if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
  129. this._mouseStarted =
  130. (this._mouseStart(this._mouseDownEvent, event) !== false);
  131. (this._mouseStarted ? this._mouseDrag(event) : this._mouseUp(event));
  132. }
  133. return !this._mouseStarted;
  134. },
  135. _mouseUp: function(event) {
  136. this.document
  137. .unbind( "mousemove." + this.widgetName, this._mouseMoveDelegate )
  138. .unbind( "mouseup." + this.widgetName, this._mouseUpDelegate );
  139. if (this._mouseStarted) {
  140. this._mouseStarted = false;
  141. if (event.target === this._mouseDownEvent.target) {
  142. $.data(event.target, this.widgetName + ".preventClickEvent", true);
  143. }
  144. this._mouseStop(event);
  145. }
  146. mouseHandled = false;
  147. return false;
  148. },
  149. _mouseDistanceMet: function(event) {
  150. return (Math.max(
  151. Math.abs(this._mouseDownEvent.pageX - event.pageX),
  152. Math.abs(this._mouseDownEvent.pageY - event.pageY)
  153. ) >= this.options.distance
  154. );
  155. },
  156. _mouseDelayMet: function(/* event */) {
  157. return this.mouseDelayMet;
  158. },
  159. // These are placeholder methods, to be overriden by extending plugin
  160. _mouseStart: function(/* event */) {},
  161. _mouseDrag: function(/* event */) {},
  162. _mouseStop: function(/* event */) {},
  163. _mouseCapture: function(/* event */) { return true; }
  164. });
  165. }));