default_gutter_handler.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 oop = require("../lib/oop");
  34. var event = require("../lib/event");
  35. var Tooltip = require("../tooltip").Tooltip;
  36. function GutterHandler(mouseHandler) {
  37. var editor = mouseHandler.editor;
  38. var gutter = editor.renderer.$gutterLayer;
  39. var tooltip = new GutterTooltip(editor.container);
  40. mouseHandler.editor.setDefaultHandler("guttermousedown", function(e) {
  41. if (!editor.isFocused() || e.getButton() != 0)
  42. return;
  43. var gutterRegion = gutter.getRegion(e);
  44. if (gutterRegion == "foldWidgets")
  45. return;
  46. var row = e.getDocumentPosition().row;
  47. var selection = editor.session.selection;
  48. if (e.getShiftKey())
  49. selection.selectTo(row, 0);
  50. else {
  51. if (e.domEvent.detail == 2) {
  52. editor.selectAll();
  53. return e.preventDefault();
  54. }
  55. mouseHandler.$clickSelection = editor.selection.getLineRange(row);
  56. }
  57. mouseHandler.setState("selectByLines");
  58. mouseHandler.captureMouse(e);
  59. return e.preventDefault();
  60. });
  61. var tooltipTimeout, mouseEvent, tooltipAnnotation;
  62. function showTooltip() {
  63. var row = mouseEvent.getDocumentPosition().row;
  64. var annotation = gutter.$annotations[row];
  65. if (!annotation)
  66. return hideTooltip();
  67. var maxRow = editor.session.getLength();
  68. if (row == maxRow) {
  69. var screenRow = editor.renderer.pixelToScreenCoordinates(0, mouseEvent.y).row;
  70. var pos = mouseEvent.$pos;
  71. if (screenRow > editor.session.documentToScreenRow(pos.row, pos.column))
  72. return hideTooltip();
  73. }
  74. if (tooltipAnnotation == annotation)
  75. return;
  76. tooltipAnnotation = annotation.text.join("<br/>");
  77. tooltip.setHtml(tooltipAnnotation);
  78. tooltip.show();
  79. editor.on("mousewheel", hideTooltip);
  80. if (mouseHandler.$tooltipFollowsMouse) {
  81. moveTooltip(mouseEvent);
  82. } else {
  83. var gutterElement = gutter.$cells[editor.session.documentToScreenRow(row, 0)].element;
  84. var rect = gutterElement.getBoundingClientRect();
  85. var style = tooltip.getElement().style;
  86. style.left = rect.right + "px";
  87. style.top = rect.bottom + "px";
  88. }
  89. }
  90. function hideTooltip() {
  91. if (tooltipTimeout)
  92. tooltipTimeout = clearTimeout(tooltipTimeout);
  93. if (tooltipAnnotation) {
  94. tooltip.hide();
  95. tooltipAnnotation = null;
  96. editor.removeEventListener("mousewheel", hideTooltip);
  97. }
  98. }
  99. function moveTooltip(e) {
  100. tooltip.setPosition(e.x, e.y);
  101. }
  102. mouseHandler.editor.setDefaultHandler("guttermousemove", function(e) {
  103. var target = e.domEvent.target || e.domEvent.srcElement;
  104. if (dom.hasCssClass(target, "ace_fold-widget"))
  105. return hideTooltip();
  106. if (tooltipAnnotation && mouseHandler.$tooltipFollowsMouse)
  107. moveTooltip(e);
  108. mouseEvent = e;
  109. if (tooltipTimeout)
  110. return;
  111. tooltipTimeout = setTimeout(function() {
  112. tooltipTimeout = null;
  113. if (mouseEvent && !mouseHandler.isMousePressed)
  114. showTooltip();
  115. else
  116. hideTooltip();
  117. }, 50);
  118. });
  119. event.addListener(editor.renderer.$gutter, "mouseout", function(e) {
  120. mouseEvent = null;
  121. if (!tooltipAnnotation || tooltipTimeout)
  122. return;
  123. tooltipTimeout = setTimeout(function() {
  124. tooltipTimeout = null;
  125. hideTooltip();
  126. }, 50);
  127. });
  128. editor.on("changeSession", hideTooltip);
  129. }
  130. function GutterTooltip(parentNode) {
  131. Tooltip.call(this, parentNode);
  132. }
  133. oop.inherits(GutterTooltip, Tooltip);
  134. (function(){
  135. this.setPosition = function(x, y) {
  136. var windowWidth = window.innerWidth || document.documentElement.clientWidth;
  137. var windowHeight = window.innerHeight || document.documentElement.clientHeight;
  138. var width = this.getWidth();
  139. var height = this.getHeight();
  140. x += 15;
  141. y += 15;
  142. if (x + width > windowWidth) {
  143. x -= (x + width) - windowWidth;
  144. }
  145. if (y + height > windowHeight) {
  146. y -= 20 + height;
  147. }
  148. Tooltip.prototype.setPosition.call(this, x, y);
  149. };
  150. }).call(GutterTooltip.prototype);
  151. exports.GutterHandler = GutterHandler;
  152. });