-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·116 lines (103 loc) · 2.97 KB
/
index.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
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
// index.js
var REST_DATA = 'api/todolist';
var REST_ENV = 'api/dbinfo';
var KEY_ENTER = 13;
function loadItems(){
xhrGet(REST_DATA, function(data){
document.getElementById("loading").innerHTML = "";
var receivedItems = data.body || [];
var items = [];
var i;
// Make sure the received items have correct format
for(i = 0; i < receivedItems.length; ++i){
var item = receivedItems[i];
if(item && 'id' in item && 'name' in item){
items.push(item);
}
}
for(i = 0; i < items.length; ++i){
addItem(items[i], false);
}
}, function(err){
console.error(err);
document.getElementById("loading").innerHTML = "ERROR";
});
}
function addItem(item, isNew){
var row = document.createElement('tr');
var id = item && item.id;
if(id){
row.setAttribute('data-id', id);
}
row.innerHTML = "<td style='width:90%'><textarea onchange='saveChange(this)' onkeydown='onKey(event)'></textarea></td>" +
"<td class='deleteBtn' onclick='deleteItem(this)' title='delete me'></td>";
var table = document.getElementById('notes');
console.log(table.lastChild);
table.lastChild.appendChild(row);
var textarea = row.firstChild.firstChild;
if(item){
textarea.value = item.name;
}
row.isNew = !item || isNew;
textarea.focus();
}
function deleteItem(deleteBtnNode){
var row = deleteBtnNode.parentNode;
row.parentNode.removeChild(row);
xhrDelete(REST_DATA + '?id=' + row.getAttribute('data-id'), function(){
}, function(err){
console.error(err);
});
}
function onKey(evt){
if(evt.keyCode == KEY_ENTER && !evt.shiftKey){
evt.stopPropagation();
evt.preventDefault();
var row = evt.target.parentNode.parentNode;
if(row.nextSibling){
row.nextSibling.firstChild.firstChild.focus();
}else{
addItem();
}
}
}
function saveChange(contentNode, callback){
var row = contentNode.parentNode.parentNode;
var data = {
name: contentNode.value
};
if(row.isNew){
delete row.isNew;
xhrPost(REST_DATA, data, function(item){
row.setAttribute('data-id', item.id);
callback && callback();
}, function(err){
console.error(err);
});
}else{
data.id = row.getAttribute('data-id');
xhrPut(REST_DATA, data, function(){
console.log('updated: ', data);
}, function(err){
console.error(err);
});
}
}
function toggleServiceInfo(){
var node = document.getElementById('dbserviceinfo');
node.style.display = node.style.display == 'none' ? '' : 'none';
}
function updateServiceInfo(){
xhrGet(REST_ENV, function(dbinfo){
console.log(dbinfo);
document.getElementById('envServiceName').innerHTML = dbinfo.name;
document.getElementById('envDbName').innerHTML = dbinfo.db;
document.getElementById('envHost').innerHTML = dbinfo.host;
document.getElementById('envPort').innerHTML = dbinfo.port;
document.getElementById('envUrl').innerHTML = dbinfo.jdbcurl;
}, function(err){
console.error(err);
});
}
updateServiceInfo();
loadItems();