LaravelTest
133 строки · 4.5 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function (mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror"], mod);9else // Plain browser env10mod(CodeMirror);11})(function (CodeMirror) {12CodeMirror.defineExtension("addPanel", function (node, options) {13options = options || {};14
15if (!this.state.panels) initPanels(this);16
17var info = this.state.panels;18var wrapper = info.wrapper;19var cmWrapper = this.getWrapperElement();20var replace = options.replace instanceof Panel && !options.replace.cleared;21
22if (options.after instanceof Panel && !options.after.cleared) {23wrapper.insertBefore(node, options.before.node.nextSibling);24} else if (options.before instanceof Panel && !options.before.cleared) {25wrapper.insertBefore(node, options.before.node);26} else if (replace) {27wrapper.insertBefore(node, options.replace.node);28options.replace.clear(true);29} else if (options.position == "bottom") {30wrapper.appendChild(node);31} else if (options.position == "before-bottom") {32wrapper.insertBefore(node, cmWrapper.nextSibling);33} else if (options.position == "after-top") {34wrapper.insertBefore(node, cmWrapper);35} else {36wrapper.insertBefore(node, wrapper.firstChild);37}38
39var height = (options && options.height) || node.offsetHeight;40
41var panel = new Panel(this, node, options, height);42info.panels.push(panel);43
44this.setSize();45if (options.stable && isAtTop(this, node))46this.scrollTo(null, this.getScrollInfo().top + height);47
48return panel;49});50
51function Panel(cm, node, options, height) {52this.cm = cm;53this.node = node;54this.options = options;55this.height = height;56this.cleared = false;57}58
59/* when skipRemove is true, clear() was called from addPanel().60* Thus removePanels() should not be called (issue 5518) */
61Panel.prototype.clear = function (skipRemove) {62if (this.cleared) return;63this.cleared = true;64var info = this.cm.state.panels;65info.panels.splice(info.panels.indexOf(this), 1);66this.cm.setSize();67if (this.options.stable && isAtTop(this.cm, this.node))68this.cm.scrollTo(null, this.cm.getScrollInfo().top - this.height)69info.wrapper.removeChild(this.node);70if (info.panels.length == 0 && !skipRemove) removePanels(this.cm);71};72
73Panel.prototype.changed = function () {74this.height = this.node.getBoundingClientRect().height;75this.cm.setSize();76};77
78function initPanels(cm) {79var wrap = cm.getWrapperElement()80var style = window.getComputedStyle ? window.getComputedStyle(wrap) : wrap.currentStyle;81var height = parseInt(style.height);82var info = cm.state.panels = {83setHeight: wrap.style.height,84panels: [],85wrapper: document.createElement("div")86};87var hasFocus = cm.hasFocus(), scrollPos = cm.getScrollInfo()88wrap.parentNode.insertBefore(info.wrapper, wrap);89info.wrapper.appendChild(wrap);90cm.scrollTo(scrollPos.left, scrollPos.top)91if (hasFocus) cm.focus();92
93cm._setSize = cm.setSize;94if (height != null) cm.setSize = function (width, newHeight) {95if (!newHeight) newHeight = info.wrapper.offsetHeight;96info.setHeight = newHeight;97if (typeof newHeight != "number") {98var px = /^(\d+\.?\d*)px$/.exec(newHeight);99if (px) {100newHeight = Number(px[1]);101} else {102info.wrapper.style.height = newHeight;103newHeight = info.wrapper.offsetHeight;104}105}106var editorheight = newHeight - info.panels107.map(function (p) { return p.node.getBoundingClientRect().height; })108.reduce(function (a, b) { return a + b; }, 0);109cm._setSize(width, editorheight);110height = newHeight;111};112}113
114function removePanels(cm) {115var info = cm.state.panels;116cm.state.panels = null;117
118var wrap = cm.getWrapperElement()119var hasFocus = cm.hasFocus(), scrollPos = cm.getScrollInfo()120info.wrapper.parentNode.replaceChild(wrap, info.wrapper);121cm.scrollTo(scrollPos.left, scrollPos.top)122if (hasFocus) cm.focus();123wrap.style.height = info.setHeight;124cm.setSize = cm._setSize;125cm.setSize();126}127
128function isAtTop(cm, dom) {129for (var sibling = dom.nextSibling; sibling; sibling = sibling.nextSibling)130if (sibling == cm.getWrapperElement()) return true131return false132}133});134