inventory.js 4.8 KB

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