xref: /plugin/discussion/script.js (revision f3dc5acf941f5cf5bd2d2d0332d4d15fc7c8be7f)
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    preview.innerHTML = '<img src="'+DOKU_BASE+'/lib/images/throbber.gif" />';
63
64    // We use SACK to do the AJAX requests
65    var ajax = new sack(DOKU_BASE+'lib/exe/ajax.php');
66    ajax.AjaxFailedAlert = '';
67    ajax.encodeURIString = false;
68    ajax.setVar('call', 'discussion_preview');
69    ajax.setVar('comment', comment);
70
71    // define callback
72    ajax.onCompletion = function(){
73        var data = this.response;
74        if(data === ''){ return; }
75        preview.style.visibility = 'hidden';
76        preview.innerHTML = data;
77        preview.style.visibility = 'visible';
78    };
79
80    ajax.runAJAX();
81}
82
83// init toolbar
84addInitEvent(function() {
85    if(typeof window.initToolbar == 'function') {
86        initToolbar("discussion__comment_toolbar", "discussion__comment_text", toolbar)
87    }
88});
89
90// init preview button
91addInitEvent(function() {
92    var btn = $('discussion__btn_preview');
93    if(!btn) return;
94    addEvent(btn, 'click', discussion_ajax_preview);
95});
96
97// init field check
98addInitEvent(function() {
99    var form = $('discussion__comment_form');
100    if(!form) return;
101    addEvent(form, 'submit', function() { return validate(form); });
102});
103