aboutsummaryrefslogtreecommitdiff
path: root/scripts/util.js
blob: 99a5ef3484ce65a4bdf08a55f68aba2315ac1e27 (plain)
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
function edit_note(btn, title, qcontainer, quill, url){
    console.log(editing);
    var formElement = document.querySelector("form");
    if (editing === false) {
        title.setAttribute("contenteditable", true);
        title.classList.add('highlight')
        qcontainer.classList.remove('inactive')
        quill.enable(true);
        btn.innerHTML = "Save"
        editing = true;
    } else {
        if (window.quillchange === true) {
            var form_note_title = document.getElementById('id_title');
            var note_html = document.getElementById('id_body_html');
            var note_qjson = document.getElementById('id_body_qjson');
            form_note_title.value = title.innerHTML;
            note_html.innerHTML = quill.root.innerHTML;
            note_qjson.innerHTML = JSON.stringify(quill.getContents());
            var request = new XMLHttpRequest();
            request.open("PATCH", url);
            var csrftoken = Cookies.get('csrftoken');
            request.setRequestHeader("X-CSRFToken", csrftoken)
            request.onload = function() {
                if (request.status >= 200 && request.status < 400) {
                    console.log(request); 
                    window.quillchange = false;
                } else {
                    console.log(request); 
                    console.log("server error");
                }
            };
            request.onerror = function() {
                console.log("error on request");
            };
            request.send(new FormData(formElement));           
        }
        title.setAttribute("contenteditable", false);
        title.classList.remove('highlight')
        qcontainer.classList.add('inactive');
        quill.enable(false);
        btn.innerHTML = "Edit"
        document.body.focus();
        editing = false;
    }
    return false;
}


function get_login_form() {
    var request = new XMLHttpRequest();
    request.open('GET', '/login/', true);
    request.onload = function() {
        if (request.status >= 200 && request.status < 400) {
        } else {
            console.log("server error");
        }
    };
    request.onerror = function() {
        console.log("error on request");
    };
    request.send();
}

//Global init for Quill 
function initQuill(el) {
    window.quill = new Quill(el, {
        modules: {
            syntax: true,              // Include syntax module
            toolbar: [
                [{ header: [1, 2, 3, 4, false] }],
                ['bold', 'italic', 'underline', 'blockquote'],
                [{ 'list': 'bullet'}, { 'list': 'ordered'},{ 'list': 'check'} ],
                ['link', 'code-block', 'image', 'video', 'formula',],
                [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
                [{ 'font': [] }],
            ] 
        },
        theme: 'snow',
        enable: false
    });
    window.quill.on('text-change', function() {
        window.quillchange = true;
    });
}