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.

jquery.ui.mouse.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*!
  2. * jQuery UI Mouse 1.8.20
  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. $(document)
  44. .unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
  45. .unbind('mouseup.'+this.widgetName, this._mouseUpDelegate);
  46. },
  47. _mouseDown: function(event) {
  48. // don't let more than one widget handle mouseStart
  49. if( mouseHandled ) { return };
  50. // we may have missed mouseup (out of window)
  51. (this._mouseStarted && this._mouseUp(event));
  52. this._mouseDownEvent = event;
  53. var self = this,
  54. btnIsLeft = (event.which == 1),
  55. // event.target.nodeName works around a bug in IE 8 with
  56. // disabled inputs (#7620)
  57. elIsCancel = (typeof this.options.cancel == "string" && event.target.nodeName ? $(event.target).closest(this.options.cancel).length : false);
  58. if (!btnIsLeft || elIsCancel || !this._mouseCapture(event)) {
  59. return true;
  60. }
  61. this.mouseDelayMet = !this.options.delay;
  62. if (!this.mouseDelayMet) {
  63. this._mouseDelayTimer = setTimeout(function() {
  64. self.mouseDelayMet = true;
  65. }, this.options.delay);
  66. }
  67. if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
  68. this._mouseStarted = (this._mouseStart(event) !== false);
  69. if (!this._mouseStarted) {
  70. event.preventDefault();
  71. return true;
  72. }
  73. }
  74. // Click event may never have fired (Gecko & Opera)
  75. if (true === $.data(event.target, this.widgetName + '.preventClickEvent')) {
  76. $.removeData(event.target, this.widgetName + '.preventClickEvent');
  77. }
  78. // these delegates are required to keep context
  79. this._mouseMoveDelegate = function(event) {
  80. return self._mouseMove(event);
  81. };
  82. this._mouseUpDelegate = function(event) {
  83. return self._mouseUp(event);
  84. };
  85. $(document)
  86. .bind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
  87. .bind('mouseup.'+this.widgetName, this._mouseUpDelegate);
  88. event.preventDefault();
  89. mouseHandled = true;
  90. return true;
  91. },
  92. _mouseMove: function(event) {
  93. // IE mouseup check - mouseup happened when mouse was out of window
  94. if ($.browser.msie && !(document.documentMode >= 9) && !event.button) {
  95. return this._mouseUp(event);
  96. }
  97. if (this._mouseStarted) {
  98. this._mouseDrag(event);
  99. return event.preventDefault();
  100. }
  101. if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
  102. this._mouseStarted =
  103. (this._mouseStart(this._mouseDownEvent, event) !== false);
  104. (this._mouseStarted ? this._mouseDrag(event) : this._mouseUp(event));
  105. }
  106. return !this._mouseStarted;
  107. },
  108. _mouseUp: function(event) {
  109. $(document)
  110. .unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
  111. .unbind('mouseup.'+this.widgetName, this._mouseUpDelegate);
  112. if (this._mouseStarted) {
  113. this._mouseStarted = false;
  114. if (event.target == this._mouseDownEvent.target) {
  115. $.data(event.target, this.widgetName + '.preventClickEvent', true);
  116. }
  117. this._mouseStop(event);
  118. }
  119. return false;
  120. },
  121. _mouseDistanceMet: function(event) {
  122. return (Math.max(
  123. Math.abs(this._mouseDownEvent.pageX - event.pageX),
  124. Math.abs(this._mouseDownEvent.pageY - event.pageY)
  125. ) >= this.options.distance
  126. );
  127. },
  128. _mouseDelayMet: function(event) {
  129. return this.mouseDelayMet;
  130. },
  131. // These are placeholder methods, to be overriden by extending plugin
  132. _mouseStart: function(event) {},
  133. _mouseDrag: function(event) {},
  134. _mouseStop: function(event) {},
  135. _mouseCapture: function(event) { return true; }
  136. });
  137. })(jQuery);