// // Copyright (C) Stanislaw Adaszewski, 2020 // Contact: s.adaszewski@gmail.com // Website: https://adared.ch/wba // License: GNU Affero General Public License, Version 3 // import { h, Component, createRef } from 'preact'; import WBBrowseDialogProjectList from 'wb-browse-dialog-project-list'; import WBBrowseDialogCollectionList from 'wb-browse-dialog-collection-list'; import WBBrowseDialogCollectionContent from 'wb-browse-dialog-collection-content'; import WBBrowseDialogUserList from 'wb-browse-dialog-user-list'; import linkState from 'linkstate'; import { Router } from 'preact-router'; import { createHashHistory } from 'history'; // // internal URLs look like this // // /browse-dialog/browse/( owner-uuid )/( project-page )/( text-search ) // /browse-dialog/users//( users-page )/( text-search ) // /browse-dialog/shared-with-me//( project-page )/( collection-page )/( text-search ) // /browse-dialog/content/( collection-uuid )//( content-page )/( text-search )/( collection-path ) // // general pattern therefore: // /browse-dialog/( mode )/( uuid )/( top-page )/( bottom-page )/( text-search ) // // props: // selectMany: Boolean // selectWhat: [ 'file', 'directory', 'owner' ] // // state: // selected: Array of UUID // textSearch: string // textSearchInput: string // class WBBrowseDialog extends Component { constructor(...args) { super(...args); this.state.history = []; this.state.selected = {}; this.state.selectedOrder = []; const { currentUser } = this.props.app.state; this.state.currentUrl = '/browse-dialog/browse/' + currentUser.uuid; this.state.uuid = currentUser.uuid; this.state.mode = 'browse'; this.state.topPage = 0; this.state.bottomPage = 0; this.state.collectionPath = ''; this.state.textSearch = ''; this.state.id = ('id' in this.props) ? this.props.id : uuid.v4(); this.state.accept = () => {}; this.modalRef = createRef(); } navigateBack() { if (this.state.history.length === 0) return; const url = this.state.history.pop(); this.navigate(url, false); } navigate(url, useHistory=true, stateUpdate={}) { if (typeof(url) === 'object') { url = ['', 'browse-dialog', 'mode' in url ? url.mode : this.state.mode, 'uuid' in url ? url.uuid : this.state.uuid, 'topPage' in url ? url.topPage : this.state.topPage, 'bottomPage' in url ? url.bottomPage : this.state.bottomPage, 'textSearch' in url ? url.textSearch : this.state.textSearch, encodeURIComponent('collectionPath' in url ? url.collectionPath : this.state.collectionPath) ].join('/'); } url = url.substr(url.indexOf('/browse-dialog/')); if (useHistory) this.state.history.push(this.state.currentUrl); let [ _1, _2, mode, uuid, topPage, bottomPage, textSearch, collectionPath ] = url.split('/'); topPage = parseInt(topPage, 10) || 0; bottomPage = parseInt(bottomPage, 10) || 0; collectionPath = decodeURIComponent(collectionPath || ''); this.setState(Object.assign({ 'currentUrl': url, mode, uuid, topPage, bottomPage, textSearch, collectionPath }, stateUpdate)); } select(uuid) { let { selected, selectedOrder } = this.state; if (uuid in selected) { const n = selectedOrder.indexOf(uuid); selectedOrder = selected.splice(n, n + 1); } selected[uuid] = true; selectedOrder.push(uuid); /* this.setState({ selected, selectedOrder }); */ } deselect(uuid) { let { selected, selectedOrder } = this.state; if (!(uuid in selected)) return; const n = selectedOrder.indexOf(uuid); selectedOrder = selected.splice(n, n + 1); delete selected[uuid]; /* this.setState({ selected, selectedOrder }); */ } resetSelection() { this.setState({ 'selected': {}, 'selectedOrder': [] }); } makeSelectionCell(uuid) { const { selected, accept, selectMany, id } = this.state; return selectMany ? (
{ if (e.target.checked) this.select(uuid); else this.deselect(uuid); } } /> { '\u00A0' }
) : ( ); } show(selectWhat, selectMany, accept=(() => {})) { const { app } = this.props; const { currentUser } = app.state; this.navigate('/browse-dialog/browse/' + currentUser.uuid, false, { selectWhat, selectMany, accept, history: [], selected: {}, selectedOrder: [] }); $('#' + this.state.id).modal(); } componentWillUnmount() { $(this.modalRef.current).modal('hide'); } render({ app }, { history, currentUrl, mode, uuid, topPage, bottomPage, textSearch, collectionPath, id, accept, selectedOrder, selectMany, selectWhat }) { return ( ); } } WBBrowseDialog.defaultProps = { 'accept': () => {} }; export default WBBrowseDialog;