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!
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

249 řádky
8.9KB

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