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.

244 lines
8.8KB

  1. import { h, Component } from 'preact';
  2. import WBBrowseDialogProjectList from 'wb-browse-dialog-project-list';
  3. import WBBrowseDialogCollectionList from 'wb-browse-dialog-collection-list';
  4. import WBBrowseDialogCollectionContent from 'wb-browse-dialog-collection-content';
  5. import WBBrowseDialogUserList from 'wb-browse-dialog-user-list';
  6. import linkState from 'linkstate';
  7. import { Router } from 'preact-router';
  8. import { createHashHistory } from 'history';
  9. //
  10. // internal URLs look like this
  11. //
  12. // /browse-dialog/browse/( owner-uuid )/( project-page )/( text-search )
  13. // /browse-dialog/users//( users-page )/( text-search )
  14. // /browse-dialog/shared-with-me//( project-page )/( collection-page )/( text-search )
  15. // /browse-dialog/content/( collection-uuid )//( content-page )/( text-search )/( collection-path )
  16. //
  17. // general pattern therefore:
  18. // /browse-dialog/( mode )/( uuid )/( top-page )/( bottom-page )/( text-search )
  19. //
  20. // props:
  21. // selectMany: Boolean
  22. // selectWhat: [ 'file', 'directory' ]
  23. //
  24. // state:
  25. // selected: Array of UUID
  26. // textSearch: string
  27. // textSearchInput: string
  28. //
  29. class WBBrowseDialog extends Component {
  30. constructor(...args) {
  31. super(...args);
  32. this.state.history = [];
  33. this.state.selected = {};
  34. this.state.selectedOrder = [];
  35. const { currentUser } = this.props.app.state;
  36. this.state.currentUrl = '/browse-dialog/browse/' + currentUser.uuid;
  37. this.state.uuid = currentUser.uuid;
  38. this.state.mode = 'browse';
  39. this.state.topPage = 0;
  40. this.state.bottomPage = 0;
  41. this.state.collectionPath = '';
  42. this.state.textSearch = '';
  43. }
  44. navigateBack() {
  45. if (this.state.history.length === 0)
  46. return;
  47. const url = this.state.history.pop();
  48. this.navigate(url, false);
  49. }
  50. navigate(url, useHistory=true) {
  51. if (typeof(url) === 'object') {
  52. url = ['', 'browse-dialog',
  53. 'mode' in url ? url.mode : this.state.mode,
  54. 'uuid' in url ? url.uuid : this.state.uuid,
  55. 'topPage' in url ? url.topPage : this.state.topPage,
  56. 'bottomPage' in url ? url.bottomPage : this.state.bottomPage,
  57. 'textSearch' in url ? url.textSearch : this.state.textSearch,
  58. encodeURIComponent('collectionPath' in url ? url.collectionPath : this.state.collectionPath)
  59. ].join('/');
  60. }
  61. url = url.substr(url.indexOf('/browse-dialog/'));
  62. if (useHistory)
  63. this.state.history.push(this.state.currentUrl);
  64. let [ _1, _2, mode, uuid, topPage, bottomPage, textSearch, collectionPath ] = url.split('/');
  65. topPage = parseInt(topPage, 10) || 0;
  66. bottomPage = parseInt(bottomPage, 10) || 0;
  67. collectionPath = decodeURIComponent(collectionPath);
  68. this.setState({
  69. 'currentUrl': url,
  70. mode, uuid, topPage, bottomPage, textSearch, collectionPath
  71. });
  72. }
  73. select(uuid) {
  74. let { selected, selectedOrder } = this.state;
  75. if (uuid in selected) {
  76. const n = selectedOrder.indexOf(uuid);
  77. selectedOrder = selected.splice(n, n + 1);
  78. }
  79. selected[uuid] = true;
  80. selectedOrder.push(uuid);
  81. this.setState({
  82. selected, selectedOrder
  83. });
  84. }
  85. deselect(uuid) {
  86. let { selected, selectedOrder } = this.state;
  87. if (!(uuid in selected))
  88. return;
  89. const n = selectedOrder.indexOf(uuid);
  90. selectedOrder = selected.splice(n, n + 1);
  91. delete selected[uuid];
  92. this.setState({
  93. selected, selectedOrder
  94. });
  95. }
  96. resetSelection() {
  97. this.setState({
  98. 'selected': {},
  99. 'selectedOrder': []
  100. });
  101. }
  102. makeSelectionCell(uuid) {
  103. const { selectMany, id, accept } = this.props;
  104. const { selected } = this.state;
  105. return selectMany ? (
  106. <div>
  107. <input type="checkbox" checked={ (uuid in selected) }
  108. onChange={ e => {
  109. if (e.target.value === 'on')
  110. this.select(uuid);
  111. else
  112. this.deselect(uuid);
  113. } } /> { '\u00A0' }
  114. </div>
  115. ) : (
  116. <button class="btn btn-outline-primary" title="Use"
  117. onclick={ () => {
  118. $('#' + id).modal('hide');
  119. accept(uuid);
  120. } }>
  121. <i class="fas fa-hand-pointer"></i>
  122. </button>
  123. );
  124. }
  125. render({ app, id, selectMany, selectWhat },
  126. { history, currentUrl, mode, uuid,
  127. topPage, bottomPage, textSearch,
  128. collectionPath }) {
  129. return (
  130. <div class="modal" id={ id } tabindex="-1" role="dialog">
  131. <div class="modal-dialog modal-lg" role="document">
  132. <div class="modal-content">
  133. <div class="modal-header">
  134. { false ? <h5 class="modal-title">Browse</h5> : null }
  135. <div>{ currentUrl }</div>
  136. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  137. <span aria-hidden="true">&times;</span>
  138. </button>
  139. </div>
  140. <div class="modal-body">
  141. <div class="mb-3">
  142. <a href="#" class={ 'btn btn-outline-secondary mr-2' +
  143. (history.length === 0 ? ' disabled': '') }
  144. onclick={ e => { e.preventDefault();
  145. this.navigateBack(); } }>Back</a>
  146. <a href="#" class="btn btn-outline-primary mr-2"
  147. onclick={ e => { e.preventDefault();
  148. this.navigate('/browse-dialog/browse/' + app.state.currentUser.uuid); } }>Home</a>
  149. <a href="#" class="btn btn-outline-primary mr-2"
  150. onclick={ e => { e.preventDefault();
  151. this.navigate('/browse-dialog/browse'); } }>All Projects</a>
  152. <a href="#" class="btn btn-outline-primary mr-2"
  153. onclick={ e => { e.preventDefault();
  154. this.navigate('/browse-dialog/users'); } }>All Users</a>
  155. <a href="#" class="btn btn-outline-primary mr-2"
  156. onclick={ e => { e.preventDefault();
  157. this.navigate('/browse-dialog/shared-with-me'); } }>Shared with Me</a>
  158. </div>
  159. <div class="input-group mb-3">
  160. <input type="text" class="form-control" placeholder="Search"
  161. aria-label="Search" value={ textSearch }
  162. onChange={ e => this.navigate({
  163. 'textSearch': e.target.value,
  164. 'topPage': 0,
  165. 'bottomPage': 0}) } />
  166. <div class="input-group-append">
  167. <button class="btn btn-outline-primary" type="button">Search</button>
  168. </div>
  169. </div>
  170. { (mode === 'browse' || mode === 'shared-with-me') ? (
  171. <div>
  172. <h5>Projects</h5>
  173. <WBBrowseDialogProjectList app={ app }
  174. navigate={ url => this.navigate(url) }
  175. mode={ mode } ownerUuid={ uuid }
  176. page={ topPage } textSearch={ textSearch }
  177. selectWhat={ selectWhat }
  178. makeSelectionCell={ uuid => this.makeSelectionCell(uuid) } />
  179. </div>
  180. ) : null }
  181. { (mode === 'users') ? (
  182. <WBBrowseDialogUserList app={ app }
  183. navigate={ url => this.navigate(url) }
  184. page={ topPage } textSearch={ textSearch }/>
  185. ) : null }
  186. { (mode === 'content') ? (
  187. <div>
  188. <h5>Content</h5>
  189. <WBBrowseDialogCollectionContent app={ app }
  190. collectionUuid={ uuid } collectionPath={ collectionPath }
  191. page={ bottomPage } selectWhat={ selectWhat }
  192. makeSelectionCell={ uuid => this.makeSelectionCell(uuid) }
  193. navigate={ url => this.navigate(url) }
  194. textSearch={ textSearch } />
  195. </div>
  196. ) : (selectWhat !== 'owner' && mode === 'browse') ? (
  197. <div>
  198. <h5>Collections</h5>
  199. <WBBrowseDialogCollectionList app={ app }
  200. page={ bottomPage } textSearch={ textSearch }
  201. navigate={ url => this.navigate(url) }
  202. ownerUuid={ uuid } selectWhat={ selectWhat }
  203. makeSelectionCell={ uuid => this.makeSelectionCell(uuid) } />
  204. </div>
  205. ) : null }
  206. </div>
  207. <div class="modal-footer">
  208. <button type="button" class="btn btn-primary">Accept</button>
  209. <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
  210. </div>
  211. </div>
  212. </div>
  213. </div>
  214. );
  215. }
  216. }
  217. WBBrowseDialog.defaultProps = {
  218. 'accept': () => {}
  219. };
  220. export default WBBrowseDialog;