1
0

user-management.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // specify the columns
  2. var columnDefs = [
  3. {headerName:'Email', field:'Email', width:80, editable:true, sortable:true, sort: 'asc'},
  4. {headerName:'Password', field:'Password', width:150, editable:true, sortable:true},
  5. ];
  6. var URLPathPrefix;
  7. var gridOptions = {
  8. columnDefs: columnDefs,
  9. rowData: null, // change to null since we're filling it from our own servers
  10. rowSelection: 'multiple',
  11. enableColResize: true,
  12. enableSorting: true,
  13. enableFilter: true,
  14. enableRangeSelection: true,
  15. rowHeight: 22,
  16. animateRows: true,
  17. debug: true,
  18. editType: 'fullRow',
  19. onRowValueChanged: function(event) {
  20. var data = event.data;
  21. console.log('onRowValueChanged: (' + data.Email + ', ' + data.Password + ')');
  22. // we should see if the password changed, and if that's the case, MD5 it first
  23. var httpRequest = new XMLHttpRequest(); // see https://stackoverflow.com/questions/6418220/javascript-send-json-object-with-ajax
  24. httpRequest.open('POST', URLPathPrefix + '/uiUserManagementUpdate/');
  25. httpRequest.setRequestHeader("Content-Type", "application/json");
  26. httpRequest.onreadystatechange = function() { // see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange
  27. if(httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) {
  28. console.log(httpRequest.responseText);
  29. }
  30. };
  31. var response = JSON.stringify(data);
  32. // console.log('Response is going to be: ' + response)
  33. httpRequest.send(response);
  34. },
  35. onGridReady: function() {
  36. gridOptions.api.sizeColumnsToFit();
  37. }
  38. };
  39. // wait for the document to be loaded, otherwise ag-Grid will not find the div in the document.
  40. document.addEventListener("DOMContentLoaded", function() {
  41. URLPathPrefix = document.getElementById("URLPathPrefix").innerText;
  42. // lookup the container we want the Grid to use
  43. var eGridDiv = document.querySelector('#userManagementGrid');
  44. // create the grid passing in the div to use together with the columns & data we want to use
  45. new agGrid.Grid(eGridDiv, gridOptions);
  46. // do http request to get our sample data (see https://www.ag-grid.com/javascript-grid-value-getters/?framework=all#gsc.tab=0)
  47. var httpRequest = new XMLHttpRequest();
  48. httpRequest.open('GET', URLPathPrefix + '/uiUserManagement/');
  49. httpRequest.send();
  50. httpRequest.onreadystatechange = function() {
  51. if (httpRequest.readyState == 4 && httpRequest.status == 200) {
  52. var httpResult = JSON.parse(httpRequest.responseText);
  53. gridOptions.api.setRowData(httpResult);
  54. }
  55. };
  56. });
  57. function onRemoveSelected() {
  58. BootstrapDialog.confirm({
  59. title: 'WARNING',
  60. message: 'Are you sure you want to delete the selected rows?',
  61. type: BootstrapDialog.TYPE_WARNING,
  62. size: BootstrapDialog.SIZE_SMALL,
  63. closable: true,
  64. draggable: true,
  65. btnCancelLabel: 'Cancel',
  66. btnCancelIcon: 'glyphicon glyphicon-remove',
  67. btnOKLabel: 'Ok',
  68. btnOkIcon: 'glyphicon glyphicon-ok',
  69. btnOKClass: 'btn-warning',
  70. callback: function(result) {
  71. if (result) {
  72. var selectedRows = gridOptions.api.getSelectedRows();
  73. var ids = "";
  74. selectedRows.forEach( function(selectedRow, index) {
  75. if (index!==0) {
  76. ids += '", "';
  77. }
  78. else {
  79. ids = '"';
  80. }
  81. ids += selectedRow.Email;
  82. });
  83. ids += '"';
  84. // console.log('Removing Emails: ' + ids);
  85. var httpRequest = new XMLHttpRequest();
  86. httpRequest.open('POST', URLPathPrefix + '/uiUserManagementRemove/');
  87. httpRequest.setRequestHeader("Content-Type", "text/plain");
  88. httpRequest.onreadystatechange = function() {
  89. if (httpRequest.readyState === XMLHttpRequest.DONE) {
  90. if (httpRequest.status === 200) {
  91. console.log(httpRequest.responseText);
  92. gridOptions.api.updateRowData({remove: selectedRows});
  93. gridOptions.api.refreshView();
  94. } else {
  95. console.log(httpRequest.responseText);
  96. BootstrapDialog.show({
  97. title: httpRequest.status,
  98. message: 'Error: ' + httpRequest.responseText,
  99. type: BootstrapDialog.TYPE_DANGER
  100. });
  101. }
  102. }
  103. };
  104. httpRequest.send(ids);
  105. gridOptions.api.updateRowData({remove: selectedRows});
  106. gridOptions.api.refreshView();
  107. } else {
  108. console.log("Canceled removing nodes.");
  109. }
  110. }
  111. });
  112. }
  113. function onInsertRow() {
  114. var dateNow = new Date();
  115. var newItem = {
  116. Email: "@Fake email here",
  117. Password: "Fake password here",
  118. };
  119. gridOptions.api.updateRowData({add: [newItem], addIndex: 0});
  120. gridOptions.api.setFocusedCell(0, newItem, 'top');
  121. var newItemNode = gridOptions.api.getDisplayedRowAtIndex(0);
  122. newItemNode.setSelected(true);
  123. gridOptions.api.ensureIndexVisible(0);
  124. gridOptions.api.refreshView();
  125. }