[ Index ] |
|
Code source de Horde 3.1.3 |
1 /** 2 * Javascript code for finding all tables with classname "striped" and 3 * dynamically striping their row colors, and finding all tables with 4 * classname "sortable" and making them dynamically sortable. 5 * 6 * $Horde: horde/js/tables.js,v 1.4.2.3 2005/12/01 22:58:26 chuck Exp $ 7 * 8 * See the enclosed file COPYING for license information (LGPL). If you did not 9 * receive this file, see http://www.fsf.org/copyleft/lgpl.html. 10 */ 11 12 /* We do everything onload so that the entire document is present 13 * before we start searching it for tables. */ 14 if (window.addEventListener) { 15 window.addEventListener('load', table_init, false); 16 } else if (window.attachEvent) { 17 window.attachEvent('onload', table_init); 18 } else if (window.onload != null) { 19 var old_onload = window.onload; 20 window.onload = function(e) 21 { 22 old_onload(e); 23 table_init(); 24 }; 25 } else { 26 window.onload = table_init; 27 } 28 29 var SORT_COLUMN_INDEX; 30 31 function table_init() 32 { 33 if (!document.getElementsByTagName) { 34 return; 35 } 36 tables = document.getElementsByTagName('table'); 37 for (i = 0; i < tables.length; i++) { 38 if (hasClass(tables[i], 'striped')) { 39 table_stripe(tables[i]); 40 } 41 if (hasClass(tables[i], 'sortable') && tables[i].id) { 42 table_makeSortable(tables[i]); 43 } 44 } 45 } 46 47 function table_stripe(table) 48 { 49 // The flag we'll use to keep track of whether the current row is 50 // odd or even. 51 var even = false; 52 53 // Tables can have more than one tbody element; get all child 54 // tbody tags and interate through them. 55 var tbodies = table.childNodes; 56 for (var c = 0; c < tbodies.length; c++) { 57 if (tbodies[c].tagName != 'TBODY') { 58 continue; 59 } 60 61 var trs = tbodies[c].childNodes; 62 for (var i = 0; i < trs.length; i++) { 63 if (trs[i].tagName == 'TR') { 64 removeClass(trs[i], 'rowEven'); 65 removeClass(trs[i], 'rowOdd'); 66 addClass(trs[i], even ? 'rowEven' : 'rowOdd'); 67 68 // Flip from odd to even, or vice-versa. 69 even = !even; 70 } 71 } 72 } 73 } 74 75 function table_makeSortable(table) 76 { 77 if (table.rows && table.rows.length > 0) { 78 var firstRow = table.rows[0]; 79 } 80 if (!firstRow) { 81 return; 82 } 83 84 // We have a first row: assume it's the header, and make its 85 // contents clickable links. 86 for (var i = 0; i < firstRow.cells.length; i++) { 87 var cell = firstRow.cells[i]; 88 if (hasClass(cell, 'nosort')) { 89 continue; 90 } 91 92 cell.columnIndex = i; 93 cell.style.cursor = 'pointer'; 94 cell.onclick = function(e) 95 { 96 var e = e || window.event; 97 98 if (e.target) { 99 if (e.target.nodeType == 3) { 100 e.target = e.target.parentNode; 101 } 102 } else if (e.srcElement) { 103 e.target = e.srcElement; 104 } 105 106 el = hasParent(e.target, 'A', 'TH'); 107 if (el && !hasClass(el, 'sortlink')) { 108 return true; 109 } 110 111 table_resortTable(getParent(e.target, 'TH')); 112 return false; 113 } 114 } 115 } 116 117 function table_getInnerText(el) 118 { 119 if (typeof el == 'string') { 120 return el; 121 } 122 if (typeof el == 'undefined') { 123 return el; 124 } 125 if (el.innerText) { 126 // Not needed but it is faster. 127 return el.innerText; 128 } 129 130 var str = ""; 131 var cs = el.childNodes; 132 var l = cs.length; 133 for (var i = 0; i < l; i++) { 134 switch (cs[i].nodeType) { 135 case 1: 136 // ELEMENT_NODE 137 str += table_getInnerText(cs[i]); 138 break; 139 140 case 3: 141 // TEXT_NODE 142 str += cs[i].nodeValue; 143 break; 144 } 145 } 146 147 return str; 148 } 149 150 function table_resortTable(th) 151 { 152 table = getParent(th, 'TABLE'); 153 sortColumn = th.columnIndex; 154 sortDown = 0; 155 156 // Loop through <thead> to find the current sort column and 157 // direction. 158 theads = table.tHead.getElementsByTagName('th'); 159 for (i = 0; i < theads.length; i++) { 160 if (th == theads[i]) { 161 if (hasClass(theads[i], 'sortup')) { 162 removeClass(theads[i], 'sortup'); 163 addClass(theads[i], 'sortdown'); 164 } else if (hasClass(theads[i], 'sortdown')) { 165 removeClass(theads[i], 'sortdown'); 166 addClass(theads[i], 'sortup'); 167 sortDown = 1; 168 } else { 169 addClass(theads[i], 'sortdown'); 170 } 171 } else { 172 removeClass(theads[i], 'sortup'); 173 removeClass(theads[i], 'sortdown'); 174 } 175 } 176 177 // Work out a type for the column 178 if (table.rows.length <= 1) { 179 return; 180 } 181 182 var itm = table_getInnerText(table.rows[1].cells[sortColumn]); 183 sortfn = table_sort_caseinsensitive; 184 if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) { 185 sortfn = table_sort_date; 186 } 187 if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) { 188 sortfn = table_sort_date; 189 } 190 if (itm.match(/^[£$]/)) { 191 sortfn = table_sort_currency; 192 } 193 if (itm.match(/^[\d\.]+$/)) { 194 sortfn = table_sort_numeric; 195 } 196 197 SORT_COLUMN_INDEX = sortColumn; 198 199 // Don't mix up seperate tbodies; sort each in turn. 200 for (i = 0; i < table.tBodies.length; i++) { 201 trs = table.tBodies[i].getElementsByTagName('tr'); 202 newRows = new Array(); 203 for (j = 0; j < trs.length; j++) { 204 newRows[j] = trs[j]; 205 } 206 207 newRows.sort(sortfn); 208 if (sortDown) { 209 newRows.reverse(); 210 } 211 212 // We appendChild rows that already exist to the tbody, so it 213 // moves them rather than creating new ones. Don't do 214 // sortbottom rows. 215 for (j = 0; j < newRows.length; j++) { 216 if (!hasClass(newRows[j], 'sortbottom')) { 217 table.tBodies[i].appendChild(newRows[j]); 218 } 219 } 220 221 // Do sortbottom rows only. 222 for (j = 0; j < newRows.length; j++) { 223 if (hasClass(newRows[j], 'sortbottom')) { 224 table.tBodies[i].appendChild(newRows[j]); 225 } 226 } 227 } 228 229 // If we just resorted a striped table, re-stripe it. 230 if (hasClass(table, 'striped')) { 231 table_stripe(table); 232 } 233 234 // Finally, see if we have a callback function to trigger. 235 if (typeof(table_sortCallback) == 'function') { 236 table_sortCallback(table.id, th.id, sortDown); 237 } 238 } 239 240 function getParent(el, pTagName) 241 { 242 if (el == null) { 243 return null; 244 } else if (pTagName == null) { 245 return el.parentNode; 246 } else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) { 247 // Gecko bug, supposed to be uppercase. 248 return el; 249 } else { 250 return getParent(el.parentNode, pTagName); 251 } 252 } 253 254 function table_sort_date(a, b) 255 { 256 // Two digit years less than 50 are treated as 20XX, greater than 257 // 50 are treated as 19XX. 258 aa = table_getInnerText(a.cells[SORT_COLUMN_INDEX]); 259 bb = table_getInnerText(b.cells[SORT_COLUMN_INDEX]); 260 if (aa.length == 10) { 261 dt1 = aa.substr(6, 4) + aa.substr(3, 2) + aa.substr(0, 2); 262 } else { 263 yr = aa.substr(6, 2); 264 if (parseInt(yr) < 50) { 265 yr = '20' + yr; 266 } else { 267 yr = '19' + yr; 268 } 269 dt1 = yr+aa.substr(3, 2) + aa.substr(0, 2); 270 } 271 if (bb.length == 10) { 272 dt2 = bb.substr(6, 4) + bb.substr(3, 2) + bb.substr(0, 2); 273 } else { 274 yr = bb.substr(6, 2); 275 if (parseInt(yr) < 50) { 276 yr = '20' + yr; 277 } else { 278 yr = '19' + yr; 279 } 280 dt2 = yr + bb.substr(3, 2) + bb.substr(0, 2); 281 } 282 if (dt1 == dt2) { 283 return 0; 284 } else if (dt1 < dt2) { 285 return -1; 286 } 287 return 1; 288 } 289 290 function table_sort_currency(a, b) 291 { 292 aa = table_getInnerText(a.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g, ''); 293 bb = table_getInnerText(b.cells[SORT_COLUMN_INDEX]).replace(/[^0-9.]/g, ''); 294 return parseFloat(aa) - parseFloat(bb); 295 } 296 297 function table_sort_numeric(a, b) 298 { 299 aa = parseFloat(table_getInnerText(a.cells[SORT_COLUMN_INDEX])); 300 if (isNaN(aa)) { 301 aa = 0; 302 } 303 bb = parseFloat(table_getInnerText(b.cells[SORT_COLUMN_INDEX])); 304 if (isNaN(bb)) { 305 bb = 0; 306 } 307 return aa - bb; 308 } 309 310 function table_sort_caseinsensitive(a, b) 311 { 312 aa = table_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase(); 313 bb = table_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase(); 314 if (aa == bb) { 315 return 0; 316 } else if (aa < bb) { 317 return -1; 318 } 319 return 1; 320 } 321 322 function table_sort_default(a,b) 323 { 324 aa = table_getInnerText(a.cells[SORT_COLUMN_INDEX]); 325 bb = table_getInnerText(b.cells[SORT_COLUMN_INDEX]); 326 if (aa == bb) { 327 return 0; 328 } else if (aa < bb) { 329 return -1; 330 } 331 return 1; 332 } 333 334 /** 335 * DOM utility functions. 336 */ 337 function hasParent(el, tagName, tagStop) 338 { 339 if (el.tagName == tagName) { 340 return el; 341 } else if (tagStop != null && el.tagName == tagStop) { 342 return false; 343 } else { 344 return hasParent(getParent(el), tagName, tagStop); 345 } 346 } 347 348 function addClass(el, className) 349 { 350 el.className += el.className ? ' ' + className : className; 351 } 352 353 function removeClass(el, className) 354 { 355 el.className = el.className.replace(new RegExp(' ?' + className + ' ?'), ''); 356 } 357 358 function hasClass(el, className) 359 { 360 return (el.className.indexOf(className) != -1); 361 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |