aboutsummaryrefslogtreecommitdiff
path: root/scripts/src/note-edit.js
blob: 390b93d77117b8fddd594768f8d355fc67119ea9 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function edit_note(btn, form, title, quill, qcontainer, url){
    var form_inputs = form.getElementsByTagName('input');
    var form_texts = form.getElementsByTagName('textarea');
    var tags = form.getElementsByTagName('div')['tags'];
    var tags_edit = document.getElementById('t-edit');;
    var notebook_display = document.getElementById('n-link');
    var notebook_wrapper = form.getElementsByTagName('select')['notebook'].parentNode;
    var tags_display = document.getElementById('t-display');
    var colorbox = document.getElementById('n-box');
    var fs_url = document.getElementById('fs-url');
    if (btn.editing === false) {
        title.setAttribute('contenteditable', true);
        title.classList.add('highlight');
        qcontainer.classList.remove('inactive');
        notebook_wrapper.classList.remove('hide');
        //document.getElementsByTagName('select')['notebook'].parentNode.classList.remove('hide');
        notebook_display.classList.add('hide');
        tags_display.classList.add('hide');
        tags_edit.classList.remove('hide');
        fs_url.classList.toggle('hide');
        quill.enable(true);
        btn.innerHTML = 'Save';
        btn.classList.add('save');
        btn.editing = true;
        title.titlecontents = title.innerHTML; 
    } else {
        if (quill.change === true || title.titlecontents != title.innerHTML || form.formchange) {
            form_inputs['title'].value = title.innerHTML;
            form_texts['body_html'].innerHTML = quill.root.innerHTML;
            form_texts['body_text'].innerHTML = quill.getText();
            form_texts['body_qjson'].innerHTML = JSON.stringify(quill.getContents());
            var request = new XMLHttpRequest();
            request.open('POST', url);
            var csrftoken = getCookie('csrftoken');
            request.setRequestHeader('X-CSRFToken', csrftoken);
            request.setRequestHeader('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
            request.onload = function() {
                if (request.status >= 200 && request.status < 400) {
                    var data = JSON.parse(request.responseText);
                    document.getElementById('n-box').style.backgroundColor = data['notebook']['color'];
                    notebook_display.getElementsByTagName('span')['n-name'].textContent = data['notebook']['name'];
                    var tags = JSON.parse(data['tags']);
                    while (tags_display.firstChild) {
                        tags_display.firstChild.remove();
                    }
                    for(var i in tags) {
                        var span = document.createElement('span');
                        span.classList.add('tag-wrapper');
                        span.innerHTML = tags[i]['fields']['name']
                        var link = document.createElement('a');
                        link.classList.add('t-link');
                        link.setAttribute('href', '/n/t/'+tags[i]['fields']['slug']);
                        link.appendChild(span);
                        tags_display.appendChild(link);
                        if (window.overlay !== undefined) {
                            window.overlay.destroy();
                        }
                    }
                    quill.change = false;
                } else {
                    console.log(request); 
                    console.log('server error');
                }
            };
            request.onerror = function() {
                console.log('error on request');
            };
            //create a modal to let the user know we're saving
            var saveModal = document.createElement('div');
            var loader = document.createElement('div');
            loader.classList.add('loader');
            saveModal.appendChild(loader);
            saveModal.modalHed = "Saving changes"; //modal text content
            saveModal.wrapperClass = 'loading'; //add a wrapper class to the modal for styling
            var modal = modalBox(saveModal);
            document.getElementById('close-btn').classList.add('hide');
            //send the request to the REST API
            console.log(request);
            console.log(form);
            request.send(new FormData(form));           
        }
        title.setAttribute('contenteditable', false);
        title.classList.remove('highlight');
        qcontainer.classList.add('inactive');
        quill.enable(false);
        fs_url.classList.toggle('hide');
        notebook_wrapper.classList.add('hide');
        btn.innerHTML = 'Edit';
        btn.classList.remove('save');
        tags_display.classList.remove('hide');
        tags_edit.classList.add('hide');
        notebook_display.classList.remove('hide');
        document.body.focus();
        btn.editing = false;
    }
    return false;
}
function notebookCreate(form) {
    var request = new XMLHttpRequest();
    var url = form.action
    request.open('POST', url);
    var csrftoken = getCookie('csrftoken');
    request.setRequestHeader('X-CSRFToken', csrftoken);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    request.onload = function() {
        if (request.status >= 200 && request.status < 400) {
            var data = JSON.parse(request.responseText);

            // close modal if exists
            if (window.overlay !== undefined) {
                window.overlay.destroy();
            }
            var select = document.getElementById('id_notebook');
            var parentForm = document.getElementById('note-edit-form');
            select.options.add(new Option(data['name'], data['id'], false, true));
            if (parentForm !== null) {
                parentForm.formchange = true;
            }
        } else {
            var data = JSON.parse(request.responseText);
            console.log(data);
            if (data['non_field_errors'][0] === "The fields owner, name must make a unique set.") {
                document.getElementById('non-field-errors').innerHTML = "A Notebook with that name already exists, please choose another.";
            }
            console.log('server error');
        }
    };
    request.onerror = function() {
        console.log('error on request');
    };
    request.send(new FormData(form));           
}
if (typeof(document.getElementById('note-edit-form')) != 'undefined' && document.getElementById('note-edit-form') != null) {
    document.addEventListener("DOMContentLoaded", function () {
        var quill = initQuill("#note-body");
        quill.change = false;
        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.formchange = false;
        form.addEventListener('input', function (e) {
            form.formchange = true;
        });
        var formbtn = document.getElementById('btn-js-hide')
        formbtn.classList.add('hide');
        console.log(formbtn);
        btn.classList.remove('hide');
        btn.editing = false;
        btn.addEventListener('click', function(e){
            e.preventDefault();
            edit_note(e.target, form, title, quill, qcontainer, window.location.href );
        }, false);
        var notebookAddForm = document.getElementById('nb-create-form');
        initColorPicker(notebookAddForm);    
        ajaxHijack(notebookAddForm, notebookCreate)
        var create_notebook_btn = document.getElementById('add_id_notebook');
        addHandler(create_notebook_btn);
        function addHandler(el){
            el.addEventListener('click', function(e){
                e.preventDefault();
                var modalContent = document.getElementById('js-overlay-notebook');
                modalContent.wrapperClass = 'overlay'; //add a wrapper class to the modal for styling
                var modal = modalBox(modalContent, el);
            });
        }
    });
}