1/** 2 * @license http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.html 3 * @author Francois Merciol <dokuplugin@merciol.fr> 4 * 5 * Javascript functionality for the glossary plugin 6 */ 7 8// ======================================== 9// Util 10// ======================================== 11function trim (str) { 12 return str.replace(/^\s+/g,'').replace(/\s+$/g,'') ; 13} 14 15function glossaryClearMsgJ (item) { 16 // efface le popup 17 item.closest ("div").find ("div.popup").each (function () { 18 jQuery (this).remove (); 19 }); 20} 21 22function glossaryZebraJ (jTable) { 23 // alterne les couleurs des lignes du tableau 24 var oddEven = new Array ("odd", "even"); 25 var even = 0; 26 jTable.find ("tr").each (function () { 27 var item = jQuery (this); 28 if (item.hasClass ("title")) 29 return; 30 if (item.css ("display") == "none") 31 return; 32 this.className = oddEven [even]; 33 even = 1 - even; 34 }); 35} 36 37// ======================================== 38// Clear form 39// ======================================== 40function glossaryReset (input) { 41 42 // met à blanc le formulaire 43 var jForm = jQuery (input).closest ("form"); 44 jForm.find ("input[type=text]").each (function () { 45 this.defaultValue = ""; 46 jQuery (this).val (""); 47 }); 48 jForm.find ("input[type=textarea]").each (function () { 49 this.defaultValue = ""; 50 jQuery (this).val (""); 51 }); 52 glossaryClearMsgJ (jForm); 53} 54 55function glossaryUpdateProposalLabel (form) { 56 form.elements[4].value = LANG["plugins"]["glossary"][form.elements[7].value ? "update" : "proposal"]; 57} 58 59// ======================================== 60// Glossary Util 61// ======================================== 62function glossaryComparatorWord (a, b) { 63 return glossaryComparator (a, b, "word", false); 64} 65 66function glossaryComparatorTranslate (a, b) { 67 return glossaryComparator (a, b, "translate", false); 68} 69 70function glossaryComparatorDate (a, b) { 71 return -glossaryComparator (a, b, "date", false); 72} 73 74function glossaryComparatorView (a, b) { 75 return -glossaryComparator (a, b, "view", true); 76} 77 78function glossaryComparatorScore (a, b) { 79 return -glossaryComparator (a, b, "score", false); 80} 81 82function glossaryComparator (a, b, name, num) { 83 var valA = ""; 84 for (var i = 0; i < a.attributes.length; i++) 85 if (a.attributes [i].name == name) { 86 valA = a.attributes [i].nodeValue; 87 break; 88 } 89 var valB = ""; 90 for (var i = 0; i < b.attributes.length; i++) 91 if (b.attributes [i].name == name) { 92 valB = b.attributes [i].nodeValue; 93 break; 94 } 95 if (num) { 96 if (!valA) 97 valA = 0; 98 if (!valB) 99 valB = 0; 100 return parseInt (valA) - parseInt (valB); 101 } 102 if (valB == valA) 103 return 0; 104 if (valB > valA) 105 return -1; 106 return 1; 107} 108 109function glossaryGetRowsJ (jTable) { 110 // prend les lignes du tableau 111 var result = new Array (); 112 jTable.find ("tr").each (function () { 113 if (jQuery (this).hasClass ("title")) 114 return; 115 result.push (this); 116 }); 117 return result; 118} 119 120function glossarySetRowsJ (jTable, rows) { 121 // fixe les lignes du tableau 122 var titles = new Array (); 123 jTable.children ().not ("tr").each (function () { 124 titles.push (this); 125 }); 126 jTable.children (".title").each (function () { 127 titles.push (this); 128 }); 129 jTable.empty (); 130 for (var i in titles) 131 jTable.append (titles [i]); 132 for (var i in rows) 133 jTable.append (rows [i]); 134} 135 136function glossarySort (input, comparator) { 137 // trie le tableau 138 var jTable = jQuery (input).closest ("tr").parent (); 139 var rows = glossaryGetRowsJ (jTable); 140 if (rows.length < 2) 141 return; 142 rows.sort (comparator); 143 glossarySetRowsJ (jTable, rows); 144 glossaryZebraJ (jTable); 145} 146 147// ======================================== 148// focus table 149// ======================================== 150function glossarySearch (input) { 151 // filtre les lignes du tableau 152 var jInput = jQuery (input); 153 glossaryClearMsgJ (jInput); 154 var value = trim (jInput.val ()).toLowerCase (); 155 var jTable = jInput.closest ("tr").parent (); 156 jTable.find ("tr").each (function () { 157 var item = jQuery (this); 158 if (item.hasClass ("title")) 159 return; 160 item.css ("display", ""); 161 if (!value) 162 return; 163 var hidden = true; 164 item.find ("td.word,td.translate").each (function () { 165 if (this.textContent && this.textContent.toLowerCase ().indexOf (value) >= 0) 166 hidden = false; 167 }); 168 if (hidden) 169 item.css ("display", "none"); 170 }); 171 glossaryZebraJ (jTable); 172 return false; 173} 174 175// ======================================== 176// sending form 177// ======================================== 178function glossaryPoll (ancor, ticket, opinion, NS) { 179 // vote 180 var params = "glossary[operation]=poll&glossary[ticket]="+ticket+"&glossary[opinion]="+opinion+"&glossary[ns]="+NS; 181 glossarySend (ancor, DOKU_BASE+"lib/plugins/glossary/ajax.php", params); 182} 183 184function glossaryAjax (form) { 185 var params = ""; 186 for (var idx = 0; idx < form.elements.length; idx++) { 187 var elem = form.elements[idx]; 188 if (elem.type == "checkbox") { 189 if (elem.checked) 190 params += "&"+elem.name+"="+elem.value; 191 } else 192 params += "&"+elem.name+"="+elem.value; 193 } 194 glossarySend (form, DOKU_BASE+"lib/plugins/glossary/ajax.php", params); 195 return false; 196} 197function glossarySendClear (action, ns) { 198 jQuery.ajax ({ 199 type: "POST", 200 url: DOKU_BASE+"lib/plugins/glossary/ajax.php", 201 cache: false, 202 async: true, 203 data: "glossary[action]="+action+"&glossary[ns]="+ns, 204 }); 205} 206 207// ======================================== 208// Ajax function 209// ======================================== 210function glossarySend (sender, uri, params) { 211 var jDiv = jQuery (sender).closest ("div"); 212 glossaryClearMsgJ (jQuery (sender)); 213 jQuery ('<div class="popup">'+LANG["plugins"]["glossary"]["pleaseWait"]+'</div>'). 214 insertBefore (jDiv.children ().first ()); 215 jQuery.ajax ({ 216 type: "POST", 217 url: uri, 218 cache: false, 219 async: true, 220 data: params, 221 success: function (response) { 222 jDiv.html (trim (response)); 223 } 224 }); 225} 226 227// ======================================== 228