-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
88 lines (74 loc) · 2.33 KB
/
script.js
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
(function(){
var url = 'https://git-backend-simple.herokuapp.com/';
var flashEl = document.getElementById('flash');
var filesEl = document.getElementById('files');
var bodyEl = document.getElementById('body');
var pathEl = document.getElementById('path');
var messageEl = document.getElementById('message');
var loadingEL = document.getElementsByClassName('loading')[0];
request('post', 'clone').end(handler(function(payload) {
updateFiles(payload);
hideSpinner();
read(payload.files[0]);
}));
function request(method, path) {
return superagent[method](url + path).withCredentials();
}
function handler(success) {
return function(err, res) { err ? flash('error', err) : success(res.body); };
}
function flash(type, message) {
flashEl.innerHTML = message;
flashEl.style.display = 'block';
flashEl.className = type;
setTimeout(function() { flashEl.style.display = 'none'; }, 2000);
}
function showSpinner() { loadingEL.style.display = 'block'; }
function hideSpinner() { loadingEL.style.display = 'none'; }
function updateFiles(payload) {
filesEl.innerHTML = payload.files.map(function(path) {
return '<option>' + path + '</option>'
}).join('');
}
function ls() {
request('get', 'ls').end(handler(updateFiles));
}
window.read = function(path) {
request('get', 'read/' + path).end(handler(function(payload) {
pathEl.value = path;
bodyEl.value = payload.content;
}));
}
window.newFile = function() {
bodyEl.value = '';
pathEl.value = '';
filesEl.value = '';
}
window.deleteFile = function() {
if(filesEl.value) {
request('post', 'delete/' + filesEl.value).end(handler(ls));
}
}
window.save = function() {
if(!pathEl.value) { return flash('Must provide path'); }
request('post', 'write/' + pathEl.value)
.send(bodyEl.value)
.end(handler(function() {
if(filesEl.value !== pathEl.value) {
if(filesEl.value) { deleteFile(); }
ls();
}
flash('success', 'File saved!');
}));
}
window.push = function() {
showSpinner();
request('post', 'push')
.query({ 'message': messageEl.value })
.end(handler(function() {
messageEl.value = '';
hideSpinner();
flash('success', 'Committed and Pushed!');
}));
}
})();