ext-split.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. ace.define("ace/split",[], function(require, exports, module) {
  2. "use strict";
  3. var oop = require("./lib/oop");
  4. var lang = require("./lib/lang");
  5. var EventEmitter = require("./lib/event_emitter").EventEmitter;
  6. var Editor = require("./editor").Editor;
  7. var Renderer = require("./virtual_renderer").VirtualRenderer;
  8. var EditSession = require("./edit_session").EditSession;
  9. var Split = function(container, theme, splits) {
  10. this.BELOW = 1;
  11. this.BESIDE = 0;
  12. this.$container = container;
  13. this.$theme = theme;
  14. this.$splits = 0;
  15. this.$editorCSS = "";
  16. this.$editors = [];
  17. this.$orientation = this.BESIDE;
  18. this.setSplits(splits || 1);
  19. this.$cEditor = this.$editors[0];
  20. this.on("focus", function(editor) {
  21. this.$cEditor = editor;
  22. }.bind(this));
  23. };
  24. (function(){
  25. oop.implement(this, EventEmitter);
  26. this.$createEditor = function() {
  27. var el = document.createElement("div");
  28. el.className = this.$editorCSS;
  29. el.style.cssText = "position: absolute; top:0px; bottom:0px";
  30. this.$container.appendChild(el);
  31. var editor = new Editor(new Renderer(el, this.$theme));
  32. editor.on("focus", function() {
  33. this._emit("focus", editor);
  34. }.bind(this));
  35. this.$editors.push(editor);
  36. editor.setFontSize(this.$fontSize);
  37. return editor;
  38. };
  39. this.setSplits = function(splits) {
  40. var editor;
  41. if (splits < 1) {
  42. throw "The number of splits have to be > 0!";
  43. }
  44. if (splits == this.$splits) {
  45. return;
  46. } else if (splits > this.$splits) {
  47. while (this.$splits < this.$editors.length && this.$splits < splits) {
  48. editor = this.$editors[this.$splits];
  49. this.$container.appendChild(editor.container);
  50. editor.setFontSize(this.$fontSize);
  51. this.$splits ++;
  52. }
  53. while (this.$splits < splits) {
  54. this.$createEditor();
  55. this.$splits ++;
  56. }
  57. } else {
  58. while (this.$splits > splits) {
  59. editor = this.$editors[this.$splits - 1];
  60. this.$container.removeChild(editor.container);
  61. this.$splits --;
  62. }
  63. }
  64. this.resize();
  65. };
  66. this.getSplits = function() {
  67. return this.$splits;
  68. };
  69. this.getEditor = function(idx) {
  70. return this.$editors[idx];
  71. };
  72. this.getCurrentEditor = function() {
  73. return this.$cEditor;
  74. };
  75. this.focus = function() {
  76. this.$cEditor.focus();
  77. };
  78. this.blur = function() {
  79. this.$cEditor.blur();
  80. };
  81. this.setTheme = function(theme) {
  82. this.$editors.forEach(function(editor) {
  83. editor.setTheme(theme);
  84. });
  85. };
  86. this.setKeyboardHandler = function(keybinding) {
  87. this.$editors.forEach(function(editor) {
  88. editor.setKeyboardHandler(keybinding);
  89. });
  90. };
  91. this.forEach = function(callback, scope) {
  92. this.$editors.forEach(callback, scope);
  93. };
  94. this.$fontSize = "";
  95. this.setFontSize = function(size) {
  96. this.$fontSize = size;
  97. this.forEach(function(editor) {
  98. editor.setFontSize(size);
  99. });
  100. };
  101. this.$cloneSession = function(session) {
  102. var s = new EditSession(session.getDocument(), session.getMode());
  103. var undoManager = session.getUndoManager();
  104. s.setUndoManager(undoManager);
  105. s.setTabSize(session.getTabSize());
  106. s.setUseSoftTabs(session.getUseSoftTabs());
  107. s.setOverwrite(session.getOverwrite());
  108. s.setBreakpoints(session.getBreakpoints());
  109. s.setUseWrapMode(session.getUseWrapMode());
  110. s.setUseWorker(session.getUseWorker());
  111. s.setWrapLimitRange(session.$wrapLimitRange.min,
  112. session.$wrapLimitRange.max);
  113. s.$foldData = session.$cloneFoldData();
  114. return s;
  115. };
  116. this.setSession = function(session, idx) {
  117. var editor;
  118. if (idx == null) {
  119. editor = this.$cEditor;
  120. } else {
  121. editor = this.$editors[idx];
  122. }
  123. var isUsed = this.$editors.some(function(editor) {
  124. return editor.session === session;
  125. });
  126. if (isUsed) {
  127. session = this.$cloneSession(session);
  128. }
  129. editor.setSession(session);
  130. return session;
  131. };
  132. this.getOrientation = function() {
  133. return this.$orientation;
  134. };
  135. this.setOrientation = function(orientation) {
  136. if (this.$orientation == orientation) {
  137. return;
  138. }
  139. this.$orientation = orientation;
  140. this.resize();
  141. };
  142. this.resize = function() {
  143. var width = this.$container.clientWidth;
  144. var height = this.$container.clientHeight;
  145. var editor;
  146. if (this.$orientation == this.BESIDE) {
  147. var editorWidth = width / this.$splits;
  148. for (var i = 0; i < this.$splits; i++) {
  149. editor = this.$editors[i];
  150. editor.container.style.width = editorWidth + "px";
  151. editor.container.style.top = "0px";
  152. editor.container.style.left = i * editorWidth + "px";
  153. editor.container.style.height = height + "px";
  154. editor.resize();
  155. }
  156. } else {
  157. var editorHeight = height / this.$splits;
  158. for (var i = 0; i < this.$splits; i++) {
  159. editor = this.$editors[i];
  160. editor.container.style.width = width + "px";
  161. editor.container.style.top = i * editorHeight + "px";
  162. editor.container.style.left = "0px";
  163. editor.container.style.height = editorHeight + "px";
  164. editor.resize();
  165. }
  166. }
  167. };
  168. }).call(Split.prototype);
  169. exports.Split = Split;
  170. });
  171. ace.define("ace/ext/split",[], function(require, exports, module) {
  172. "use strict";
  173. module.exports = require("../split");
  174. });
  175. (function() {
  176. ace.require(["ace/ext/split"], function(m) {
  177. if (typeof module == "object" && typeof exports == "object" && module) {
  178. module.exports = m;
  179. }
  180. });
  181. })();