Commit | Line | Data |
---|---|---|
6a1aa64f DV |
1 | /* |
2 | ||
3 | Do syntax highlighting on every textarea inside of a "codeview" element | |
4 | ||
5 | The content of textareas are URLs, not code! | |
6 | ||
7 | */ | |
8 | var viewSource = function () { | |
9 | var filename = location.hash; | |
10 | if (!filename) { | |
11 | filename = location.hash = "view-source/view-source.js"; | |
12 | } | |
13 | filename = lstrip(filename, "#"); | |
14 | var href = "../" + filename; | |
15 | replaceChildNodes("filename", "loading ", A({"href": href}, filename)); | |
16 | replaceChildNodes("code", href); | |
17 | ext = filename.split(".").pop(); | |
18 | var classes = { | |
19 | "html": "xml", | |
20 | "js": "javascript", | |
21 | "json": "javascript", | |
22 | "xml": "xml" | |
23 | }; | |
24 | updateNodeAttributes("code", {"class": classes[ext]}); | |
25 | syntaxHighlight(filename); | |
26 | }; | |
27 | ||
28 | var syntaxHighlight = function (filename) { | |
29 | var swapContents = function (dest, req) { | |
30 | replaceChildNodes(dest, req.responseText); | |
31 | }; | |
32 | ||
33 | var showParsing = function () { | |
34 | replaceChildNodes("filename", | |
35 | "parsing ", | |
36 | A({"href": "../" + filename}, filename) | |
37 | ); | |
38 | return wait(0); | |
39 | }; | |
40 | ||
41 | var finishSyntaxHighlight = function () { | |
42 | dp.sh.HighlightAll("code", true, true, false); | |
43 | replaceChildNodes("filename", A({"href": "../" + filename}, filename)); | |
44 | removeElementClass("codeview", "invisible"); | |
45 | }; | |
46 | ||
47 | var elems = getElementsByTagAndClassName("textarea", null, "codeview"); | |
48 | var dl = new Deferred(); | |
49 | var deferredCount = 0; | |
50 | var checkDeferredList = function () { | |
51 | deferredCount -= 1; | |
52 | if (!deferredCount) { | |
53 | dl.callback(); | |
54 | } | |
55 | }; | |
56 | for (var i = 0; i < elems.length; i++) { | |
57 | var elem = elems[i]; | |
58 | if (elem.name != "code") { | |
59 | continue; | |
60 | } | |
61 | var url = strip(scrapeText(elem)) | |
62 | var d = doXHR(url).addCallback( | |
63 | partial(swapContents, elem) | |
64 | ); | |
65 | deferredCount += 1; | |
66 | d.addCallback(checkDeferredList); | |
67 | } | |
68 | dl.addCallback(showParsing); | |
69 | dl.addCallback(finishSyntaxHighlight); | |
70 | }; |