aboutsummaryrefslogtreecommitdiffstats
path: root/src/wwwroot/scripts/back/dokumenter.js
blob: c92bdbf48e0a54b9cb74b71956058bb5584b5d79 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {$, pageInit, toaster} from "../base";
import Quill from "../vendor/quill";
import {uploadDocumentImages, getDocument, setDocument} from "../api/documents-api";
import {configuration} from "../configuration";
import {utilites} from "../utilities";
import {strings} from "../i10n";

if (location.pathname.startsWith("/kontoret/dokumenter")) {
    const documentSelect = $("#document-selector");
    const publishButton = $("#publish-button");
    const toolbarOptions = [["link", "image"],
        ["bold", "italic", "underline", "strike"],
        ["blockquote", "code-block"],
        [{"header": 1}, {"header": 2}],
        [{"list": "ordered"}, {"list": "bullet"}],
        [{"script": "sub"}, {"script": "super"}],
        [{"indent": "-1"}, {"indent": "+1"}],
        [{"direction": "rtl"}],
        [{"size": ["small", false, "large", "huge"]}],
        [{"header": [1, 2, 3, 4, 5, 6, false]}],
        [{"color": []}, {"background": []}],
        [{"font": []}],
        [{"align": []}],
        ["clean"]];


    pageInit(() => {
        documentSelect.onchange = () => setDocumentContent();
        publishButton.onclick = () => publishDocument();
        const editor = new Quill("#editor", {
            theme: "snow",
            modules: {
                toolbar: toolbarOptions,
                blotFormatter: true,
                imageDrop: true,
                imageUploader: {
                    upload: (file) => new Promise(((resolve, reject) => {
                        uploadDocumentImages([file]).then(res => {
                            if (res.ok) {
                                res.json().then(fileNames => {
                                    fileNames = utilites.resolveReferences(fileNames);
                                    resolve(configuration.paths.documents + fileNames[0]);
                                });
                            } else {
                                reject(res);
                            }
                        }).catch(error => {
                            reject(error);
                        });
                    })),
                },
            },
        });

        function publishDocument() {
            const html = editor.container.querySelector(".ql-editor").innerHTML;
            if (!html || html === "<p><br></p>") return;
            setDocument(documentSelect.value, html).then(res => {
                if (res.ok) {
                    toaster.success("Dokumentet er publisert");
                } else {
                    utilites.handleError(res, {
                        title: "Kunne ikke publisere dokumentet",
                        message: "Prøv igjen senere",
                    });
                }
            }).catch(error => {
                console.error(error);
                toaster.errorObj({
                    title: strings.languageSpesific.could_not_reach_server,
                    message: strings.languageSpesific.try_again_soon,
                });
            });
        }

        function setDocumentContent() {
            getDocument(documentSelect.value).then(res => {
                if (res.ok) {
                    res.text().then(content => {
                        editor.setText("");
                        if (content) {
                            editor.clipboard.dangerouslyPasteHTML(0, content);
                        }
                    });
                } else {
                    utilites.handleError(res, {
                        title: strings.languageSpesific.an_error_occured,
                        message: strings.languageSpesific.try_again_soon,
                    });
                }
            }).catch(error => {
                console.error(error);
                toaster.errorObj({
                    title: strings.languageSpesific.could_not_reach_server,
                    message: strings.languageSpesific.try_again_soon,
                });
            });
        }

        setDocumentContent();
    });
}