xref: /plugin/discussion/script.js (revision b227f7c83a5a56f80a86f28b835124ece71c1d11)
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 (var i = 0; i < s.length; i++){
14    var 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    if(!document.getElementById) return;
56
57    var textarea = $('discussion__comment_text');
58    var comment = textarea.value;
59    if(!comment) return;
60
61    var preview = $('discussion__comment_preview');
62    var wikisyntaxok = $('discussion__comment_wikisyntaxok');
63
64    // We use SACK to do the AJAX requests
65    var ajax = new sack(DOKU_BASE+'lib/plugins/discussion/ajax.php');
66    ajax_qsearch.sack.AjaxFailedAlert = '';
67    ajax_qsearch.sack.encodeURIString = false;
68
69    // define callback
70    ajax.onCompletion = function(){
71        var data = this.response;
72        if(data === ''){ return; }
73        preview.style.visibility = 'hidden';
74        preview.innerHTML = data;
75        preview.style.visibility = 'visible';
76    };
77
78    ajax.runAJAX('comment='+comment+'&wikisyntaxok='+wikisyntaxok.value);
79}
80
81// init toolbar
82addInitEvent(function() {
83    if(typeof window.initToolbar == 'function') {
84        initToolbar("discussion__comment_toolbar", "discussion__comment_text", toolbar)
85    }
86});
87
88// init preview button
89addInitEvent(function() {
90    var btn = $('discussion__btn_preview');
91    if(!btn) return;
92    addEvent(btn, 'click', discussion_ajax_preview);
93});
94
95// init field check
96addInitEvent(function() {
97    var form = $('discussion__comment_form');
98    if(!form) return;
99    addEvent(form, 'submit', function() { return validate(form); });
100});
101