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/** 84 * Toggle the visibility of the discussion section 85 */ 86function discussion_toggle_visibility() { 87 discussion_section = $('comment_wrapper'); 88 if(discussion_section.style.display == "none") { 89 discussion_section.style.display = "block"; 90 } else { 91 discussion_section.style.display = "none"; 92 } 93} 94 95// init toolbar 96addInitEvent(function() { 97 if(typeof window.initToolbar == 'function') { 98 initToolbar("discussion__comment_toolbar", "discussion__comment_text", toolbar); 99 } 100}); 101 102// init preview button 103addInitEvent(function() { 104 var btn = $('discussion__btn_preview'); 105 if(!btn) return; 106 addEvent(btn, 'click', discussion_ajax_preview); 107}); 108 109// init field check 110addInitEvent(function() { 111 var form = $('discussion__comment_form'); 112 if(!form) return; 113 addEvent(form, 'submit', function() { return validate(form); }); 114}); 115 116// toggle section visibility 117addInitEvent(function() { 118 var togglebtn = $('discussion__btn_toggle_visibility'); 119 if(!togglebtn) return; 120 addEvent(togglebtn, 'click', discussion_toggle_visibility); 121}) 122