1
0

panel.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. CodeMirror.defineExtension("addPanel", function(node, options) {
  12. if (!this.state.panels) initPanels(this);
  13. var info = this.state.panels;
  14. if (options && options.position == "bottom")
  15. info.wrapper.appendChild(node);
  16. else
  17. info.wrapper.insertBefore(node, info.wrapper.firstChild);
  18. var height = (options && options.height) || node.offsetHeight;
  19. this._setSize(null, info.heightLeft -= height);
  20. info.panels++;
  21. return new Panel(this, node, options, height);
  22. });
  23. function Panel(cm, node, options, height) {
  24. this.cm = cm;
  25. this.node = node;
  26. this.options = options;
  27. this.height = height;
  28. this.cleared = false;
  29. }
  30. Panel.prototype.clear = function() {
  31. if (this.cleared) return;
  32. this.cleared = true;
  33. var info = this.cm.state.panels;
  34. this.cm._setSize(null, info.heightLeft += this.height);
  35. info.wrapper.removeChild(this.node);
  36. if (--info.panels == 0) removePanels(this.cm);
  37. };
  38. Panel.prototype.changed = function(height) {
  39. var newHeight = height == null ? this.node.offsetHeight : height;
  40. var info = this.cm.state.panels;
  41. this.cm._setSize(null, info.height += (newHeight - this.height));
  42. this.height = newHeight;
  43. };
  44. function initPanels(cm) {
  45. var wrap = cm.getWrapperElement();
  46. var style = window.getComputedStyle ? window.getComputedStyle(wrap) : wrap.currentStyle;
  47. var height = parseInt(style.height);
  48. var info = cm.state.panels = {
  49. setHeight: wrap.style.height,
  50. heightLeft: height,
  51. panels: 0,
  52. wrapper: document.createElement("div")
  53. };
  54. wrap.parentNode.insertBefore(info.wrapper, wrap);
  55. var hasFocus = cm.hasFocus();
  56. info.wrapper.appendChild(wrap);
  57. if (hasFocus) cm.focus();
  58. cm._setSize = cm.setSize;
  59. if (height != null) cm.setSize = function(width, newHeight) {
  60. if (newHeight == null) return this._setSize(width, newHeight);
  61. info.setHeight = newHeight;
  62. if (typeof newHeight != "number") {
  63. var px = /^(\d+\.?\d*)px$/.exec(newHeight);
  64. if (px) {
  65. newHeight = Number(px[1]);
  66. } else {
  67. info.wrapper.style.height = newHeight;
  68. newHeight = info.wrapper.offsetHeight;
  69. info.wrapper.style.height = "";
  70. }
  71. }
  72. cm._setSize(width, info.heightLeft += (newHeight - height));
  73. height = newHeight;
  74. };
  75. }
  76. function removePanels(cm) {
  77. var info = cm.state.panels;
  78. cm.state.panels = null;
  79. var wrap = cm.getWrapperElement();
  80. info.wrapper.parentNode.replaceChild(wrap, info.wrapper);
  81. wrap.style.height = info.setHeight;
  82. cm.setSize = cm._setSize;
  83. cm.setSize();
  84. }
  85. });