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
|
jQuery(function($) {
"use strict";
var TEMPLATES = new LuigiTemplate.Cache({
user: [
"<a ",
"href='#' ",
"class='list-group-item %{css|h}' ",
"title='Edit user \"%{user_name|h}\".' ",
"data-row='%{row|json|h}' ",
"data-q='%{q|h}' ",
">",
"<i class='fa fa-fw fa-spinner fa-spin hidden loading'></i>",
"<i class='fa fa-fw fa-user loading'></i>",
" ",
"%{user_name|h} (%{email|h})",
"<span class='badge pull-right'>",
"%{role_name|h}",
"</span>",
"</a>",
],
loading: [
"<span class='list-group-item disabled'>",
"<i class='fa fa-spinner fa-spin'></i>",
" ",
"Loading...",
"</span>",
],
error: [
"<span class='list-group-item list-group-item-danger disabled'>",
"<i class='fa fa-exclamation-triangle'></i>",
" ",
"Error: %{responseText|h}",
"</span>",
],
});
function filter() {
var qs = $('#users-q').val().replace(/^\s+|\s+$/g, '').toLowerCase().split(/\s+/);
if (qs.length > 0) {
// hide all users
$('#users .list-group-item').addClass('hidden');
// show matching users
$($.grep($('#users .list-group-item'), function(el) {
var eq = $(el).data('q');
return ($.grep(qs, function(q) {
return eq.indexOf(q) !== -1;
}).length == qs.length);
})).removeClass('hidden');
} else {
// show all users
$('#users .list-group-item').removeClass('hidden');
}
}
function reload() {
var btn = $('#users-reload'),
list = $('#users');
// show loading
btn.toggleClass('disabled').find('.loading').toggleClass('hidden');
list.html(TEMPLATES.run('loading'));
send('test/get_users').always(function() {
btn.toggleClass('disabled').find('.loading').toggleClass('hidden');
list.html('');
}).fail(function(r) {
console.log('get_users failed: ' + r.responseText);
list.html(TEMPLATES.run('error', r));
}).done(function(r) {
list.html($.map(r.users, function(row) {
return TEMPLATES.run('user', $.extend({}, row, {
q: [row.user_id, row.user_name, row.email, row.role_name].join(' ').toLowerCase(),
row: row,
}));
}).join(''));
// refresh filters
filter();
});
// stop event
return false;
}
$('#users-q').on('search-update', function() {
filter();
});
$('#users').on('click', 'a.list-group-item', function() {
var me = $(this);
// update highlight
$('#users .active').removeClass('active');
me.addClass('active');
$('#user-edit-dialog').data('row', me.data('row')).modal('show');
// stop event
return false;
});
$('#user-add-dialog').on('show.bs.modal', function() {
$.each(['name', 'email', 'password'], function(i, id) {
$('#user-add-' + id).val('');
});
$('#user-add-role').val('guest');
});
$('#user-add-dialog').on('shown.bs.modal', function() {
$('#user-add-name').focus();
});
$('#user-add-name, #user-add-email, #user-add-password').keydown(function(ev) {
if (ev.which == 13) {
setTimeout(function() {
$('#user-add-confirm').click();
}, 10);
// stop event
return false;
}
});
$('#user-add-confirm').click(function() {
var me = $(this);
if (me.hasClass('disabled'))
return false;
// toggle loading
me.toggleClass('disabled').find('.loading').toggleClass('hidden');
send('test/add_user', {
name: $('#user-add-name').val(),
email: $('#user-add-email').val(),
password: $('#user-add-password').val(),
role: $('#user-add-role').val(),
active: 't',
}).always(function() {
// toggle loading
me.toggleClass('disabled').find('.loading').toggleClass('hidden');
}).fail(function(r) {
alert('Error ' + r.responseText);
}).done(function(r) {
console.log(r);
reload();
$('#user-add-dialog').modal('hide');
});
// stop event
return false;
});
$('#user-edit-dialog').on('show.bs.modal', function() {
var row = $(this).data('row');
$.each({
name: 'user_name',
email: 'email',
role: 'role_name',
}, function(id, col) {
$('#user-edit-' + id).val(row[col]);
});
});
$('#user-edit-dialog').on('shown.bs.modal', function() {
$('#user-edit-name').focus();
});
$('#user-edit-dialog').on('hide.bs.modal', function() {
// clear highlight
$('#users .active').removeClass('active');
});
$('#user-edit-name, #user-edit-email').keydown(function(ev) {
if (ev.which == 13) {
setTimeout(function() {
$('#user-edit-confirm').click();
}, 10);
// stop event
return false;
}
});
$('#user-edit-confirm').click(function() {
var me = $(this),
user_id = $('#user-edit-dialog').data('row').user_id;
if (me.hasClass('disabled'))
return false;
// toggle loading
me.toggleClass('disabled').find('.loading').toggleClass('hidden');
send('test/edit_user', {
user_id: user_id,
name: $('#user-edit-name').val(),
email: $('#user-edit-email').val(),
role: $('#user-edit-role').val(),
}).always(function() {
// toggle loading
me.toggleClass('disabled').find('.loading').toggleClass('hidden');
}).fail(function(r) {
alert('Error ' + r.responseText);
}).done(function(r) {
console.log(r);
reload();
$('#user-edit-dialog').modal('hide');
});
// stop event
return false;
});
$('#users-reload').click(reload);
// load users
$('#admin-tab-users').on('show.bs.tab', function() {
reload();
});
});
|