summaryrefslogtreecommitdiffstats
path: root/js/pkgsubmit.js
blob: 6f161e422f915bbeb95fc1365f8198e35c4a6c42 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
function isLogFile(path) {

    var ext = path.split(".").pop();
    if (["log", "done", "youri"].indexOf(ext) < 0) {
        return true;
    }

    return false;
}

function isShortFile(path) {

    var ext  = path.split(".").pop();
    var file = path.split("/").pop();

    if (["done"].indexOf(ext) >= 0
        || ["status.log"].indexOf(file) >= 0) {
        return true;
    }

    return false;
}

/**
 * Inject <span /> elements with appropriate classes into given text
 * to allow for highlighting specific portions of a text file.
 *
 * Here, log files with ok|success|test|warning|info|error|fail|etc.
 *
 * @param string text
 *
 * @return string
*/
function highlight_text(text) {
    return text.replace(/.*(ok|succe|test|warn|info|deprecat|error|fail).*/gi, function (match, p1, p2, offset, string) {
        console.log([match, p1, offset, string]);
        var cl = 'none';
        switch (p1.toLowerCase()) {
        case 'succe':
        case 'ok':
            cl = 'ok';
            break

        case 'test':
        case 'info':
            cl = 'info';
            break;

        case 'warn':
        case 'deprecat':
            cl = 'warn';
            break;

        case 'error':
        case 'fail':
            cl = 'error';
            break;
        }
        return '<span class="hl hl-' + cl + '">' + match + '</span>';
    });
}

$(function () {

    $('.status-link').on("click", function (ev) {
        if (!ev.metaKey) {
            ev.preventDefault();

            var key  = $(this).attr("href");
            var elId = 'e' + key.replace(/\/|\./g, '-');
            var el   = $("#" + elId);

            if (el.length == 0) {
                $(this).parent().parent().after($("<tr />",
                    {
                        class: "build-files-list",
                        id: elId,
                        html: '<td colspan="4">loading</td>'
                    }
                ));
                $.get(
                    "/log_files.php",
                    {"k": $(this).attr("href")},
                    function (data) {
                        $("#" + elId).html('<td colspan="4">' + data + '</td>');
                    }
                );
            } else {
                el.toggle();
            }
        }
    });

    $("table#submitted-packages tbody").on("click", "tr td li a.view-inline", function (ev) {

        if (isLogFile($(this).attr("href"))) {
            return true;
        }

        if (!ev.metaKey) {
            ev.preventDefault();

            var elId = 'view-' + $(this).attr("href").replace(/\/|\./g, '-');
            var cId  = elId + '-container';
            var c    = $("#" + cId);
            var el   = $("#" + elId);

            if (c.length == 0) {
                $(this).after($("<div />", {
                        id: cId
                    })
                    .addClass(isShortFile($(this).attr("href")) ? "short" : "")
                    .append($("<div />", {
                        id: elId,
                        class: "file-view",
                        html: "loading..."
                    }))
                );

                $.get(
                    "/" + $(this).attr("href"),
                    {},
                    function (data) {
                        $("#" + elId).html(highlight_text(data))
                        .before(
                            $("<div />", {
                                class: "controls"
                            })
                            .append($("<button />", {
                                    class: "gototop",
                                    html: "top"
                                }).on("click", function (ev) {
                                    $("#" + elId).animate({ scrollTop: 0 }, 200);
                                })
                            )
                            .append($("<button />", {
                                    class: "gotobo",
                                    html: "bottom"
                                }).on("click", function (ev) {
                                    var d = $("#" + elId);
                                    d.animate({ scrollTop: d.prop("scrollHeight") }, 200);
                                })
                            )
                        )
                    }
                );
            } else {
                c.toggle();
            }
        }
    });
});