1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
|
# -*- Mode: HTML -*-
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is mozilla.org viewsource frontend.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 2003
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Blake Ross <blake@cs.stanford.edu> (Original Author)
# Masayuki Nakano <masayuki@d-toybox.com>
# Ben Basson <contact@cusser.net>
# Jason Barnabe <jason_barnabe@fastmail.fm>
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the LGPL or the GPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
#***** END LICENSE BLOCK ***** -->
const FIND_NORMAL = 0;
const FIND_TYPEAHEAD = 1;
const FIND_LINKS = 2;
const CHAR_CODE_SPACE = " ".charCodeAt(0);
const CHAR_CODE_SLASH = "/".charCodeAt(0);
const CHAR_CODE_APOSTROPHE = "'".charCodeAt(0);
/*
* WARNING:
* Don't define global variables and functions in this file. Because this file
* is included by some files. So, the global name space is not safety.
*
* But we need global functions for the Timer Event Handlers or for the Event
* Listener Handlers. In this case, we MUST define global functions with
* "findBar_" prefix.
*/
var gFindBar = {
SELECTION_CONTROLLER: Components.interfaces.nsISelectionController,
mFindEnabled: true,
mFindMode: FIND_NORMAL,
mFoundLink: null,
mFoundEditable: null,
mCurrentWindow: null,
mTmpOutline: null,
mTmpOutlineOffset: "0",
mDrawOutline: false,
mQuickFindTimeout: null,
mQuickFindTimeoutLength: 0,
mHighlightTimeout: null,
mUseTypeAheadFind: false,
mWrappedToTopStr: "",
mWrappedToBottomStr: "",
mNotFoundStr: "",
mCaseSensitiveStr: "",
mFlashFindBar: 0,
mFlashFindBarCount: 6,
mFlashFindBarTimeout: null,
mLastHighlightString: "",
mTypeAheadLinksOnly: false,
mTypeAheadCaseSensitive: -1,
mIsIMEComposing: false,
mTextToSubURIService: null,
// DOMRange used during highlighting
mSearchRange: null,
mStartPt: null,
mEndPt: null,
mTypeAheadFind: {
useTAFPref: "accessibility.typeaheadfind",
searchLinksPref: "accessibility.typeaheadfind.linksonly",
caseSensePref: "accessibility.typeaheadfind.casesensitive",
observe: function(aSubject, aTopic, aPrefName)
{
if (aTopic != "nsPref:changed" ||
(aPrefName != this.useTAFPref && aPrefName != this.searchLinksPref &&
aPrefName != this.caseSensePref))
return;
var prefService =
Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
gFindBar.mUseTypeAheadFind =
prefService.getBoolPref("accessibility.typeaheadfind");
gFindBar.mTypeAheadLinksOnly =
prefService.getBoolPref("accessibility.typeaheadfind.linksonly");
gFindBar.mTypeAheadCaseSensitive =
prefService.getIntPref("accessibility.typeaheadfind.casesensitive");
gFindBar.setCaseSensitivity();
}
},
initFindBar: function ()
{
getBrowser().addEventListener("keypress",
findBar_OnBrowserKeyPress, false);
getBrowser().addEventListener("mouseup",
findBar_OnBrowserMouseUp, false);
var prefService =
Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
var pbi = prefService.QueryInterface(Components.interfaces.nsIPrefBranch2);
this.mQuickFindTimeoutLength =
prefService.getIntPref("accessibility.typeaheadfind.timeout");
this.mFlashFindBar =
prefService.getIntPref("accessibility.typeaheadfind.flashBar");
pbi.addObserver(this.mTypeAheadFind.useTAFPref,
this.mTypeAheadFind, false);
pbi.addObserver(this.mTypeAheadFind.searchLinksPref,
this.mTypeAheadFind, false);
pbi.addObserver(this.mTypeAheadFind.caseSensePref,
this.mTypeAheadFind, false);
this.mUseTypeAheadFind =
prefService.getBoolPref("accessibility.typeaheadfind");
this.mTypeAheadLinksOnly =
prefService.getBoolPref("accessibility.typeaheadfind.linksonly");
this.mTypeAheadCaseSensitive =
prefService.getIntPref("accessibility.typeaheadfind.casesensitive");
var findField = document.getElementById("find-field");
findField.addEventListener("dragdrop", findBar_OnDrop, true);
this.mTextToSubURIService =
Components.classes["@mozilla.org/intl/texttosuburi;1"]
.getService(Components.interfaces.nsITextToSubURI);
},
mFindbarObserver: {
onDrop: function (aEvent, aXferData, aDragSession)
{
var findField = document.getElementById("find-field");
findField.value = aXferData.data;
gFindBar.find(aXferData.data);
},
getSupportedFlavours: function ()
{
var flavourSet = new FlavourSet();
flavourSet.appendFlavour("text/unicode");
return flavourSet;
}
},
uninitFindBar: function ()
{
var prefService =
Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
var pbi = prefService.QueryInterface(Components.interfaces.nsIPrefBranch2);
pbi.removeObserver(this.mTypeAheadFind.useTAFPref,
this.mTypeAheadFind);
pbi.removeObserver(this.mTypeAheadFind.searchLinksPref,
this.mTypeAheadFind);
pbi.removeObserver(this.mTypeAheadFind.caseSensePref,
this.mTypeAheadFind);
getBrowser().removeEventListener("keypress",
findBar_OnBrowserKeyPress, false);
getBrowser().removeEventListener("mouseup",
findBar_OnBrowserMouseUp, false);
},
/**
* Turns highlighting on or off.
*
* @param aHighlight true to turn highlighting on
*/
toggleHighlight: function (aHighlight)
{
var word = document.getElementById("find-field").value;
if (aHighlight) {
// We have to update the status because we might still have the status
// of another tab
if (this.highlightDoc('yellow', 'black', word))
this.updateStatus(Components.interfaces.nsITypeAheadFind.FIND_FOUND, false);
else
this.updateStatus(Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND, false);
} else {
this.highlightDoc(null, null, null);
this.mLastHighlightString = null;
}
},
/**
* (Un)highlights each instance of the searched word in the passed window's content.
*
* @param highBackColor the background color for the highlight
* @param highTextColor the text color for the highlight, or null to make it unhighlight
* @param word the word to search for
* @param win the window to search in. Passing undefined will search the current window.
* @return true if the text was found
*/
highlightDoc: function (highBackColor, highTextColor, word, win)
{
if (!win)
win = window.content;
var textFound = false;
for (var i = 0; win.frames && i < win.frames.length; i++) {
if (this.highlightDoc(highBackColor, highTextColor, word, win.frames[i]))
textFound = true;
}
var doc = win.document;
if (!doc || !("body" in doc))
return textFound;
var body = doc.body;
var count = body.childNodes.length;
this.mSearchRange = doc.createRange();
this.mStartPt = doc.createRange();
this.mEndPt = doc.createRange();
this.mSearchRange.setStart(body, 0);
this.mSearchRange.setEnd(body, count);
this.mStartPt.setStart(body, 0);
this.mStartPt.setEnd(body, 0);
this.mEndPt.setStart(body, count);
this.mEndPt.setEnd(body, count);
if (!highBackColor) {
// Remove highlighting. We use the find API again rather than
// searching for our span elements by id so that we gain access to the
// anonymous content that nsIFind searches.
if (!this.mLastHighlightString)
return textFound;
var retRange = null;
var finder = Components.classes["@mozilla.org/embedcomp/rangefind;1"]
.createInstance()
.QueryInterface(Components.interfaces.nsIFind);
while ((retRange = finder.Find(this.mLastHighlightString,
this.mSearchRange, this.mStartPt,
this.mEndPt))) {
var startContainer = retRange.startContainer;
var elem = null;
try {
elem = startContainer.parentNode;
}
catch (e) { }
if (elem && elem.getAttribute("id") == "__firefox-findbar-search-id") {
var child = null;
var docfrag = doc.createDocumentFragment();
var next = elem.nextSibling;
var parent = elem.parentNode;
while ((child = elem.firstChild)) {
docfrag.appendChild(child);
}
this.mStartPt = doc.createRange();
this.mStartPt.setStartAfter(elem);
parent.removeChild(elem);
parent.insertBefore(docfrag, next);
parent.normalize();
}
else {
// Somehow we didn't highlight this instance; just skip it.
this.mStartPt = doc.createRange();
this.mStartPt.setStart(retRange.endContainer, retRange.endOffset);
}
this.mStartPt.collapse(true);
textFound = true;
}
return textFound;
}
var baseNode = doc.createElement("span");
baseNode.setAttribute("style", "background-color: " + highBackColor + "; " +
"color: " + highTextColor + "; " +
"display: inline; font-size: inherit;" +
" padding: 0;");
baseNode.setAttribute("id", "__firefox-findbar-search-id");
return this.highlightText(word, baseNode) || textFound;
},
/**
* Highlights each instance of the searched word in the current range.
*
* @param word the word to search for
* @param baseNode a node to use as a template for what will replace the
* searched word
* @return true if the text was found
*/
highlightText: function (word, baseNode)
{
var retRange = null;
var finder = Components.classes["@mozilla.org/embedcomp/rangefind;1"]
.createInstance()
.QueryInterface(Components.interfaces.nsIFind);
finder.caseSensitive = this.shouldBeCaseSensitive(word);
var textFound = false;
while((retRange = finder.Find(word, this.mSearchRange,
this.mStartPt, this.mEndPt))) {
// Highlight
var nodeSurround = baseNode.cloneNode(true);
var node = this.highlight(retRange, nodeSurround);
this.mStartPt = node.ownerDocument.createRange();
this.mStartPt.setStart(node, node.childNodes.length);
this.mStartPt.setEnd(node, node.childNodes.length);
textFound = true;
}
this.mLastHighlightString = word;
return textFound;
},
/**
* Highlights the word in the passed range.
*
* @param range the range that contains the word to highlight
* @param node the node replace the searched word with
* @return the node that replaced the searched word
*/
highlight: function (range, node)
{
var startContainer = range.startContainer;
var startOffset = range.startOffset;
var endOffset = range.endOffset;
var docfrag = range.extractContents();
var before = startContainer.splitText(startOffset);
var parent = before.parentNode;
node.appendChild(docfrag);
parent.insertBefore(node, before);
return node;
},
setCaseSensitivity: function (val)
{
var findToolbar = document.getElementById("FindToolbar");
if (findToolbar.hidden)
return;
if (!val)
val = document.getElementById("find-field").value;
var matchCaseCheckbox = document.getElementById("find-case-sensitive");
matchCaseCheckbox.checked = this.shouldBeCaseSensitive(val);
var matchCaseText = document.getElementById("match-case-status");
matchCaseText.value = matchCaseCheckbox.checked ?
this.mCaseSensitiveStr : "";
// Show the checkbox on the full Find bar in non-auto mode. Show the label
// in all other cases.
matchCaseCheckbox.hidden = this.mUsingMinimalUI ||
(this.mTypeAheadCaseSensitive != 0 && this.mTypeAheadCaseSensitive != 1);
matchCaseText.hidden = !matchCaseCheckbox.hidden;
var fastFind = getBrowser().fastFind;
fastFind.caseSensitive = matchCaseCheckbox.checked;
},
toggleCaseSensitiveCheckbox: function (aCaseSensitive)
{
var prefService =
Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
// Just set the pref; our observer will change the find bar behavior
prefService.setIntPref("accessibility.typeaheadfind.casesensitive",
aCaseSensitive ? 1 : 0);
},
/**
* Opens and displays the find bar.
*
* @param showMinimalUI
* true if the find bar is to contain minimalist UI, false (or
* undefined, as happens if no arguments are provided) if the find
* bar is to display more generalized search UI
* @returns bool
* true if the find bar wasn't previously open, false otherwise
*/
openFindBar: function (showMinimalUI)
{
// Notify anyone else that might want to handle this event
var findActivatedEvent = document.createEvent("Events");
findActivatedEvent.initEvent("find-activated", false, true);
window.dispatchEvent(findActivatedEvent);
if (!this.mFindEnabled)
throw Components.results.NS_OK;
if (!this.mNotFoundStr || !this.mWrappedToTopStr ||
!this.mWrappedToBottomStr || !this.mNormalFindStr ||
!this.mFastFindStr || !this.mCaseSensitiveStr) {
var bundle = document.getElementById("bundle_findBar");
this.mNotFoundStr = bundle.getString("NotFound");
this.mWrappedToTopStr = bundle.getString("WrappedToTop");
this.mWrappedToBottomStr = bundle.getString("WrappedToBottom");
this.mNormalFindStr = bundle.getString("NormalFindLabel");
this.mFastFindStr = bundle.getString("FastFindLabel");
this.mCaseSensitiveStr = bundle.getString("CaseSensitive");
}
this.updateFindUI(showMinimalUI);
var findToolbar = document.getElementById("FindToolbar");
if (findToolbar.hidden) {
findToolbar.hidden = false;
var findField = document.getElementById("find-field");
this.setCaseSensitivity(findField.value);
this.updateStatus(Components.interfaces.nsITypeAheadFind.FIND_FOUND, false);
return true;
}
return false;
},
focusFindBar: function ()
{
var findField = document.getElementById("find-field");
findField.focus();
},
selectFindBar: function ()
{
var findField = document.getElementById("find-field");
findField.select();
},
closeFindBar: function ()
{
// ensure the dom is ready...
setTimeout(findBar_DelayedCloseFindBar, 0);
},
fireKeypressEvent: function (target, evt)
{
if (!target)
return;
var event = document.createEvent("KeyEvents");
event.initKeyEvent(evt.type, evt.canBubble, evt.cancelable,
evt.view, evt.ctrlKey, evt.altKey, evt.shiftKey,
evt.metaKey, evt.keyCode, evt.charCode);
target.dispatchEvent(event);
},
updateStatusBar: function ()
{
var xulBrowserWindow = window.XULBrowserWindow;
if (!xulBrowserWindow)
return false;
if (!this.mFoundLink || !this.mFoundLink.href ||
this.mFoundLink.href == "") {
xulBrowserWindow.setOverLink("");
return true;
}
var docCharset = "";
var ownerDoc = this.mFoundLink.ownerDocument;
if (ownerDoc)
docCharset = ownerDoc.characterSet;
var url =
this.mTextToSubURIService.unEscapeURIForUI(docCharset,
this.mFoundLink.href);
xulBrowserWindow.setOverLink(url);
return true;
},
setFoundLink: function (foundLink)
{
if (this.mFoundLink == foundLink)
return;
if (this.mFoundLink && this.mDrawOutline) {
// restore original outline
this.mFoundLink.style.outline = this.mTmpOutline;
this.mFoundLink.style.outlineOffset = this.mTmpOutlineOffset;
}
this.mDrawOutline = (foundLink && this.mFindMode != FIND_NORMAL);
if (this.mDrawOutline) {
// backup original outline
this.mTmpOutline = foundLink.style.outline;
this.mTmpOutlineOffset = foundLink.style.outlineOffset;
// draw pseudo focus rect
// XXX Should we change the following style for FAYT pseudo focus?
// XXX Shouldn't we change default design if outline is visible already?
foundLink.style.outline = "1px dotted invert";
foundLink.style.outlineOffset = "0";
}
this.mFoundLink = foundLink;
// We should update status bar. But we need delay. If the mouse cursor is
// on the document, the status bar text is changed by mouse event that is
// fired by scroll event. So, we need to change the status bar text after
// mouse event.
if (this.mFindMode != FIND_NORMAL)
setTimeout(findBar_UpdateStatusBar, 0);
},
finishFAYT: function (aKeypressEvent)
{
try {
if (this.mFoundLink)
this.mFoundLink.focus();
else if (this.mFoundEditable) {
this.mFoundEditable.focus();
var fastFind = getBrowser().fastFind;
fastFind.collapseSelection();
} else if (this.mCurrentWindow)
this.mCurrentWindow.focus();
else
return false;
}
catch(e) {
return false;
}
if (aKeypressEvent)
aKeypressEvent.preventDefault();
this.closeFindBar();
return true;
},
delayedCloseFindBar: function ()
{
var findField = document.getElementById("find-field");
var findToolbar = document.getElementById("FindToolbar");
var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
.getService(Components.interfaces.nsIWindowWatcher);
if (window == ww.activeWindow) {
var focusedElement = document.commandDispatcher.focusedElement;
if (focusedElement && focusedElement.parentNode &&
(focusedElement.parentNode == findToolbar ||
focusedElement.parentNode.parentNode == findField)) {
// block scrolling on focus since find already scrolls, further
// scrolling is due to user action, so don't override this
var suppressedScroll = document.commandDispatcher.suppressFocusScroll;
document.commandDispatcher.suppressFocusScroll = true;
// We MUST reset suppressFocusScroll.
try {
if (this.mFoundLink)
this.mFoundLink.focus();
else if (this.mFoundEditable)
this.mFoundEditable.focus();
else if (this.mCurrentWindow)
this.mCurrentWindow.focus();
else
window.content.focus();
}
catch(e) {
// Retry to set focus.
try {
window.content.focus();
}
catch(e) { /* We lose focused element! */ }
}
document.commandDispatcher.suppressFocusScroll = suppressedScroll;
}
}
findToolbar.hidden = true;
var fastFind = getBrowser().fastFind;
fastFind.setSelectionModeAndRepaint(this.SELECTION_CONTROLLER.SELECTION_ON);
this.setFoundLink(null);
this.mFoundEditable = null;
this.mCurrentWindow = null;
if (this.mQuickFindTimeout) {
clearTimeout(this.mQuickFindTimeout);
this.mQuickFindTimeout = null;
}
},
shouldFastFind: function (evt)
{
if (evt.ctrlKey || evt.altKey || evt.metaKey || evt.getPreventDefault())
return false;
var elt = document.commandDispatcher.focusedElement;
if (elt) {
if (elt instanceof HTMLInputElement) {
// block FAYT when an <input> textfield element is focused
var inputType = elt.type;
switch (inputType) {
case "text":
case "password":
case "file":
return false;
}
}
else if (elt instanceof HTMLTextAreaElement ||
elt instanceof HTMLSelectElement ||
elt instanceof HTMLIsIndexElement ||
elt instanceof HTMLObjectElement ||
elt instanceof HTMLEmbedElement)
return false;
}
// disable FAYT in about:config and about:blank to prevent FAYT opening
// unexpectedly - to fix bugs 264562, 267150, 269712
var url = getBrowser().currentURI.spec;
if (url == "about:blank" || url == "about:config")
return false;
var win = document.commandDispatcher.focusedWindow;
if (win && win.document.designMode == "on")
return false;
return true;
},
shouldBeCaseSensitive: function (str)
{
if (this.mTypeAheadCaseSensitive == 0)
return false;
if (this.mTypeAheadCaseSensitive == 1)
return true;
return (str != str.toLowerCase());
},
onFindBarBlur: function ()
{
var fastFind = getBrowser().fastFind;
if (this.mFoundEditable) {
fastFind.collapseSelection();
} else {
fastFind.setSelectionModeAndRepaint(this.SELECTION_CONTROLLER.SELECTION_ON);
}
this.setFoundLink(null);
this.mFoundEditable = null;
this.mCurrentWindow = null;
},
onBrowserMouseUp: function (evt)
{
var findToolbar = document.getElementById("FindToolbar");
if (!findToolbar.hidden && this.mFindMode != FIND_NORMAL)
this.closeFindBar();
},
onBrowserKeyPress: function (evt)
{
// Check focused elt
if (!this.shouldFastFind(evt))
return;
var findField = document.getElementById("find-field");
if (this.mFindMode != FIND_NORMAL && this.mQuickFindTimeout) {
if (!evt.charCode)
return;
this.selectFindBar();
this.focusFindBar();
this.fireKeypressEvent(findField.inputField, evt);
evt.preventDefault();
return;
}
if (evt.charCode == CHAR_CODE_APOSTROPHE ||
evt.charCode == CHAR_CODE_SLASH ||
(this.mUseTypeAheadFind &&
evt.charCode && evt.charCode != CHAR_CODE_SPACE)) {
var findMode =
(evt.charCode == CHAR_CODE_APOSTROPHE ||
(this.mTypeAheadLinksOnly && evt.charCode != CHAR_CODE_SLASH)) ?
FIND_LINKS : FIND_TYPEAHEAD;
this.setFindMode(findMode);
// Clear bar first, so that when openFindBar() calls setCaseSensitivity()
// it doesn't get confused by a lingering value
findField.value = "";
try {
var opened = this.openFindBar(true);
}
catch (e) {
return;
}
if (opened) {
this.setFindCloseTimeout();
this.selectFindBar();
this.focusFindBar();
if (this.mUseTypeAheadFind &&
evt.charCode != CHAR_CODE_APOSTROPHE &&
evt.charCode != CHAR_CODE_SLASH)
this.fireKeypressEvent(findField.inputField, evt);
else
this.updateStatus(Components.interfaces.nsITypeAheadFind.FIND_FOUND, false);
evt.preventDefault();
}
else {
this.selectFindBar();
this.focusFindBar();
if (this.mFindMode != FIND_NORMAL) {
if (evt.charCode != CHAR_CODE_APOSTROPHE &&
evt.charCode != CHAR_CODE_SLASH)
this.fireKeypressEvent(findField.inputField, evt);
else
this.updateStatus(Components.interfaces.nsITypeAheadFind.FIND_FOUND, false);
evt.preventDefault();
}
}
}
},
onFindBarKeyPress: function (evt)
{
if (evt.keyCode == KeyEvent.DOM_VK_RETURN) {
if (this.mFindMode == FIND_NORMAL) {
var findString = document.getElementById("find-field");
if (!findString.value)
return;
#ifdef XP_MACOSX
if (evt.metaKey) {
#else
if (evt.ctrlKey) {
#endif
document.getElementById("highlight").click();
return;
}
if (evt.shiftKey)
this.findPrevious();
else
this.findNext();
}
else {
if (this.mFoundLink) {
var tmpLink = this.mFoundLink;
if (this.finishFAYT(evt))
this.fireKeypressEvent(tmpLink, evt);
}
}
}
else if (evt.keyCode == KeyEvent.DOM_VK_TAB) {
var shouldHandle = !evt.altKey && !evt.ctrlKey && !evt.metaKey;
if (shouldHandle && this.mFindMode != FIND_NORMAL &&
this.finishFAYT(evt)) {
if (evt.shiftKey)
document.commandDispatcher.rewindFocus();
else
document.commandDispatcher.advanceFocus();
}
}
else if (evt.keyCode == KeyEvent.DOM_VK_ESCAPE) {
this.closeFindBar();
evt.preventDefault();
}
else if (evt.keyCode == KeyEvent.DOM_VK_PAGE_UP) {
window.top._content.scrollByPages(-1);
evt.preventDefault();
}
else if (evt.keyCode == KeyEvent.DOM_VK_PAGE_DOWN) {
window.top._content.scrollByPages(1);
evt.preventDefault();
}
else if (evt.keyCode == KeyEvent.DOM_VK_UP) {
window.top._content.scrollByLines(-1);
evt.preventDefault();
}
else if (evt.keyCode == KeyEvent.DOM_VK_DOWN) {
window.top._content.scrollByLines(1);
evt.preventDefault();
}
},
enableFindButtons: function (aEnable)
{
var findNext = document.getElementById("find-next");
var findPrev = document.getElementById("find-previous");
var highlight = document.getElementById("highlight");
findNext.disabled = findPrev.disabled = highlight.disabled = !aEnable;
},
/**
* Determines whether minimalist or general-purpose search UI is to be
* displayed when the find bar is activated.
*
* @param showMinimalUI
* true if minimalist UI should be used, false if general-purpose UI
* should be used
*/
updateFindUI: function (showMinimalUI)
{
this.mUsingMinimalUI = showMinimalUI;
var findBar = document.getElementById("FindToolbar");
for (var i = 0; i < findBar.childNodes.length; i++) {
var node = findBar.childNodes[i];
if (node.className == "find-fast")
continue;
node.hidden = showMinimalUI;
}
var findLabel = document.getElementById("find-label");
if (showMinimalUI)
findLabel.value = this.mFastFindStr;
else
findLabel.value = this.mNormalFindStr;
},
updateFoundLink: function (res)
{
var val = document.getElementById("find-field").value;
if (res == Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND || !val) {
this.setFoundLink(null);
this.mFoundEditable = null;
this.mCurrentWindow = null;
} else {
this.setFoundLink(getBrowser().fastFind.foundLink);
this.mFoundEditable = getBrowser().fastFind.foundEditable;
this.mCurrentWindow = getBrowser().fastFind.currentWindow;
}
},
find: function (val)
{
if (!val)
val = document.getElementById("find-field").value;
this.enableFindButtons(val);
var highlightBtn = document.getElementById("highlight");
if (highlightBtn.checked)
this.setHighlightTimeout();
this.setCaseSensitivity(val);
var fastFind = getBrowser().fastFind;
var res = fastFind.find(val, this.mFindMode == FIND_LINKS);
this.updateFoundLink(res);
this.updateStatus(res, true);
if (this.mFindMode != FIND_NORMAL)
this.setFindCloseTimeout();
},
flashFindBar: function ()
{
var findToolbar = document.getElementById("FindToolbar");
if (this.mFlashFindBarCount-- == 0) {
clearInterval(this.mFlashFindBarTimeout);
findToolbar.removeAttribute("flash");
this.mFlashFindBarCount = 6;
return;
}
findToolbar.setAttribute("flash",
(this.mFlashFindBarCount % 2 == 0) ?
"false" : "true");
},
onFindCmd: function ()
{
try {
this.openFindBar();
}
catch (e) {
}
this.setFindMode(FIND_NORMAL);
if (this.mFlashFindBar) {
this.mFlashFindBarTimeout = setInterval(findBar_FlashFindBar, 500);
var prefService =
Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
prefService.setIntPref("accessibility.typeaheadfind.flashBar",
--this.mFlashFindBar);
}
this.selectFindBar();
this.focusFindBar();
},
onFindAgainCmd: function ()
{
var findString = getBrowser().findString;
if (!findString) {
this.onFindCmd();
return;
}
var res = this.findNext();
if (res == Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND) {
try {
var opened = this.openFindBar(this.mUsingMinimalUI);
}
catch(e) {
}
if (opened) {
this.focusFindBar();
this.selectFindBar();
if (this.mFindMode != FIND_NORMAL)
this.setFindCloseTimeout();
this.updateStatus(res, true);
}
}
},
onFindPreviousCmd: function ()
{
var findString = getBrowser().findString;
if (!findString) {
this.onFindCmd();
return;
}
var res = this.findPrevious();
if (res == Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND) {
try {
var opened = this.openFindBar(this.mUsingMinimalUI);
}
catch (e) {
}
if (opened) {
this.focusFindBar();
this.selectFindBar();
if (this.mFindMode != FIND_NORMAL)
this.setFindCloseTimeout();
this.updateStatus(res, false);
}
}
},
setHighlightTimeout: function ()
{
if (this.mHighlightTimeout)
clearTimeout(this.mHighlightTimeout);
this.mHighlightTimeout =
setTimeout(function() {
gFindBar.toggleHighlight(false);
gFindBar.toggleHighlight(true);
}, 500);
},
isFindBarVisible: function ()
{
var findBar = document.getElementById("FindToolbar");
return !findBar.hidden;
},
findNext: function ()
{
var fastFind = getBrowser().fastFind;
var res = fastFind.findNext();
this.updateFoundLink(res);
this.updateStatus(res, true);
if (this.mFindMode != FIND_NORMAL && this.isFindBarVisible())
this.setFindCloseTimeout();
return res;
},
findPrevious: function ()
{
var fastFind = getBrowser().fastFind;
var res = fastFind.findPrevious();
this.updateFoundLink(res);
this.updateStatus(res, false);
if (this.mFindMode != FIND_NORMAL && this.isFindBarVisible())
this.setFindCloseTimeout();
return res;
},
updateStatus: function (res, findNext)
{
var findBar = document.getElementById("FindToolbar");
var field = document.getElementById("find-field");
var statusIcon = document.getElementById("find-status-icon");
var statusText = document.getElementById("find-status");
switch(res) {
case Components.interfaces.nsITypeAheadFind.FIND_WRAPPED:
statusIcon.setAttribute("status", "wrapped");
statusText.value =
findNext ? this.mWrappedToTopStr : this.mWrappedToBottomStr;
break;
case Components.interfaces.nsITypeAheadFind.FIND_NOTFOUND:
statusIcon.setAttribute("status", "notfound");
statusText.value = this.mNotFoundStr;
field.setAttribute("status", "notfound");
break;
case Components.interfaces.nsITypeAheadFind.FIND_FOUND:
default:
statusIcon.removeAttribute("status");
statusText.value = "";
field.removeAttribute("status");
break;
}
},
setFindCloseTimeout: function ()
{
if (this.mQuickFindTimeout)
clearTimeout(this.mQuickFindTimeout);
// Don't close the find toolbar while IME is composing.
if (this.mIsIMEComposing) {
this.mQuickFindTimeout = null;
return;
}
this.mQuickFindTimeout =
setTimeout(function() {
if (gFindBar.mFindMode != FIND_NORMAL)
gFindBar.closeFindBar();
},
this.mQuickFindTimeoutLength);
},
findBarOnDrop: function (evt)
{
nsDragAndDrop.drop(evt, this.mFindbarObserver);
},
onFindBarCompositionStart: function (evt)
{
this.mIsIMEComposing = true;
// Don't close the find toolbar while IME is composing.
if (this.mQuickFindTimeout) {
clearTimeout(this.mQuickFindTimeout);
this.mQuickFindTimeout = null;
}
},
onFindBarCompositionEnd: function (evt)
{
this.mIsIMEComposing = false;
if (this.mFindMode != FIND_NORMAL && this.isFindBarVisible())
this.setFindCloseTimeout();
},
setFindMode: function (mode)
{
this.mFindMode = mode;
}
};
// ================
// Event Handlers
// ================
function findBar_OnDrop(aEvt)
{
gFindBar.findBarOnDrop(aEvt);
}
function findBar_OnBrowserKeyPress(aEvt)
{
gFindBar.onBrowserKeyPress(aEvt);
}
function findBar_OnBrowserMouseUp(aEvt)
{
gFindBar.onBrowserMouseUp(aEvt);
}
// ======================
// Timer Event Handlers
// ======================
function findBar_DelayedCloseFindBar()
{
gFindBar.delayedCloseFindBar();
}
function findBar_UpdateStatusBar()
{
gFindBar.updateStatusBar();
}
function findBar_FlashFindBar()
{
gFindBar.flashFindBar();
}
|