| 1 | # Copyright 2007-2026, Paul Johnson (paul@pjcj.net) | ||
| 2 | # except where otherwise noted. | ||
| 3 | |||
| 4 | # This software is free. It is licensed under the same terms as Perl itself, | ||
| 5 | # except where otherwise noted. | ||
| 6 | |||
| 7 | # The latest version of this software should be available from my homepage: | ||
| 8 | # https://pjcj.net | ||
| 9 | |||
| 10 | package Devel::Cover::Web; | ||
| 11 | |||
| 12 | use 5.20.0; | ||
| 13 | use warnings; | ||
| 14 | |||
| 15 | use feature "signatures"; | ||
| 16 | no warnings "experimental::signatures"; | ||
| 17 | |||
| 18 | our $VERSION = '1.52'; # VERSION | ||
| 19 | |||
| 20 | use Exporter; | ||
| 21 | |||
| 22 | our @ISA = "Exporter"; | ||
| 23 | our @EXPORT_OK = qw( write_file ); | ||
| 24 | |||
| 25 | my %Files; | ||
| 26 | |||
| 27 | sub write_file ($directory, $file) { | ||
| 28 | my @files = $file eq "all" ? keys %Files : $file eq "js" ? grep /\.js$/, | ||
| 29 | keys %Files : $file eq "css" ? grep /\.css$/, keys %Files : ($file); | ||
| 30 | for my $f (@files) { | ||
| 31 | my $contents = $Files{$f} // next; | ||
| 32 | my $path = "$directory/$f"; | ||
| 33 | open my $fh, ">", $path or die "Can't open $path: $!\n"; | ||
| 34 | print $fh $contents; | ||
| 35 | close $fh or die "Can't close $path: $!\n"; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | my $Common_css = <<'EOF'; | ||
| 40 | /* Stylesheet for Devel::Cover HTML reports */ | ||
| 41 | |||
| 42 | /* You may modify this file to alter the appearance of your coverage | ||
| 43 | * reports. If you do, you should probably flag it read-only to prevent | ||
| 44 | * future runs from overwriting it. | ||
| 45 | */ | ||
| 46 | |||
| 47 | /* Note: default values use the color-safe web palette. */ | ||
| 48 | |||
| 49 | body { | ||
| 50 | font-family: sans-serif; | ||
| 51 | } | ||
| 52 | |||
| 53 | h1 { | ||
| 54 | text-align : center; | ||
| 55 | background-color : #cc99ff; | ||
| 56 | border : solid 1px #999999; | ||
| 57 | padding : 0.2em; | ||
| 58 | -moz-border-radius : 10px; | ||
| 59 | } | ||
| 60 | |||
| 61 | a { | ||
| 62 | color: #000000; | ||
| 63 | } | ||
| 64 | |||
| 65 | a:visited { | ||
| 66 | color: #333333; | ||
| 67 | } | ||
| 68 | |||
| 69 | table { | ||
| 70 | border-spacing: 0px; | ||
| 71 | } | ||
| 72 | |||
| 73 | tr { | ||
| 74 | text-align : center; | ||
| 75 | vertical-align : top; | ||
| 76 | } | ||
| 77 | |||
| 78 | th,.h,.hh { | ||
| 79 | background-color : #cccccc; | ||
| 80 | border : solid 1px #333333; | ||
| 81 | padding : 0em 0.2em; | ||
| 82 | -moz-border-radius : 4px; | ||
| 83 | } | ||
| 84 | |||
| 85 | td { | ||
| 86 | border : solid 1px #cccccc; | ||
| 87 | border-top : none; | ||
| 88 | border-left : none; | ||
| 89 | -moz-border-radius : 4px; | ||
| 90 | } | ||
| 91 | |||
| 92 | .hblank { | ||
| 93 | height: 0.5em; | ||
| 94 | } | ||
| 95 | |||
| 96 | .dblank { | ||
| 97 | border: none; | ||
| 98 | } | ||
| 99 | |||
| 100 | /* source code */ | ||
| 101 | pre,.s { | ||
| 102 | text-align : left; | ||
| 103 | font-family : monospace; | ||
| 104 | white-space : pre; | ||
| 105 | padding : 0.2em 0.5em 0em 0.5em; | ||
| 106 | } | ||
| 107 | |||
| 108 | /* Classes for color-coding coverage information: | ||
| 109 | * c0 : path not covered or coverage < 75% | ||
| 110 | * c1 : coverage >= 75% | ||
| 111 | * c2 : coverage >= 90% | ||
| 112 | * c3 : path covered or coverage = 100% | ||
| 113 | */ | ||
| 114 | .c0 { | ||
| 115 | background-color : #ff9999; | ||
| 116 | border : solid 1px #cc0000; | ||
| 117 | } | ||
| 118 | .c1 { | ||
| 119 | background-color : #ffcc99; | ||
| 120 | border : solid 1px #ff9933; | ||
| 121 | } | ||
| 122 | .c2 { | ||
| 123 | background-color : #ffff99; | ||
| 124 | border : solid 1px #cccc66; | ||
| 125 | } | ||
| 126 | .c3 { | ||
| 127 | background-color : #99ff99; | ||
| 128 | border : solid 1px #009900; | ||
| 129 | } | ||
| 130 | EOF | ||
| 131 | |||
| 132 | my $Extra_css = <<'EOF'; | ||
| 133 | |||
| 134 | .sh,.sv { | ||
| 135 | background-color : #cccccc; | ||
| 136 | border : solid 1px #333333; | ||
| 137 | padding : 0em 0.2em; | ||
| 138 | -moz-border-radius : 4px; | ||
| 139 | } | ||
| 140 | |||
| 141 | .sh { | ||
| 142 | color : #CD5555; | ||
| 143 | font-weight : bold; | ||
| 144 | padding : 0.2em; | ||
| 145 | } | ||
| 146 | |||
| 147 | .sv { | ||
| 148 | padding : 0.2em; | ||
| 149 | } | ||
| 150 | |||
| 151 | table.sortable a.sortheader { | ||
| 152 | text-decoration: none; | ||
| 153 | } | ||
| 154 | |||
| 155 | /* For syntax highlighting with PPI::HTML */ | ||
| 156 | .line_number { color: #aaaaaa; } | ||
| 157 | .comment { color: #228B22; } | ||
| 158 | .symbol { color: #00688B; } | ||
| 159 | .word { color: #8B008B; font-weight:bold; } | ||
| 160 | .pragma { color: #8B008B; font-weight:bold; } | ||
| 161 | .structure { color: #000000; } | ||
| 162 | .number { color: #B452CD; } | ||
| 163 | .single { color: #CD5555; } | ||
| 164 | .double { color: #CD5555; } | ||
| 165 | .match { color: #CD5555; } | ||
| 166 | .substitute { color: #CD5555; } | ||
| 167 | .heredoc_content { color: #CD5555; } | ||
| 168 | .interpolate { color: #CD5555; } | ||
| 169 | .words { color: #CD5555; } | ||
| 170 | |||
| 171 | /* for syntax highlighting with Perl::Tidy */ | ||
| 172 | .c { color: #228B22; } /* comment */ | ||
| 173 | .cm { color: #000000; } /* comma */ | ||
| 174 | .co { color: #000000; } /* colon */ | ||
| 175 | .h { color: #CD5555; font-weight:bold; } /* here-doc-target */ | ||
| 176 | .hh { color: #CD5555; font-style:italic; } /* here-doc-text */ | ||
| 177 | .i { color: #00688B; } /* identifier */ | ||
| 178 | .j { color: #000000; font-weight:bold; } /* label */ | ||
| 179 | .k { color: #8B4513; font-weight:bold; } /* keyword */ | ||
| 180 | .m { color: #FF0000; font-weight:bold; } /* subroutine */ | ||
| 181 | .n { color: #B452CD; } /* numeric */ | ||
| 182 | .p { color: #000000; } /* paren */ | ||
| 183 | .pd { color: #228B22; font-style:italic; } /* pod-text */ | ||
| 184 | .pu { color: #000000; } /* punctuation */ | ||
| 185 | .q { color: #CD5555; } /* quote */ | ||
| 186 | .s { color: #000000; } /* structure */ | ||
| 187 | .sc { color: #000000; } /* semicolon */ | ||
| 188 | .v { color: #B452CD; } /* v-string */ | ||
| 189 | .w { color: #000000; } /* bareword */ | ||
| 190 | EOF | ||
| 191 | |||
| 192 | $Files{"collection.css"} = $Common_css; | ||
| 193 | $Files{"cover.css"} = $Common_css . $Extra_css; | ||
| 194 | |||
| 195 | $Files{"common.js"} = <<'EOF'; | ||
| 196 | /** | ||
| 197 | * addEvent written by Dean Edwards, 2005 | ||
| 198 | * with input from Tino Zijdel | ||
| 199 | * | ||
| 200 | * http://dean.edwards.name/weblog/2005/10/add-event/ | ||
| 201 | * licensed under http://creativecommons.org/licenses/by/2.5/ | ||
| 202 | **/ | ||
| 203 | function addEvent(element, type, handler) { | ||
| 204 | // assign each event handler a unique ID | ||
| 205 | if (!handler.$$guid) handler.$$guid = addEvent.guid++; | ||
| 206 | // create a hash table of event types for the element | ||
| 207 | if (!element.events) element.events = {}; | ||
| 208 | // create a hash table of event handlers for each element/event pair | ||
| 209 | var handlers = element.events[type]; | ||
| 210 | if (!handlers) { | ||
| 211 | handlers = element.events[type] = {}; | ||
| 212 | // store the existing event handler (if there is one) | ||
| 213 | if (element["on" + type]) { | ||
| 214 | handlers[0] = element["on" + type]; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | // store the event handler in the hash table | ||
| 218 | handlers[handler.$$guid] = handler; | ||
| 219 | // assign a global event handler to do all the work | ||
| 220 | element["on" + type] = handleEvent; | ||
| 221 | }; | ||
| 222 | // a counter used to create unique IDs | ||
| 223 | addEvent.guid = 1; | ||
| 224 | |||
| 225 | function removeEvent(element, type, handler) { | ||
| 226 | // delete the event handler from the hash table | ||
| 227 | if (element.events && element.events[type]) { | ||
| 228 | delete element.events[type][handler.$$guid]; | ||
| 229 | } | ||
| 230 | }; | ||
| 231 | |||
| 232 | function handleEvent(event) { | ||
| 233 | var returnValue = true; | ||
| 234 | // grab the event object (IE uses a global event object) | ||
| 235 | event = event || fixEvent(window.event); | ||
| 236 | // get a reference to the hash table of event handlers | ||
| 237 | var handlers = this.events[event.type]; | ||
| 238 | // execute each event handler | ||
| 239 | for (var i in handlers) { | ||
| 240 | this.$$handleEvent = handlers[i]; | ||
| 241 | if (this.$$handleEvent(event) === false) { | ||
| 242 | returnValue = false; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | return returnValue; | ||
| 246 | }; | ||
| 247 | |||
| 248 | function fixEvent(event) { | ||
| 249 | // add W3C standard event methods | ||
| 250 | event.preventDefault = fixEvent.preventDefault; | ||
| 251 | event.stopPropagation = fixEvent.stopPropagation; | ||
| 252 | return event; | ||
| 253 | }; | ||
| 254 | fixEvent.preventDefault = function() { | ||
| 255 | this.returnValue = false; | ||
| 256 | }; | ||
| 257 | fixEvent.stopPropagation = function() { | ||
| 258 | this.cancelBubble = true; | ||
| 259 | }; | ||
| 260 | |||
| 261 | // end from Dean Edwards | ||
| 262 | |||
| 263 | |||
| 264 | /** | ||
| 265 | * Creates an Element for insertion into the DOM tree. | ||
| 266 | * From http://simon.incutio.com/archive/2003/06/15/javascriptWithXML | ||
| 267 | * | ||
| 268 | * @param element the element type to be created. | ||
| 269 | * e.g. ul (no angle brackets) | ||
| 270 | **/ | ||
| 271 | function createElement(element) { | ||
| 272 | if (typeof document.createElementNS != 'undefined') { | ||
| 273 | return document.createElementNS( | ||
| 274 | 'https://www.w3.org/1999/xhtml', element); | ||
| 275 | } | ||
| 276 | if (typeof document.createElement != 'undefined') { | ||
| 277 | return document.createElement(element); | ||
| 278 | } | ||
| 279 | return false; | ||
| 280 | } | ||
| 281 | |||
| 282 | /** | ||
| 283 | * "targ" is the element which caused this function to be called | ||
| 284 | * from https://www.quirksmode.org/js/events_properties.html | ||
| 285 | * see https://www.quirksmode.org/about/copyright.html | ||
| 286 | **/ | ||
| 287 | function getEventTarget(e) { | ||
| 288 | var targ; | ||
| 289 | if (!e) { | ||
| 290 | e = window.event; | ||
| 291 | } | ||
| 292 | if (e.target) { | ||
| 293 | targ = e.target; | ||
| 294 | } else if (e.srcElement) { | ||
| 295 | targ = e.srcElement; | ||
| 296 | } | ||
| 297 | if (targ.nodeType == 3) { // defeat Safari bug | ||
| 298 | targ = targ.parentNode; | ||
| 299 | } | ||
| 300 | |||
| 301 | return targ; | ||
| 302 | } | ||
| 303 | EOF | ||
| 304 | |||
| 305 | $Files{"css.js"} = <<'EOF'; | ||
| 306 | /** | ||
| 307 | * Written by Neil Crosby. | ||
| 308 | * http://www.workingwith.me.uk/ | ||
| 309 | * | ||
| 310 | * Use this wherever you want, but please keep this comment at the top of | ||
| 311 | * this file. | ||
| 312 | * | ||
| 313 | * Copyright (c) 2006 Neil Crosby | ||
| 314 | * | ||
| 315 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 316 | * of this software and associated documentation files (the "Software"), to deal | ||
| 317 | * in the Software without restriction, including without limitation the rights | ||
| 318 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 319 | * copies of the Software, and to permit persons to whom the Software is | ||
| 320 | * furnished to do so, subject to the following conditions: | ||
| 321 | * | ||
| 322 | * The above copyright notice and this permission notice shall be included in | ||
| 323 | * all copies or substantial portions of the Software. | ||
| 324 | * | ||
| 325 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 326 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 327 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 328 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 329 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 330 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 331 | * SOFTWARE. | ||
| 332 | **/ | ||
| 333 | var css = { | ||
| 334 | /** | ||
| 335 | * Returns an array containing references to all elements | ||
| 336 | * of a given tag type within a certain node which have a given class | ||
| 337 | * | ||
| 338 | * @param node the node to start from | ||
| 339 | * (e.g. document, | ||
| 340 | * getElementById('whateverStartpointYouWant') | ||
| 341 | * ) | ||
| 342 | * @param searchClass the class we're wanting | ||
| 343 | * (e.g. 'some_class') | ||
| 344 | * @param tag the tag that the found elements are allowed to be | ||
| 345 | * (e.g. '*', 'div', 'li') | ||
| 346 | **/ | ||
| 347 | getElementsByClass : function(node, searchClass, tag) { | ||
| 348 | var classElements = new Array(); | ||
| 349 | var els = node.getElementsByTagName(tag); | ||
| 350 | var elsLen = els.length; | ||
| 351 | var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"); | ||
| 352 | |||
| 353 | |||
| 354 | for (var i = 0, j = 0; i < elsLen; i++) { | ||
| 355 | if (this.elementHasClass(els[i], searchClass) ) { | ||
| 356 | classElements[j] = els[i]; | ||
| 357 | j++; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | return classElements; | ||
| 361 | }, | ||
| 362 | |||
| 363 | |||
| 364 | /** | ||
| 365 | * PRIVATE. Returns an array containing all the classes applied to this | ||
| 366 | * element. | ||
| 367 | * | ||
| 368 | * Used internally by elementHasClass(), addClassToElement() and | ||
| 369 | * removeClassFromElement(). | ||
| 370 | **/ | ||
| 371 | privateGetClassArray: function(el) { | ||
| 372 | return el.className.split(' '); | ||
| 373 | }, | ||
| 374 | |||
| 375 | /** | ||
| 376 | * PRIVATE. Creates a string from an array of class names which can be used | ||
| 377 | * by the className function. | ||
| 378 | * | ||
| 379 | * Used internally by addClassToElement(). | ||
| 380 | **/ | ||
| 381 | privateCreateClassString: function(classArray) { | ||
| 382 | return classArray.join(' '); | ||
| 383 | }, | ||
| 384 | |||
| 385 | /** | ||
| 386 | * Returns true if the given element has been assigned the given class. | ||
| 387 | **/ | ||
| 388 | elementHasClass: function(el, classString) { | ||
| 389 | if (!el) { | ||
| 390 | return false; | ||
| 391 | } | ||
| 392 | |||
| 393 | var regex = new RegExp('\\b'+classString+'\\b'); | ||
| 394 | if (el.className.match(regex)) { | ||
| 395 | return true; | ||
| 396 | } | ||
| 397 | |||
| 398 | return false; | ||
| 399 | }, | ||
| 400 | |||
| 401 | /** | ||
| 402 | * Adds classString to the classes assigned to the element with id equal to | ||
| 403 | * idString. | ||
| 404 | **/ | ||
| 405 | addClassToId: function(idString, classString) { | ||
| 406 | this.addClassToElement(document.getElementById(idString), classString); | ||
| 407 | }, | ||
| 408 | |||
| 409 | /** | ||
| 410 | * Adds classString to the classes assigned to the given element. | ||
| 411 | * If the element already has the class which was to be added, then | ||
| 412 | * it is not added again. | ||
| 413 | **/ | ||
| 414 | addClassToElement: function(el, classString) { | ||
| 415 | var classArray = this.privateGetClassArray(el); | ||
| 416 | |||
| 417 | if (this.elementHasClass(el, classString)) { | ||
| 418 | return; // already has element so don't need to add it | ||
| 419 | } | ||
| 420 | |||
| 421 | classArray.push(classString); | ||
| 422 | |||
| 423 | el.className = this.privateCreateClassString(classArray); | ||
| 424 | }, | ||
| 425 | |||
| 426 | /** | ||
| 427 | * Removes the given classString from the list of classes assigned to the | ||
| 428 | * element with id equal to idString | ||
| 429 | **/ | ||
| 430 | removeClassFromId: function(idString, classString) { | ||
| 431 | this.removeClassFromElement( | ||
| 432 | document.getElementById(idString), classString); | ||
| 433 | }, | ||
| 434 | |||
| 435 | /** | ||
| 436 | * Removes the given classString from the list of classes assigned to the | ||
| 437 | * given element. If the element has the same class assigned to it twice, | ||
| 438 | * then only the first instance of that class is removed. | ||
| 439 | **/ | ||
| 440 | removeClassFromElement: function(el, classString) { | ||
| 441 | var classArray = this.privateGetClassArray(el); | ||
| 442 | |||
| 443 | for (x in classArray) { | ||
| 444 | if (classString == classArray[x]) { | ||
| 445 | classArray[x] = ''; | ||
| 446 | break; | ||
| 447 | } | ||
| 448 | } | ||
| 449 | |||
| 450 | el.className = this.privateCreateClassString(classArray); | ||
| 451 | } | ||
| 452 | } | ||
| 453 | EOF | ||
| 454 | |||
| 455 | $Files{"standardista-table-sorting.js"} = <<'EOF'; | ||
| 456 | /** | ||
| 457 | * Written by Neil Crosby. | ||
| 458 | * http://www.workingwith.me.uk/articles/scripting/standardista_table_sorting | ||
| 459 | * | ||
| 460 | * This module is based on Stuart Langridge's "sorttable" code. Specifically, | ||
| 461 | * the determineSortFunction, sortCaseInsensitive, sortDate, sortNumeric, and | ||
| 462 | * sortCurrency functions are heavily based on his code. This module would not | ||
| 463 | * have been possible without Stuart's earlier outstanding work. | ||
| 464 | * | ||
| 465 | * Use this wherever you want, but please keep this comment at the top of | ||
| 466 | * this file. | ||
| 467 | * | ||
| 468 | * Copyright (c) 2006 Neil Crosby | ||
| 469 | * | ||
| 470 | * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 471 | * of this software and associated documentation files (the "Software"), to deal | ||
| 472 | * in the Software without restriction, including without limitation the rights | ||
| 473 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 474 | * copies of the Software, and to permit persons to whom the Software is | ||
| 475 | * furnished to do so, subject to the following conditions: | ||
| 476 | * | ||
| 477 | * The above copyright notice and this permission notice shall be included in | ||
| 478 | * all copies or substantial portions of the Software. | ||
| 479 | * | ||
| 480 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 481 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 482 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 483 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 484 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 485 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 486 | * SOFTWARE. | ||
| 487 | **/ | ||
| 488 | var standardistaTableSorting = { | ||
| 489 | |||
| 490 | that: false, | ||
| 491 | isOdd: false, | ||
| 492 | |||
| 493 | sortColumnIndex : -1, | ||
| 494 | lastAssignedId : 0, | ||
| 495 | newRows: -1, | ||
| 496 | lastSortedTable: -1, | ||
| 497 | |||
| 498 | /** | ||
| 499 | * Initialises the Standardista Table Sorting module | ||
| 500 | **/ | ||
| 501 | init : function() { | ||
| 502 | // first, check whether this web browser is capable of running this | ||
| 503 | // script | ||
| 504 | if (!document.getElementsByTagName) { | ||
| 505 | return; | ||
| 506 | } | ||
| 507 | |||
| 508 | this.that = this; | ||
| 509 | |||
| 510 | this.run(); | ||
| 511 | |||
| 512 | }, | ||
| 513 | |||
| 514 | /** | ||
| 515 | * Runs over each table in the document, making it sortable if it has a | ||
| 516 | * class | ||
| 517 | * assigned named "sortable" and an id assigned. | ||
| 518 | **/ | ||
| 519 | run : function() { | ||
| 520 | var tables = document.getElementsByTagName("table"); | ||
| 521 | |||
| 522 | for (var i=0; i < tables.length; i++) { | ||
| 523 | var thisTable = tables[i]; | ||
| 524 | |||
| 525 | if (css.elementHasClass(thisTable, 'sortable')) { | ||
| 526 | this.makeSortable(thisTable); | ||
| 527 | } | ||
| 528 | } | ||
| 529 | }, | ||
| 530 | |||
| 531 | /** | ||
| 532 | * Makes the given table sortable. | ||
| 533 | **/ | ||
| 534 | makeSortable : function(table) { | ||
| 535 | |||
| 536 | // first, check if the table has an id. if it doesn't, give it one | ||
| 537 | if (!table.id) { | ||
| 538 | table.id = 'sortableTable'+this.lastAssignedId++; | ||
| 539 | } | ||
| 540 | |||
| 541 | // if this table does not have a thead, we don't want to know about it | ||
| 542 | if (!table.tHead || !table.tHead.rows || 0 == table.tHead.rows.length) { | ||
| 543 | return; | ||
| 544 | } | ||
| 545 | |||
| 546 | // we'll assume that the last row of headings in the thead is the row | ||
| 547 | // that | ||
| 548 | // wants to become clickable | ||
| 549 | var row = table.tHead.rows[table.tHead.rows.length - 1]; | ||
| 550 | |||
| 551 | for (var i=0; i < row.cells.length; i++) { | ||
| 552 | |||
| 553 | // create a link with an onClick event which will | ||
| 554 | // control the sorting of the table | ||
| 555 | var linkEl = createElement('a'); | ||
| 556 | linkEl.href = '#'; | ||
| 557 | linkEl.onclick = this.headingClicked; | ||
| 558 | linkEl.setAttribute('columnId', i); | ||
| 559 | linkEl.title = 'Click to sort'; | ||
| 560 | // add class - pjcj | ||
| 561 | linkEl.className = 'sortheader'; | ||
| 562 | |||
| 563 | // move the current contents of the cell that we're | ||
| 564 | // hyperlinking into the hyperlink | ||
| 565 | var innerEls = row.cells[i].childNodes; | ||
| 566 | for (var j = 0; j < innerEls.length; j++) { | ||
| 567 | linkEl.appendChild(innerEls[j]); | ||
| 568 | } | ||
| 569 | |||
| 570 | // and finally add the new link back into the cell | ||
| 571 | row.cells[i].appendChild(linkEl); | ||
| 572 | |||
| 573 | // Don't add space for arrow until we sort - pjcj | ||
| 574 | // var spanEl = createElement('span'); | ||
| 575 | // spanEl.className = 'tableSortArrow'; | ||
| 576 | // spanEl.appendChild(document.createTextNode('\u00A0\u00A0')); | ||
| 577 | // row.cells[i].appendChild(spanEl); | ||
| 578 | |||
| 579 | } | ||
| 580 | |||
| 581 | if (css.elementHasClass(table, 'autostripe')) { | ||
| 582 | this.isOdd = false; | ||
| 583 | var rows = table.tBodies[0].rows; | ||
| 584 | |||
| 585 | // We appendChild rows that already exist to the tbody, so it moves | ||
| 586 | // them rather than creating new ones | ||
| 587 | for (var i=0;i<rows.length;i++) { | ||
| 588 | this.doStripe(rows[i]); | ||
| 589 | } | ||
| 590 | } | ||
| 591 | }, | ||
| 592 | |||
| 593 | headingClicked: function(e) { | ||
| 594 | |||
| 595 | var that = standardistaTableSorting.that; | ||
| 596 | |||
| 597 | // linkEl is the hyperlink that was clicked on which caused | ||
| 598 | // this method to be called | ||
| 599 | var linkEl = getEventTarget(e); | ||
| 600 | |||
| 601 | // directly outside it is a td, tr, thead and table | ||
| 602 | var td = linkEl.parentNode; | ||
| 603 | var tr = td.parentNode; | ||
| 604 | var thead = tr.parentNode; | ||
| 605 | var table = thead.parentNode; | ||
| 606 | |||
| 607 | // if the table we're looking at doesn't have any rows | ||
| 608 | // (or only has one) then there's no point trying to sort it | ||
| 609 | if (!table.tBodies || table.tBodies[0].rows.length <= 1) { | ||
| 610 | return false; | ||
| 611 | } | ||
| 612 | |||
| 613 | // the column we want is indicated by td.cellIndex | ||
| 614 | var column = linkEl.getAttribute('columnId') || td.cellIndex; | ||
| 615 | //var column = td.cellIndex; | ||
| 616 | |||
| 617 | // find out what the current sort order of this column is | ||
| 618 | var arrows = css.getElementsByClass(td, 'tableSortArrow', 'span'); | ||
| 619 | var previousSortOrder = ''; | ||
| 620 | if (arrows.length > 0) { | ||
| 621 | previousSortOrder = arrows[0].getAttribute('sortOrder'); | ||
| 622 | } | ||
| 623 | |||
| 624 | // work out how we want to sort this column using the data in the | ||
| 625 | // first cell but just getting the first cell is no good if it | ||
| 626 | // contains no data so if the first cell just contains white space | ||
| 627 | // then we need to track | ||
| 628 | // down until we find a cell which does contain some actual data | ||
| 629 | var itm = '' | ||
| 630 | var rowNum = 0; | ||
| 631 | while ('' == itm && rowNum < table.tBodies[0].rows.length) { | ||
| 632 | itm = that.getInnerText( | ||
| 633 | table.tBodies[0].rows[rowNum].cells[column]); | ||
| 634 | rowNum++; | ||
| 635 | } | ||
| 636 | var sortfn = that.determineSortFunction(itm); | ||
| 637 | // if the last column that was sorted was this one, then all we need | ||
| 638 | // to do is reverse the sorting on this column | ||
| 639 | if (table.id == that.lastSortedTable && | ||
| 640 | column == that.sortColumnIndex) { | ||
| 641 | newRows = that.newRows; | ||
| 642 | newRows.reverse(); | ||
| 643 | // otherwise, we have to do the full sort | ||
| 644 | } else { | ||
| 645 | that.sortColumnIndex = column; | ||
| 646 | |||
| 647 | // alert("sorting on " + column); | ||
| 648 | |||
| 649 | var newRows = new Array(); | ||
| 650 | |||
| 651 | for (var j = 0; j < table.tBodies[0].rows.length; j++) { | ||
| 652 | newRows[j] = table.tBodies[0].rows[j]; | ||
| 653 | // alert("element " + j + " is " + that.getInnerText( | ||
| 654 | // newRows[j].cells[that.sortColumnIndex])); | ||
| 655 | } | ||
| 656 | |||
| 657 | newRows.sort(sortfn); | ||
| 658 | } | ||
| 659 | |||
| 660 | that.moveRows(table, newRows); | ||
| 661 | that.newRows = newRows; | ||
| 662 | that.lastSortedTable = table.id; | ||
| 663 | |||
| 664 | // now, give the user some feedback about which way the column is sorted | ||
| 665 | |||
| 666 | // first, get rid of any arrows in any heading cells | ||
| 667 | var arrows = css.getElementsByClass(tr, 'tableSortArrow', 'span'); | ||
| 668 | for (var j = 0; j < arrows.length; j++) { | ||
| 669 | var arrowParent = arrows[j].parentNode; | ||
| 670 | arrowParent.removeChild(arrows[j]); | ||
| 671 | |||
| 672 | if (arrowParent != td) { | ||
| 673 | spanEl = createElement('span'); | ||
| 674 | spanEl.className = 'tableSortArrow'; | ||
| 675 | spanEl.appendChild(document.createTextNode('\u00A0\u00A0')); | ||
| 676 | arrowParent.appendChild(spanEl); | ||
| 677 | } | ||
| 678 | } | ||
| 679 | |||
| 680 | // now, add back in some feedback | ||
| 681 | var spanEl = createElement('span'); | ||
| 682 | spanEl.className = 'tableSortArrow'; | ||
| 683 | if (null == previousSortOrder || '' == previousSortOrder || | ||
| 684 | 'DESC' == previousSortOrder) { | ||
| 685 | spanEl.appendChild(document.createTextNode(' \u2191')); | ||
| 686 | spanEl.setAttribute('sortOrder', 'ASC'); | ||
| 687 | } else { | ||
| 688 | spanEl.appendChild(document.createTextNode(' \u2193')); | ||
| 689 | spanEl.setAttribute('sortOrder', 'DESC'); | ||
| 690 | } | ||
| 691 | |||
| 692 | td.appendChild(spanEl); | ||
| 693 | |||
| 694 | return false; | ||
| 695 | }, | ||
| 696 | |||
| 697 | getInnerText : function(el) { | ||
| 698 | |||
| 699 | if ('string' == typeof el || 'undefined' == typeof el) { | ||
| 700 | return el; | ||
| 701 | } | ||
| 702 | |||
| 703 | if (el.innerText) { | ||
| 704 | return el.innerText; // Not needed but it is faster | ||
| 705 | } | ||
| 706 | |||
| 707 | var str = el.getAttribute('standardistaTableSortingInnerText'); | ||
| 708 | if (null != str && '' != str) { | ||
| 709 | return str; | ||
| 710 | } | ||
| 711 | str = ''; | ||
| 712 | |||
| 713 | var cs = el.childNodes; | ||
| 714 | var l = cs.length; | ||
| 715 | for (var i = 0; i < l; i++) { | ||
| 716 | // 'if' is considerably quicker than a 'switch' statement, | ||
| 717 | // in Internet Explorer which translates up to a good time | ||
| 718 | // reduction since this is a very often called recursive function | ||
| 719 | // alert("node " + i + " is [" + cs[i].nodeType + "] [" + | ||
| 720 | // cs[i].nodeValue + "] [" + cs[i].childNodes.length + "]"); | ||
| 721 | if (cs[i].childNodes.length) | ||
| 722 | { | ||
| 723 | str += this.getInnerText(cs[i]); | ||
| 724 | } | ||
| 725 | else if (1 == cs[i].nodeType) { // ELEMENT NODE | ||
| 726 | str += this.getInnerText(cs[i]); | ||
| 727 | } else if (3 == cs[i].nodeType) { //TEXT_NODE | ||
| 728 | str += cs[i].nodeValue; | ||
| 729 | } | ||
| 730 | } | ||
| 731 | |||
| 732 | // set the innertext for this element directly on the element | ||
| 733 | // so that it can be retrieved early next time the innertext | ||
| 734 | // is requested | ||
| 735 | el.setAttribute('standardistaTableSortingInnerText', str); | ||
| 736 | |||
| 737 | return str; | ||
| 738 | }, | ||
| 739 | |||
| 740 | determineSortFunction : function(itm) { | ||
| 741 | |||
| 742 | var sortfn = this.sortCaseInsensitive; | ||
| 743 | |||
| 744 | /* | ||
| 745 | |||
| 746 | Only need the modified numeric column | ||
| 747 | |||
| 748 | if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) { | ||
| 749 | sortfn = this.sortDate; | ||
| 750 | } | ||
| 751 | if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) { | ||
| 752 | sortfn = this.sortDate; | ||
| 753 | } | ||
| 754 | if (itm.match(/^[£$]/)) { | ||
| 755 | sortfn = this.sortCurrency; | ||
| 756 | } | ||
| 757 | if (itm.match(/^\d?\.?\d+$/)) { | ||
| 758 | sortfn = this.sortNumeric; | ||
| 759 | } | ||
| 760 | if (itm.match(/^[+-]?\d*\.?\d+([eE]-?\d+)?$/)) { | ||
| 761 | sortfn = this.sortNumeric; | ||
| 762 | } | ||
| 763 | var defined = '([01]?\\d\\d?|2[0-4]\\d|25[0-5])'; | ||
| 764 | var ipRegex = new RegExp( | ||
| 765 | '^' + defined + '\\.' + defined + '\\.' + | ||
| 766 | defined + '\\.' + defined + '$'); | ||
| 767 | if (itm.match(ipRegex)) { | ||
| 768 | sortfn = this.sortIP; | ||
| 769 | } | ||
| 770 | */ | ||
| 771 | |||
| 772 | // alert("sorting on [" + itm + "]"); | ||
| 773 | if (itm.match(/\d+\.\d+/) || itm == "n/a") { | ||
| 774 | sortfn = this.sortNumeric; | ||
| 775 | } | ||
| 776 | |||
| 777 | return sortfn; | ||
| 778 | }, | ||
| 779 | |||
| 780 | sortCaseInsensitive : function(a, b) { | ||
| 781 | var that = standardistaTableSorting.that; | ||
| 782 | |||
| 783 | var aa = that.getInnerText(a.cells[that.sortColumnIndex]).toLowerCase(); | ||
| 784 | var bb = that.getInnerText(b.cells[that.sortColumnIndex]).toLowerCase(); | ||
| 785 | if (aa==bb) { | ||
| 786 | return 0; | ||
| 787 | } else if (aa<bb) { | ||
| 788 | return -1; | ||
| 789 | } else { | ||
| 790 | return 1; | ||
| 791 | } | ||
| 792 | }, | ||
| 793 | |||
| 794 | sortDate : function(a,b) { | ||
| 795 | var that = standardistaTableSorting.that; | ||
| 796 | |||
| 797 | // y2k notes: two digit years less than 50 are treated as 20XX, | ||
| 798 | // greater than 50 are treated as 19XX | ||
| 799 | var aa = that.getInnerText(a.cells[that.sortColumnIndex]); | ||
| 800 | var bb = that.getInnerText(b.cells[that.sortColumnIndex]); | ||
| 801 | |||
| 802 | var dt1, dt2, yr = -1; | ||
| 803 | |||
| 804 | if (aa.length == 10) { | ||
| 805 | dt1 = aa.substr(6,4)+aa.substr(3,2)+aa.substr(0,2); | ||
| 806 | } else { | ||
| 807 | yr = aa.substr(6,2); | ||
| 808 | if (parseInt(yr) < 50) { | ||
| 809 | yr = '20'+yr; | ||
| 810 | } else { | ||
| 811 | yr = '19'+yr; | ||
| 812 | } | ||
| 813 | dt1 = yr+aa.substr(3,2)+aa.substr(0,2); | ||
| 814 | } | ||
| 815 | |||
| 816 | if (bb.length == 10) { | ||
| 817 | dt2 = bb.substr(6,4)+bb.substr(3,2)+bb.substr(0,2); | ||
| 818 | } else { | ||
| 819 | yr = bb.substr(6,2); | ||
| 820 | if (parseInt(yr) < 50) { | ||
| 821 | yr = '20'+yr; | ||
| 822 | } else { | ||
| 823 | yr = '19'+yr; | ||
| 824 | } | ||
| 825 | dt2 = yr+bb.substr(3,2)+bb.substr(0,2); | ||
| 826 | } | ||
| 827 | |||
| 828 | if (dt1==dt2) { | ||
| 829 | return 0; | ||
| 830 | } else if (dt1<dt2) { | ||
| 831 | return -1; | ||
| 832 | } | ||
| 833 | return 1; | ||
| 834 | }, | ||
| 835 | |||
| 836 | sortCurrency : function(a,b) { | ||
| 837 | var that = standardistaTableSorting.that; | ||
| 838 | |||
| 839 | var aa = that.getInnerText(a.cells[that.sortColumnIndex]) | ||
| 840 | .replace(/[^0-9.]/g,''); | ||
| 841 | var bb = that.getInnerText(b.cells[that.sortColumnIndex]) | ||
| 842 | .replace(/[^0-9.]/g,''); | ||
| 843 | return parseFloat(aa) - parseFloat(bb); | ||
| 844 | }, | ||
| 845 | |||
| 846 | get_val : function(x) { | ||
| 847 | var that = standardistaTableSorting.that; | ||
| 848 | |||
| 849 | var val = that.getInnerText(x.cells[that.sortColumnIndex]); | ||
| 850 | var v = val == "n/a" ? -1 : parseFloat(val); | ||
| 851 | return isNaN(v) ? 0 : v; | ||
| 852 | }, | ||
| 853 | |||
| 854 | sortNumeric : function(a, b) { | ||
| 855 | var that = standardistaTableSorting.that; | ||
| 856 | |||
| 857 | var aval = that.get_val(a); | ||
| 858 | var bval = that.get_val(b); | ||
| 859 | |||
| 860 | return aval - bval; | ||
| 861 | }, | ||
| 862 | |||
| 863 | makeStandardIPAddress : function(val) { | ||
| 864 | var vals = val.split('.'); | ||
| 865 | |||
| 866 | for (x in vals) { | ||
| 867 | val = vals[x]; | ||
| 868 | |||
| 869 | while (3 > val.length) { | ||
| 870 | val = '0'+val; | ||
| 871 | } | ||
| 872 | vals[x] = val; | ||
| 873 | } | ||
| 874 | |||
| 875 | val = vals.join('.'); | ||
| 876 | |||
| 877 | return val; | ||
| 878 | }, | ||
| 879 | |||
| 880 | sortIP : function(a,b) { | ||
| 881 | var that = standardistaTableSorting.that; | ||
| 882 | |||
| 883 | var aa = that.makeStandardIPAddress( | ||
| 884 | that.getInnerText(a.cells[that.sortColumnIndex]).toLowerCase()); | ||
| 885 | var bb = that.makeStandardIPAddress( | ||
| 886 | that.getInnerText(b.cells[that.sortColumnIndex]).toLowerCase()); | ||
| 887 | if (aa==bb) { | ||
| 888 | return 0; | ||
| 889 | } else if (aa<bb) { | ||
| 890 | return -1; | ||
| 891 | } else { | ||
| 892 | return 1; | ||
| 893 | } | ||
| 894 | }, | ||
| 895 | |||
| 896 | moveRows : function(table, newRows) { | ||
| 897 | this.isOdd = false; | ||
| 898 | |||
| 899 | // We appendChild rows that already exist to the tbody, so it moves | ||
| 900 | // them rather than creating new ones | ||
| 901 | for (var i=0;i<newRows.length;i++) { | ||
| 902 | var rowItem = newRows[i]; | ||
| 903 | |||
| 904 | this.doStripe(rowItem); | ||
| 905 | |||
| 906 | table.tBodies[0].appendChild(rowItem); | ||
| 907 | } | ||
| 908 | }, | ||
| 909 | |||
| 910 | doStripe : function(rowItem) { | ||
| 911 | if (this.isOdd) { | ||
| 912 | css.addClassToElement(rowItem, 'odd'); | ||
| 913 | } else { | ||
| 914 | css.removeClassFromElement(rowItem, 'odd'); | ||
| 915 | } | ||
| 916 | |||
| 917 | this.isOdd = !this.isOdd; | ||
| 918 | } | ||
| 919 | |||
| 920 | } | ||
| 921 | |||
| 922 | function standardistaTableSortingInit() { | ||
| 923 | standardistaTableSorting.init(); | ||
| 924 | } | ||
| 925 | |||
| 926 | addEvent(window, 'load', standardistaTableSortingInit) | ||
| 927 | EOF | ||
| 928 | |||
| 929 | 1 | ||
| 930 | |||
| 931 | __END__ |