xref: /plugin/discussion/script.js (revision 7b735e0c846a97dd6ef881eb40e4da588b4e255c)
1/**
2 * Javascript functionality for the discussion plugin
3 */
4
5/**
6 * Check if a field is blank
7 */
8function isBlank(s) {
9    if ((s === null) || (s.length === 0)) {
10        return true;
11    }
12
13    for (let i = 0; i < s.length; i++) {
14        let c = s.charAt(i);
15        if (c !== ' ' && c !== '\n' && c !== '\t') {
16            return false;
17        }
18    }
19    return true;
20}
21
22/**
23 * Validate an input field
24 */
25function validate(form) {
26    if (!form) return;
27
28    if (isBlank(form.name.value)) {
29        form.name.focus();
30        form.name.style.backgroundColor = '#fcc';
31        return false;
32    } else {
33        form.name.style.backgroundColor = '#fff';
34    }
35    if (isBlank(form.mail.value) || form.mail.value.indexOf("@") === -1) {
36        form.mail.focus();
37        form.mail.style.backgroundColor = '#fcc';
38        return false;
39    } else {
40        form.mail.style.backgroundColor = '#fff';
41    }
42    if (isBlank(form.text.value)) {
43        form.text.focus();
44        form.text.style.borderColor = '#fcc';
45        return false;
46    }
47}
48
49/**
50 * AJAX preview
51 *
52 * @author Michael Klier <chi@chimeric.de>
53 */
54function discussion_ajax_preview() {
55    let $textarea = jQuery('#discussion__comment_text');
56    let comment = $textarea.val();
57
58    let $preview = jQuery('#discussion__comment_preview');
59    if (!comment) {
60        $preview.hide();
61        return;
62    }
63    $preview.html('<img src="' + DOKU_BASE + 'lib/images/throbber.gif" />');
64    $preview.show();
65
66    jQuery.post(DOKU_BASE + 'lib/exe/ajax.php',
67        {
68            'call':    'discussion_preview',
69            'comment': comment
70        },
71        function (data) {
72            if (data === '') {
73                $preview.hide();
74                return;
75            }
76            $preview.html(data);
77            $preview.show();
78            $preview.css('visibility', 'visible');
79            $preview.css('display', 'inline');
80        }, 'html');
81}
82
83jQuery(function () {
84    // init toolbar
85    if (typeof window.initToolbar == 'function') {
86        initToolbar("discussion__comment_toolbar", "discussion__comment_text", toolbar);
87    }
88
89    // init preview button
90    jQuery('#discussion__btn_preview').on('click', discussion_ajax_preview);
91
92    // init field check
93    jQuery('#discussion__comment_form').on('submit', function () {
94        return validate(this);
95    });
96
97    //confirm delete actions
98    jQuery('input.dcs_confirmdelete').on('click', function () {
99        return confirm(LANG.plugins.discussion.confirmdelete);
100    });
101
102    // toggle section visibility
103    jQuery('#discussion__btn_toggle_visibility').on('click', function () {
104        jQuery('#comment_wrapper').toggle();
105    });
106});
107