Caddy-Web-UI/static/js/main.js

72 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-10-31 15:01:43 +00:00
var getJSON = function(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send();
};
2019-11-01 15:08:37 +00:00
var postJSON = function(url, data, callback) {
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.responseType = 'json';
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function() {
var status = xhr.status;
if (status === 200) {
callback(null, xhr.response);
} else {
callback(status, xhr.response);
}
};
xhr.send(data);
};
2019-10-31 15:01:43 +00:00
function loadSites(){
getJSON('http://localhost:8000/api/Sites',
function(err, data) {
2019-11-01 15:08:37 +00:00
data = Object.values(data);
2019-10-31 15:01:43 +00:00
if (err !== null) {
alert('Something went wrong: ' + err);
} else {
2019-11-01 15:08:37 +00:00
$(".rendered").remove();
data.forEach (function(site) {
renderSite(site);
})
2019-10-31 15:01:43 +00:00
}
}
);
}
function renderSite(site){
2019-11-01 15:08:37 +00:00
siteString = `
<div class="card float-sm-left border-success lg-3 md-4 sm-2 rendered" >
<div class="card-header"><a href="https://${site.domain}">${site.domain}</a></div>
<div class="card-body">
<h4 class="card-title">${site.title}</h4>
<h6 class="card-text">${site.source} -> ${site.target}</p>
<h6 class="card-text">tls: ${site.email}</p>
<p class="card-text">${site.description}</p>
</div>
</div>
`
document.getElementById("main").innerHTML += siteString;
}
2019-10-31 15:01:43 +00:00
2019-11-01 15:08:37 +00:00
function newSite(){
let json = {};
json["domain"] = document.getElementById('domain').value;
json["title"] = document.getElementById('title').value;
json["description"] = document.getElementById('description').value;
json["source"] = document.getElementById('source').value;
json["target"] = document.getElementById('target').value;
json["email"] = document.getElementById('email').value;
postJSON("http://localhost:8000/api/Sites", json, loadSites);
2019-10-31 15:01:43 +00:00
}