split.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 lang = require("./lib/lang");
  34. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  35. var Editor = require("./editor").Editor;
  36. var Renderer = require("./virtual_renderer").VirtualRenderer;
  37. var EditSession = require("./edit_session").EditSession;
  38. /**
  39. * @class Split
  40. *
  41. *
  42. *
  43. **/
  44. var Split = function(container, theme, splits) {
  45. this.BELOW = 1;
  46. this.BESIDE = 0;
  47. this.$container = container;
  48. this.$theme = theme;
  49. this.$splits = 0;
  50. this.$editorCSS = "";
  51. this.$editors = [];
  52. this.$orientation = this.BESIDE;
  53. this.setSplits(splits || 1);
  54. this.$cEditor = this.$editors[0];
  55. this.on("focus", function(editor) {
  56. this.$cEditor = editor;
  57. }.bind(this));
  58. };
  59. (function(){
  60. oop.implement(this, EventEmitter);
  61. this.$createEditor = function() {
  62. var el = document.createElement("div");
  63. el.className = this.$editorCSS;
  64. el.style.cssText = "position: absolute; top:0px; bottom:0px";
  65. this.$container.appendChild(el);
  66. var editor = new Editor(new Renderer(el, this.$theme));
  67. editor.on("focus", function() {
  68. this._emit("focus", editor);
  69. }.bind(this));
  70. this.$editors.push(editor);
  71. editor.setFontSize(this.$fontSize);
  72. return editor;
  73. };
  74. this.setSplits = function(splits) {
  75. var editor;
  76. if (splits < 1) {
  77. throw "The number of splits have to be > 0!";
  78. }
  79. if (splits == this.$splits) {
  80. return;
  81. } else if (splits > this.$splits) {
  82. while (this.$splits < this.$editors.length && this.$splits < splits) {
  83. editor = this.$editors[this.$splits];
  84. this.$container.appendChild(editor.container);
  85. editor.setFontSize(this.$fontSize);
  86. this.$splits ++;
  87. }
  88. while (this.$splits < splits) {
  89. this.$createEditor();
  90. this.$splits ++;
  91. }
  92. } else {
  93. while (this.$splits > splits) {
  94. editor = this.$editors[this.$splits - 1];
  95. this.$container.removeChild(editor.container);
  96. this.$splits --;
  97. }
  98. }
  99. this.resize();
  100. };
  101. /**
  102. *
  103. * Returns the number of splits.
  104. * @returns {Number}
  105. **/
  106. this.getSplits = function() {
  107. return this.$splits;
  108. };
  109. /**
  110. * @param {Number} idx The index of the editor you want
  111. *
  112. * Returns the editor identified by the index `idx`.
  113. *
  114. **/
  115. this.getEditor = function(idx) {
  116. return this.$editors[idx];
  117. };
  118. /**
  119. *
  120. * Returns the current editor.
  121. * @returns {Editor}
  122. **/
  123. this.getCurrentEditor = function() {
  124. return this.$cEditor;
  125. };
  126. /**
  127. * Focuses the current editor.
  128. * @related Editor.focus
  129. **/
  130. this.focus = function() {
  131. this.$cEditor.focus();
  132. };
  133. /**
  134. * Blurs the current editor.
  135. * @related Editor.blur
  136. **/
  137. this.blur = function() {
  138. this.$cEditor.blur();
  139. };
  140. /**
  141. *
  142. * @param {String} theme The name of the theme to set
  143. *
  144. * Sets a theme for each of the available editors.
  145. * @related Editor.setTheme
  146. **/
  147. this.setTheme = function(theme) {
  148. this.$editors.forEach(function(editor) {
  149. editor.setTheme(theme);
  150. });
  151. };
  152. /**
  153. *
  154. * @param {String} keybinding
  155. *
  156. * Sets the keyboard handler for the editor.
  157. * @related editor.setKeyboardHandler
  158. **/
  159. this.setKeyboardHandler = function(keybinding) {
  160. this.$editors.forEach(function(editor) {
  161. editor.setKeyboardHandler(keybinding);
  162. });
  163. };
  164. /**
  165. *
  166. * @param {Function} callback A callback function to execute
  167. * @param {String} scope The default scope for the callback
  168. *
  169. * Executes `callback` on all of the available editors.
  170. *
  171. **/
  172. this.forEach = function(callback, scope) {
  173. this.$editors.forEach(callback, scope);
  174. };
  175. this.$fontSize = "";
  176. /**
  177. * @param {Number} size The new font size
  178. *
  179. * Sets the font size, in pixels, for all the available editors.
  180. *
  181. **/
  182. this.setFontSize = function(size) {
  183. this.$fontSize = size;
  184. this.forEach(function(editor) {
  185. editor.setFontSize(size);
  186. });
  187. };
  188. this.$cloneSession = function(session) {
  189. var s = new EditSession(session.getDocument(), session.getMode());
  190. var undoManager = session.getUndoManager();
  191. if (undoManager) {
  192. var undoManagerProxy = new UndoManagerProxy(undoManager, s);
  193. s.setUndoManager(undoManagerProxy);
  194. }
  195. // Overwrite the default $informUndoManager function such that new delas
  196. // aren't added to the undo manager from the new and the old session.
  197. s.$informUndoManager = lang.delayedCall(function() { s.$deltas = []; });
  198. // Copy over 'settings' from the session.
  199. s.setTabSize(session.getTabSize());
  200. s.setUseSoftTabs(session.getUseSoftTabs());
  201. s.setOverwrite(session.getOverwrite());
  202. s.setBreakpoints(session.getBreakpoints());
  203. s.setUseWrapMode(session.getUseWrapMode());
  204. s.setUseWorker(session.getUseWorker());
  205. s.setWrapLimitRange(session.$wrapLimitRange.min,
  206. session.$wrapLimitRange.max);
  207. s.$foldData = session.$cloneFoldData();
  208. return s;
  209. };
  210. /**
  211. *
  212. * @param {EditSession} session The new edit session
  213. * @param {Number} idx The editor's index you're interested in
  214. *
  215. * Sets a new [[EditSession `EditSession`]] for the indicated editor.
  216. * @related Editor.setSession
  217. **/
  218. this.setSession = function(session, idx) {
  219. var editor;
  220. if (idx == null) {
  221. editor = this.$cEditor;
  222. } else {
  223. editor = this.$editors[idx];
  224. }
  225. // Check if the session is used already by any of the editors in the
  226. // split. If it is, we have to clone the session as two editors using
  227. // the same session can cause terrible side effects (e.g. UndoQueue goes
  228. // wrong). This also gives the user of Split the possibility to treat
  229. // each session on each split editor different.
  230. var isUsed = this.$editors.some(function(editor) {
  231. return editor.session === session;
  232. });
  233. if (isUsed) {
  234. session = this.$cloneSession(session);
  235. }
  236. editor.setSession(session);
  237. // Return the session set on the editor. This might be a cloned one.
  238. return session;
  239. };
  240. /**
  241. *
  242. * Returns the orientation.
  243. * @returns {Number}
  244. **/
  245. this.getOrientation = function() {
  246. return this.$orientation;
  247. };
  248. /**
  249. *
  250. * Sets the orientation.
  251. * @param {Number} orientation The new orientation value
  252. *
  253. *
  254. **/
  255. this.setOrientation = function(orientation) {
  256. if (this.$orientation == orientation) {
  257. return;
  258. }
  259. this.$orientation = orientation;
  260. this.resize();
  261. };
  262. /**
  263. * Resizes the editor.
  264. **/
  265. this.resize = function() {
  266. var width = this.$container.clientWidth;
  267. var height = this.$container.clientHeight;
  268. var editor;
  269. if (this.$orientation == this.BESIDE) {
  270. var editorWidth = width / this.$splits;
  271. for (var i = 0; i < this.$splits; i++) {
  272. editor = this.$editors[i];
  273. editor.container.style.width = editorWidth + "px";
  274. editor.container.style.top = "0px";
  275. editor.container.style.left = i * editorWidth + "px";
  276. editor.container.style.height = height + "px";
  277. editor.resize();
  278. }
  279. } else {
  280. var editorHeight = height / this.$splits;
  281. for (var i = 0; i < this.$splits; i++) {
  282. editor = this.$editors[i];
  283. editor.container.style.width = width + "px";
  284. editor.container.style.top = i * editorHeight + "px";
  285. editor.container.style.left = "0px";
  286. editor.container.style.height = editorHeight + "px";
  287. editor.resize();
  288. }
  289. }
  290. };
  291. }).call(Split.prototype);
  292. function UndoManagerProxy(undoManager, session) {
  293. this.$u = undoManager;
  294. this.$doc = session;
  295. }
  296. (function() {
  297. this.execute = function(options) {
  298. this.$u.execute(options);
  299. };
  300. this.undo = function() {
  301. var selectionRange = this.$u.undo(true);
  302. if (selectionRange) {
  303. this.$doc.selection.setSelectionRange(selectionRange);
  304. }
  305. };
  306. this.redo = function() {
  307. var selectionRange = this.$u.redo(true);
  308. if (selectionRange) {
  309. this.$doc.selection.setSelectionRange(selectionRange);
  310. }
  311. };
  312. this.reset = function() {
  313. this.$u.reset();
  314. };
  315. this.hasUndo = function() {
  316. return this.$u.hasUndo();
  317. };
  318. this.hasRedo = function() {
  319. return this.$u.hasRedo();
  320. };
  321. }).call(UndoManagerProxy.prototype);
  322. exports.Split = Split;
  323. });