From 635db376a7b410ca6948457ebc3c9e6d04383d15 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sat, 2 Apr 2016 23:37:38 -0400 Subject: mv auth.js tab-users.js --- data/stuff/test/auth.js | 286 --------------------------------------- data/stuff/test/tab-users.js | 286 +++++++++++++++++++++++++++++++++++++++ src/guff/views/html/test/auth.cr | 2 +- 3 files changed, 287 insertions(+), 287 deletions(-) delete mode 100644 data/stuff/test/auth.js create mode 100644 data/stuff/test/tab-users.js diff --git a/data/stuff/test/auth.js b/data/stuff/test/auth.js deleted file mode 100644 index b973e25..0000000 --- a/data/stuff/test/auth.js +++ /dev/null @@ -1,286 +0,0 @@ -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 = $('#filter-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 = $('#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; - } - - // old test, not used any more - function set_user(user_id) { - return send('test/set_user', { - user_id: user_id, - }).fail(function(r) { - alert('Error ' + r.responseText); - }).done(function(r) { - // do nothing - }); - } - - $('#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; - }); - - $('#login-dialog').on('show.bs.modal', function() { - $('#login-email').val(''); - $('#login-password').val(''); - }); - - $('#login-dialog').on('shown.bs.modal', function() { - $('#login-email').focus(); - }); - - $('#login-confirm').click(function() { - var me = $(this); - - if (me.hasClass('disabled')) - return false; - - // toggle loading - me.toggleClass('disabled').find('.loading').toggleClass('hidden'); - - send('test/login', { - email: $('#login-email').val(), - password: $('#login-password').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); - $('#login-dialog').modal('hide'); - }); - - // stop event - return false; - }); - - $('#login-user, #login-password').keydown(function(ev) { - if (ev.which == 13) { - setTimeout(function() { - // trigger login - $('#login-confirm').click(); - }, 10); - - // stop event - return false; - } - }); - - $('#reload').click(reload); - - // load users - $('#tab-users').on('show.bs.tab', function() { - reload(); - }); -}); diff --git a/data/stuff/test/tab-users.js b/data/stuff/test/tab-users.js new file mode 100644 index 0000000..b973e25 --- /dev/null +++ b/data/stuff/test/tab-users.js @@ -0,0 +1,286 @@ +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 = $('#filter-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 = $('#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; + } + + // old test, not used any more + function set_user(user_id) { + return send('test/set_user', { + user_id: user_id, + }).fail(function(r) { + alert('Error ' + r.responseText); + }).done(function(r) { + // do nothing + }); + } + + $('#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; + }); + + $('#login-dialog').on('show.bs.modal', function() { + $('#login-email').val(''); + $('#login-password').val(''); + }); + + $('#login-dialog').on('shown.bs.modal', function() { + $('#login-email').focus(); + }); + + $('#login-confirm').click(function() { + var me = $(this); + + if (me.hasClass('disabled')) + return false; + + // toggle loading + me.toggleClass('disabled').find('.loading').toggleClass('hidden'); + + send('test/login', { + email: $('#login-email').val(), + password: $('#login-password').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); + $('#login-dialog').modal('hide'); + }); + + // stop event + return false; + }); + + $('#login-user, #login-password').keydown(function(ev) { + if (ev.which == 13) { + setTimeout(function() { + // trigger login + $('#login-confirm').click(); + }, 10); + + // stop event + return false; + } + }); + + $('#reload').click(reload); + + // load users + $('#tab-users').on('show.bs.tab', function() { + reload(); + }); +}); diff --git a/src/guff/views/html/test/auth.cr b/src/guff/views/html/test/auth.cr index e30a4a2..0e3d02f 100644 --- a/src/guff/views/html/test/auth.cr +++ b/src/guff/views/html/test/auth.cr @@ -6,7 +6,7 @@ class Guff::TestAuthHTMLView FEATURES = %w{bootstrap font-awesome guff/util} SCRIPTS = %w{ /guff-stuff/js/search-field.js - /guff-stuff/test/auth.js + /guff-stuff/test/tab-users.js } TEMPLATES = TemplateCache.new({ -- cgit v1.2.3