$(document).ready(function(){ // Initialize the editable-table functionality $('#usertable').editableTableWidget(); // Prevent clicking *through* popup-screens $('#sessions').click(function(event) { event.stopPropagation(); }); // Add eventhandlers to buttons $('#usertable button.session').click(function() { event.stopPropagation(); $('#sessions tbody').empty(); $('#sessions').fadeToggle(); $.post("lucidAuth.manage.php", { do: "retrievesessions", userid: $(this).closest('tr').find('td:nth-child(1)').data('userid') }) .done(function(data,_status) { if (data.Result === 'Success') { var Sessions = JSON.parse(data.UserSessions); for (var i = 0; i < data.SessionCount; i++) { try { var fingerPrint = JSON.parse(atob(Sessions[i]['fp'])); var sessionDetails = ''; sessionDetails += fingerPrint['browser'] + ' -- ' + fingerPrint['platform']; sessionDetails += '
' + fingerPrint['city'] + ' (' + fingerPrint['countrycode'] + ')'; } catch(e) { // Do nothing } $('#sessiontable tbody').append($('') .append($('', { text: new Date(Sessions[i]['iat'] * 1000).toLocaleString('en-GB') })) .append($('', { text: Sessions[i]['iss'] })) .append($('', { html: sessionDetails ? sessionDetails : '' })) ); } } else { } }); }); $('#usertable button.delete').click(function() { $(this).closest('tr').addClass('removed'); }); $('#btnnewuser').click(function() { // Create a new user; generate pseudo-random username var newUser = 'User' + String(Math.floor(Math.random() * Math.floor(9999))).padStart(4, '0'); // Add new user to the interface // (new `` in ``) $('#usertable tbody').append($('', {class: 'new'}) .append($('` // To prevent recreating multiple new editors; reference the already existing `` $('#usertable').editableTableWidget({editor: $('#editor')}); // Add eventhandlers to buttons of newly added `` $('#usertable .new button.session').unbind().click(function() { console.log('New user, unlikely to have sessions already, lets do nothing for now'); }); $('#usertable .new button.delete').unbind().click(function() { $(this).closest('tr').remove(); }); }); $('#btnsave').click(function() { var newEntries = []; $('#usertable .new').each(function() { newEntries.push({ 'userName': $(this).find('td:nth-child(1)').text(), 'roleName': $(this).find('td:nth-child(2)').text() }); }); var removedEntries = []; $('#usertable .removed').each(function() { removedEntries.push({ 'userId': $(this).find('td:nth-child(1)').data('userid'), 'userName': $(this).find('td:nth-child(1)').text(), 'roleName': $(this).find('td:nth-child(2)').text() }); }); console.log({'new': newEntries, 'removed': removedEntries}); /* $.get("psworker.php", { do: "mutate", mutations: { new: newEntries, removed: removedEntries } })*/ }); $('#btncancel').click(function() { window.location.reload(); }); $('#linklogout').click(function() { console.log('Logging out!'); }); if (localStorage.getItem('theme') !== null) { $('#theme').addClass(localStorage.getItem('theme')); } $('.logo .sub').click(function(event) { var classes = ["tablecloth", "weave", "madras", "tartan", "seigaiha"]; if (event.ctrlKey) { var selectedTheme = classes[~~(Math.random()*classes.length)]; $('#theme').removeClass(classes.join(' ')).addClass(selectedTheme); localStorage.setItem('theme', selectedTheme); } if (event.altKey) { $('#theme').removeClass(classes.join(' ')); localStorage.removeItem('theme'); } }); }); $(document).click(function () { $('#sessions').fadeOut(); });
', { text: newUser })) .append($('', { text: 'User' })) .append($('', { class: 'immutable', html: ' ' + '' })) ); // Call `editableTableWidget()` again to include the newly added `