cursor.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 isIE8;
  34. var Cursor = function(parentEl) {
  35. this.element = dom.createElement("div");
  36. this.element.className = "ace_layer ace_cursor-layer";
  37. parentEl.appendChild(this.element);
  38. if (isIE8 === undefined)
  39. isIE8 = !("opacity" in this.element.style);
  40. this.isVisible = false;
  41. this.isBlinking = true;
  42. this.blinkInterval = 1000;
  43. this.smoothBlinking = false;
  44. this.cursors = [];
  45. this.cursor = this.addCursor();
  46. dom.addCssClass(this.element, "ace_hidden-cursors");
  47. this.$updateCursors = (isIE8
  48. ? this.$updateVisibility
  49. : this.$updateOpacity).bind(this);
  50. };
  51. (function() {
  52. this.$updateVisibility = function(val) {
  53. var cursors = this.cursors;
  54. for (var i = cursors.length; i--; )
  55. cursors[i].style.visibility = val ? "" : "hidden";
  56. };
  57. this.$updateOpacity = function(val) {
  58. var cursors = this.cursors;
  59. for (var i = cursors.length; i--; )
  60. cursors[i].style.opacity = val ? "" : "0";
  61. };
  62. this.$padding = 0;
  63. this.setPadding = function(padding) {
  64. this.$padding = padding;
  65. };
  66. this.setSession = function(session) {
  67. this.session = session;
  68. };
  69. this.setBlinking = function(blinking) {
  70. if (blinking != this.isBlinking){
  71. this.isBlinking = blinking;
  72. this.restartTimer();
  73. }
  74. };
  75. this.setBlinkInterval = function(blinkInterval) {
  76. if (blinkInterval != this.blinkInterval){
  77. this.blinkInterval = blinkInterval;
  78. this.restartTimer();
  79. }
  80. };
  81. this.setSmoothBlinking = function(smoothBlinking) {
  82. if (smoothBlinking != this.smoothBlinking && !isIE8) {
  83. this.smoothBlinking = smoothBlinking;
  84. dom.setCssClass(this.element, "ace_smooth-blinking", smoothBlinking);
  85. this.$updateCursors(true);
  86. this.$updateCursors = (this.$updateOpacity).bind(this);
  87. this.restartTimer();
  88. }
  89. };
  90. this.addCursor = function() {
  91. var el = dom.createElement("div");
  92. el.className = "ace_cursor";
  93. this.element.appendChild(el);
  94. this.cursors.push(el);
  95. return el;
  96. };
  97. this.removeCursor = function() {
  98. if (this.cursors.length > 1) {
  99. var el = this.cursors.pop();
  100. el.parentNode.removeChild(el);
  101. return el;
  102. }
  103. };
  104. this.hideCursor = function() {
  105. this.isVisible = false;
  106. dom.addCssClass(this.element, "ace_hidden-cursors");
  107. this.restartTimer();
  108. };
  109. this.showCursor = function() {
  110. this.isVisible = true;
  111. dom.removeCssClass(this.element, "ace_hidden-cursors");
  112. this.restartTimer();
  113. };
  114. this.restartTimer = function() {
  115. var update = this.$updateCursors;
  116. clearInterval(this.intervalId);
  117. clearTimeout(this.timeoutId);
  118. if (this.smoothBlinking) {
  119. dom.removeCssClass(this.element, "ace_smooth-blinking");
  120. }
  121. update(true);
  122. if (!this.isBlinking || !this.blinkInterval || !this.isVisible)
  123. return;
  124. if (this.smoothBlinking) {
  125. setTimeout(function(){
  126. dom.addCssClass(this.element, "ace_smooth-blinking");
  127. }.bind(this));
  128. }
  129. var blink = function(){
  130. this.timeoutId = setTimeout(function() {
  131. update(false);
  132. }, 0.6 * this.blinkInterval);
  133. }.bind(this);
  134. this.intervalId = setInterval(function() {
  135. update(true);
  136. blink();
  137. }, this.blinkInterval);
  138. blink();
  139. };
  140. this.getPixelPosition = function(position, onScreen) {
  141. if (!this.config || !this.session)
  142. return {left : 0, top : 0};
  143. if (!position)
  144. position = this.session.selection.getCursor();
  145. var pos = this.session.documentToScreenPosition(position);
  146. var cursorLeft = this.$padding + pos.column * this.config.characterWidth;
  147. var cursorTop = (pos.row - (onScreen ? this.config.firstRowScreen : 0)) *
  148. this.config.lineHeight;
  149. return {left : cursorLeft, top : cursorTop};
  150. };
  151. this.update = function(config) {
  152. this.config = config;
  153. var selections = this.session.$selectionMarkers;
  154. var i = 0, cursorIndex = 0;
  155. if (selections === undefined || selections.length === 0){
  156. selections = [{cursor: null}];
  157. }
  158. for (var i = 0, n = selections.length; i < n; i++) {
  159. var pixelPos = this.getPixelPosition(selections[i].cursor, true);
  160. if ((pixelPos.top > config.height + config.offset ||
  161. pixelPos.top < 0) && i > 1) {
  162. continue;
  163. }
  164. var style = (this.cursors[cursorIndex++] || this.addCursor()).style;
  165. if (!this.drawCursor) {
  166. style.left = pixelPos.left + "px";
  167. style.top = pixelPos.top + "px";
  168. style.width = config.characterWidth + "px";
  169. style.height = config.lineHeight + "px";
  170. } else {
  171. this.drawCursor(style, pixelPos, config, selections[i], this.session);
  172. }
  173. }
  174. while (this.cursors.length > cursorIndex)
  175. this.removeCursor();
  176. var overwrite = this.session.getOverwrite();
  177. this.$setOverwrite(overwrite);
  178. // cache for textarea and gutter highlight
  179. this.$pixelPos = pixelPos;
  180. this.restartTimer();
  181. };
  182. this.drawCursor = null;
  183. this.$setOverwrite = function(overwrite) {
  184. if (overwrite != this.overwrite) {
  185. this.overwrite = overwrite;
  186. if (overwrite)
  187. dom.addCssClass(this.element, "ace_overwrite-cursors");
  188. else
  189. dom.removeCssClass(this.element, "ace_overwrite-cursors");
  190. }
  191. };
  192. this.destroy = function() {
  193. clearInterval(this.intervalId);
  194. clearTimeout(this.timeoutId);
  195. };
  196. }).call(Cursor.prototype);
  197. exports.Cursor = Cursor;
  198. });