~linuxgoose/bocpress

ref: ea974b0aa571d976764ecd593298392f8a41e35d bocpress/main/templates/assets/save-snapshot.js -rw-r--r-- 1.6 KiB
ea974b0aJordan Robinson change markdown library to markdown-it-python and add Graphviz support 2 months ago
                                                                                
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
// keep timeout ids in an array so we reset them
var TIMEOUT_IDS = [];

// save post title and body as a Snapshot connected to current user
function saveLogEntry() {
    console.log("saving...");
    var title = document.getElementById('id_title').value;
    if (!title) {
        title = "Untitled"
    }
    var body = document.getElementById('id_body').value;

    // prepare form data
    var formData = new FormData();
    formData.append("title", title);
    formData.append("body", body);

    // upload request
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function alertContents() {
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                console.log("success");
                // success, show feedback
            } else {
                console.log("failure");
                // failure, show feedback
            }
        } else {
            // this branch runs first
            // uplading, show feedback
            console.log("uplading...");
        }
    };

    xhr.open('POST', '/post-backups/create/');
    xhr.setRequestHeader('X-CSRFToken', '{{ csrf_token }}');
    xhr.send(formData);
}

// clear timeout ids from given array
function clearTimeoutList(timeoutList) {
    timeoutList.forEach(function (timeoutId) {
        clearTimeout(timeoutId);
    });
}

// listen for body textarea changes
function initAutoSave() {
    document.getElementById('id_body').addEventListener('keyup', function () {
        clearTimeoutList(TIMEOUT_IDS);
        var timeoutId = setTimeout(saveLogEntry, 2500);
        TIMEOUT_IDS.push(timeoutId);
    });
}

// init
initAutoSave();