positions.js 5.9 KB

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