You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.5KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"), "cjs");
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], function(CM) { mod(CM, "amd"); });
  8. else // Plain browser env
  9. mod(CodeMirror, "plain");
  10. })(function(CodeMirror, env) {
  11. if (!CodeMirror.modeURL) CodeMirror.modeURL = "../mode/%N/%N.js";
  12. var loading = {};
  13. function splitCallback(cont, n) {
  14. var countDown = n;
  15. return function() { if (--countDown == 0) cont(); };
  16. }
  17. function ensureDeps(mode, cont, options) {
  18. var modeObj = CodeMirror.modes[mode], deps = modeObj && modeObj.dependencies;
  19. if (!deps) return cont();
  20. var missing = [];
  21. for (var i = 0; i < deps.length; ++i) {
  22. if (!CodeMirror.modes.hasOwnProperty(deps[i]))
  23. missing.push(deps[i]);
  24. }
  25. if (!missing.length) return cont();
  26. var split = splitCallback(cont, missing.length);
  27. for (var i = 0; i < missing.length; ++i)
  28. CodeMirror.requireMode(missing[i], split, options);
  29. }
  30. CodeMirror.requireMode = function(mode, cont, options) {
  31. if (typeof mode != "string") mode = mode.name;
  32. if (CodeMirror.modes.hasOwnProperty(mode)) return ensureDeps(mode, cont, options);
  33. if (loading.hasOwnProperty(mode)) return loading[mode].push(cont);
  34. var file = options && options.path ? options.path(mode) : CodeMirror.modeURL.replace(/%N/g, mode);
  35. if (options && options.loadMode) {
  36. options.loadMode(file, function() { ensureDeps(mode, cont, options) })
  37. } else if (env == "plain") {
  38. var script = document.createElement("script");
  39. script.src = file;
  40. var others = document.getElementsByTagName("script")[0];
  41. var list = loading[mode] = [cont];
  42. CodeMirror.on(script, "load", function() {
  43. ensureDeps(mode, function() {
  44. for (var i = 0; i < list.length; ++i) list[i]();
  45. }, options);
  46. });
  47. others.parentNode.insertBefore(script, others);
  48. } else if (env == "cjs") {
  49. require(file);
  50. cont();
  51. } else if (env == "amd") {
  52. requirejs([file], cont);
  53. }
  54. };
  55. CodeMirror.autoLoadMode = function(instance, mode, options) {
  56. if (!CodeMirror.modes.hasOwnProperty(mode))
  57. CodeMirror.requireMode(mode, function() {
  58. instance.setOption("mode", instance.getOption("mode"));
  59. }, options);
  60. };
  61. });