2017-09-10 05:12:59 +00:00
|
|
|
// @flow
|
2020-06-20 20:59:15 +00:00
|
|
|
import { observable } from "mobx";
|
|
|
|
import { inject, observer } from "mobx-react";
|
2020-08-09 05:53:59 +00:00
|
|
|
import * as React from "react";
|
2020-11-30 04:04:58 +00:00
|
|
|
import { withTranslation, type TFunction } from "react-i18next";
|
2020-06-20 20:59:15 +00:00
|
|
|
import { withRouter, type RouterHistory } from "react-router-dom";
|
2020-08-09 05:53:59 +00:00
|
|
|
import DocumentsStore from "stores/DocumentsStore";
|
|
|
|
import PoliciesStore from "stores/PoliciesStore";
|
|
|
|
import UiStore from "stores/UiStore";
|
|
|
|
import Collection from "models/Collection";
|
2020-08-12 17:49:15 +00:00
|
|
|
import CollectionDelete from "scenes/CollectionDelete";
|
|
|
|
import CollectionEdit from "scenes/CollectionEdit";
|
|
|
|
import CollectionExport from "scenes/CollectionExport";
|
2020-06-20 20:59:15 +00:00
|
|
|
import CollectionMembers from "scenes/CollectionMembers";
|
2020-11-15 04:44:31 +00:00
|
|
|
import { DropdownMenu } from "components/DropdownMenu";
|
|
|
|
import DropdownMenuItems from "components/DropdownMenu/DropdownMenuItems";
|
2020-08-09 05:53:59 +00:00
|
|
|
import Modal from "components/Modal";
|
|
|
|
import VisuallyHidden from "components/VisuallyHidden";
|
2020-06-20 20:59:15 +00:00
|
|
|
import getDataTransferFiles from "utils/getDataTransferFiles";
|
2020-08-09 05:53:59 +00:00
|
|
|
import { newDocumentUrl } from "utils/routeHelpers";
|
2017-09-10 05:12:59 +00:00
|
|
|
|
2018-01-30 06:31:49 +00:00
|
|
|
type Props = {
|
2020-06-20 20:59:15 +00:00
|
|
|
position?: "left" | "right" | "center",
|
2018-01-30 06:31:49 +00:00
|
|
|
ui: UiStore,
|
2019-10-06 01:42:03 +00:00
|
|
|
policies: PoliciesStore,
|
2018-01-30 06:31:49 +00:00
|
|
|
documents: DocumentsStore,
|
|
|
|
collection: Collection,
|
2019-08-09 06:09:09 +00:00
|
|
|
history: RouterHistory,
|
2019-07-13 17:15:38 +00:00
|
|
|
onOpen?: () => void,
|
|
|
|
onClose?: () => void,
|
2020-11-30 04:04:58 +00:00
|
|
|
t: TFunction,
|
2018-01-30 06:31:49 +00:00
|
|
|
};
|
|
|
|
|
2017-11-10 22:14:30 +00:00
|
|
|
@observer
|
2018-05-05 23:16:08 +00:00
|
|
|
class CollectionMenu extends React.Component<Props> {
|
2018-11-07 05:58:32 +00:00
|
|
|
file: ?HTMLInputElement;
|
2020-08-12 17:49:15 +00:00
|
|
|
@observable showCollectionMembers = false;
|
|
|
|
@observable showCollectionEdit = false;
|
|
|
|
@observable showCollectionDelete = false;
|
|
|
|
@observable showCollectionExport = false;
|
2017-09-10 05:12:59 +00:00
|
|
|
|
2019-08-09 06:09:09 +00:00
|
|
|
onNewDocument = (ev: SyntheticEvent<>) => {
|
2017-11-16 07:49:20 +00:00
|
|
|
ev.preventDefault();
|
2019-01-19 08:23:39 +00:00
|
|
|
const { collection } = this.props;
|
2019-04-18 02:11:23 +00:00
|
|
|
this.props.history.push(newDocumentUrl(collection.id));
|
2017-09-19 07:13:49 +00:00
|
|
|
};
|
|
|
|
|
2019-08-09 06:09:09 +00:00
|
|
|
onImportDocument = (ev: SyntheticEvent<>) => {
|
2018-01-30 07:04:45 +00:00
|
|
|
ev.preventDefault();
|
2019-10-13 06:03:58 +00:00
|
|
|
ev.stopPropagation();
|
2018-01-30 07:04:45 +00:00
|
|
|
|
2018-01-30 06:31:49 +00:00
|
|
|
// simulate a click on the file upload input element
|
2018-11-07 05:58:32 +00:00
|
|
|
if (this.file) this.file.click();
|
2018-01-30 06:31:49 +00:00
|
|
|
};
|
|
|
|
|
2019-08-09 06:09:09 +00:00
|
|
|
onFilePicked = async (ev: SyntheticEvent<>) => {
|
2018-01-30 06:31:49 +00:00
|
|
|
const files = getDataTransferFiles(ev);
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
try {
|
2020-09-17 04:54:33 +00:00
|
|
|
const file = files[0];
|
|
|
|
const document = await this.props.documents.import(
|
|
|
|
file,
|
|
|
|
null,
|
|
|
|
this.props.collection.id,
|
|
|
|
{ publish: true }
|
|
|
|
);
|
2019-01-26 17:28:08 +00:00
|
|
|
this.props.history.push(document.url);
|
2018-12-05 06:24:30 +00:00
|
|
|
} catch (err) {
|
|
|
|
this.props.ui.showToast(err.message);
|
|
|
|
}
|
2018-01-30 06:31:49 +00:00
|
|
|
};
|
|
|
|
|
2020-08-12 17:49:15 +00:00
|
|
|
handleEditCollectionOpen = (ev: SyntheticEvent<>) => {
|
2017-11-16 07:49:20 +00:00
|
|
|
ev.preventDefault();
|
2020-08-12 17:49:15 +00:00
|
|
|
this.showCollectionEdit = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
handleEditCollectionClose = () => {
|
|
|
|
this.showCollectionEdit = false;
|
2017-09-10 05:12:59 +00:00
|
|
|
};
|
|
|
|
|
2020-08-12 17:49:15 +00:00
|
|
|
handleDeleteCollectionOpen = (ev: SyntheticEvent<>) => {
|
2017-11-16 07:49:20 +00:00
|
|
|
ev.preventDefault();
|
2020-08-12 17:49:15 +00:00
|
|
|
this.showCollectionDelete = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
handleDeleteCollectionClose = () => {
|
|
|
|
this.showCollectionDelete = false;
|
2017-09-10 05:12:59 +00:00
|
|
|
};
|
|
|
|
|
2020-08-12 17:49:15 +00:00
|
|
|
handleExportCollectionOpen = (ev: SyntheticEvent<>) => {
|
2018-06-21 04:33:21 +00:00
|
|
|
ev.preventDefault();
|
2020-08-12 17:49:15 +00:00
|
|
|
this.showCollectionExport = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
handleExportCollectionClose = () => {
|
|
|
|
this.showCollectionExport = false;
|
2018-06-21 04:33:21 +00:00
|
|
|
};
|
|
|
|
|
2020-08-12 17:49:15 +00:00
|
|
|
handleMembersModalOpen = (ev: SyntheticEvent<>) => {
|
2019-01-05 21:37:33 +00:00
|
|
|
ev.preventDefault();
|
2020-08-12 17:49:15 +00:00
|
|
|
this.showCollectionMembers = true;
|
2019-01-05 21:37:33 +00:00
|
|
|
};
|
|
|
|
|
2019-10-06 01:42:03 +00:00
|
|
|
handleMembersModalClose = () => {
|
2020-08-12 17:49:15 +00:00
|
|
|
this.showCollectionMembers = false;
|
2019-01-05 21:37:33 +00:00
|
|
|
};
|
|
|
|
|
2017-09-10 05:12:59 +00:00
|
|
|
render() {
|
2020-09-17 04:54:33 +00:00
|
|
|
const {
|
|
|
|
policies,
|
|
|
|
documents,
|
|
|
|
collection,
|
|
|
|
position,
|
|
|
|
onOpen,
|
|
|
|
onClose,
|
2020-11-30 04:04:58 +00:00
|
|
|
t,
|
2020-09-17 04:54:33 +00:00
|
|
|
} = this.props;
|
2019-10-06 01:42:03 +00:00
|
|
|
const can = policies.abilities(collection.id);
|
2017-09-10 05:12:59 +00:00
|
|
|
|
|
|
|
return (
|
2020-08-09 16:48:04 +00:00
|
|
|
<>
|
2019-10-13 06:03:58 +00:00
|
|
|
<VisuallyHidden>
|
|
|
|
<input
|
|
|
|
type="file"
|
2020-08-09 01:53:11 +00:00
|
|
|
ref={(ref) => (this.file = ref)}
|
2019-10-13 06:03:58 +00:00
|
|
|
onChange={this.onFilePicked}
|
2020-08-09 01:53:11 +00:00
|
|
|
onClick={(ev) => ev.stopPropagation()}
|
2020-09-17 04:54:33 +00:00
|
|
|
accept={documents.importFileTypes.join(", ")}
|
2019-10-13 06:03:58 +00:00
|
|
|
/>
|
|
|
|
</VisuallyHidden>
|
|
|
|
|
2019-01-05 21:37:33 +00:00
|
|
|
<Modal
|
2020-11-30 04:04:58 +00:00
|
|
|
title={t("Collection permissions")}
|
2019-10-06 01:42:03 +00:00
|
|
|
onRequestClose={this.handleMembersModalClose}
|
2020-08-12 17:49:15 +00:00
|
|
|
isOpen={this.showCollectionMembers}
|
2019-01-05 21:37:33 +00:00
|
|
|
>
|
2019-10-06 01:42:03 +00:00
|
|
|
<CollectionMembers
|
2019-01-05 21:37:33 +00:00
|
|
|
collection={collection}
|
2019-10-06 01:42:03 +00:00
|
|
|
onSubmit={this.handleMembersModalClose}
|
2020-08-12 17:49:15 +00:00
|
|
|
handleEditCollectionOpen={this.handleEditCollectionOpen}
|
2020-08-26 04:00:50 +00:00
|
|
|
onEdit={this.handleEditCollectionOpen}
|
2019-01-05 21:37:33 +00:00
|
|
|
/>
|
|
|
|
</Modal>
|
2019-10-06 01:42:03 +00:00
|
|
|
<DropdownMenu onOpen={onOpen} onClose={onClose} position={position}>
|
2020-11-15 04:44:31 +00:00
|
|
|
<DropdownMenuItems
|
|
|
|
items={[
|
|
|
|
{
|
2020-11-30 04:04:58 +00:00
|
|
|
title: t("New document"),
|
2020-11-15 04:44:31 +00:00
|
|
|
visible: !!(collection && can.update),
|
|
|
|
onClick: this.onNewDocument,
|
|
|
|
},
|
|
|
|
{
|
2020-11-30 04:04:58 +00:00
|
|
|
title: t("Import document"),
|
2020-11-15 04:44:31 +00:00
|
|
|
visible: !!(collection && can.update),
|
|
|
|
onClick: this.onImportDocument,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "separator",
|
|
|
|
},
|
|
|
|
{
|
2020-11-30 04:04:58 +00:00
|
|
|
title: t("Edit…"),
|
2020-11-15 04:44:31 +00:00
|
|
|
visible: !!(collection && can.update),
|
|
|
|
onClick: this.handleEditCollectionOpen,
|
|
|
|
},
|
|
|
|
{
|
2020-11-30 04:04:58 +00:00
|
|
|
title: t("Permissions…"),
|
2020-11-15 04:44:31 +00:00
|
|
|
visible: !!(collection && can.update),
|
|
|
|
onClick: this.handleMembersModalOpen,
|
|
|
|
},
|
|
|
|
{
|
2020-11-30 04:04:58 +00:00
|
|
|
title: t("Export…"),
|
2020-11-15 04:44:31 +00:00
|
|
|
visible: !!(collection && can.export),
|
|
|
|
onClick: this.handleExportCollectionOpen,
|
|
|
|
},
|
|
|
|
{
|
2020-11-30 04:04:58 +00:00
|
|
|
title: t("Delete…"),
|
2020-11-15 04:44:31 +00:00
|
|
|
visible: !!(collection && can.delete),
|
|
|
|
onClick: this.handleDeleteCollectionOpen,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
2018-01-30 06:31:49 +00:00
|
|
|
</DropdownMenu>
|
2020-08-12 17:49:15 +00:00
|
|
|
<Modal
|
2020-11-30 04:04:58 +00:00
|
|
|
title={t("Edit collection")}
|
2020-08-12 17:49:15 +00:00
|
|
|
isOpen={this.showCollectionEdit}
|
|
|
|
onRequestClose={this.handleEditCollectionClose}
|
|
|
|
>
|
|
|
|
<CollectionEdit
|
|
|
|
onSubmit={this.handleEditCollectionClose}
|
|
|
|
collection={collection}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
<Modal
|
2020-11-30 04:04:58 +00:00
|
|
|
title={t("Delete collection")}
|
2020-08-12 17:49:15 +00:00
|
|
|
isOpen={this.showCollectionDelete}
|
|
|
|
onRequestClose={this.handleDeleteCollectionClose}
|
|
|
|
>
|
|
|
|
<CollectionDelete
|
|
|
|
onSubmit={this.handleDeleteCollectionClose}
|
|
|
|
collection={collection}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
<Modal
|
2020-11-30 04:04:58 +00:00
|
|
|
title={t("Export collection")}
|
2020-08-12 17:49:15 +00:00
|
|
|
isOpen={this.showCollectionExport}
|
|
|
|
onRequestClose={this.handleExportCollectionClose}
|
|
|
|
>
|
|
|
|
<CollectionExport
|
|
|
|
onSubmit={this.handleExportCollectionClose}
|
|
|
|
collection={collection}
|
|
|
|
/>
|
|
|
|
</Modal>
|
2020-08-09 16:48:04 +00:00
|
|
|
</>
|
2017-09-10 05:12:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 04:04:58 +00:00
|
|
|
export default withTranslation()<CollectionMenu>(
|
|
|
|
inject("ui", "documents", "policies")(withRouter(CollectionMenu))
|
|
|
|
);
|