agents.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // specify the columns
  2. var columnDefs = [
  3. {headerName:'UUID', field:'UUID', width:60, editable:true, sortable:true},
  4. {headerName:'Name', field:'Name', width:80, editable:true, sortable:true},
  5. {headerName:'OwnerKey', field:'OwnerKey', width:60, editable:true, sortable:true},
  6. {headerName:'OwnerName', field:'OwnerName', width:80, editable:true, sortable:true},
  7. {headerName:'Location', field:'Location', width:60, editable:true, sortable:true},
  8. {headerName:'Position', field:'Position', width:60, editable:true, sortable:true},
  9. {headerName:'Rotation', field:'Rotation', width:60, editable:true},
  10. {headerName:'Velocity', field:'Velocity', width:60, editable:true},
  11. {headerName:'Energy', field:'Energy', width:35, editable:true, sortable:true},
  12. {headerName:'Money', field:'Money', width:35, editable:true, sortable:true},
  13. {headerName:'Happiness', field:'Happiness', width:35, editable:true, sortable:true},
  14. {headerName:'Class', field:'Class', width:35, editable:true, sortable:true},
  15. {headerName:'SubType', field:'SubType', width:35, editable:true},
  16. {headerName:'PermURL', field:'PermURL', width:80, editable:true},
  17. {headerName:'LastUpdate', field:'LastUpdate', width:95, editable:true, sortable:true, sort:'desc'},
  18. {headerName:'BestPath', field:'BestPath', width:30, editable:true, sortable:true},
  19. {headerName:'SecondBestPath', field:'SecondBestPath', width:30, editable:true, sortable:true},
  20. {headerName:'CurrentTarget', field:'CurrentTarget', width:60, editable:true, sortable:true},
  21. ];
  22. var URLPathPrefix;
  23. var gridOptions = {
  24. columnDefs: columnDefs,
  25. rowData: null,
  26. rowSelection: 'multiple',
  27. enableColResize: true,
  28. enableSorting: true,
  29. enableFilter: true,
  30. enableRangeSelection: true,
  31. rowHeight: 22,
  32. animateRows: true,
  33. debug: true,
  34. editType: 'fullRow',
  35. onRowValueChanged: function(event) {
  36. var data = event.data;
  37. console.log('onRowValueChanged: (' + data.UUID + ', ' + data.Name + ', ' + data.Energy + ' ...)');
  38. var httpRequest = new XMLHttpRequest(); // see https://stackoverflow.com/questions/6418220/javascript-send-json-object-with-ajax
  39. httpRequest.open('POST', URLPathPrefix + '/uiAgentsUpdate/');
  40. httpRequest.setRequestHeader("Content-Type", "application/json");
  41. httpRequest.onreadystatechange = function() { // see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange
  42. if (httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) {
  43. console.log(httpRequest.responseText);
  44. }
  45. };
  46. var response = JSON.stringify(data);
  47. // console.log('Response is going to be: ' + response)
  48. httpRequest.send(response);
  49. },
  50. onGridReady: function() {
  51. gridOptions.api.sizeColumnsToFit();
  52. }
  53. };
  54. // wait for the document to be loaded, otherwise ag-Grid will not find the div in the document.
  55. document.addEventListener("DOMContentLoaded", function() {
  56. URLPathPrefix = document.getElementById("URLPathPrefix").innerText;
  57. // lookup the container we want the Grid to use
  58. var eGridDiv = document.querySelector('#agentGrid');
  59. // create the grid passing in the div to use together with the columns & data we want to use
  60. new agGrid.Grid(eGridDiv, gridOptions);
  61. // do http request to get our sample data (see https://www.ag-grid.com/javascript-grid-value-getters/?framework=all#gsc.tab=0)
  62. var httpRequest = new XMLHttpRequest();
  63. httpRequest.open('GET', URLPathPrefix + '/uiAgents/');
  64. httpRequest.send();
  65. httpRequest.onreadystatechange = function() {
  66. if (httpRequest.readyState == 4 && httpRequest.status == 200) {
  67. var httpResult = JSON.parse(httpRequest.responseText);
  68. gridOptions.api.setRowData(httpResult);
  69. }
  70. };
  71. // see if we have passed an UUID on the command line; this will allow us to set a filter
  72. var UUID = getURLParameter("UUID");
  73. gridOptions.api.setQuickFilter(UUID); // should just show our UUID
  74. });
  75. function onRemoveSelected() {
  76. BootstrapDialog.confirm({
  77. title: 'WARNING',
  78. message: 'Are you sure you want to delete the selected rows?',
  79. type: BootstrapDialog.TYPE_WARNING,
  80. size: BootstrapDialog.SIZE_SMALL,
  81. closable: true,
  82. draggable: true,
  83. btnCancelLabel: 'Cancel',
  84. btnCancelIcon: 'glyphicon glyphicon-remove',
  85. btnOKLabel: 'Ok',
  86. btnOkIcon: 'glyphicon glyphicon-ok',
  87. btnOKClass: 'btn-warning',
  88. callback: function(result) {
  89. if (result) {
  90. var selectedRows = gridOptions.api.getSelectedRows();
  91. var ids = "";
  92. selectedRows.forEach( function(selectedRow, index) {
  93. if (index!==0) {
  94. ids += '", "';
  95. }
  96. else {
  97. ids = '"';
  98. }
  99. ids += selectedRow.UUID;
  100. });
  101. ids += '"';
  102. // console.log('Removing UUIDs: ' + ids);
  103. var httpRequest = new XMLHttpRequest();
  104. httpRequest.open('POST', URLPathPrefix + '/uiAgentsRemove/');
  105. httpRequest.setRequestHeader("Content-Type", "text/plain");
  106. httpRequest.onreadystatechange = function() {
  107. if (httpRequest.readyState === XMLHttpRequest.DONE) {
  108. if (httpRequest.status === 200) {
  109. console.log(httpRequest.responseText);
  110. gridOptions.api.updateRowData({remove: selectedRows});
  111. gridOptions.api.refreshView();
  112. } else {
  113. console.log(httpRequest.responseText);
  114. BootstrapDialog.show({
  115. title: httpRequest.status,
  116. message: 'Error: ' + httpRequest.responseText,
  117. type: BootstrapDialog.TYPE_DANGER
  118. });
  119. }
  120. }
  121. };
  122. httpRequest.send(ids);
  123. gridOptions.api.updateRowData({remove: selectedRows});
  124. gridOptions.api.refreshView();
  125. } else {
  126. console.log("Canceled removing nodes.");
  127. }
  128. }
  129. });
  130. }
  131. function onInsertRow() {
  132. var dateNow = new Date();
  133. var newItem = {
  134. UUID: "00000000-0000-0000-0000-000000000000",
  135. Name: "empty",
  136. LastUpdate: dateNow.toISOString()
  137. };
  138. gridOptions.api.updateRowData({add: [newItem], addIndex: 0});
  139. gridOptions.api.setFocusedCell(0, newItem, 'top');
  140. var newItemNode = gridOptions.api.getDisplayedRowAtIndex(0);
  141. newItemNode.setSelected(true);
  142. gridOptions.api.ensureIndexVisible(0);
  143. gridOptions.api.refreshView();
  144. }
  145. // this checks for the URL to see if it has an UUID as a parameter
  146. function getURLParameter(name) {
  147. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
  148. }