home *** CD-ROM | disk | FTP | other *** search
- var focusedURLBar;
- var lastBrowserKeyPress = 0;
- var lastURLKeyPress = 0;
- var lastBrowserMouseDown = 0;
- var lastURLMouseDown = 0;
- var lastURLRightMouseDown = 0;
- var lastRegainFocus = 0;
-
- function URLBarFocusHandler(aEvent, aElt)
- {
- if (gIgnoreFocus) {
- gIgnoreFocus = false;
- }
- else if (gClickSelectsAll)
- {
- aElt.select();
- }
-
- // Remember which url bar is in focus, and listen to window key and mouse events until the when we willingly loose the focus grip on url bar
- focusedURLBar = aElt;
- window.addEventListener("keypress", onBrowserKeyPressForUrlbar, false);
- window.addEventListener("mousedown", onBrowserMouseEventForUrlbar, false);
- window.addEventListener("mouseup", onBrowserMouseEventForUrlbar, false);
- }
-
-
- function URLBarBlurHandler(aEvent, aElt) {
- closeURLBarPopup();
-
- if(focusedURLBar == aElt) {
- setTimeout("URLBarRegainFocus()", 10);
- }
- }
-
- // If the URLBar's focus is "stolen", regain the focus back if it should be.
- function URLBarRegainFocus() {
- var now = new Date().getTime();
- var regain;
-
- if(now - Math.max(lastURLKeyPress, lastURLMouseDown) < 500) {
- // if url key or mouse event is recent (0.5 second), regain focus
- regain = true;
- }
- else {
- regain = false;
- /*
- var recentBrowserUserInput = Math.max(lastBrowserKeyPress, lastBrowserMouseDown);
-
- // get Trident plug in doc last mouse down event timestamp
- var hpDoc = getTridentDocument();
- if (hpDoc) {
- recentBrowserUserInput = Math.max(recentBrowserUserInput, hpDoc.lastMouseDown);
- }
-
- // if last browser key or mouse event is recent, loose the grip on url bar
- // otherwise, always regain focus back to url bar
- regain = (now - recentBrowserUserInput > 200);
- */
-
- }
-
- if(regain) {
- // Wanna steal the focus, no way!
- gIgnoreFocus = true;
- lastRegainFocus = new Date().getTime();
- focusedURLBar.focus();
- }
- else {
- // OK, give up the grip on the focus
- focusedURLBar = null;
- window.removeEventListener("keypress", onBrowserKeyPressForUrlbar, false);
- window.removeEventListener("mousedown", onBrowserMouseEventForUrlbar, false);
- window.removeEventListener("mouseup", onBrowserMouseEventForUrlbar, false);
- }
- }
-
- function onBrowserKeyPressForUrlbar(evt) {
- closeURLBarPopup();
- lastBrowserKeyPress = new Date().getTime();
- }
-
- function onBrowserMouseEventForUrlbar(evt) {
- lastBrowserMouseDown = new Date().getTime();
- }
-
- function URLBarKeyPressHandler(aEvent, aElt) {
- if(aEvent.keyCode == 9 || aEvent.keyCode == 10 || aEvent.keyCode == 13) {
- lastURLKeyPress = 0;
- }
- else {
- lastURLKeyPress = new Date().getTime();
- }
- }
-
- function URLBarMouseDownHandler(aEvent, aElt)
- {
- if(aEvent.which == 3) {
- lastURLRightMouseDown = new Date().getTime();
- }
-
- if(focusedURLBar == aElt) {
- lastURLMouseDown = new Date().getTime();
- }
-
- if (aElt.hasAttribute("focused")) {
- gIgnoreClick = true;
- } else {
- gIgnoreFocus = true;
- gIgnoreClick = false;
- aElt.setSelectionRange(0, 0);
- }
- }
-
- function URLBarClickHandler(aEvent, aElt)
- {
- if (!gIgnoreClick && gClickSelectsAll && aElt.selectionStart == aElt.selectionEnd)
- {
- aElt.select();
- }
- }
-
-
- function handleURLBarCommand(aTriggeringEvent)
- {
- var postData = { };
- canonizeUrl(aTriggeringEvent, postData);
-
- try {
- addToUrlbarHistory();
- } catch (ex) {
- // Things may go wrong when adding url to session history,
- // but don't let that interfere with the loading of the url.
- }
-
- BrowserLoadURL(aTriggeringEvent, postData.value);
- // BrowserLoadURL(aTriggeringEvent, postData.value, "browser.tabs.history.open");
- }
-
- // If "ESC" is pressed in the url bar, we replace the urlbar's value with the url of the page
- // and highlight it, unless it is about:blank, where we reset it to "".
- function handleURLBarRevert()
- {
- var url = getWebNavigation().currentURI.spec;
- var throbberElement = document.getElementById("throbberBroadcaster");
-
- var isScrolling = gURLBar.popupOpen;
-
- // don't revert to last valid url unless page is NOT loading
- // and user is NOT key-scrolling through autocomplete list
- if ((!throbberElement || !throbberElement.hasAttribute("busy")) && !isScrolling) {
- if (url != "about:blank") {
- gURLBar.value = url;
- gURLBar.select();
- SetPageProxyState("valid", null); // XXX Build a URI and pass it in here.
- } else { //if about:blank, urlbar becomes ""
- gURLBar.value = "";
- }
- }
-
- gBrowser.userTypedValue = null;
-
- // tell widget to revert to last typed text only if the user
- // was scrolling when they hit escape
- return !isScrolling;
- }
-
- function closeURLBarPopup() {
- try {
- document.getElementById('URLBarPopup').hidePopup();
- }
- catch(ex) { }
- }
-
- function openURLBarPopup() {
- //dump("\n\n BML MERC - urlbar.js openURLBarPopup called...\n\n");
- // JMC - If there's a better Popup already open, just exit
- if (balloonhelp && balloonhelp.isHelpShown())
- {
- return;
- }
-
- // BML - fixes bugzilla 2454 - check if nav history drop down is open and exit if so...
- if (document.getElementById('urlbar').popup.mPopupOpen)
- {
- return;
- }
-
- var now = new Date().getTime();
- if(now - Math.max(lastURLRightMouseDown, lastRegainFocus) < 200) {
- return;
- }
-
- try {
- // check if URLBarPopup preference flag is set
- if (gPrefService === null){
- throw ("No Prefservice...");
- }
- var urlBarPopupOn = gPrefService.getBoolPref("browser.urlbar.topSitesPopup.enabled");
-
- var theURLBar = document.getElementById('urlbar');
- if (!theURLBar){
- throw ("No urlbar found...");
- }
-
- if (urlBarPopupOn) {
- // update the URLBarPopup
- setURLBarPopupButtons();
-
- // show the popup
- var urlPopup = document.getElementById("URLBarPopup");
- if (!urlPopup){
- throw ("No urlPopup found...");
- }
-
- // The urlbarpopup is not friendly with autocompletepopup. Make sure only one shows up
- var autocompletePopupId = document.getElementById("urlbar").getAttribute("autocompletepopup");
- if (autocompletePopupId) {
- var autopopup = document.getElementById(autocompletePopupId);
- if(autopopup) {
- autopopup.hidePopup();
- }
- }
- // show popup
- //dump("\n\n ~~~ BML change- urlbar.js - openURLBarPopup - showing top sites list\n\n");
- urlPopup.showPopup(theURLBar, -1, -1, 'popup', 'bottomleft', 'topleft');
- }
- } catch(ex) {dump("\n\nException thown - urlbar.js: openURLBarPopup:\n" + ex + "\n\n");}
- //dump("\n\n::::: openURLBarPopup finished...\n");
- }
-
- var topSiteSelectedTime = 0;
- function urlPopupSelected(theURI, theCommand) {
- //dump("\n\n::::: urlPopupSelected called...\n");
- topSiteSelectedTime = new Date().getTime();
- var currentTopSite;
- if(theURI) {
- currentTopSite = "loadURI('" + theURI + "');";
- }
- else {
- currentTopSite = theCommand;
- }
- closeURLBarPopup();
- eval(currentTopSite);
- //dump("\n\n::::: urlPopupSelected finished...\n");
- }
-
- function urlPopupClicked(theURI, theCommand) {
- //dump("\n\n::::: urlPopupClicked called...\n");
- // onselect always happen before onclick. If the onselect is not called yet,
- // that is because the user clicked the currently selected item. We need to force to call it now.
- if(new Date().getTime() - topSiteSelectedTime > 200) {
- urlPopupSelected(theURI, theCommand);
- }
- }
-
- // MERC BML Sets the showHistory & showBookmarks menu items in URLBarPopup
- // to state of sidebar panels they open
- function setURLBarPopupButtons(){
- try {
- // get popup checkboxes
- var showHistoryCheckbox = document.getElementById("showHistory-checkbox");
- var showBookmarksCheckbox = document.getElementById("showBookmarks-checkbox");
- if(!showHistoryCheckbox){
- throw("No showHistoryCheckbox found...");
- }
- if(!showBookmarksCheckbox){
- throw("No showBookmarksCheckbox found...");
- }
-
- // set showHistory
- if(isHistoryPanelOpen()){
- showHistoryCheckbox.setAttribute('checked', true);
- } else {
- showHistoryCheckbox.setAttribute('checked', false);
- }
-
- // set showBookmarks
- if(isBookmarksPanelOpen()){
- showBookmarksCheckbox.setAttribute('checked', true);
- } else {
- showBookmarksCheckbox.setAttribute('checked', false);
- }
- } catch(ex) { dump("Exception in urlbar.js: SetURLBarPopupButtons\n" + ex + "\n"); }
- }
-
- // when the popup is hidden destroy the list of topsites
- function destroyTopSitesList()
- {
- //dump("\n\n BML MERC - urlbar.js destroyTopSitesList called...\n\n");
- try{
-
- // hide label
- var topsitesLabel = document.getElementById("topsites-label");
- topsitesLabel.setAttribute("hidden", "true");
-
- // hide separator
- var endSeparator = document.getElementById("endTopSitesSeparator");
- endSeparator.setAttribute("hidden", "true");
-
- // Destroy the items.
- var urlPopup = document.getElementById("URLBarPopup-box");
- var destroy = false;
- for (var i = 0; i < urlPopup.childNodes.length; i++) {
- var item = urlPopup.childNodes[i];
- if (item == endSeparator)
- break;
-
- if (destroy) {
- i--;
- urlPopup.removeChild(item);
- }
-
- if (item == topsitesLabel)
- destroy = true;
- }
- }catch (ex) {dump("Exception thrown in destroyTopSitesList...\n"+ex);}
- //dump("\n\n BML MERC - urlbar.js destroyTopSitesList finished...\n\n");
- }
-
- /*
- MERC BML : when the popup is shown get topsites from history rdf using xpcom service
- & add to urlpopup as menuitems
- */
- function buildTopSitesList()
- {
- try{
- //dump("\n\n BML MERC - urlbar.js buildtopSitesList called...\n\n");
- var URLBarPopup = document.getElementById("URLBarPopup");
-
- // In case the timer didn't fire.
- destroyTopSitesList();
-
- // get a list of topsites from data source
- var siteList = getTopsites();
-
- // should we display topsites?
- if(siteList.length === 0){
- // no history so no topsites
- return;
- }
- // show label
- var topsitesLabel = document.getElementById("topsites-label");
- topsitesLabel.setAttribute("hidden", "false");
-
- // get the end of the topsites list as we'll be adding items in reverse
- var beforeItem = document.getElementById("endTopSitesSeparator");
-
- // show separator while we've got it
- beforeItem.setAttribute("hidden", "false");
-
- // get container for topsites list
- var urlBarPopup = document.getElementById("URLBarPopup-box");
-
- // add menu items to topsites list from bottom to top
- for (var i = siteList.length - 1; i >= 0; i--) {
- //dump("\n\n siteList["+i+"] = "+siteList[i]);
- var aTopsite = siteList[i].split("|");
- var aName = aTopsite[0];
- var aURL = aTopsite[1];
- //dump("\naURL" + i + "= " + aURL +" aName =" +aName+"...\n");
- beforeItem = constructTopSitesMenuItem(urlBarPopup, beforeItem, aURL, aName);
- }
- } catch(ex){dump("\n\n Exception thrown in urlbar.js - buildTopSitesList:\n"+ ex +"...");}
- //dump("\n\n BML MERC - urlbar.js leaving buildTopSitesList...\n\n");
- }
- /*
- MERC BML : this helps buildTopSitesList in creating a topsites
- menuitem and adding it to the urlBarpopup before returning the added
- menuitem.
- Have hard coded the width of the left column based on width
- of URLBar because using structures like listbox caused crashes when removed from popup
- */
- function constructTopSitesMenuItem(urlBarPopup, beforeItem, aURL, aName)
- {
- //dump("\n\n BML MERC - urlbar.js constructTopSitesMenuItem called...\n\n");
-
- // create a menuItem, 2 containers for labels & labels for a topSites entry
- var menuItem = document.createElementNS(XUL_NS, "menuitem");
- var leftHbox = document.createElementNS(XUL_NS, "hbox");
- var rightHbox = document.createElementNS(XUL_NS, "hbox");
-
- var nameLabel = document.createElementNS(XUL_NS, "label");
- var urlLabel = document.createElementNS(XUL_NS, "label");
-
- // set the menuitem
- menuItem.setAttribute("class", "topsites-menuitem");
- menuItem.setAttribute("flex", "1");
- var theCommand = 'urlPopupSelected("'+ aURL +'", null);';
- //dump("\n\ntheCommand = "+theCommand+"\n");
- menuItem.setAttribute("oncommand", theCommand);
-
- // set name & it's container
- leftHbox.setAttribute("id", "topsites-nameBox");
- leftHbox.setAttribute("class", "topsites-leftcolumn");
- var leftColumnWidth = calcTopsitesLeftColumn();
- leftHbox.setAttribute("width", leftColumnWidth);
- nameLabel.setAttribute("class", "topsitesName");
- nameLabel.setAttribute("value", aName);
- nameLabel.setAttribute("crop", "end");
- leftHbox.appendChild(nameLabel);
-
- // set url & it's container
- rightHbox.setAttribute("id", "topsites-urlBox");
- rightHbox.setAttribute("class", "topsites-rightcolumn");
- urlLabel.setAttribute("class", "topsitesURL");
- urlLabel.setAttribute("value", aURL);
- urlLabel.setAttribute("crop", "end");
- rightHbox.appendChild(urlLabel);
-
- // add contents to menuItem
- menuItem.appendChild(leftHbox);
- menuItem.appendChild(rightHbox);
-
- // add menuItem to urlPopup
- urlBarPopup.insertBefore(menuItem, beforeItem);
- //dump("\n\n BML MERC - urlbar.js constructTopSitesMenuItem finished...\n\n");
- return menuItem;
- }
-
- /*
- MERC BML/AAM : helper function that retrieves an array of urls & names
- sorted by their visitcount from history rdf.
- the length of array should be based on pref : browser.urlbar.topSitesPopup.maxLines
- */
- function getTopsites() {
-
- try {
- netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
- const cid = "@mozilla.org/browser/minihistory;1";
- var obj = Components.classes[cid].createInstance(Components.interfaces.nsIMiniHistory);
-
- var count = {}; // ensure this is an object
- var aStrings = obj.getHistory(count);
- //dump('count=' + count.value);
- var i;
- var splitstr;
- for (i=0; i < count.value; i++)
- {
- splitstr = aStrings[i].split("|");
- //dump('index=' + i + '\nName="' + splitstr[0] + '"\nURL=' + splitstr[1]);
- }
- } catch (err) {dump("Exception thrown in urlbar.js - getTopsitese\n" + err);return;}
- return aStrings;
- }
-
- // Helper function for dynamically controlling column width of topsites list
- function calcTopsitesLeftColumn(){
- try{
- var urlBarWidth = document.getElementById('urlbar').boxObject.width;
- // set width of left column to 40% of urlBar (and as an integer)
- var leftColWidth = Math.floor(urlBarWidth * 0.4);
- }catch(ex){dump("Exception thrown in urlbar.js - calcTopsitesLeftColumn\n" + ex);return;}
- return leftColWidth;
- }