-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.js
135 lines (121 loc) · 3.89 KB
/
admin.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
$(function(){
//initiate the client
var oh = Ohmage("/app", "setup-request");
var uuid;
//global error handler. In ohmage 200 means unauthenticated
oh.callback("error", function(msg, code, req){
(code == 200) ? window.location.replace("/#login") : message("<strong>Error! </strong>" + msg);
});
var table = $('#requesttable').DataTable({
"dom" : '<"pull-right"l><"pull-left"f>tip',
'bPaginate': false,
"aoColumnDefs": [
{ 'bSortable': false, 'aTargets': [ ] },
{ 'bSearchable': false, 'aTargets': [ ] },
{ 'bVisible' : false, 'aTargets' : [ ] }
]
});
//expand table row
$('#requesttable').on('click', "tbody td:not('.buttontd')", function () {
var tr = $(this).parent()
var row = table.row(tr);
if(tr.attr("role") != "row") return;
if ( row.child.isShown() ) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
} else {
// Open this row
row.child( expand(row.data(), tr.data("requestdata"))).show();
tr.addClass('shown');
}
});
//init app
oh.user.whoami().done(function(username){
//prevent timeout
oh.keepalive();
//user info
oh.user.read().done(function(data){
//prefill some form fields
var userdata = data[username];
if(!userdata.permissions.admin){
alert("You are not admin!");
location.replace(".");
}
});
//find outstanding requests
oh.request.read().done(function(data){
$.each(data, function(uuid, val){
var tr = $("<tr />");
var btn = $(".hidden .widget-template").clone();
var button = btn.find("button");
var a = btn.find("a").click(function(e){
e.preventDefault();
var status = $(this).data("status");
button.attr("disabled", "disabled");
if(status == "delete"){
var req = oh.request.delete(uuid).done(function(){
table.row(tr).remove().draw();
});
} else {
var req = oh.request.setstatus(uuid, status).done(function(){
setbutton(btn, status);
});
}
req.always(function(){
button.removeAttr("disabled");
});
});
tr.append(td(val["creation_time"])).append(td(val["user"])).append(td(val["email_address"])).append(td(val["type"])).append(td(btn).addClass("buttontd"));
//.append(td(makelabel(val["status"])));
setbutton(btn, val["status"]);
tr.data("requestdata", val.content.request);
table.row.add(tr).draw(false);
});
});
});
function expand(classdata, requestdata) {
var row = $('<div />').addClass('row').addClass("response-row");
var col = $("<div />").addClass("col-md-12").appendTo(row);
var ul = $("<ul />").appendTo(col)
$.each(requestdata, function(i, obj){
var name = Object.keys(obj)[0];
var value = obj[name];
row.append('<li class="list-unstyled"><b>' + name + ':</b> <i>' + value + '</i></li>');
});
return row;
}
function td(el){
return $("<td />").append(el);
}
function setbutton(btn, status){
btn.find(".btn-text").empty().append(status);
var button = btn.find("button");
if(status == "approved"){
button.removeClass("btn-danger").removeClass("btn-default").addClass("btn-success");
} else if(status == "rejected"){
button.removeClass("btn-default").removeClass("btn-success").addClass("btn-danger");
} else {
button.removeClass("btn-danger").removeClass("btn-success").addClass("btn-default");
}
}
function makelabel(status){
var label = $("<span />").addClass("label").text(status);
if(status == "approved"){
label.addClass("label-success");
} else if(status == "rejected"){
label.addClass("label-danger");
} else {
label.addClass("label-primary");
}
return label;
}
function message(msg, type){
// type must be one of success, info, warning, danger
type = type || "danger"
$("#errordiv").append('<div class="alert alert-' + type + '"><a href="#" class="close" data-dismiss="alert">×</a>' + msg + '</div>');
$('html, body').animate({
scrollTop: 100
}, 200);
}
});