aboutsummaryrefslogtreecommitdiff
path: root/scripts/src/note-edit.js
blob: c9126f1dd022cb3a0ee35988a138355056a77f02 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
function edit_note(btn, form, title, qcontainer, url){
    var form_inputs = form.getElementsByTagName('input');
    var notebook_display = document.getElementById('n-link');
    var tags_display = document.getElementById('t-display');
    var tags_edit = form.getElementsByTagName('input')['tags']
    var form_texts = form.getElementsByTagName('textarea');
    var colorbox = document.getElementById('n-box');
    if (window.editing === false) {
        title.setAttribute('contenteditable', true);
        title.classList.add('highlight');
        qcontainer.classList.remove('inactive');
        form.getElementsByTagName('select')['notebook'].parentNode.classList.remove('hide');
        document.getElementsByTagName('select')['notebook'].parentNode.classList.remove('hide');
        notebook_display.classList.add('hide');
        tags_display.classList.add('hide');
        tags_edit.parentNode.classList.remove('hide');
        window.quill.enable(true);
        btn.innerHTML = 'Save';
        btn.classList.add('save');
        window.editing = true;
        window.titlecontents = title.innerHTML; 
    } else {
        if (window.quillchange === true || window.titlecontents != title.innerHTML || window.formchange) {
            form_inputs['title'].value = title.innerHTML;
            form_texts['body_html'].innerHTML = window.quill.root.innerHTML;
            form_texts['body_text'].innerHTML = window.quill.getText();
            form_texts['body_qjson'].innerHTML = JSON.stringify(window.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) {
                    var data = JSON.parse(request.responseText);
                    var request2 = new XMLHttpRequest();
                    request2.open('GET', '/api/v1/notes/notebook/'+data['notebook']+'/');
                    request2.setRequestHeader('X-CSRFToken', csrftoken);
                    request2.onload = function() {
                        var data2 = JSON.parse(request2.responseText);
                        var select = form.getElementsByTagName('select')['notebook'];
                        document.getElementById('n-box').style.backgroundColor = data2['color_rgb'];
                        notebook_display.getElementsByTagName('span')['n-name'].textContent = select.item(select.selectedIndex).innerHTML;
                    }
                    request2.send();
                    window.quillchange = false;
                } else {
                    console.log(request); 
                    console.log('server error');
                }
            };
            request.onerror = function() {
                console.log('error on request');
            };
            request.send(new FormData(form));           
        }
        title.setAttribute('contenteditable', false);
        title.classList.remove('highlight');
        qcontainer.classList.add('inactive');
        window.quill.enable(false);
        form.getElementsByTagName('select')['notebook'].parentNode.classList.add('hide');
        btn.innerHTML = 'Edit';
        btn.classList.remove('save');
        tags_display.classList.remove('hide');
        tags_edit.parentNode.classList.add('hide');
        notebook_display.classList.remove('hide');
        document.body.focus();
        window.editing = false;
    }
    return false;
}

if (typeof(document.getElementById('note-edit-form')) != "undefined" && document.getElementById('note-edit-form') != null) {
    var btn = document.getElementById('edit-toggle-btn'),
        qcontainer = document.getElementById('q-container'),
        title = document.getElementById('note-title'),
        tag_wrapper = document.getElementById('tag-wrapper'),
        form = document.getElementById('note-edit-form'),
        note_html = document.createElement('textarea'),
        note_qjson = document.createElement('textarea');

    note_html.setAttribute('name', 'body_html');
    note_html.setAttribute('class', 'hide');
    note_html.setAttribute('id', 'id_body_html');
    note_qjson.setAttribute('name', 'body_qjson');
    note_qjson.setAttribute('id', 'id_body_qjson');
    note_qjson.setAttribute('class', 'hide');
    form.appendChild(note_html);
    form.appendChild(note_qjson);
    form.addEventListener('input', function () {
        window.formchange = true;
    });
    document.getElementById('btn-js-hide').classList.add('hide');
    btn.classList.remove('hide');
    btn.addEventListener('click', function(e){
        e.preventDefault();
        edit_note(this, form, title, qcontainer, window.url);
    }, false);
    window.editing = false;
    window.quillchange = false;
}