line_widgets.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 oop = require("./lib/oop");
  33. var dom = require("./lib/dom");
  34. var Range = require("./range").Range;
  35. function LineWidgets(session) {
  36. this.session = session;
  37. this.session.widgetManager = this;
  38. this.session.getRowLength = this.getRowLength;
  39. this.session.$getWidgetScreenLength = this.$getWidgetScreenLength;
  40. this.updateOnChange = this.updateOnChange.bind(this);
  41. this.renderWidgets = this.renderWidgets.bind(this);
  42. this.measureWidgets = this.measureWidgets.bind(this);
  43. this.session._changedWidgets = [];
  44. this.detach = this.detach.bind(this);
  45. this.session.on("change", this.updateOnChange);
  46. };
  47. (function() {
  48. this.getRowLength = function(row) {
  49. if (this.lineWidgets)
  50. var h = this.lineWidgets[row] && this.lineWidgets[row].rowCount || 0;
  51. else
  52. h = 0;
  53. if (!this.$useWrapMode || !this.$wrapData[row]) {
  54. return 1 + h;
  55. } else {
  56. return this.$wrapData[row].length + 1 + h;
  57. }
  58. };
  59. this.$getWidgetScreenLength = function() {
  60. var screenRows = 0;
  61. this.lineWidgets.forEach(function(w){
  62. if (w && w.rowCount)
  63. screenRows +=w.rowCount;
  64. });
  65. return screenRows;
  66. };
  67. this.attach = function(editor) {
  68. if (editor.widgetManager && editor.widgetManager != this)
  69. editor.widgetManager.detach();
  70. if (this.editor == editor)
  71. return;
  72. this.detach();
  73. this.editor = editor;
  74. this.editor.on("changeSession", this.detach);
  75. editor.widgetManager = this;
  76. editor.setOption("enableLineWidgets", true);
  77. editor.renderer.on("beforeRender", this.measureWidgets);
  78. editor.renderer.on("afterRender", this.renderWidgets);
  79. };
  80. this.detach = function(e) {
  81. if (e && e.session == this.session)
  82. return; // sometimes attach can be called before setSession
  83. var editor = this.editor;
  84. if (!editor)
  85. return;
  86. editor.off("changeSession", this.detach);
  87. this.editor = null;
  88. editor.widgetManager = null;
  89. editor.renderer.off("beforeRender", this.measureWidgets);
  90. editor.renderer.off("afterRender", this.renderWidgets);
  91. this.session.lineWidgets.forEach(function(w) {
  92. if (w && w.el && w.el.parentNode) {
  93. w._inDocument = false;
  94. w.el.parentNode.removeChild(w.el);
  95. }
  96. });
  97. };
  98. this.updateOnChange = function(e) {
  99. var cells = this.session.lineWidgets;
  100. if (!cells) return;
  101. var delta = e.data;
  102. var range = delta.range;
  103. var startRow = range.start.row;
  104. var len = range.end.row - startRow;
  105. if (len === 0) {
  106. // return
  107. } else if (delta.action == "removeText" || delta.action == "removeLines") {
  108. var removed = cells.splice(startRow + 1, len);
  109. removed.forEach(function(w) {
  110. w && this.removeLineWidget(w);
  111. }, this);
  112. this.$updateRows();
  113. } else {
  114. var args = Array(len);
  115. args.unshift(startRow, 0);
  116. cells.splice.apply(cells, args);
  117. this.$updateRows();
  118. }
  119. };
  120. this.$updateRows = function() {
  121. var lw = this.session.lineWidgets;
  122. if (!lw) return;
  123. var noWidgets = true;
  124. lw.forEach(function(w, i) {
  125. if (w) {
  126. noWidgets = false;
  127. w.row = i;
  128. }
  129. });
  130. if (noWidgets)
  131. this.session.lineWidgets = null;
  132. }
  133. this.addLineWidget = function(w) {
  134. if (!this.session.lineWidgets)
  135. this.session.lineWidgets = Array(this.session.getLength())
  136. this.session.lineWidgets[w.row] = w;
  137. var renderer = this.editor.renderer;
  138. if (w.html && !w.el) {
  139. w.el = dom.createElement("div");
  140. w.el.innerHTML = w.html;
  141. }
  142. if (w.el) {
  143. dom.addCssClass(w.el, "ace_lineWidgetContainer");
  144. renderer.container.appendChild(w.el);
  145. w._inDocument = true;
  146. }
  147. if (!w.coverGutter) {
  148. w.el.style.zIndex = 3;
  149. }
  150. if (!w.pixelHeight) {
  151. w.pixelHeight = w.el.offsetHeight;
  152. }
  153. if (w.rowCount == null)
  154. w.rowCount = w.pixelHeight / renderer.layerConfig.lineHeight;
  155. this.session._emit("changeFold", {data:{start:{row: w.row}}});
  156. this.$updateRows();
  157. this.renderWidgets(null, renderer);
  158. return w;
  159. };
  160. this.removeLineWidget = function(w) {
  161. w._inDocument = false;
  162. if (w.el && w.el.parentNode)
  163. w.el.parentNode.removeChild(w.el);
  164. if (w.editor && w.editor.destroy) try {
  165. w.editor.destroy();
  166. } catch(e){}
  167. this.session.lineWidgets[w.row] = undefined;
  168. this.session._emit("changeFold", {data:{start:{row: w.row}}});
  169. this.$updateRows();
  170. };
  171. this.onWidgetChanged = function(w) {
  172. this.session._changedWidgets.push(w);
  173. this.editor && this.editor.renderer.updateFull();
  174. };
  175. this.measureWidgets = function(e, renderer) {
  176. var ws = this.session._changedWidgets;
  177. var config = renderer.layerConfig;
  178. if (!ws || !ws.length) return;
  179. var min = Infinity;
  180. for (var i = 0; i < ws.length; i++) {
  181. var w = ws[i].lineWidget;
  182. if (!w._inDocument) {
  183. w._inDocument = true;
  184. renderer.container.appendChild(w.el);
  185. }
  186. w.h = w.el.offsetHeight;
  187. if (!w.fixedWidth) {
  188. w.w = w.el.offsetWidth;
  189. w.screenWidth = Math.ceil(w.w / config.characterWidth);
  190. }
  191. var rowCount = w.h / config.lineHeight;
  192. if (w.coverLine) {
  193. rowCount -= this.session.getRowLineCount(w.row);
  194. if (rowCount < 0)
  195. rowCount = 0;
  196. }
  197. if (w.rowCount != rowCount) {
  198. w.rowCount = rowCount;
  199. if (w.row < min)
  200. min = w.row;
  201. }
  202. }
  203. if (min != Infinity) {
  204. this.session._emit("changeFold", {data:{start:{row: min}}});
  205. this.session.lineWidgetWidth = null;
  206. }
  207. this.session._changedWidgets = [];
  208. };
  209. this.renderWidgets = function(e, renderer) {
  210. var config = renderer.layerConfig;
  211. var ws = this.session.lineWidgets;
  212. if (!ws)
  213. return;
  214. var first = Math.min(this.firstRow, config.firstRow);
  215. var last = Math.max(this.lastRow, config.lastRow, ws.length);
  216. while (first > 0 && !ws[first])
  217. first--;
  218. this.firstRow = config.firstRow;
  219. this.lastRow = config.lastRow;
  220. renderer.$cursorLayer.config = config;
  221. for (var i = first; i <= last; i++) {
  222. var w = ws[i];
  223. if (!w || !w.el) continue;
  224. if (!w._inDocument) {
  225. w._inDocument = true;
  226. renderer.container.appendChild(w.el);
  227. }
  228. var top = renderer.$cursorLayer.getPixelPosition({row: i, column:0}, true).top;
  229. if (!w.coverLine)
  230. top += config.lineHeight * this.session.getRowLineCount(w.row);
  231. w.el.style.top = top - config.offset + "px";
  232. var left = w.coverGutter ? 0 : renderer.gutterWidth;
  233. if (!w.fixedWidth)
  234. left -= renderer.scrollLeft;
  235. w.el.style.left = left + "px";
  236. if (w.fixedWidth) {
  237. w.el.style.right = renderer.scrollBar.getWidth() + "px";
  238. } else {
  239. w.el.style.right = "";
  240. }
  241. }
  242. };
  243. }).call(LineWidgets.prototype);
  244. exports.LineWidgets = LineWidgets;
  245. });