File: view-source.js

package info (click to toggle)
mochikit 1.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,240 kB
  • ctags: 1,415
  • sloc: xml: 40; makefile: 6
file content (70 lines) | stat: -rw-r--r-- 2,017 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*

Do syntax highlighting on every textarea inside of a "codeview" element

The content of textareas are URLs, not code!

*/
var viewSource = function () {
    var filename = location.hash;
    if (!filename) {
        filename = location.hash = "view-source/view-source.js";
    }
    filename = lstrip(filename, "#");
    var href = "../" + filename;
    replaceChildNodes("filename", "loading ", A({"href": href}, filename));
    replaceChildNodes("code", href);
    ext = filename.split(".").pop();
    var classes = {
        "html": "xml",
        "js": "javascript",
        "json": "javascript",
        "xml": "xml"
    };
    updateNodeAttributes("code", {"class": classes[ext]});
    syntaxHighlight(filename);
};

var syntaxHighlight = function (filename) {
    var swapContents = function (dest, req) {
        replaceChildNodes(dest, req.responseText);
    };

    var showParsing = function () {
        replaceChildNodes("filename",
            "parsing ", 
            A({"href": "../" + filename}, filename)
        );
        return wait(0);
    };

    var finishSyntaxHighlight = function () {
        dp.sh.HighlightAll("code", true, true, false);
        replaceChildNodes("filename", A({"href": "../" + filename}, filename));
        removeElementClass("codeview", "invisible");
    };

    var elems = getElementsByTagAndClassName("textarea", null, "codeview");
    var dl = new Deferred();
    var deferredCount = 0;
    var checkDeferredList = function () {
        deferredCount -= 1;
        if (!deferredCount) {
            dl.callback();
        }
    };
    for (var i = 0; i < elems.length; i++) {
        var elem = elems[i];
        if (elem.name != "code") {
            continue;
        }
        var url = strip(scrapeText(elem))
        var d = doXHR(url).addCallback(
            partial(swapContents, elem)
        );
        deferredCount += 1;
        d.addCallback(checkDeferredList);
    }
    dl.addCallback(showParsing);
    dl.addCallback(finishSyntaxHighlight);
};