summaryrefslogtreecommitdiff
path: root/app/utils/static
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2018-02-05 13:38:59 -0600
committerluxagraf <sng@luxagraf.net>2018-02-05 13:38:59 -0600
commit3398e7349e2bafddaa491c01f4c16c6dae14cd00 (patch)
tree0ae25201ce6fc50146dff819b03819ddce148cfe /app/utils/static
parentad112641f01a0fd36d0b738b6903dea3eadbf000 (diff)
abstracted the next prev links into utils so now they work for every
model that implements a get_%S_admin_link method and loads the JS
Diffstat (limited to 'app/utils/static')
-rw-r--r--app/utils/static/next-prev-links.js80
1 files changed, 53 insertions, 27 deletions
diff --git a/app/utils/static/next-prev-links.js b/app/utils/static/next-prev-links.js
index 7af1b66..82a1dd8 100644
--- a/app/utils/static/next-prev-links.js
+++ b/app/utils/static/next-prev-links.js
@@ -1,33 +1,59 @@
function build_next_prev() {
+
var url = window.location.href
- 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);
var cur = Number(url.split('/')[6]);
- var base_url = url.split(cur)[0];
- var next = cur+1;
- var prev = cur-1;
- 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.href = base_url + next + "/change/";
- next_link.textContent = "Next";
- prev_link.href = base_url + prev + "/change/";
- prev_link.textContent = "Prev";
- next_li.appendChild(next_link);
- prev_li.appendChild(prev_link);
- 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);
- })
+ 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();