2017-05-23 07:45:15 +00:00
|
|
|
// @flow
|
2020-12-31 20:51:12 +00:00
|
|
|
import { pick, trim } from "lodash";
|
2020-06-20 20:59:15 +00:00
|
|
|
import { action, computed, observable } from "mobx";
|
|
|
|
import BaseModel from "models/BaseModel";
|
|
|
|
import Document from "models/Document";
|
|
|
|
import type { NavigationNode } from "types";
|
2020-08-09 05:53:59 +00:00
|
|
|
import { client } from "utils/ApiClient";
|
2017-05-23 07:45:15 +00:00
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
export default class Collection extends BaseModel {
|
2019-01-05 21:37:33 +00:00
|
|
|
@observable isSaving: boolean;
|
|
|
|
@observable isLoadingUsers: boolean;
|
2017-07-09 17:27:29 +00:00
|
|
|
|
2017-05-23 07:45:15 +00:00
|
|
|
id: string;
|
|
|
|
name: string;
|
2018-12-05 06:24:30 +00:00
|
|
|
description: string;
|
2020-06-20 00:18:03 +00:00
|
|
|
icon: string;
|
2017-10-30 06:22:46 +00:00
|
|
|
color: string;
|
2019-01-05 21:37:33 +00:00
|
|
|
private: boolean;
|
2018-05-08 05:08:47 +00:00
|
|
|
documents: NavigationNode[];
|
2018-12-05 06:24:30 +00:00
|
|
|
createdAt: ?string;
|
|
|
|
updatedAt: ?string;
|
2019-04-18 02:11:23 +00:00
|
|
|
deletedAt: ?string;
|
2020-12-31 20:51:12 +00:00
|
|
|
sort: { field: string, direction: "asc" | "desc" };
|
2017-05-23 07:45:15 +00:00
|
|
|
url: string;
|
|
|
|
|
2019-04-09 04:25:13 +00:00
|
|
|
@computed
|
|
|
|
get isPrivate(): boolean {
|
|
|
|
return this.private;
|
|
|
|
}
|
|
|
|
|
2017-11-20 04:16:49 +00:00
|
|
|
@computed
|
|
|
|
get isEmpty(): boolean {
|
|
|
|
return this.documents.length === 0;
|
|
|
|
}
|
|
|
|
|
2017-11-28 16:55:15 +00:00
|
|
|
@computed
|
|
|
|
get documentIds(): string[] {
|
|
|
|
const results = [];
|
|
|
|
const travelDocuments = (documentList, path) =>
|
2020-08-09 01:53:11 +00:00
|
|
|
documentList.forEach((document) => {
|
2017-11-28 16:55:15 +00:00
|
|
|
results.push(document.id);
|
|
|
|
travelDocuments(document.children);
|
|
|
|
});
|
|
|
|
|
|
|
|
travelDocuments(this.documents);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2020-12-31 20:51:12 +00:00
|
|
|
@computed
|
2021-01-02 17:09:06 +00:00
|
|
|
get hasDescription(): boolean {
|
2020-12-31 20:51:12 +00:00
|
|
|
return !!trim(this.description, "\\").trim();
|
|
|
|
}
|
|
|
|
|
2018-05-08 05:08:47 +00:00
|
|
|
@action
|
|
|
|
updateDocument(document: Document) {
|
|
|
|
const travelDocuments = (documentList, path) =>
|
2020-08-09 01:53:11 +00:00
|
|
|
documentList.forEach((d) => {
|
2018-05-08 05:08:47 +00:00
|
|
|
if (d.id === document.id) {
|
|
|
|
d.title = document.title;
|
|
|
|
d.url = document.url;
|
|
|
|
} else {
|
|
|
|
travelDocuments(d.children);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
travelDocuments(this.documents);
|
|
|
|
}
|
|
|
|
|
2019-10-08 15:01:30 +00:00
|
|
|
getDocumentChildren(documentId: string): NavigationNode[] {
|
|
|
|
let result = [];
|
2020-08-09 01:53:11 +00:00
|
|
|
const traveler = (nodes) => {
|
|
|
|
nodes.forEach((childNode) => {
|
2019-10-08 15:01:30 +00:00
|
|
|
if (childNode.id === documentId) {
|
|
|
|
result = childNode.children;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return traveler(childNode.children);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.documents) {
|
|
|
|
traveler(this.documents);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-12-16 03:07:29 +00:00
|
|
|
pathToDocument(documentId: string) {
|
2019-04-18 02:11:23 +00:00
|
|
|
let path;
|
|
|
|
const traveler = (nodes, previousPath) => {
|
2020-08-09 01:53:11 +00:00
|
|
|
nodes.forEach((childNode) => {
|
2019-04-18 02:11:23 +00:00
|
|
|
const newPath = [...previousPath, childNode];
|
2020-12-16 03:07:29 +00:00
|
|
|
if (childNode.id === documentId) {
|
2019-04-18 02:11:23 +00:00
|
|
|
path = newPath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return traveler(childNode.children, newPath);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.documents) {
|
|
|
|
traveler(this.documents, []);
|
|
|
|
if (path) return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
toJS = () => {
|
2020-06-20 00:18:03 +00:00
|
|
|
return pick(this, [
|
2020-06-20 20:59:15 +00:00
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
"color",
|
|
|
|
"description",
|
|
|
|
"icon",
|
|
|
|
"private",
|
2020-12-31 20:51:12 +00:00
|
|
|
"sort",
|
2020-06-20 00:18:03 +00:00
|
|
|
]);
|
2017-05-23 07:45:15 +00:00
|
|
|
};
|
|
|
|
|
2018-12-05 06:24:30 +00:00
|
|
|
export = () => {
|
2019-07-30 05:35:34 +00:00
|
|
|
return client.get(
|
2020-06-20 20:59:15 +00:00
|
|
|
"/collections.export",
|
2019-07-30 05:35:34 +00:00
|
|
|
{ id: this.id },
|
|
|
|
{ download: true }
|
|
|
|
);
|
2018-06-21 04:33:21 +00:00
|
|
|
};
|
2017-05-23 07:45:15 +00:00
|
|
|
}
|