line_widgets.js 9.8 KB

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