panel.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. options = options || {};
  13. if (!this.state.panels) initPanels(this);
  14. var info = this.state.panels;
  15. var wrapper = info.wrapper;
  16. var cmWrapper = this.getWrapperElement();
  17. if (options.after instanceof Panel && !options.after.cleared) {
  18. wrapper.insertBefore(node, options.before.node.nextSibling);
  19. } else if (options.before instanceof Panel && !options.before.cleared) {
  20. wrapper.insertBefore(node, options.before.node);
  21. } else if (options.replace instanceof Panel && !options.replace.cleared) {
  22. wrapper.insertBefore(node, options.replace.node);
  23. options.replace.clear();
  24. } else if (options.position == "bottom") {
  25. wrapper.appendChild(node);
  26. } else if (options.position == "before-bottom") {
  27. wrapper.insertBefore(node, cmWrapper.nextSibling);
  28. } else if (options.position == "after-top") {
  29. wrapper.insertBefore(node, cmWrapper);
  30. } else {
  31. wrapper.insertBefore(node, wrapper.firstChild);
  32. }
  33. var height = (options && options.height) || node.offsetHeight;
  34. this._setSize(null, info.heightLeft -= height);
  35. info.panels++;
  36. if (options.stable && isAtTop(this, node))
  37. this.scrollTo(null, this.getScrollInfo().top + height)
  38. return new Panel(this, node, options, height);
  39. });
  40. function Panel(cm, node, options, height) {
  41. this.cm = cm;
  42. this.node = node;
  43. this.options = options;
  44. this.height = height;
  45. this.cleared = false;
  46. }
  47. Panel.prototype.clear = function() {
  48. if (this.cleared) return;
  49. this.cleared = true;
  50. var info = this.cm.state.panels;
  51. this.cm._setSize(null, info.heightLeft += this.height);
  52. if (this.options.stable && isAtTop(this.cm, this.node))
  53. this.cm.scrollTo(null, this.cm.getScrollInfo().top - this.height)
  54. info.wrapper.removeChild(this.node);
  55. if (--info.panels == 0) removePanels(this.cm);
  56. };
  57. Panel.prototype.changed = function(height) {
  58. var newHeight = height == null ? this.node.offsetHeight : height;
  59. var info = this.cm.state.panels;
  60. this.cm._setSize(null, info.heightLeft -= (newHeight - this.height));
  61. this.height = newHeight;
  62. };
  63. function initPanels(cm) {
  64. var wrap = cm.getWrapperElement();
  65. var style = window.getComputedStyle ? window.getComputedStyle(wrap) : wrap.currentStyle;
  66. var height = parseInt(style.height);
  67. var info = cm.state.panels = {
  68. setHeight: wrap.style.height,
  69. heightLeft: height,
  70. panels: 0,
  71. wrapper: document.createElement("div")
  72. };
  73. wrap.parentNode.insertBefore(info.wrapper, wrap);
  74. var hasFocus = cm.hasFocus();
  75. info.wrapper.appendChild(wrap);
  76. if (hasFocus) cm.focus();
  77. cm._setSize = cm.setSize;
  78. if (height != null) cm.setSize = function(width, newHeight) {
  79. if (newHeight == null) return this._setSize(width, newHeight);
  80. info.setHeight = newHeight;
  81. if (typeof newHeight != "number") {
  82. var px = /^(\d+\.?\d*)px$/.exec(newHeight);
  83. if (px) {
  84. newHeight = Number(px[1]);
  85. } else {
  86. info.wrapper.style.height = newHeight;
  87. newHeight = info.wrapper.offsetHeight;
  88. info.wrapper.style.height = "";
  89. }
  90. }
  91. cm._setSize(width, info.heightLeft += (newHeight - height));
  92. height = newHeight;
  93. };
  94. }
  95. function removePanels(cm) {
  96. var info = cm.state.panels;
  97. cm.state.panels = null;
  98. var wrap = cm.getWrapperElement();
  99. info.wrapper.parentNode.replaceChild(wrap, info.wrapper);
  100. wrap.style.height = info.setHeight;
  101. cm.setSize = cm._setSize;
  102. cm.setSize();
  103. }
  104. function isAtTop(cm, dom) {
  105. for (var sibling = dom.nextSibling; sibling; sibling = sibling.nextSibling)
  106. if (sibling == cm.getWrapperElement()) return true
  107. return false
  108. }
  109. });