objects.js 6.6 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:'Bot Key', field:'BotKey', width:60, editable:true, sortable:true},
  6. {headerName:'Bot Name', field:'BotName', width:70, editable:true, sortable:true},
  7. {headerName:'Type', field:'Type', width:35, filter: 'number', editable:true},
  8. {headerName:'Origin', field:'Origin', width:35, editable:true},
  9. {headerName:'Position', field:'Position', width:60, editable:true, sortable:true},
  10. {headerName:'Rotation', field:'Rotation', width:60, editable:true},
  11. {headerName:'Velocity', field:'Velocity', width:60, editable:true},
  12. {headerName:'Phantom', field:'Phantom', width:6, filter: 'number', editable:false},
  13. {headerName:'Prims', field:'Prims', width:6, filter: 'number', editable:false},
  14. {headerName:'BB High', field:'BBHigh', width:35, editable:false},
  15. {headerName:'BB Low', field:'BBLow', width:35, editable:false},
  16. {headerName:'LastUpdate', field:'LastUpdate', width:90, editable:true, sortable:true, sort:'desc'}
  17. ];
  18. // get URLPathPrefix (hopefully it's already loaded by now)
  19. var URLPathPrefix;
  20. // let the grid know which columns and what data to use
  21. var gridOptions = {
  22. columnDefs: columnDefs,
  23. rowData: null, // change to null since we're filling it from our own servers
  24. rowSelection: 'multiple',
  25. enableColResize: true,
  26. enableSorting: true,
  27. enableFilter: true,
  28. enableRangeSelection: true,
  29. rowHeight: 22,
  30. animateRows: true,
  31. debug: true,
  32. editType: 'fullRow',
  33. onRowValueChanged: function(event) {
  34. var data = event.data;
  35. console.log('onRowValueChanged: (' + data.UUID + ', ' + data.Name + ', ' + data.BotKey + ' ...)');
  36. // call the bloody fucking stuff from our REST API to update the database
  37. var httpRequest = new XMLHttpRequest(); // see https://stackoverflow.com/questions/6418220/javascript-send-json-object-with-ajax
  38. httpRequest.open('POST', URLPathPrefix + '/uiObjectsUpdate/');
  39. httpRequest.setRequestHeader("Content-Type", "application/json");
  40. httpRequest.onreadystatechange = function() { // see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange
  41. if (httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) {
  42. console.log(httpRequest.responseText);
  43. }
  44. };
  45. var response = JSON.stringify(data);
  46. // console.log('Response is going to be: ' + response)
  47. httpRequest.send(response);
  48. },
  49. onGridReady: function() {
  50. gridOptions.api.sizeColumnsToFit();
  51. }
  52. };
  53. // wait for the document to be loaded, otherwise ag-Grid will not find the div in the document.
  54. document.addEventListener("DOMContentLoaded", function() {
  55. URLPathPrefix = document.getElementById("URLPathPrefix").innerText;
  56. // console.log('URLPathPrefix is "' + URLPathPrefix +'"');
  57. // lookup the container we want the Grid to use
  58. var eGridDiv = document.querySelector('#objectGrid');
  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 + '/uiObjects/');
  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({ // from https://nakupanda.github.io/bootstrap3-dialog/#advanced-confirm-window
  77. title: 'WARNING',
  78. message: 'Are you sure you want to delete the selected rows?',
  79. type: BootstrapDialog.TYPE_WARNING, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
  80. size: BootstrapDialog.SIZE_SMALL,
  81. closable: true,
  82. draggable: true, // <-- Default value is false
  83. btnCancelLabel: 'Cancel', // <-- Default value is 'Cancel',
  84. btnCancelIcon: 'glyphicon glyphicon-remove',
  85. btnOKLabel: 'Ok', // <-- Default value is 'OK',
  86. btnOkIcon: 'glyphicon glyphicon-ok',
  87. btnOKClass: 'btn-warning', // <-- If you didn't specify it, dialog type will be used,
  88. callback: function(result) {
  89. // result will be true if button was click, while it will be false if users close the dialog directly.
  90. if (result) {
  91. var selectedRows = gridOptions.api.getSelectedRows();
  92. var ids = "";
  93. selectedRows.forEach( function(selectedRow, index) {
  94. if (index!==0) {
  95. ids += '", "';
  96. }
  97. else {
  98. ids = '"';
  99. }
  100. ids += selectedRow.UUID;
  101. });
  102. ids += '"';
  103. // console.log('Removing UUIDs: ' + ids);
  104. var httpRequest = new XMLHttpRequest();
  105. httpRequest.open('POST', URLPathPrefix + '/uiObjectsRemove/');
  106. httpRequest.setRequestHeader("Content-Type", "text/plain");
  107. httpRequest.onreadystatechange = function() {
  108. if (httpRequest.readyState === XMLHttpRequest.DONE) {
  109. if (httpRequest.status === 200) {
  110. console.log(httpRequest.responseText);
  111. gridOptions.api.updateRowData({remove: selectedRows});
  112. gridOptions.api.refreshView();
  113. } else {
  114. console.log(httpRequest.responseText);
  115. BootstrapDialog.show({
  116. title: httpRequest.status,
  117. message: 'Error: ' + httpRequest.responseText,
  118. type: BootstrapDialog.TYPE_DANGER
  119. });
  120. }
  121. }
  122. };
  123. httpRequest.send(ids);
  124. } else {
  125. console.log("Canceled removing nodes.");
  126. }
  127. }
  128. });
  129. }
  130. function onInsertRow() {
  131. var dateNow = new Date(); // see https://stackoverflow.com/questions/39217275/date-now-toisostring-throwing-error-not-a-function
  132. var newItem = {
  133. UUID: "00000000-0000-0000-0000-000000000000",
  134. Name: "empty",
  135. LastUpdate: dateNow.toISOString() // this makes it the first row inserted, since it's reverse sorted by date
  136. };
  137. gridOptions.api.updateRowData({add: [newItem], addIndex: 0});
  138. gridOptions.api.setFocusedCell(0, newItem, 'top');
  139. var newItemNode = gridOptions.api.getDisplayedRowAtIndex(0);
  140. newItemNode.setSelected(true);
  141. gridOptions.api.ensureIndexVisible(0);
  142. gridOptions.api.refreshView();
  143. }
  144. // this checks for the URL to see if it has an UUID as a parameter
  145. function getURLParameter(name) {
  146. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
  147. }