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!
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
9.3KB

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