aboutsummaryrefslogtreecommitdiff
path: root/apps/utils/static/next-prev-links.js
blob: dddc8798889b4f32fc0c050476f4e78dec1e4799 (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
function build_next_prev() {

    var url = window.location.href 
    var cur = Number(url.split('/')[6]);
    var app = url.split('/')[4];
    var model = url.split('/')[5];
    if (cur) {
        var style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = '.np-container {padding-left: 0;} .prev, .next {display: inline-block; margin-right: .5em;} .prev:after { content: "|"; margin-left:.5em;} .prev a:before {content: "\u00AB"; margin-right: 3px;} .next a:after{content: "\u00BB"; margin-left: 3px;}';
        document.getElementsByTagName('head')[0].appendChild(style);

        json_url = '/admin/data/'+app+'/'+model+'/'+cur+'/';
        //console.log(json_url);

        var container = document.createElement("ul");
        var next_li = document.createElement("li");
        var next_link = document.createElement("a");
        var prev_li = document.createElement("li");
        var prev_link = document.createElement("a");
        prev_li.className = "prev";
        next_li.className = "next";
        container.className = "np-container";
        next_link.textContent = "Next";
        prev_link.textContent = "Prev";

        var request = new XMLHttpRequest();
        request.open('GET', json_url, true);
        request.onload = function() {
            if (request.status >= 200 && request.status < 400) {
                var data = JSON.parse(request.responseText);
                next_link.href = data['next'];
                prev_link.href = data['prev'];
                if (data['next'] != '') {
                    next_li.appendChild(next_link);
                }
                if (data['prev']) {
                    prev_li.appendChild(prev_link);
                }
            } else {
                console.log("server error");
            }
        };
        request.onerror = function() {
            console.log("error on request");
        };
        request.send();
        container.appendChild(prev_li);
        container.appendChild(next_li);
        //console.log(container);
        Array.from(document.getElementsByClassName('object-tools')).forEach(function(item) {
            item.parentNode.insertBefore(container, item.nextSibling);
        })
    } else {
        return;
    }
};
document.addEventListener("DOMContentLoaded", function(event) { 
    build_next_prev();
});