Newer
Older
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<div class="studydocuments-table-wrapper">
<table class="table table-hover studydocuments-table">
<thead>
<tr>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script type="text/javascript">
var studydocuments = {
showInTable: ['_id', '_updated', '_created', 'author'],
curStudydocumentsData: null,
// Page
page: {
max: Number.MAX_VALUE,
cur: function() {
return parseInt(tools.mem.session.get('curPage'));
},
set: function(num) {
num = parseInt(num);
if (num > 0 && num < studydocuments.page.max + 1)
tools.mem.session.set('curPage', num);
$('.studydocuments-cur-page-cont').html(studydocuments.page.cur());
studydocuments.get();
},
inc: function() {
studydocuments.page.set(studydocuments.page.cur() + 1);
},
dec: function() {
studydocuments.page.set(studydocuments.page.cur() - 1);
}
},
//Sorting
sort: {
cur: function() {
return tools.mem.session.get('curSort');
},
set: function(sort) {
tools.mem.session.set('curSort', sort);
studydocuments.get();
},
inv: function() {
var tmp = studydocuments.sort.cur();
if (tmp.charAt(0) == '-')
studydocuments.sort.set(tmp.slice(1));
else
studydocuments.sort.set('-' + tmp);
}
},
//Searching
search: {
cur: function() {
return tools.mem.session.get('search');
},
set: function(dom, val) {
tools.mem.session.set('search', dom + '==' + val);
studydocuments.page.set(1);
},
clr: function() {
tools.mem.session.set('search', '');
studydocuments.page.set(1);
},
},
// Get studydocuments
get: function() {
amivcore.studydocuments.GET({
data: {
'max_results': '50',
page: studydocuments.page.cur(),
sort: studydocuments.sort.cur(),
where: studydocuments.search.cur(),
}
}, function(ret) {
if (ret === undefined || ret['_items'].length == 0) {
tools.log('No Data', 'w');
return;
}
studydocuments.meta = ret['_meta'];
studydocuments.page.max = Math.ceil(studydocuments.meta.total / studydocuments.meta.max_results);
$('.studydocuments-page-max-cont').html(studydocuments.page.max);
// Clear table from previous contentent
$('.studydocuments-table thead tr, .studydocuments-table tbody').html('');
studydocuments.showInTable.forEach(function(i) {
$('.studydocuments-table thead tr').append('<th>' + i + '</th>');
});
for (var n in ret['_items']) {
var tmp = '';
studydocuments.showInTable.forEach(function(i) {
tmp += '<td>' + ret['_items'][n][i] + '</td>';
});
$('.studydocuments-table tbody').append('<tr data-id="' + ret['_items'][n]['_id'] + '">' + tmp + '</tr>');
}
$('.studydocuments-table tbody tr').click(studydocuments.showDetails);
});
},
// Make Modal with editable table data
showDetails: function() {
amivcore.studydocuments.GET({
id: $(this).attr('data-id')
}, function(ret) {
curStudydocumentsData = ret;
var tmp = '<table class="table table-hover studydocuments-studydocuments-edit-table" data-etag="' + ret['_etag'] + '" data-id="' + ret._id + '"><tbody>';
for (var cur in ret)
if (cur.charAt(0) != '_'){
if (cur == 'files'){
tmp += '<tr><td>' + cur + '</td><td contenteditable>';
for (var fileIdx in ret[cur]){
var file = ret[cur][fileIdx];
console.log(file)
tmp += file.name;
}
tmp += '</td></tr>';
}else{
tmp += '<tr><td>' + cur + '</td><td contenteditable>' + ret[cur] + '</td></tr>';
}
}
tmp += '</tbody></table>';
tools.modal({
head: ret.firstname + ' ' + ret.lastname,
body: tmp,
button: {
'Delete': {
type: 'danger',
callback: function() {
if (confirm('Fo\' shizzle my nizzle? U fo\' real?'))
amivcore.studydocuments.DELETE({
id: $('.studydocuments-studydocuments-edit-table').first().attr('data-id'),
header: {
'If-Match': $('.studydocuments-studydocuments-edit-table').attr('data-etag')
},
}, function(ret) {
if (ret === undefined) {
tools.log('Studydocuments successfully deleted', 's');
studydocuments.get();
tools.modalClose();
} else {
tools.log('Error', 'e');
}
});
}
},
'Update': {
type: 'success',
close: true,
callback: studydocuments.inspectStudydocuments,
}
}
});
});
},
// Check wether changes were maid and saves it in that case
inspectStudydocuments: function() {
var newStudydocumentsData = {};
$('.studydocuments-studydocuments-edit-table tr').each(function() {
newStudydocumentsData[$(this).children('td:nth-child(1)').html()] = $(this).children('td:nth-child(2)').html();
});
var changed = false,
curStudydocumentsDataChanged = {};
for (var i in newStudydocumentsData) {
if (newStudydocumentsData[i] != String(curStudydocumentsData[i])) {
changed = true;
curStudydocumentsDataChanged[i] = newStudydocumentsData[i];
}
}
if (changed) {
amivcore.studydocuments.PATCH({
id: curStudydocumentsData._id,
header: {
'If-Match': $('.studydocuments-studydocuments-edit-table').attr('data-etag')
},
data: curStudydocumentsDataChanged
}, function() {
tools.log('Studydocuments Updated', 's');
studydocuments.get();
});
}
},
//Make new studydocuments
add: function() {
var tmp = '<div class="row studydocuments-studydocuments-add-form"><div class="form-group col-xs-6"><label for="studydocuments-fn">Firstname:</label><input type="text" class="form-control" name="firstname" id="studydocuments-fn"></div>' +
'<div class="form-group col-xs-6"><label for="studydocuments-ln">Lastname:</label><input type="text" class="form-control" id="studydocuments-ln" name="lastname"></div>' +
'<div class="form-group"><label for="studydocuments-email">E-Mail:</label><input type="email" class="form-control" id="studydocuments-email" name="email"></div>' +
'<div class="form-group col-xs-6"><label for="studydocuments-membership">Membership:</label><select class="form-control" id="studydocuments-membership" name="membership"><option>regular</option><option>honorary</option><option>extraordinary</option></select></div>' +
'<div class="form-group col-xs-6"><label for="studydocuments-gender">Gender:</label><select class="form-control" id="studydocuments-gender" name="gender"><option>male</option><option>female</option></select></div>' +
'</div>';
tools.modal({
head: 'Spawn new AMIV slave',
body: tmp,
button: {
'Add': {
type: 'success',
callback: function() {
var newStudydocumentsData = {};
$('.studydocuments-studydocuments-add-form input, .studydocuments-studydocuments-add-form select').each(function() {
newStudydocumentsData[$(this).attr('name')] = $(this).val();
});
amivcore.studydocuments.POST({
data: newStudydocumentsData
}, function(ret) {
if (!ret.hasOwnProperty('_status') || ret['_status'] != 'OK')
tools.log(JSON.stringify(ret.responseJSON['_issues']), 'e');
else {
tools.modalClose();
tools.log('Studydocuments Added', 's');
studydocuments.get();
}
});
}
}
}
});
}
};
// Setup Menu
tools.ui.menu({
'<span class="glyphicon glyphicon-plus" aria-hidden="true" data-toggle="tooltip" data-placement="bottom" title="Create Studydocuments"></span>': {
callback: studydocuments.add
},
'<span class="glyphicon glyphicon-arrow-left" aria-hidden="true" data-toggle="tooltip" data-placement="bottom" title="Previous Page"></span>': {
callback: studydocuments.page.dec
},
'<span data-toggle="tooltip" data-placement="bottom" title="Set Page"><span class="studydocuments-cur-page-cont" aria-hidden="true"></span> / <span class="studydocuments-page-max-cont" aria-hidden="true"></span></span>': {
callback: function() {
tools.modal({
head: 'Go To Page:',
body: '<div class="form-group"><input type="number" value="' + studydocuments.page.cur() + '" class="form-control studydocuments-go-page"></div>',
button: {
'Go': {
type: 'success',
close: true,
callback: function() {
studydocuments.page.set($('.studydocuments-go-page').val());
},
}
}
});
}
},
'<span class="glyphicon glyphicon-arrow-right" aria-hidden="true" data-toggle="tooltip" data-placement="bottom" title="Next Page"></span>': {
callback: studydocuments.page.inc
},
'<span class="glyphicon glyphicon-sort" aria-hidden="true" data-toggle="tooltip" data-placement="bottom" title="Sort"></span>': {
callback: function() {
var tmp = '<div class="form-group"><select class="form-control studydocuments-sort-select">';
var cur = studydocuments.sort.cur();
studydocuments.showInTable.forEach(function(i) {
tmp += '<option value="' + i + '"' + ((i == cur) ? ' selected' : '') + '>⇡ ' + i + '</option>';
tmp += '<option value="-' + i + '"' + (('-' + i == cur) ? ' selected' : '') + '>⇣ ' + i + '</option>';
});
tmp += '</select></div>';
tools.modal({
head: 'Sort',
body: tmp,
button: {
'Sort': {
type: 'success',
close: true,
callback: function() {
studydocuments.sort.set($('.studydocuments-sort-select').val());
}
}
}
});
}
},
'<span class="glyphicon glyphicon-search" aria-hidden="true" data-toggle="tooltip" data-placement="bottom" title="Search"></span>': {
callback: function() {
var tmp = '<div class="form-group"><select class="form-control studydocuments-search-select">';
var cur = studydocuments.search.cur();
if (cur === null || cur == '')
cur = '';
else
cur = cur.split('==')[1];
['_id', 'firstname', 'lastname'].forEach(function(i) {
tmp += '<option value="' + i + '"' + ((i == cur) ? ' selected' : '') + '>' + i + '</option>';
});
tmp += '</select><br><input type="text" value="' + cur + '" class="form-control studydocuments-search-val"></div>';
tools.modal({
head: 'Search',
body: tmp,
button: {
'Clear': {
type: 'warning',
close: true,
callback: studydocuments.search.clr,
},
'Search': {
type: 'success',
close: true,
callback: function() {
studydocuments.search.set($('.studydocuments-search-select').val(), $('.studydocuments-search-val').val());
}
},
}
})
}
}
});
//Set Toolpit
$('[data-toggle="tooltip"]').tooltip()
// Set Initail Page and get first studydocuments
if (studydocuments.page.cur() === null || isNaN(studydocuments.page.cur()))
studydocuments.page.set(1);
else
studydocuments.page.set(studydocuments.page.cur());
</script>