IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.0KB

  1. import { h, Component } from 'preact';
  2. import WBTable from 'wb-table';
  3. import WBNameAndUuid from 'wb-name-and-uuid';
  4. import wbFetchObjects from 'wb-fetch-objects';
  5. import wbFormatDate from 'wb-format-date';
  6. class WBToolboxDialog extends Component {
  7. constructor(...args) {
  8. super(...args);
  9. this.state.rows = [];
  10. this.state.selectedValues = {};
  11. }
  12. componentDidMount() {
  13. this.fetchRows();
  14. }
  15. componentWillReceiveProps(nextProps) {
  16. this.props = nextProps;
  17. this.fetchRows();
  18. }
  19. fetchRows() {
  20. const { items, id, selectMany, onAccepted } = this.props;
  21. const { arvHost, arvToken } = this.props.app.state;
  22. const { selectedValues } = this.state;
  23. let prom = wbFetchObjects(arvHost, arvToken,
  24. items);
  25. let lookup;
  26. prom = prom.then(lkup => (lookup = lkup));
  27. prom = prom.then(() => wbFetchObjects(arvHost, arvToken,
  28. items.map(uuid => lookup[uuid].owner_uuid)));
  29. let ownerLookup;
  30. prom = prom.then(lkup => (ownerLookup = lkup));
  31. prom = prom.then(() => {
  32. const rows = items.map((uuid, idx) => {
  33. const it = lookup[uuid];
  34. const ow = ownerLookup[it.owner_uuid];
  35. let r = [];
  36. if (selectMany)
  37. r.push();
  38. r = r.concat([
  39. selectMany ? (
  40. <div>
  41. <input type="checkbox" checked={ (uuid in selectedValues) }
  42. onChange={ e => {
  43. if (e.target.value === 'on')
  44. selectedValues[uuid] = true;
  45. else
  46. delete selectedValues[uuid];
  47. } } /> { '\u00A0' }
  48. </div>
  49. ) : (
  50. <button class="btn btn-outline-primary" title="Use"
  51. onclick={ () => {
  52. $('#' + id).modal('hide');
  53. onAccepted(uuid);
  54. } }>
  55. <i class="fas fa-hand-pointer"></i>
  56. </button>
  57. ),
  58. ( <WBNameAndUuid uuid={ uuid } lookup={ lookup }
  59. onLinkClicked={ () => $('#' + id).modal('hide') } /> ),
  60. it.kind,
  61. wbFormatDate(it.created_at),
  62. ( <WBNameAndUuid uuid={ it.owner_uuid } lookup={ ownerLookup }
  63. onLinkClicked={ () => $('#' + id).modal('hide') } /> )
  64. ]);
  65. return r;
  66. });
  67. this.setState({ rows });
  68. });
  69. }
  70. render({ id, selectMany, onAccepted, items, app }, { rows, selectedValues }) {
  71. return (
  72. <div class="modal" id={ id } tabindex="-1" role="dialog">
  73. <div class="modal-dialog modal-lg" role="document">
  74. <div class="modal-content">
  75. <div class="modal-header">
  76. <h5 class="modal-title">Browse Toolbox</h5>
  77. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  78. <span aria-hidden="true">&times;</span>
  79. </button>
  80. </div>
  81. <div class="modal-body">
  82. <div class="mb-2">
  83. { selectMany ? (
  84. <button class="btn btn-outline-primary mr-2" onclick={ () => {
  85. items.map(uuid => (selectedValues[uuid] = true));
  86. this.fetchRows();
  87. } }>
  88. Select All
  89. </button>
  90. ) : null }
  91. { selectMany ? (
  92. <button class="btn btn-outline-primary mr-2" onclick={ () => {
  93. this.setState({ 'selectedValues' : {} });
  94. this.fetchRows();
  95. } }>
  96. Select None
  97. </button>
  98. ) : null }
  99. <button class="btn btn-outline-primary mr-2" onclick={ () => {
  100. app.clearToolbox();
  101. this.props.items = [];
  102. this.fetchRows();
  103. } } >
  104. Clear Toolbox
  105. </button>
  106. <button class="btn btn-outline-primary mr-2" onclick={ () => {
  107. app.loadToolbox();
  108. this.props.items = app.state.toolboxItems;
  109. this.fetchRows();
  110. } } >
  111. Refresh Toolbox
  112. </button>
  113. </div>
  114. <WBTable columns={ [ '', 'Name', 'Kind', 'Created At', 'Owner' ] }
  115. rows={ rows } />
  116. </div>
  117. <div class="modal-footer">
  118. { selectMany ? (
  119. <button type="button" class="btn btn-primary" onclick={
  120. () => {
  121. $('#' + id).modal('hide');
  122. onAccepted(items.filter(uuid => (uuid in selectedValues)));
  123. }
  124. }>Accept</button>
  125. ) : null }
  126. <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
  127. </div>
  128. </div>
  129. </div>
  130. </div>
  131. );
  132. }
  133. }
  134. WBToolboxDialog.defaultProps = {
  135. 'onAccepted': () => {}
  136. };
  137. export default WBToolboxDialog;