users.tool 5.36 KB
Newer Older
CupCakeArmy's avatar
Users    
CupCakeArmy committed
1
<div class="users-table-wrapper">
CupCakeArmy's avatar
CupCakeArmy committed
2
3
4
5
6
7
8
9
	<table class="table table-hover users-table">
		<thead>
			<tr>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
CupCakeArmy's avatar
CupCakeArmy committed
10
</div>
CupCakeArmy's avatar
Users    
CupCakeArmy committed
11

CupCakeArmy's avatar
CupCakeArmy committed
12
<style>
CupCakeArmy's avatar
CupCakeArmy committed
13
	.users-table-wrapper {
CupCakeArmy's avatar
CupCakeArmy committed
14
15
		width: 100%;
		height: 100%;
CupCakeArmy's avatar
CupCakeArmy committed
16
17
		overflow: auto;
	}
CupCakeArmy's avatar
CupCakeArmy committed
18
19
</style>
<script type="text/javascript">
CupCakeArmy's avatar
CupCakeArmy committed
20
21
22
	var users = {
		showInTable: ['firstname', 'lastname', 'email', 'membership'],
		curUserData: null,
CupCakeArmy's avatar
CupCakeArmy committed
23
24
25
26
27
28
29
		page: {
			max: Number.MAX_VALUE,
			cur: function() {
				return parseInt(tools.mem.session.get('curPage'));
			},
			set: function(num) {
				num = parseInt(num);
CupCakeArmy's avatar
CupCakeArmy committed
30
				if (num > 0 && num < users.page.max + 1)
CupCakeArmy's avatar
CupCakeArmy committed
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
					tools.mem.session.set('curPage', num);
				$('.users-cur-page-cont').html(users.page.cur());
				users.get();
			},
			inc: function() {
				users.page.set(users.page.cur() + 1);
			},
			dec: function() {
				users.page.set(users.page.cur() - 1);
			}
		},
		get: function() {
			amivcore.users.GET({
				data: {
					'max_results': '50',
					page: users.page.cur(),
				}
			}, function(ret) {

				if (ret === undefined || ret['_items'].length == 0) {
					tools.log('No Data', 'w');
					return;
				}

				users.meta = ret['_meta'];
				users.page.max = Math.ceil(users.meta.total / users.meta.max_results);
				$('.users-page-max-cont').html(users.page.max);

				// Clear table from previous contentent
				$('.users-table thead, .users-table tbody').html('');

				users.showInTable.forEach(function(i) {
					$('.users-table thead tr').append('<th>' + i + '</th>');
				});

				for (var n in ret['_items']) {
					var tmp = '';
					users.showInTable.forEach(function(i) {
						tmp += '<td>' + ret['_items'][n][i] + '</td>';
					});
					$('.users-table tbody').append('<tr data-id="' + ret['_items'][n]['id'] + '">' + tmp + '</tr>');
				}
				$('.users-table tbody tr').click(users.showDetails);
			});
		},

		// Make Modal with editable table data
		showDetails: function() {
			amivcore.users.GET({
				id: $(this).attr('data-id')
			}, function(ret) {
				curUserData = ret;
				var tmp = '<table class="table table-hover users-user-edit-table" data-etag="' + ret['_etag'] + '"><tbody>';
				for (var cur in ret)
					if (cur.charAt(0) != '_')
						tmp += '<tr><td>' + cur + '</td><td contenteditable>' + ret[cur] + '</td></tr>'
				tmp += '</tbody></table>';

				tools.modal({
					head: ret.firstname + ' ' + ret.lastname,
					body: tmp,
CupCakeArmy's avatar
CupCakeArmy committed
92
93
94
95
96
97
98
					button: {
						'Update': {
							type: 'success',
							close: true,
							callback: users.inspectUser,
						}
					}
CupCakeArmy's avatar
CupCakeArmy committed
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
				});

			});;
		},

		// Check wether changes were maid and saves it in that case
		inspectUser: function() {
			var newUserData = {};
			$('.users-user-edit-table tr').each(function() {
				newUserData[$(this).children('td:nth-child(1)').html()] = $(this).children('td:nth-child(2)').html();
			});
			var changed = false,
				curUserDataChanged = {};
			for (var i in newUserData) {
				if (newUserData[i] != String(curUserData[i])) {
					changed = true;
					curUserDataChanged[i] = newUserData[i];
				}
			}
			if (changed) {
				amivcore.users.PATCH({
					id: curUserData.id,
					header: {
						'If-Match': $('.users-user-edit-table').attr('data-etag')
					},
					data: curUserDataChanged
				}, function() {
					tools.log('User Updated', 's');
					users.get();
				});
			}
CupCakeArmy's avatar
CupCakeArmy committed
130
131
132
133
134
135
136
137
138
139
140
141
		},

		//Make new user
		add: function() {
			var tmp = '<table class="table table-hover users-user-add-table"><tbody>',
				reqFields = amivcore.getRequiredFields('users', 'POST');
			for (var reqField in reqFields)
				tmp += '<tr><td>' + reqField + '</td><td contenteditable></td></tr>'
			tmp += '</tbody></table>';
			tools.modal({
				head: 'New User',
				body: tmp,
CupCakeArmy's avatar
CupCakeArmy committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
				button: {
					'Add': {
						type: 'success',
						close: true,
						callback: function() {
							var newUserData = {};
							$('.users-user-add-table tr').each(function() {
								newUserData[$(this).children('td:nth-child(1)').html()] = $(this).children('td:nth-child(2)').html();
							});
							amivcore.users.POST({
								data: newUserData
							}, function(ret) {
								if (!ret.hasOwnProperty('_status') || ret['_status'] != 'OK')
									tools.log(JSON.stringify(ret.responseJSON['_issues']), 'e');
								else {
									tools.log('User Added', 's');
									users.get();
								}
							});
						}
					}
CupCakeArmy's avatar
CupCakeArmy committed
163
				}
CupCakeArmy's avatar
CupCakeArmy committed
164
			});
CupCakeArmy's avatar
CupCakeArmy committed
165
		}
CupCakeArmy's avatar
CupCakeArmy committed
166
167
	};

