mouse_event.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var event = require("../lib/event");
  33. var useragent = require("../lib/useragent");
  34. /*
  35. * Custom Ace mouse event
  36. */
  37. var MouseEvent = exports.MouseEvent = function(domEvent, editor) {
  38. this.domEvent = domEvent;
  39. this.editor = editor;
  40. this.x = this.clientX = domEvent.clientX;
  41. this.y = this.clientY = domEvent.clientY;
  42. this.$pos = null;
  43. this.$inSelection = null;
  44. this.propagationStopped = false;
  45. this.defaultPrevented = false;
  46. };
  47. (function() {
  48. this.stopPropagation = function() {
  49. event.stopPropagation(this.domEvent);
  50. this.propagationStopped = true;
  51. };
  52. this.preventDefault = function() {
  53. event.preventDefault(this.domEvent);
  54. this.defaultPrevented = true;
  55. };
  56. this.stop = function() {
  57. this.stopPropagation();
  58. this.preventDefault();
  59. };
  60. /*
  61. * Get the document position below the mouse cursor
  62. *
  63. * @return {Object} 'row' and 'column' of the document position
  64. */
  65. this.getDocumentPosition = function() {
  66. if (this.$pos)
  67. return this.$pos;
  68. this.$pos = this.editor.renderer.screenToTextCoordinates(this.clientX, this.clientY);
  69. return this.$pos;
  70. };
  71. /*
  72. * Check if the mouse cursor is inside of the text selection
  73. *
  74. * @return {Boolean} whether the mouse cursor is inside of the selection
  75. */
  76. this.inSelection = function() {
  77. if (this.$inSelection !== null)
  78. return this.$inSelection;
  79. var editor = this.editor;
  80. var selectionRange = editor.getSelectionRange();
  81. if (selectionRange.isEmpty())
  82. this.$inSelection = false;
  83. else {
  84. var pos = this.getDocumentPosition();
  85. this.$inSelection = selectionRange.contains(pos.row, pos.column);
  86. }
  87. return this.$inSelection;
  88. };
  89. /*
  90. * Get the clicked mouse button
  91. *
  92. * @return {Number} 0 for left button, 1 for middle button, 2 for right button
  93. */
  94. this.getButton = function() {
  95. return event.getButton(this.domEvent);
  96. };
  97. /*
  98. * @return {Boolean} whether the shift key was pressed when the event was emitted
  99. */
  100. this.getShiftKey = function() {
  101. return this.domEvent.shiftKey;
  102. };
  103. this.getAccelKey = useragent.isMac
  104. ? function() { return this.domEvent.metaKey; }
  105. : function() { return this.domEvent.ctrlKey; };
  106. }).call(MouseEvent.prototype);
  107. });