Sin descripción

mouse.js 6.0KB

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