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