line_widgets.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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("changeFold", this.updateOnFold);
  47. this.session.on("changeEditor", this.$onChangeEditor);
  48. }
  49. (function() {
  50. this.getRowLength = function(row) {
  51. var h;
  52. if (this.lineWidgets)
  53. h = this.lineWidgets[row] && this.lineWidgets[row].rowCount || 0;
  54. else
  55. h = 0;
  56. if (!this.$useWrapMode || !this.$wrapData[row]) {
  57. return 1 + h;
  58. } else {
  59. return this.$wrapData[row].length + 1 + h;
  60. }
  61. };
  62. this.$getWidgetScreenLength = function() {
  63. var screenRows = 0;
  64. this.lineWidgets.forEach(function(w){
  65. if (w && w.rowCount && !w.hidden)
  66. screenRows += w.rowCount;
  67. });
  68. return screenRows;
  69. };
  70. this.$onChangeEditor = function(e) {
  71. this.attach(e.editor);
  72. };
  73. this.attach = function(editor) {
  74. if (editor && editor.widgetManager && editor.widgetManager != this)
  75. editor.widgetManager.detach();
  76. if (this.editor == editor)
  77. return;
  78. this.detach();
  79. this.editor = editor;
  80. if (editor) {
  81. editor.widgetManager = this;
  82. editor.renderer.on("beforeRender", this.measureWidgets);
  83. editor.renderer.on("afterRender", this.renderWidgets);
  84. }
  85. };
  86. this.detach = function(e) {
  87. var editor = this.editor;
  88. if (!editor)
  89. return;
  90. this.editor = null;
  91. editor.widgetManager = null;
  92. editor.renderer.off("beforeRender", this.measureWidgets);
  93. editor.renderer.off("afterRender", this.renderWidgets);
  94. var lineWidgets = this.session.lineWidgets;
  95. lineWidgets && lineWidgets.forEach(function(w) {
  96. if (w && w.el && w.el.parentNode) {
  97. w._inDocument = false;
  98. w.el.parentNode.removeChild(w.el);
  99. }
  100. });
  101. };
  102. this.updateOnFold = function(e, session) {
  103. var lineWidgets = session.lineWidgets;
  104. if (!lineWidgets || !e.action)
  105. return;
  106. var fold = e.data;
  107. var start = fold.start.row;
  108. var end = fold.end.row;
  109. var hide = e.action == "add";
  110. for (var i = start + 1; i < end; i++) {
  111. if (lineWidgets[i])
  112. lineWidgets[i].hidden = hide;
  113. }
  114. if (lineWidgets[end]) {
  115. if (hide) {
  116. if (!lineWidgets[start])
  117. lineWidgets[start] = lineWidgets[end];
  118. else
  119. lineWidgets[end].hidden = hide;
  120. } else {
  121. if (lineWidgets[start] == lineWidgets[end])
  122. lineWidgets[start] = undefined;
  123. lineWidgets[end].hidden = hide;
  124. }
  125. }
  126. };
  127. this.updateOnChange = function(delta) {
  128. var lineWidgets = this.session.lineWidgets;
  129. if (!lineWidgets) return;
  130. var startRow = delta.start.row;
  131. var len = delta.end.row - startRow;
  132. if (len === 0) {
  133. // return
  134. } else if (delta.action == 'remove') {
  135. var removed = lineWidgets.splice(startRow + 1, len);
  136. removed.forEach(function(w) {
  137. w && this.removeLineWidget(w);
  138. }, this);
  139. this.$updateRows();
  140. } else {
  141. var args = new Array(len);
  142. args.unshift(startRow, 0);
  143. lineWidgets.splice.apply(lineWidgets, args);
  144. this.$updateRows();
  145. }
  146. };
  147. this.$updateRows = function() {
  148. var lineWidgets = this.session.lineWidgets;
  149. if (!lineWidgets) return;
  150. var noWidgets = true;
  151. lineWidgets.forEach(function(w, i) {
  152. if (w) {
  153. noWidgets = false;
  154. w.row = i;
  155. while (w.$oldWidget) {
  156. w.$oldWidget.row = i;
  157. w = w.$oldWidget;
  158. }
  159. }
  160. });
  161. if (noWidgets)
  162. this.session.lineWidgets = null;
  163. };
  164. this.addLineWidget = function(w) {
  165. if (!this.session.lineWidgets)
  166. this.session.lineWidgets = new Array(this.session.getLength());
  167. var old = this.session.lineWidgets[w.row];
  168. if (old) {
  169. w.$oldWidget = old;
  170. if (old.el && old.el.parentNode) {
  171. old.el.parentNode.removeChild(old.el);
  172. old._inDocument = false;
  173. }
  174. }
  175. this.session.lineWidgets[w.row] = w;
  176. w.session = this.session;
  177. var renderer = this.editor.renderer;
  178. if (w.html && !w.el) {
  179. w.el = dom.createElement("div");
  180. w.el.innerHTML = w.html;
  181. }
  182. if (w.el) {
  183. dom.addCssClass(w.el, "ace_lineWidgetContainer");
  184. w.el.style.position = "absolute";
  185. w.el.style.zIndex = 5;
  186. renderer.container.appendChild(w.el);
  187. w._inDocument = true;
  188. }
  189. if (!w.coverGutter) {
  190. w.el.style.zIndex = 3;
  191. }
  192. if (!w.pixelHeight) {
  193. w.pixelHeight = w.el.offsetHeight;
  194. }
  195. if (w.rowCount == null) {
  196. w.rowCount = w.pixelHeight / renderer.layerConfig.lineHeight;
  197. }
  198. var fold = this.session.getFoldAt(w.row, 0);
  199. w.$fold = fold;
  200. if (fold) {
  201. var lineWidgets = this.session.lineWidgets;
  202. if (w.row == fold.end.row && !lineWidgets[fold.start.row])
  203. lineWidgets[fold.start.row] = w;
  204. else
  205. w.hidden = true;
  206. }
  207. this.session._emit("changeFold", {data:{start:{row: w.row}}});
  208. this.$updateRows();
  209. this.renderWidgets(null, renderer);
  210. this.onWidgetChanged(w);
  211. return w;
  212. };
  213. this.removeLineWidget = function(w) {
  214. w._inDocument = false;
  215. w.session = null;
  216. if (w.el && w.el.parentNode)
  217. w.el.parentNode.removeChild(w.el);
  218. if (w.editor && w.editor.destroy) try {
  219. w.editor.destroy();
  220. } catch(e){}
  221. if (this.session.lineWidgets) {
  222. var w1 = this.session.lineWidgets[w.row]
  223. if (w1 == w) {
  224. this.session.lineWidgets[w.row] = w.$oldWidget;
  225. if (w.$oldWidget)
  226. this.onWidgetChanged(w.$oldWidget);
  227. } else {
  228. while (w1) {
  229. if (w1.$oldWidget == w) {
  230. w1.$oldWidget = w.$oldWidget;
  231. break;
  232. }
  233. w1 = w1.$oldWidget;
  234. }
  235. }
  236. }
  237. this.session._emit("changeFold", {data:{start:{row: w.row}}});
  238. this.$updateRows();
  239. };
  240. this.getWidgetsAtRow = function(row) {
  241. var lineWidgets = this.session.lineWidgets;
  242. var w = lineWidgets && lineWidgets[row];
  243. var list = [];
  244. while (w) {
  245. list.push(w);
  246. w = w.$oldWidget;
  247. }
  248. return list;
  249. };
  250. this.onWidgetChanged = function(w) {
  251. this.session._changedWidgets.push(w);
  252. this.editor && this.editor.renderer.updateFull();
  253. };
  254. this.measureWidgets = function(e, renderer) {
  255. var changedWidgets = this.session._changedWidgets;
  256. var config = renderer.layerConfig;
  257. if (!changedWidgets || !changedWidgets.length) return;
  258. var min = Infinity;
  259. for (var i = 0; i < changedWidgets.length; i++) {
  260. var w = changedWidgets[i];
  261. if (!w || !w.el) continue;
  262. if (w.session != this.session) continue;
  263. if (!w._inDocument) {
  264. if (this.session.lineWidgets[w.row] != w)
  265. continue;
  266. w._inDocument = true;
  267. renderer.container.appendChild(w.el);
  268. }
  269. w.h = w.el.offsetHeight;
  270. if (!w.fixedWidth) {
  271. w.w = w.el.offsetWidth;
  272. w.screenWidth = Math.ceil(w.w / config.characterWidth);
  273. }
  274. var rowCount = w.h / config.lineHeight;
  275. if (w.coverLine) {
  276. rowCount -= this.session.getRowLineCount(w.row);
  277. if (rowCount < 0)
  278. rowCount = 0;
  279. }
  280. if (w.rowCount != rowCount) {
  281. w.rowCount = rowCount;
  282. if (w.row < min)
  283. min = w.row;
  284. }
  285. }
  286. if (min != Infinity) {
  287. this.session._emit("changeFold", {data:{start:{row: min}}});
  288. this.session.lineWidgetWidth = null;
  289. }
  290. this.session._changedWidgets = [];
  291. };
  292. this.renderWidgets = function(e, renderer) {
  293. var config = renderer.layerConfig;
  294. var lineWidgets = this.session.lineWidgets;
  295. if (!lineWidgets)
  296. return;
  297. var first = Math.min(this.firstRow, config.firstRow);
  298. var last = Math.max(this.lastRow, config.lastRow, lineWidgets.length);
  299. while (first > 0 && !lineWidgets[first])
  300. first--;
  301. this.firstRow = config.firstRow;
  302. this.lastRow = config.lastRow;
  303. renderer.$cursorLayer.config = config;
  304. for (var i = first; i <= last; i++) {
  305. var w = lineWidgets[i];
  306. if (!w || !w.el) continue;
  307. if (w.hidden) {
  308. w.el.style.top = -100 - (w.pixelHeight || 0) + "px";
  309. continue;
  310. }
  311. if (!w._inDocument) {
  312. w._inDocument = true;
  313. renderer.container.appendChild(w.el);
  314. }
  315. var top = renderer.$cursorLayer.getPixelPosition({row: i, column:0}, true).top;
  316. if (!w.coverLine)
  317. top += config.lineHeight * this.session.getRowLineCount(w.row);
  318. w.el.style.top = top - config.offset + "px";
  319. var left = w.coverGutter ? 0 : renderer.gutterWidth;
  320. if (!w.fixedWidth)
  321. left -= renderer.scrollLeft;
  322. w.el.style.left = left + "px";
  323. if (w.fullWidth && w.screenWidth) {
  324. w.el.style.minWidth = config.width + 2 * config.padding + "px";
  325. }
  326. if (w.fixedWidth) {
  327. w.el.style.right = renderer.scrollBar.getWidth() + "px";
  328. } else {
  329. w.el.style.right = "";
  330. }
  331. }
  332. };
  333. }).call(LineWidgets.prototype);
  334. exports.LineWidgets = LineWidgets;
  335. });