xref: /plugin/discussion/script.js (revision 93e5c891ce5cfd6a24b157583d4fe6d59f5ee3ee)
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.mail.value) || form.mail.value.indexOf("@") == -1){
29        form.mail.style.class = form.mail.style.class + ' error';
30        form.mail.focus();
31        return false;
32    }
33    if (isBlank(form.name.value)){
34        form.name.focus();
35        return false;
36    }
37    if (isBlank(form.text.value)){
38        form.text.focus();
39        return false;
40    }
41}
42
43/**
44 * AJAX preview
45 *
46 * @author Michael Klier <chi@chimeric.de>
47 */
48function discussion_ajax_preview() {
49    if(!document.getElementById) return;
50
51    var textarea = $('discussion__comment_text');
52    var comment = textarea.value;
53    if(!comment) return;
54
55    var preview = $('discussion__comment_preview');
56    var wikisyntaxok = $('discussion__comment_wikisyntaxok');
57
58    // We use SACK to do the AJAX requests
59    var ajax = new sack(DOKU_BASE+'lib/plugins/discussion/ajax.php');
60    ajax_qsearch.sack.AjaxFailedAlert = '';
61    ajax_qsearch.sack.encodeURIString = false;
62
63    // define callback
64    ajax.onCompletion = function(){
65        var data = this.response;
66        if(data === ''){ return; }
67        preview.style.visibility = 'hidden';
68        preview.innerHTML = data;
69        preview.style.visibility = 'visible';
70    };
71
72    ajax.runAJAX('comment='+comment+'&wikisyntaxok='+wikisyntaxok.value);
73}
74
75// init toolbar
76addInitEvent(function() {initToolbar("discussion__comment_toolbar", "discussion__comment_text", toolbar)});
77
78// init preview button
79addInitEvent(function() {
80    var btn = $('discussion__btn_preview');
81    if(!btn) return;
82    addEvent(btn, 'click', discussion_ajax_preview);
83});
84
85// init field check
86addInitEvent(function() {
87    var form = $('discussion__comment_form');
88    if(!form) return;
89    addEvent(form, 'submit', function() { return validate(form); });
90});
91