xref: /plugin/discussion/script.js (revision bea6926435b9d27494b4726e56f27bd5811496cd)
1/**
2 * ehck if a field is blank
3 */
4function isBlank(s){
5  if ((s === null) || (s.length === 0)){
6    return true;
7  }
8
9  for (var i = 0; i < s.length; i++){
10    var c = s.charAt(i);
11	  if ((c != ' ') && (c != '\n') && (c != '\t')){
12	    return false;
13    }
14  }
15  return true;
16}
17
18/**
19 * Validate an input field
20 */
21function validate(form){
22    if(!form) return;
23
24    if (isBlank(form.mail.value) || form.mail.value.indexOf("@") == -1){
25        form.mail.focus();
26        return false;
27    }
28    if (isBlank(form.name.value)){
29        form.name.focus();
30        return false;
31    }
32    if (isBlank(form.text.value)){
33        form.text.focus();
34        return false;
35    }
36}
37
38/**
39 * AJAX preview
40 *
41 * @author Michael Klier <chi@chimeric.de>
42 */
43function discussion_ajax_preview() {
44    if(!document.getElementById) return;
45
46    var textarea = $('discussion__comment_text');
47    var comment = textarea.value;
48    if(!comment) return;
49
50    var preview = $('discussion__comment_preview');
51    var wikisyntaxok = $('discussion__comment_wikisyntaxok');
52
53    // We use SACK to do the AJAX requests
54    var ajax = new sack(DOKU_BASE+'lib/plugins/discussion/ajax.php');
55    ajax_qsearch.sack.AjaxFailedAlert = '';
56    ajax_qsearch.sack.encodeURIString = false;
57
58    // define callback
59    ajax.onCompletion = function(){
60        var data = this.response;
61        if(data === ''){ return; }
62        preview.style.visibility = 'hidden';
63        preview.innerHTML = data;
64        preview.style.visibility = 'visible';
65    };
66
67    ajax.runAJAX('comment='+comment+'&wikisyntaxok='+wikisyntaxok.value);
68}
69
70// init toolbar
71addInitEvent(function() {initToolbar("discussion__comment_toolbar", "discussion__comment_text", toolbar)});
72
73// init preview button
74addInitEvent(function() {
75    var btn = $('discussion__btn_preview');
76    if(!btn) return;
77    addEvent(btn, 'click', discussion_ajax_preview);
78});
79
80// init field check
81addInitEvent(function() {
82    var form = $('discussion__comment_form');
83    if(!form) return;
84    addEvent(form, 'submit', function() { return validate(form); });
85});
86