aboutsummaryrefslogtreecommitdiff
path: root/scripts/src/util.js
blob: 144deae95489d1d073e269d512fffbefbee91a51 (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
183
184
function buildComponent(tag, id, html){
    var el = document.createElement(tag.toLowerCase());
    if(id) el.id = id;
    if(typeof html === 'string') el.innerHTML = html;
    if(typeof html === 'object') el.appendChild(html);
    return el;
};

function getJSON(method, url, callback) {
    var request = new XMLHttpRequest();
    request.addEventListener('load', callback);
    request.open(method, url, true);
    request.onload = function() {
        if (request.status >= 200 && request.status < 400) {
            //console.log(request.responseText);
        } else {
            console.log('server error');
        }
    };
    request.onerror = function() {
        console.log('error on request');
    };
    request.send();
}

// hijack a form and submit with ajax
function ajaxHijack(form, func) {
    form.onsubmit = function(e) {
        e.preventDefault();
        func(e.target);
        return false;
    }
}

//global init for Color Picker
function initColorPicker(form){
    var notebook_form_inputs = form.getElementsByTagName('input');
    var p = form.getElementsByTagName('fieldset')['color-picker'];
    //document.getElementById('js-novi-backdrop').removeEventListener("click", handleMouseDown, true); 
    p.classList.add('top');
    p.popup = p.popup || new Picker({
        parent: p,
        color: 'blue',
        alpha: false,
        //editor: false,
        editorFormat: 'hex',
        onDone: function(color) {
            this.settings['parent'].style.backgroundColor = color.rgbString;
            notebook_form_inputs['color_rgb'].value = color.rgbString;
        },
    });
}

function get_login_form() {
    getJSON('GET', '/login/', function(e){
        console.log(e); 
    });
}

//first define your getCookie function
function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

//Global init for Quill 
function initQuill(el) {
    var container = document.querySelector(el);
    const q = new Quill(container, {
        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
    });
    var t = Quill.find(container)
    q.on('text-change', function(delta, oldDelta, source) {
        t.change = true;
    });
    return t;
}

//global modal function, give a DOM element, puts it in a model box.
function modalBox(content, btn){
    // if passed a btn object get data from that, otherwise check the content object
    if (btn) {
        var type = btn.dataset.modalHedClass;
        var headline = btn.dataset.modalHed;
    } else {
        var type = content.hedClass || ''; 
        var headline = content.modalHed || '';
    }
    if (window.elemsRemoved !== undefined) {
        content = window.elemsRemoved;
    }
    var options = {
        type : type, 
        headline : headline,
        content : content,
        cancelText : 'X',
        confirmText : 'OK',
        cancelAction : function(){},
        confirmAction : function(){},
        reject : false,
        class : content.wrapperClass
    };
    //build the modal
    console.log(options.class);
    var overlay = buildComponent('div', options.class);
    var outermodal = buildComponent('div', 'overlay-wrapper');
    var innermodal = buildComponent('div', 'modal');
    var headline = buildComponent('header', false, options.headline);
    headline.className = 'hed';
    content.classList.remove('hide');
    var content = buildComponent('div', 'content-wrapper', options.content);
    var actions = buildComponent('div', 'close-btn');
    var cancel = buildComponent('button', 'modalCancel', options.cancelText);
    cancel.classList.add('btn', 'btn-hollow', 'btn-light');
    actions.appendChild(cancel);
    var hed_wrapper = buildComponent('div', 'hed-wrapper');
    hed_wrapper.appendChild(headline);
    hed_wrapper.appendChild(actions); //actions includes action buttons
    innermodal.appendChild(hed_wrapper);
    innermodal.appendChild(content);
    overlay.appendChild(outermodal);
    outermodal.appendChild(innermodal); //innermodal includes headline & content
    document.body.appendChild(overlay);
    window.overlay = overlay;
    //handle removing modals 
    overlay.destroy = function(){
        if(overlay.parentNode === document.body) {
            var wrapper = document.querySelector('#content-wrapper').firstChild;
            window.elemsRemoved = wrapper;
            wrapper.remove();
            overlay.parentNode.removeChild(overlay);
        }
    }
    //add event handlers
    overlay.addEventListener('click', function(e){
        if(e.target === overlay){
            options.cancelAction();
            overlay.destroy();
        }
    });
  
    cancel.addEventListener('click', function(e){
        options.cancelAction();
        overlay.destroy();
    });
    return overlay;
};

/**
 * Check whether editor content is empty or not.
 * @return {Bool}
 */
function isQuillEmpty(quill) {
    return quill.getContents().ops[0].insert == '\n' && quill.getLength() < 2;
}
function isEmpty(quill) {
    const commentText = quill.getText().trim();
    console.log(commentText);
    const re = /^<p>(<br>|<br\/>|<br\s\/>|\s+|\r)<\/p>$/gm;
    return re.test(commentText);
};