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