diff options
author | Valerio Virgillito | 2012-07-17 11:29:09 -0700 |
---|---|---|
committer | Valerio Virgillito | 2012-07-17 11:29:09 -0700 |
commit | 58e86247524cc2d545bd6884f1b5c9fdba3b9164 (patch) | |
tree | 53305d354fce502ee39378babf47ace6c91ce2fd /imports/codemirror/lib/util/loadmode.js | |
parent | 0f0cffd4df85e7adf5081fe66c768c70b2ead9b3 (diff) | |
parent | 4f737b24c19ddc02d20f9783b8b080fc6ef11142 (diff) | |
download | ninja-58e86247524cc2d545bd6884f1b5c9fdba3b9164.tar.gz |
Merge branch 'refs/heads/master' into v0.7.1
Diffstat (limited to 'imports/codemirror/lib/util/loadmode.js')
-rw-r--r-- | imports/codemirror/lib/util/loadmode.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/imports/codemirror/lib/util/loadmode.js b/imports/codemirror/lib/util/loadmode.js new file mode 100644 index 00000000..48d5a7ab --- /dev/null +++ b/imports/codemirror/lib/util/loadmode.js | |||
@@ -0,0 +1,51 @@ | |||
1 | (function() { | ||
2 | if (!CodeMirror.modeURL) CodeMirror.modeURL = "../mode/%N/%N.js"; | ||
3 | |||
4 | var loading = {}; | ||
5 | function splitCallback(cont, n) { | ||
6 | var countDown = n; | ||
7 | return function() { if (--countDown == 0) cont(); } | ||
8 | } | ||
9 | function ensureDeps(mode, cont) { | ||
10 | var deps = CodeMirror.modes[mode].dependencies; | ||
11 | if (!deps) return cont(); | ||
12 | var missing = []; | ||
13 | for (var i = 0; i < deps.length; ++i) { | ||
14 | if (!CodeMirror.modes.hasOwnProperty(deps[i])) | ||
15 | missing.push(deps[i]); | ||
16 | } | ||
17 | if (!missing.length) return cont(); | ||
18 | var split = splitCallback(cont, missing.length); | ||
19 | for (var i = 0; i < missing.length; ++i) | ||
20 | CodeMirror.requireMode(missing[i], split); | ||
21 | } | ||
22 | |||
23 | CodeMirror.requireMode = function(mode, cont) { | ||
24 | if (typeof mode != "string") mode = mode.name; | ||
25 | if (CodeMirror.modes.hasOwnProperty(mode)) return ensureDeps(mode, cont); | ||
26 | if (loading.hasOwnProperty(mode)) return loading[mode].push(cont); | ||
27 | |||
28 | var script = document.createElement("script"); | ||
29 | script.src = CodeMirror.modeURL.replace(/%N/g, mode); | ||
30 | var others = document.getElementsByTagName("script")[0]; | ||
31 | others.parentNode.insertBefore(script, others); | ||
32 | var list = loading[mode] = [cont]; | ||
33 | var count = 0, poll = setInterval(function() { | ||
34 | if (++count > 100) return clearInterval(poll); | ||
35 | if (CodeMirror.modes.hasOwnProperty(mode)) { | ||
36 | clearInterval(poll); | ||
37 | loading[mode] = null; | ||
38 | ensureDeps(mode, function() { | ||
39 | for (var i = 0; i < list.length; ++i) list[i](); | ||
40 | }); | ||
41 | } | ||
42 | }, 200); | ||
43 | }; | ||
44 | |||
45 | CodeMirror.autoLoadMode = function(instance, mode) { | ||
46 | if (!CodeMirror.modes.hasOwnProperty(mode)) | ||
47 | CodeMirror.requireMode(mode, function() { | ||
48 | instance.setOption("mode", instance.getOption("mode")); | ||
49 | }); | ||
50 | }; | ||
51 | }()); | ||