default_gutter_handler.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 dom = require("../lib/dom");
  33. var event = require("../lib/event");
  34. function GutterHandler(mouseHandler) {
  35. var editor = mouseHandler.editor;
  36. var gutter = editor.renderer.$gutterLayer;
  37. mouseHandler.editor.setDefaultHandler("guttermousedown", function(e) {
  38. if (!editor.isFocused() || e.getButton() != 0)
  39. return;
  40. var gutterRegion = gutter.getRegion(e);
  41. if (gutterRegion == "foldWidgets")
  42. return;
  43. var row = e.getDocumentPosition().row;
  44. var selection = editor.session.selection;
  45. if (e.getShiftKey())
  46. selection.selectTo(row, 0);
  47. else {
  48. if (e.domEvent.detail == 2) {
  49. editor.selectAll();
  50. return e.preventDefault();
  51. }
  52. mouseHandler.$clickSelection = editor.selection.getLineRange(row);
  53. }
  54. mouseHandler.setState("selectByLines");
  55. mouseHandler.captureMouse(e);
  56. return e.preventDefault();
  57. });
  58. var tooltipTimeout, mouseEvent, tooltip, tooltipAnnotation;
  59. function createTooltip() {
  60. tooltip = dom.createElement("div");
  61. tooltip.className = "ace_gutter-tooltip";
  62. tooltip.style.display = "none";
  63. editor.container.appendChild(tooltip);
  64. }
  65. function showTooltip() {
  66. if (!tooltip) {
  67. createTooltip();
  68. }
  69. var row = mouseEvent.getDocumentPosition().row;
  70. var annotation = gutter.$annotations[row];
  71. if (!annotation)
  72. return hideTooltip();
  73. var maxRow = editor.session.getLength();
  74. if (row == maxRow) {
  75. var screenRow = editor.renderer.pixelToScreenCoordinates(0, mouseEvent.y).row;
  76. var pos = mouseEvent.$pos;
  77. if (screenRow > editor.session.documentToScreenRow(pos.row, pos.column))
  78. return hideTooltip();
  79. }
  80. if (tooltipAnnotation == annotation)
  81. return;
  82. tooltipAnnotation = annotation.text.join("<br/>");
  83. tooltip.style.display = "block";
  84. tooltip.innerHTML = tooltipAnnotation;
  85. editor.on("mousewheel", hideTooltip);
  86. moveTooltip(mouseEvent);
  87. }
  88. function hideTooltip() {
  89. if (tooltipTimeout)
  90. tooltipTimeout = clearTimeout(tooltipTimeout);
  91. if (tooltipAnnotation) {
  92. tooltip.style.display = "none";
  93. tooltipAnnotation = null;
  94. editor.removeEventListener("mousewheel", hideTooltip);
  95. }
  96. }
  97. function moveTooltip(e) {
  98. var rect = editor.renderer.$gutter.getBoundingClientRect();
  99. tooltip.style.left = e.x + 15 + "px";
  100. if (e.y + 3 * editor.renderer.lineHeight + 15 < rect.bottom) {
  101. tooltip.style.bottom = "";
  102. tooltip.style.top = e.y + 15 + "px";
  103. } else {
  104. tooltip.style.top = "";
  105. var innerHeight = window.innerHeight || document.documentElement.clientHeight;
  106. tooltip.style.bottom = innerHeight - e.y + 5 + "px";
  107. }
  108. }
  109. mouseHandler.editor.setDefaultHandler("guttermousemove", function(e) {
  110. var target = e.domEvent.target || e.domEvent.srcElement;
  111. if (dom.hasCssClass(target, "ace_fold-widget"))
  112. return hideTooltip();
  113. if (tooltipAnnotation)
  114. moveTooltip(e);
  115. mouseEvent = e;
  116. if (tooltipTimeout)
  117. return;
  118. tooltipTimeout = setTimeout(function() {
  119. tooltipTimeout = null;
  120. if (mouseEvent && !mouseHandler.isMousePressed)
  121. showTooltip();
  122. else
  123. hideTooltip();
  124. }, 50);
  125. });
  126. event.addListener(editor.renderer.$gutter, "mouseout", function(e) {
  127. mouseEvent = null;
  128. if (!tooltipAnnotation || tooltipTimeout)
  129. return;
  130. tooltipTimeout = setTimeout(function() {
  131. tooltipTimeout = null;
  132. hideTooltip();
  133. }, 50);
  134. });
  135. editor.on("changeSession", hideTooltip);
  136. }
  137. exports.GutterHandler = GutterHandler;
  138. });