You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
6.0 KiB

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