codemirror_fullscreen.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Altair Admin
  3. * Codemirror addon (fullscreen)
  4. */
  5. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  6. // Distributed under an MIT license: http://codemirror.net/LICENSE
  7. (function(mod) {
  8. if (typeof exports == "object" && typeof module == "object") // CommonJS
  9. mod(require("../../lib/codemirror"));
  10. else if (typeof define == "function" && define.amd) // AMD
  11. define(["../../lib/codemirror"], mod);
  12. else // Plain browser env
  13. mod(CodeMirror);
  14. })(function(CodeMirror) {
  15. "use strict";
  16. CodeMirror.defineOption("fullScreen", false, function(cm, val, old) {
  17. if (old == CodeMirror.Init) old = false;
  18. if (!old == !val) return;
  19. if (val) setFullscreen(cm);
  20. else setNormal(cm);
  21. });
  22. function setFullscreen(cm) {
  23. var wrap = cm.getWrapperElement();
  24. cm.state.fullScreenRestore = {scrollTop: window.pageYOffset, scrollLeft: window.pageXOffset,
  25. width: wrap.style.width, height: wrap.style.height};
  26. wrap.style.width = "";
  27. wrap.style.height = "auto";
  28. wrap.className += " CodeMirror-fullscreen";
  29. document.body.className += " CodeMirror-fullscreen-active";
  30. document.documentElement.style.overflow = "hidden";
  31. cm.refresh();
  32. }
  33. function setNormal(cm) {
  34. var wrap = cm.getWrapperElement();
  35. wrap.className = wrap.className.replace(/\s*CodeMirror-fullscreen\b/, "");
  36. document.documentElement.style.overflow = "";
  37. document.body.className = document.body.className.replace(" CodeMirror-fullscreen-active","");
  38. var info = cm.state.fullScreenRestore;
  39. wrap.style.width = info.width; wrap.style.height = info.height;
  40. window.scrollTo(info.scrollLeft, info.scrollTop);
  41. cm.refresh();
  42. }
  43. });