all repos — NoPaste @ de062991ccdf763b2091f26873047685f9c0c977

Resurrected - The PussTheCat.org fork of NoPaste

scripts/loadmode.js (view raw)

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