jQuery(function($) { "use strict"; var TEMPLATES = new LuigiTemplate.Cache({ user: [ "", "", "", " ", "%{user_name|h} (%{email|h})", "", "%{role_name|h}", "", "", ], loading: [ "", "", " ", "Loading...", "", ], error: [ "", "", " ", "Error: %{responseText|h}", "", ], }); 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; }); $('#users-reload').click(reload); // load users $('#admin-tab-users').on('show.bs.tab', function() { reload(); }); });