CupCakeArmy's avatar
CupCakeArmy committed
168
	// Setup Menu
CupCakeArmy's avatar
Fixes    
CupCakeArmy committed
169
	tools.ui.menu({
CupCakeArmy's avatar
CupCakeArmy committed
170
		'<span class="glyphicon glyphicon-user" aria-hidden="true"></span>': {
CupCakeArmy's avatar
CupCakeArmy committed
171
			callback: users.add
CupCakeArmy's avatar
CupCakeArmy committed
172
173
		},
		'<span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span>': {
CupCakeArmy's avatar
CupCakeArmy committed
174
			callback: users.page.dec
CupCakeArmy's avatar
CupCakeArmy committed
175
		},
CupCakeArmy's avatar
CupCakeArmy committed
176
177
178
179
180
		'<span class="users-cur-page-cont" aria-hidden="true"></span> / <span class="users-page-max-cont" aria-hidden="true"></span>': {
			callback: function() {
				tools.modal({
					head: 'Go To Page:',
					body: '<div class="form-group"><input type="number" value="' + users.page.cur() + '" class="form-control users-go-page"></div>',
CupCakeArmy's avatar
Fixes    
CupCakeArmy committed
181
182
					button: {
						'Go': {
CupCakeArmy's avatar
CupCakeArmy committed
183
							type: 'success',
CupCakeArmy's avatar
Fixes    
CupCakeArmy committed
184
185
186
187
							close: true,
							callback: function() {
								users.page.set($('.users-go-page').val());
							},
CupCakeArmy's avatar
CupCakeArmy committed
188
						}
CupCakeArmy's avatar
CupCakeArmy committed
189
					}
CupCakeArmy's avatar
CupCakeArmy committed
190
				});
CupCakeArmy's avatar
CupCakeArmy committed
191
			}
CupCakeArmy's avatar
CupCakeArmy committed
192
193
		},
		'<span class="glyphicon glyphicon-arrow-right" aria-hidden="true"></span>': {
CupCakeArmy's avatar
CupCakeArmy committed
194
			callback: users.page.inc
CupCakeArmy's avatar
CupCakeArmy committed
195
196
197
		},
		'<span class="glyphicon glyphicon-search" aria-hidden="true"></span>': {
			callback: function() {}
CupCakeArmy's avatar
CupCakeArmy committed
198
		}
CupCakeArmy's avatar
Fixes    
CupCakeArmy committed
199
200
	});

CupCakeArmy's avatar
CupCakeArmy committed
201
202
203
204
205
	// Set Initail Page and get first users
	if (users.page.cur() === null || isNaN(users.page.cur()))
		users.page.set(1);
	else
		users.page.set(users.page.cur());
CupCakeArmy's avatar
Users    
CupCakeArmy committed
206
</script>