jquery-ui-mouse.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*!
  2. * jQuery UI Mouse @VERSION
  3. * http://jqueryui.com
  4. *
  5. * Copyright 2014 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: "@VERSION",
  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. // we may have missed mouseup (out of window)
  65. (this._mouseStarted && this._mouseUp(event));
  66. this._mouseDownEvent = event;
  67. var that = this,
  68. btnIsLeft = (event.which === 1),
  69. // event.target.nodeName works around a bug in IE 8 with
  70. // disabled inputs (#7620)
  71. elIsCancel = (typeof this.options.cancel === "string" && event.target.nodeName ? $(event.target).closest(this.options.cancel).length : false);
  72. if (!btnIsLeft || elIsCancel || !this._mouseCapture(event)) {
  73. return true;
  74. }
  75. this.mouseDelayMet = !this.options.delay;
  76. if (!this.mouseDelayMet) {
  77. this._mouseDelayTimer = setTimeout(function() {
  78. that.mouseDelayMet = true;
  79. }, this.options.delay);
  80. }
  81. if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
  82. this._mouseStarted = (this._mouseStart(event) !== false);
  83. if (!this._mouseStarted) {
  84. event.preventDefault();
  85. return true;
  86. }
  87. }
  88. // Click event may never have fired (Gecko & Opera)
  89. if (true === $.data(event.target, this.widgetName + ".preventClickEvent")) {
  90. $.removeData(event.target, this.widgetName + ".preventClickEvent");
  91. }
  92. // these delegates are required to keep context
  93. this._mouseMoveDelegate = function(event) {
  94. return that._mouseMove(event);
  95. };
  96. this._mouseUpDelegate = function(event) {
  97. return that._mouseUp(event);
  98. };
  99. this.document
  100. .bind( "mousemove." + this.widgetName, this._mouseMoveDelegate )
  101. .bind( "mouseup." + this.widgetName, this._mouseUpDelegate );
  102. event.preventDefault();
  103. mouseHandled = true;
  104. return true;
  105. },
  106. _mouseMove: function(event) {
  107. // IE mouseup check - mouseup happened when mouse was out of window
  108. if ($.ui.ie && ( !document.documentMode || document.documentMode < 9 ) && !event.button) {
  109. return this._mouseUp(event);
  110. // Iframe mouseup check - mouseup occurred in another document
  111. } else if ( !event.which ) {
  112. return this._mouseUp( event );
  113. }
  114. if (this._mouseStarted) {
  115. this._mouseDrag(event);
  116. return event.preventDefault();
  117. }
  118. if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
  119. this._mouseStarted =
  120. (this._mouseStart(this._mouseDownEvent, event) !== false);
  121. (this._mouseStarted ? this._mouseDrag(event) : this._mouseUp(event));
  122. }
  123. return !this._mouseStarted;
  124. },
  125. _mouseUp: function(event) {
  126. this.document
  127. .unbind( "mousemove." + this.widgetName, this._mouseMoveDelegate )
  128. .unbind( "mouseup." + this.widgetName, this._mouseUpDelegate );
  129. if (this._mouseStarted) {
  130. this._mouseStarted = false;
  131. if (event.target === this._mouseDownEvent.target) {
  132. $.data(event.target, this.widgetName + ".preventClickEvent", true);
  133. }
  134. this._mouseStop(event);
  135. }
  136. mouseHandled = false;
  137. return false;
  138. },
  139. _mouseDistanceMet: function(event) {
  140. return (Math.max(
  141. Math.abs(this._mouseDownEvent.pageX - event.pageX),
  142. Math.abs(this._mouseDownEvent.pageY - event.pageY)
  143. ) >= this.options.distance
  144. );
  145. },
  146. _mouseDelayMet: function(/* event */) {
  147. return this.mouseDelayMet;
  148. },
  149. // These are placeholder methods, to be overriden by extending plugin
  150. _mouseStart: function(/* event */) {},
  151. _mouseDrag: function(/* event */) {},
  152. _mouseStop: function(/* event */) {},
  153. _mouseCapture: function(/* event */) { return true; }
  154. });
  155. }));