| Conditions | 62 |
| Total Lines | 304 |
| Code Lines | 205 |
| Lines | 40 |
| Ratio | 13.16 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like search.js ➔ SearchResults often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | function convertToId(search) |
||
| 404 | function SearchResults(name) |
||
| 405 | { |
||
| 406 | // The number of matches from the last run of <Search()>. |
||
| 407 | this.lastMatchCount = 0; |
||
| 408 | this.lastKey = 0; |
||
| 409 | this.repeatOn = false; |
||
| 410 | |||
| 411 | // Toggles the visibility of the passed element ID. |
||
| 412 | this.FindChildElement = function(id) |
||
| 413 | { |
||
| 414 | var parentElement = document.getElementById(id); |
||
| 415 | var element = parentElement.firstChild; |
||
| 416 | |||
| 417 | while (element && element!=parentElement) |
||
| 418 | { |
||
| 419 | if (element.nodeName == 'DIV' && element.className == 'SRChildren') |
||
| 420 | { |
||
| 421 | return element; |
||
| 422 | } |
||
| 423 | |||
| 424 | if (element.nodeName == 'DIV' && element.hasChildNodes()) |
||
| 425 | { |
||
| 426 | element = element.firstChild; |
||
| 427 | } |
||
| 428 | else if (element.nextSibling) |
||
| 429 | { |
||
| 430 | element = element.nextSibling; |
||
| 431 | } |
||
| 432 | else |
||
| 433 | { |
||
| 434 | do |
||
| 435 | { |
||
| 436 | element = element.parentNode; |
||
| 437 | } |
||
| 438 | while (element && element!=parentElement && !element.nextSibling); |
||
| 439 | |||
| 440 | if (element && element!=parentElement) |
||
| 441 | { |
||
| 442 | element = element.nextSibling; |
||
| 443 | } |
||
| 444 | } |
||
| 445 | } |
||
| 446 | }; |
||
| 447 | |||
| 448 | this.Toggle = function(id) |
||
| 449 | { |
||
| 450 | var element = this.FindChildElement(id); |
||
| 451 | if (element) |
||
| 452 | { |
||
| 453 | if (element.style.display == 'block') |
||
| 454 | { |
||
| 455 | element.style.display = 'none'; |
||
| 456 | } |
||
| 457 | else |
||
| 458 | { |
||
| 459 | element.style.display = 'block'; |
||
| 460 | } |
||
| 461 | } |
||
| 462 | }; |
||
| 463 | |||
| 464 | // Searches for the passed string. If there is no parameter, |
||
| 465 | // it takes it from the URL query. |
||
| 466 | // |
||
| 467 | // Always returns true, since other documents may try to call it |
||
| 468 | // and that may or may not be possible. |
||
| 469 | this.Search = function(search) |
||
| 470 | { |
||
| 471 | if (!search) // get search word from URL |
||
| 472 | { |
||
| 473 | search = window.location.search; |
||
| 474 | search = search.substring(1); // Remove the leading '?' |
||
| 475 | search = unescape(search); |
||
| 476 | } |
||
| 477 | |||
| 478 | search = search.replace(/^ +/, ""); // strip leading spaces |
||
| 479 | search = search.replace(/ +$/, ""); // strip trailing spaces |
||
| 480 | search = search.toLowerCase(); |
||
| 481 | search = convertToId(search); |
||
| 482 | |||
| 483 | var resultRows = document.getElementsByTagName("div"); |
||
| 484 | var matches = 0; |
||
| 485 | |||
| 486 | var i = 0; |
||
| 487 | while (i < resultRows.length) |
||
| 488 | { |
||
| 489 | var row = resultRows.item(i); |
||
| 490 | if (row.className == "SRResult") |
||
| 491 | { |
||
| 492 | var rowMatchName = row.id.toLowerCase(); |
||
| 493 | rowMatchName = rowMatchName.replace(/^sr\d*_/, ''); // strip 'sr123_' |
||
| 494 | |||
| 495 | if (search.length<=rowMatchName.length && |
||
| 496 | rowMatchName.substr(0, search.length)==search) |
||
| 497 | { |
||
| 498 | row.style.display = 'block'; |
||
| 499 | matches++; |
||
| 500 | } |
||
| 501 | else |
||
| 502 | { |
||
| 503 | row.style.display = 'none'; |
||
| 504 | } |
||
| 505 | } |
||
| 506 | i++; |
||
| 507 | } |
||
| 508 | document.getElementById("Searching").style.display='none'; |
||
| 509 | if (matches == 0) // no results |
||
| 510 | { |
||
| 511 | document.getElementById("NoMatches").style.display='block'; |
||
| 512 | } |
||
| 513 | else // at least one result |
||
| 514 | { |
||
| 515 | document.getElementById("NoMatches").style.display='none'; |
||
| 516 | } |
||
| 517 | this.lastMatchCount = matches; |
||
| 518 | return true; |
||
| 519 | }; |
||
| 520 | |||
| 521 | // return the first item with index index or higher that is visible |
||
| 522 | View Code Duplication | this.NavNext = function(index) |
|
| 523 | { |
||
| 524 | var focusItem; |
||
| 525 | while (1) |
||
| 526 | { |
||
| 527 | var focusName = 'Item'+index; |
||
| 528 | focusItem = document.getElementById(focusName); |
||
| 529 | if (focusItem && focusItem.parentNode.parentNode.style.display=='block') |
||
| 530 | { |
||
| 531 | break; |
||
| 532 | } |
||
| 533 | else if (!focusItem) // last element |
||
| 534 | { |
||
| 535 | break; |
||
| 536 | } |
||
| 537 | focusItem=null; |
||
| 538 | index++; |
||
| 539 | } |
||
| 540 | return focusItem; |
||
| 541 | }; |
||
| 542 | |||
| 543 | View Code Duplication | this.NavPrev = function(index) |
|
| 544 | { |
||
| 545 | var focusItem; |
||
| 546 | while (1) |
||
| 547 | { |
||
| 548 | var focusName = 'Item'+index; |
||
| 549 | focusItem = document.getElementById(focusName); |
||
| 550 | if (focusItem && focusItem.parentNode.parentNode.style.display=='block') |
||
| 551 | { |
||
| 552 | break; |
||
| 553 | } |
||
| 554 | else if (!focusItem) // last element |
||
| 555 | { |
||
| 556 | break; |
||
| 557 | } |
||
| 558 | focusItem=null; |
||
| 559 | index--; |
||
| 560 | } |
||
| 561 | return focusItem; |
||
| 562 | }; |
||
| 563 | |||
| 564 | this.ProcessKeys = function(e) |
||
| 565 | { |
||
| 566 | if (e.type == "keydown") |
||
| 567 | { |
||
| 568 | this.repeatOn = false; |
||
| 569 | this.lastKey = e.keyCode; |
||
| 570 | } |
||
| 571 | else if (e.type == "keypress") |
||
| 572 | { |
||
| 573 | if (!this.repeatOn) |
||
| 574 | { |
||
| 575 | if (this.lastKey) this.repeatOn = true; |
||
| 576 | return false; // ignore first keypress after keydown |
||
| 577 | } |
||
| 578 | } |
||
| 579 | else if (e.type == "keyup") |
||
| 580 | { |
||
| 581 | this.lastKey = 0; |
||
| 582 | this.repeatOn = false; |
||
| 583 | } |
||
| 584 | return this.lastKey!=0; |
||
| 585 | }; |
||
| 586 | |||
| 587 | this.Nav = function(evt,itemIndex) |
||
| 588 | { |
||
| 589 | var e = (evt) ? evt : window.event; // for IE |
||
| 590 | if (e.keyCode==13) return true; |
||
| 591 | if (!this.ProcessKeys(e)) return false; |
||
| 592 | |||
| 593 | if (this.lastKey==38) // Up |
||
| 594 | { |
||
| 595 | var newIndex = itemIndex-1; |
||
| 596 | var focusItem = this.NavPrev(newIndex); |
||
| 597 | if (focusItem) |
||
| 598 | { |
||
| 599 | var child = this.FindChildElement(focusItem.parentNode.parentNode.id); |
||
| 600 | if (child && child.style.display == 'block') // children visible |
||
| 601 | { |
||
| 602 | var n=0; |
||
| 603 | var tmpElem; |
||
| 604 | while (1) // search for last child |
||
| 605 | { |
||
| 606 | tmpElem = document.getElementById('Item'+newIndex+'_c'+n); |
||
| 607 | if (tmpElem) |
||
| 608 | { |
||
| 609 | focusItem = tmpElem; |
||
| 610 | } |
||
| 611 | else // found it! |
||
| 612 | { |
||
| 613 | break; |
||
| 614 | } |
||
| 615 | n++; |
||
| 616 | } |
||
| 617 | } |
||
| 618 | } |
||
| 619 | if (focusItem) |
||
| 620 | { |
||
| 621 | focusItem.focus(); |
||
| 622 | } |
||
| 623 | else // return focus to search field |
||
| 624 | { |
||
| 625 | parent.document.getElementById("MSearchField").focus(); |
||
| 626 | } |
||
| 627 | } |
||
| 628 | else if (this.lastKey==40) // Down |
||
| 629 | { |
||
| 630 | var newIndex = itemIndex+1; |
||
| 631 | var focusItem; |
||
| 632 | var item = document.getElementById('Item'+itemIndex); |
||
| 633 | var elem = this.FindChildElement(item.parentNode.parentNode.id); |
||
| 634 | if (elem && elem.style.display == 'block') // children visible |
||
| 635 | { |
||
| 636 | focusItem = document.getElementById('Item'+itemIndex+'_c0'); |
||
| 637 | } |
||
| 638 | if (!focusItem) focusItem = this.NavNext(newIndex); |
||
| 639 | if (focusItem) focusItem.focus(); |
||
| 640 | } |
||
| 641 | else if (this.lastKey==39) // Right |
||
| 642 | { |
||
| 643 | var item = document.getElementById('Item'+itemIndex); |
||
| 644 | var elem = this.FindChildElement(item.parentNode.parentNode.id); |
||
| 645 | if (elem) elem.style.display = 'block'; |
||
| 646 | } |
||
| 647 | else if (this.lastKey==37) // Left |
||
| 648 | { |
||
| 649 | var item = document.getElementById('Item'+itemIndex); |
||
| 650 | var elem = this.FindChildElement(item.parentNode.parentNode.id); |
||
| 651 | if (elem) elem.style.display = 'none'; |
||
| 652 | } |
||
| 653 | else if (this.lastKey==27) // Escape |
||
| 654 | { |
||
| 655 | parent.searchBox.CloseResultsWindow(); |
||
| 656 | parent.document.getElementById("MSearchField").focus(); |
||
| 657 | } |
||
| 658 | else if (this.lastKey==13) // Enter |
||
| 659 | { |
||
| 660 | return true; |
||
| 661 | } |
||
| 662 | return false; |
||
| 663 | }; |
||
| 664 | |||
| 665 | this.NavChild = function(evt,itemIndex,childIndex) |
||
| 666 | { |
||
| 667 | var e = (evt) ? evt : window.event; // for IE |
||
| 668 | if (e.keyCode==13) return true; |
||
| 669 | if (!this.ProcessKeys(e)) return false; |
||
| 670 | |||
| 671 | if (this.lastKey==38) // Up |
||
| 672 | { |
||
| 673 | if (childIndex>0) |
||
| 674 | { |
||
| 675 | var newIndex = childIndex-1; |
||
| 676 | document.getElementById('Item'+itemIndex+'_c'+newIndex).focus(); |
||
| 677 | } |
||
| 678 | else // already at first child, jump to parent |
||
| 679 | { |
||
| 680 | document.getElementById('Item'+itemIndex).focus(); |
||
| 681 | } |
||
| 682 | } |
||
| 683 | else if (this.lastKey==40) // Down |
||
| 684 | { |
||
| 685 | var newIndex = childIndex+1; |
||
| 686 | var elem = document.getElementById('Item'+itemIndex+'_c'+newIndex); |
||
| 687 | if (!elem) // last child, jump to parent next parent |
||
| 688 | { |
||
| 689 | elem = this.NavNext(itemIndex+1); |
||
| 690 | } |
||
| 691 | if (elem) |
||
| 692 | { |
||
| 693 | elem.focus(); |
||
| 694 | } |
||
| 695 | } |
||
| 696 | else if (this.lastKey==27) // Escape |
||
| 697 | { |
||
| 698 | parent.searchBox.CloseResultsWindow(); |
||
| 699 | parent.document.getElementById("MSearchField").focus(); |
||
| 700 | } |
||
| 701 | else if (this.lastKey==13) // Enter |
||
| 702 | { |
||
| 703 | return true; |
||
| 704 | } |
||
| 705 | return false; |
||
| 706 | } |
||
| 707 | } |
||
| 708 | |||
| 792 |