home *** CD-ROM | disk | FTP | other *** search
/ PC Advisor 2006 January / PCA126_DVD.iso / internet / nsb-setup.exe / chrome / browser.jar / content / browser / history / history.js < prev   
Encoding:
JavaScript  |  2004-12-16  |  11.1 KB  |  353 lines

  1. // The history window uses JavaScript in bookmarksManager.js too.
  2.  
  3. var gHistoryTree;
  4. var gGlobalHistory;
  5. var gSearchBox;
  6. var gHistoryGrouping = "";
  7.  
  8. function HistoryCommonInit()
  9. {
  10.     gHistoryTree =  document.getElementById("historyTree");    
  11.     gSearchBox = document.getElementById("search-box");
  12.  
  13.     var treeController = new nsTreeController(gHistoryTree);
  14.     gHistoryGrouping = document.getElementById("viewButton").getAttribute("selectedsort");
  15.     if (gHistoryGrouping == "site")
  16.       document.getElementById("bysite").setAttribute("checked", "true");
  17.     else if (gHistoryGrouping == "visited")
  18.       document.getElementById("byvisited").setAttribute("checked", "true");
  19.     else if (gHistoryGrouping == "lastvisited")
  20.       document.getElementById("bylastvisited").setAttribute("checked", "true");
  21.     else if (gHistoryGrouping == "dayandsite")
  22.       document.getElementById("bydayandsite").setAttribute("checked", "true");
  23.     else
  24.       document.getElementById("byday").setAttribute("checked", "true");
  25.     
  26.     // XXXBlake we should persist the last search value
  27.     // If it's empty, this will do the right thing and just group by the old grouping.
  28.     searchHistory(gSearchBox.value);
  29. }
  30.  
  31. function historyOnSelect()
  32. {
  33.   document.commandDispatcher.updateCommands("select");
  34. }
  35.  
  36. var historyDNDObserver = {
  37.     onDragStart: function (aEvent, aXferData, aDragAction)
  38.     {
  39.         var currentIndex = gHistoryTree.currentIndex;
  40.         if (isContainer(gHistoryTree, currentIndex))
  41.             return false;
  42.         var builder = gHistoryTree.builder.QueryInterface(Components.interfaces.nsIXULTreeBuilder);
  43.         var url = builder.getResourceAtIndex(currentIndex).ValueUTF8;
  44.         var title = gHistoryTree.treeBoxObject.view.getCellText(currentIndex, "Name");
  45.  
  46.         var htmlString = "<A HREF='" + url + "'>" + title + "</A>";
  47.         aXferData.data = new TransferData();
  48.         aXferData.data.addDataForFlavour("text/unicode", url);
  49.         aXferData.data.addDataForFlavour("text/html", htmlString);
  50.         aXferData.data.addDataForFlavour("text/x-moz-url", url + "\n" + title);
  51.         return true;
  52.     }
  53. };
  54.  
  55. function collapseExpand()
  56. {
  57.     var currentIndex = gHistoryTree.currentIndex;
  58.     gHistoryTree.treeBoxObject.view.toggleOpenState(currentIndex);
  59. }
  60.  
  61. function openURLIn(where)
  62. {
  63.   var count = gHistoryTree.view.selection.count;
  64.   if (count != 1)
  65.     return;
  66.  
  67.   var currentIndex = gHistoryTree.currentIndex;     
  68.   if (isContainer(gHistoryTree, currentIndex))
  69.     return;
  70.  
  71.   var builder = gHistoryTree.builder.QueryInterface(Components.interfaces.nsIXULTreeBuilder);
  72.   var url = builder.getResourceAtIndex(currentIndex).ValueUTF8;
  73.   
  74.   if (!checkURLSecurity(url)) 
  75.     return;
  76.  
  77.   openUILinkIn(url, where);
  78. }
  79.  
  80. function openURL(aEvent)
  81. {
  82.   var pref = Components.classes["@mozilla.org/preferences-service;1"]
  83.                        .getService(Components.interfaces.nsIPrefService);
  84.   pref = pref.getBranch(null);
  85.   var historyOpenPref = pref.getCharPref("browser.tabs.history.open");
  86.  
  87.   // MERC (DP): we check the open pref for history
  88.   if(whereToOpenLink(aEvent) == 'current' && historyOpenPref == 'new') {
  89.     openURLIn('tab');
  90.   } else {  
  91.     openURLIn(whereToOpenLink(aEvent));
  92.   }
  93. }
  94.  
  95. // MERC (DP):
  96. // switches on 'browser.tabs.history.open to decide whether or not
  97. // to open history url in current or new tab
  98. function PrefOpenURL() {
  99.   var pref = Components.classes["@mozilla.org/preferences-service;1"]
  100.                             .getService(Components.interfaces.nsIPrefService);
  101.   pref = pref.getBranch(null);
  102.   var historyOpenPref = pref.getCharPref("browser.tabs.history.open");
  103.  
  104.   // open a new tab or load in current tab? - consult pref
  105.   if(historyOpenPref == 'overwrite') {
  106.     OpenURL(0);
  107.   } else { // == 'new'
  108.     OpenURL(2);
  109.   }
  110. }
  111.  
  112. function handleHistoryClick(aEvent)
  113. {
  114.   dump('*********************** handleHistoryClick\n');
  115.   var tbo = gHistoryTree.treeBoxObject;
  116.  
  117.   var row = { }, col = { }, obj = { };
  118.   tbo.getCellAt(aEvent.clientX, aEvent.clientY, row, col, obj);
  119.   
  120.   var x = { }, y = { }, w = { }, h = { };
  121.   tbo.getCoordsForCellItem(row.value, col.value, "image",
  122.                            x, y, w, h);
  123.   var mouseInGutter = aEvent.clientX < x.value;
  124.  
  125.   if (row.value == -1 || obj.value == "twisty")
  126.     return;
  127.   var modifKey = aEvent.shiftKey || aEvent.ctrlKey || aEvent.altKey || 
  128.                  aEvent.metaKey  || aEvent.button == 1;
  129.   if (!modifKey && tbo.view.isContainer(row.value)) {
  130.     tbo.view.toggleOpenState(row.value);
  131.     return;
  132.   }
  133.   if (!mouseInGutter && 
  134.       aEvent.originalTarget.localName == "treechildren" && 
  135.       (aEvent.button == 0 || aEvent.button == 1)) {
  136.     // Clear all other selection since we're loading a link now. We must
  137.     // do this *before* attempting to load the link since openURL uses
  138.     // selection as an indication of which link to load. 
  139.     tbo.selection.select(row.value);
  140.  
  141.     openURL(aEvent);
  142.   }
  143. }
  144.  
  145. function checkURLSecurity(aURL)
  146. {
  147.   var uri = Components.classes["@mozilla.org/network/standard-url;1"].
  148.               createInstance(Components.interfaces.nsIURI);
  149.   uri.spec = aURL;
  150.   if (uri.schemeIs("javascript") || uri.schemeIs("data")) {
  151.     var strBundleService = Components.classes["@mozilla.org/intl/stringbundle;1"]
  152.                                       .getService(Components.interfaces.nsIStringBundleService);
  153.     var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  154.                                   .getService(Components.interfaces.nsIPromptService);
  155.     var historyBundle = strBundleService.createBundle("chrome://global/locale/history/history.properties");
  156.     var brandBundle = strBundleService.createBundle("chrome://global/locale/brand.properties");      
  157.     var brandStr = brandBundle.GetStringFromName("brandShortName");
  158.     var errorStr = historyBundle.GetStringFromName("load-js-data-url-error");
  159.     promptService.alert(window, brandStr, errorStr);
  160.     return false;
  161.   }
  162.   return true;
  163. }
  164.  
  165. function OpenURL(aWhere, event)
  166. {
  167.   dump('*** OpenURL\n');
  168.   var count = gHistoryTree.treeBoxObject.view.selection.count;
  169.   if (count != 1)
  170.     return;
  171.  
  172.   var currentIndex = gHistoryTree.currentIndex;     
  173.   if (isContainer(gHistoryTree, currentIndex))
  174.     return;
  175.  
  176.   var builder = gHistoryTree.builder.QueryInterface(Components.interfaces.nsIXULTreeBuilder);
  177.   var url = builder.getResourceAtIndex(currentIndex).ValueUTF8;
  178.   
  179.   if (!checkURLSecurity(url)) 
  180.     return;
  181.  
  182.   if (aWhere == 0)
  183.     openTopWin(url);
  184.   else if (aWhere == 1)
  185.     openNewWindowWith(url, null, false);
  186.   else
  187.     openNewTabWith(url, null, event, false);     
  188. }
  189.  
  190. function SortBy(sortKey)
  191. {
  192.   // We set the sortDirection to the one before we actually want it to be in the
  193.   // cycle list, since cycleHeader cycles it forward before doing the sort.
  194.  
  195.   var sortDirection;
  196.   switch(sortKey) {
  197.     case "visited":
  198.       sortKey = "rdf:http://home.netscape.com/NC-rdf#VisitCount";
  199.       sortDirection = "ascending";
  200.       break;
  201.     case "name":
  202.       sortKey = "rdf:http://home.netscape.com/NC-rdf#Name?sort=true";
  203.       sortDirection = "natural";
  204.       break;
  205.     case "lastvisited":
  206.       sortKey = "rdf:http://home.netscape.com/NC-rdf#Date";
  207.       sortDirection = "ascending";
  208.       break;
  209.     default:
  210.       return;    
  211.   }
  212.   var col = document.getElementById("Name");
  213.   col.setAttribute("sort", sortKey);
  214.   col.setAttribute("sortDirection", sortDirection);
  215.   gHistoryTree.treeBoxObject.view.cycleHeader(sortKey, col);
  216. }
  217.  
  218. function IsFindResource(uri)
  219. {
  220.   return (uri.substr(0, 5) == "find:");
  221. }
  222.     
  223. function GroupBy(groupingType)
  224. {
  225.   var isFind = IsFindResource(groupingType);
  226.   if (!isFind) {
  227.     gHistoryGrouping = groupingType;
  228.     gSearchBox.value = "";
  229.   }
  230.   switch(groupingType) {
  231.     case "site":
  232.       gHistoryTree.setAttribute("ref", "NC:HistoryRoot");
  233.       break;
  234.     case "dayandsite":
  235.       gHistoryTree.setAttribute("ref", "NC:HistoryByDateAndSite");
  236.       break;
  237.     case "visited":
  238.       gHistoryTree.setAttribute("ref", "NC:HistoryRoot");
  239.       break;
  240.     case "lastvisited":
  241.       gHistoryTree.setAttribute("ref", "NC:HistoryRoot");
  242.       break;
  243.     case "day":
  244.       gHistoryTree.setAttribute("ref", "NC:HistoryByDate");
  245.       break;
  246.     default:
  247.       gHistoryTree.setAttribute("ref", groupingType);
  248.   }
  249.   Sort(isFind? gHistoryGrouping : groupingType);
  250. }
  251.  
  252. function Sort(groupingType)
  253. {
  254.   switch(groupingType) {
  255.     case "site":
  256.     case "dayandsite":
  257.     case "day":
  258.       SortBy("name");
  259.       break;
  260.     case "lastvisited":
  261.       SortBy("lastvisited");
  262.       break;
  263.     case "visited":
  264.       SortBy("visited");
  265.       break;
  266.   }
  267. }
  268.  
  269. function historyAddBookmarks()
  270. {
  271.   var count = gHistoryTree.treeBoxObject.view.selection.count;
  272.   if (count != 1)
  273.     return;
  274.   
  275.   var currentIndex = gHistoryTree.currentIndex;
  276.   var builder = gHistoryTree.builder.QueryInterface(Components.interfaces.nsIXULTreeBuilder);
  277.   var url = builder.getResourceAtIndex(currentIndex).ValueUTF8;
  278.   
  279.   //XXXBlake don't use getCellText
  280.   var title = gHistoryTree.treeBoxObject.view.getCellText(currentIndex, "Name");
  281.   BookmarksUtils.addBookmark(url, title, undefined);
  282. }
  283.  
  284. function historyCopyLink()
  285. {
  286.   var builder = gHistoryTree.builder.QueryInterface(Components.interfaces.nsIXULTreeBuilder);
  287.   var url = builder.getResourceAtIndex(gHistoryTree.currentIndex).ValueUTF8;
  288.   var clipboard = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
  289.                             .getService(Components.interfaces.nsIClipboardHelper );
  290.   clipboard.copyString(url);
  291. }
  292.  
  293. function buildContextMenu(aEvent)
  294. {
  295.   // if nothing is selected, bail and don't show a context menu
  296.   var count = gHistoryTree.treeBoxObject.view.selection.count;
  297.   if (count != 1) {
  298.     aEvent.preventDefault();
  299.     return;
  300.   }
  301.  
  302.   var openItem = document.getElementById("miOpen");
  303.   var openItemInNewWindow = document.getElementById("miOpenInNewWindow");
  304.   var openItemInNewTab = document.getElementById("miOpenInNewTab");
  305.   var bookmarkItem = document.getElementById("miAddBookmark");
  306.   var copyLocationItem = document.getElementById("miCopyLink");
  307.   var sep1 = document.getElementById("pre-bookmarks-separator");
  308.   var sep2 = document.getElementById("post-bookmarks-separator");
  309.   var expandItem = document.getElementById("miExpand");
  310.   var collapseItem = document.getElementById("miCollapse");
  311.  
  312.   var currentIndex = gHistoryTree.currentIndex;
  313.   if ((gHistoryGrouping == "day" || gHistoryGrouping == "dayandsite")
  314.       && isContainer(gHistoryTree, currentIndex)) {
  315.     openItem.hidden = true;
  316.     openItemInNewWindow.hidden = true;
  317.     openItemInNewTab.hidden = true;
  318.     bookmarkItem.hidden = true;
  319.     copyLocationItem.hidden = true;
  320.     sep1.hidden = true;
  321.     sep2.hidden = false;
  322.     if (isContainerOpen(gHistoryTree, currentIndex)) {
  323.       expandItem.hidden = true;
  324.       collapseItem.hidden = false;
  325.     } else {
  326.       expandItem.hidden = false;
  327.       collapseItem.hidden = true;
  328.     }
  329.   }
  330.   else {
  331.     openItem.hidden = false;
  332.     openItemInNewWindow.hidden = false;
  333.     openItemInNewTab.hidden = false;
  334.     bookmarkItem.hidden = false;
  335.     copyLocationItem.hidden = false;
  336.     sep1.hidden = false;
  337.     sep2.hidden = false;
  338.     expandItem.hidden = true;
  339.     collapseItem.hidden = true;
  340.   }
  341. }
  342.  
  343. function searchHistory(aInput)
  344. {
  345.    if (aInput == "") {
  346.      GroupBy(gHistoryGrouping);
  347.      return;
  348.    }
  349.    
  350.    GroupBy("find:datasource=history&match=Name&method=contains&text=" + encodeURIComponent(aInput));     
  351. }
  352.  
  353.