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!
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

263 Zeilen
9.6KB

  1. import { h, Component, createRef } 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', 'owner' ]
  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. this.state.id = ('id' in this.props) ? this.props.id : uuid.v4();
  44. this.state.accept = () => {};
  45. this.modalRef = createRef();
  46. }
  47. navigateBack() {
  48. if (this.state.history.length === 0)
  49. return;
  50. const url = this.state.history.pop();
  51. this.navigate(url, false);
  52. }
  53. navigate(url, useHistory=true, stateUpdate={}) {
  54. if (typeof(url) === 'object') {
  55. url = ['', 'browse-dialog',
  56. 'mode' in url ? url.mode : this.state.mode,
  57. 'uuid' in url ? url.uuid : this.state.uuid,
  58. 'topPage' in url ? url.topPage : this.state.topPage,
  59. 'bottomPage' in url ? url.bottomPage : this.state.bottomPage,
  60. 'textSearch' in url ? url.textSearch : this.state.textSearch,
  61. encodeURIComponent('collectionPath' in url ? url.collectionPath : this.state.collectionPath)
  62. ].join('/');
  63. }
  64. url = url.substr(url.indexOf('/browse-dialog/'));
  65. if (useHistory)
  66. this.state.history.push(this.state.currentUrl);
  67. let [ _1, _2, mode, uuid, topPage, bottomPage, textSearch, collectionPath ] = url.split('/');
  68. topPage = parseInt(topPage, 10) || 0;
  69. bottomPage = parseInt(bottomPage, 10) || 0;
  70. collectionPath = decodeURIComponent(collectionPath || '');
  71. this.setState(Object.assign({
  72. 'currentUrl': url,
  73. mode, uuid, topPage, bottomPage, textSearch, collectionPath
  74. }, stateUpdate));
  75. }
  76. select(uuid) {
  77. let { selected, selectedOrder } = this.state;
  78. if (uuid in selected) {
  79. const n = selectedOrder.indexOf(uuid);
  80. selectedOrder = selected.splice(n, n + 1);
  81. }
  82. selected[uuid] = true;
  83. selectedOrder.push(uuid);
  84. /* this.setState({
  85. selected, selectedOrder
  86. }); */
  87. }
  88. deselect(uuid) {
  89. let { selected, selectedOrder } = this.state;
  90. if (!(uuid in selected))
  91. return;
  92. const n = selectedOrder.indexOf(uuid);
  93. selectedOrder = selected.splice(n, n + 1);
  94. delete selected[uuid];
  95. /* this.setState({
  96. selected, selectedOrder
  97. }); */
  98. }
  99. resetSelection() {
  100. this.setState({
  101. 'selected': {},
  102. 'selectedOrder': []
  103. });
  104. }
  105. makeSelectionCell(uuid) {
  106. const { selected, accept, selectMany, id } = this.state;
  107. return selectMany ? (
  108. <div>
  109. <input type="checkbox" checked={ (uuid in selected) }
  110. onChange={ e => {
  111. if (e.target.checked)
  112. this.select(uuid);
  113. else
  114. this.deselect(uuid);
  115. } } /> { '\u00A0' }
  116. </div>
  117. ) : (
  118. <button class="btn btn-outline-primary" title="Use"
  119. onclick={ () => {
  120. $('#' + id).modal('hide');
  121. accept(uuid);
  122. } }>
  123. <i class="fas fa-hand-pointer"></i>
  124. </button>
  125. );
  126. }
  127. show(selectWhat, selectMany, accept=(() => {})) {
  128. const { app } = this.props;
  129. const { currentUser } = app.state;
  130. this.navigate('/browse-dialog/browse/' + currentUser.uuid, false,
  131. { selectWhat, selectMany, accept, history: [],
  132. selected: {}, selectedOrder: [] });
  133. $('#' + this.state.id).modal();
  134. }
  135. componentWillUnmount() {
  136. $(this.modalRef.current).modal('hide');
  137. }
  138. render({ app },
  139. { history, currentUrl, mode, uuid,
  140. topPage, bottomPage, textSearch,
  141. collectionPath, id, accept, selectedOrder,
  142. selectMany, selectWhat }) {
  143. return (
  144. <div class="modal" id={ id } tabindex="-1" role="dialog" ref={ this.modalRef }>
  145. <div class="modal-dialog modal-lg" role="document">
  146. <div class="modal-content">
  147. <div class="modal-header">
  148. { false ? <h5 class="modal-title">Browse</h5> : null }
  149. <div>{ currentUrl }</div>
  150. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  151. <span aria-hidden="true">&times;</span>
  152. </button>
  153. </div>
  154. <div class="modal-body">
  155. <div class="mb-3">
  156. <a href="#" class={ 'btn btn-outline-secondary mr-2' +
  157. (history.length === 0 ? ' disabled': '') }
  158. onclick={ e => { e.preventDefault();
  159. this.navigateBack(); } }>Back</a>
  160. <a href="#" class="btn btn-outline-primary mr-2"
  161. onclick={ e => { e.preventDefault();
  162. this.navigate('/browse-dialog/browse/' + app.state.currentUser.uuid); } }>Home</a>
  163. <a href="#" class="btn btn-outline-primary mr-2"
  164. onclick={ e => { e.preventDefault();
  165. this.navigate('/browse-dialog/browse'); } }>All Projects</a>
  166. <a href="#" class="btn btn-outline-primary mr-2"
  167. onclick={ e => { e.preventDefault();
  168. this.navigate('/browse-dialog/users'); } }>All Users</a>
  169. <a href="#" class="btn btn-outline-primary mr-2"
  170. onclick={ e => { e.preventDefault();
  171. this.navigate('/browse-dialog/shared-with-me'); } }>Shared with Me</a>
  172. </div>
  173. <div class="input-group mb-3">
  174. <input type="text" class="form-control" placeholder="Search"
  175. aria-label="Search" value={ textSearch }
  176. onChange={ e => this.navigate({
  177. 'textSearch': e.target.value,
  178. 'topPage': 0,
  179. 'bottomPage': 0}) } />
  180. <div class="input-group-append">
  181. <button class="btn btn-outline-primary" type="button">Search</button>
  182. </div>
  183. </div>
  184. { (mode === 'browse' || mode === 'shared-with-me') ? (
  185. <div>
  186. <h5>Projects</h5>
  187. <WBBrowseDialogProjectList app={ app }
  188. navigate={ url => this.navigate(url) }
  189. mode={ mode } ownerUuid={ uuid }
  190. page={ topPage } textSearch={ textSearch }
  191. selectWhat={ selectWhat }
  192. makeSelectionCell={ uuid => this.makeSelectionCell(uuid) } />
  193. </div>
  194. ) : null }
  195. { (mode === 'users') ? (
  196. <WBBrowseDialogUserList app={ app }
  197. navigate={ url => this.navigate(url) }
  198. page={ topPage } textSearch={ textSearch }/>
  199. ) : null }
  200. { (mode === 'content') ? (
  201. <div>
  202. <h5>Content</h5>
  203. <WBBrowseDialogCollectionContent app={ app }
  204. collectionUuid={ uuid } collectionPath={ collectionPath }
  205. page={ bottomPage } selectWhat={ selectWhat }
  206. makeSelectionCell={ uuid => this.makeSelectionCell(uuid) }
  207. navigate={ url => this.navigate(url) }
  208. textSearch={ textSearch } />
  209. </div>
  210. ) : (selectWhat !== 'owner' && mode === 'browse') ? (
  211. <div>
  212. <h5>Collections</h5>
  213. <WBBrowseDialogCollectionList app={ app }
  214. page={ bottomPage } textSearch={ textSearch }
  215. navigate={ url => this.navigate(url) }
  216. ownerUuid={ uuid } selectWhat={ selectWhat }
  217. makeSelectionCell={ uuid => this.makeSelectionCell(uuid) } />
  218. </div>
  219. ) : null }
  220. </div>
  221. <div class="modal-footer">
  222. { selectMany ? (
  223. <button type="button" class="btn btn-primary"
  224. onclick={ e => { e.preventDefault(); accept(selectedOrder); $('#' + id).modal('hide'); } }>Accept</button>
  225. ) : null }
  226. <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
  227. </div>
  228. </div>
  229. </div>
  230. </div>
  231. );
  232. }
  233. }
  234. WBBrowseDialog.defaultProps = {
  235. 'accept': () => {}
  236. };
  237. export default WBBrowseDialog;