home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / browser.jar / content / browser / search / shared.js < prev   
Encoding:
JavaScript  |  2005-07-29  |  14.5 KB  |  449 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is mozilla.org code.
  15.  *
  16.  * The Initial Developer of the Original Code is Robert John Churchill.
  17.  * Portions created by the Initial Developer are Copyright (C) 2002
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s): 
  21.  *    Robert John Churchill   <rjc@netscape.com> (Original Author)
  22.  *    Karsten Duesterloh      <kd-moz@tprac.de>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. function fillContextMenu(name, treeName)
  39. {
  40.     if (!name)    return(false);
  41.     var popupNode = document.getElementById(name);
  42.     if (!popupNode)    return(false);
  43.     // remove the menu node (which tosses all of its kids);
  44.     // do this in case any old command nodes are hanging around
  45.   while (popupNode.childNodes.length)
  46.   {
  47.     popupNode.removeChild(popupNode.childNodes[0]);
  48.   }
  49.  
  50.     var treeNode = document.getElementById(treeName);
  51.     if (!treeNode)    return(false);
  52.     var db = treeNode.database;
  53.     if (!db)    return(false);
  54.  
  55.     var compositeDB = db.QueryInterface(Components.interfaces.nsIRDFDataSource);
  56.     if (!compositeDB)    return(false);
  57.  
  58.     var isupports = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  59.     if (!isupports)    return(false);
  60.     var rdf = isupports.QueryInterface(Components.interfaces.nsIRDFService);
  61.     if (!rdf)    return(false);
  62.  
  63.     var target_item = document.popupNode.parentNode.parentNode;
  64.     if (target_item && target_item.nodeName == "treeitem")
  65.     {
  66.       if (target_item.getAttribute('selected') != 'true') {
  67.         treeNode.selectItem(target_item);
  68.       }
  69.     }
  70.  
  71.     var select_list = treeNode.selectedItems;
  72.  
  73.     var separatorResource = rdf.GetResource("http://home.netscape.com/NC-rdf#BookmarkSeparator");
  74.     if (!separatorResource) return(false);
  75.  
  76.     // perform intersection of commands over selected nodes
  77.     var cmdArray = new Array();
  78.     var selectLength = select_list.length;
  79.     if (selectLength == 0)  selectLength = 1;
  80.     for (var nodeIndex=0; nodeIndex < selectLength; nodeIndex++)
  81.     {
  82.       var id = null;
  83.  
  84.   // if nothing is selected, get commands for the "ref" of the tree root
  85.       if (select_list.length == 0)
  86.       {
  87.     id = treeNode.getAttribute("ref");
  88.           if (!id)    break;
  89.       }
  90.       else
  91.       {
  92.           var node = select_list[nodeIndex];
  93.           if (!node)    break;
  94.           id = node.getAttribute("id");
  95.           if (!id)    break;
  96.   }
  97.  
  98.         var rdfNode = rdf.GetResource(id);
  99.         if (!rdfNode)    break;
  100.         var cmdEnum = db.GetAllCmds(rdfNode);
  101.         if (!cmdEnum)    break;
  102.  
  103.         var nextCmdArray = new Array();
  104.         while (cmdEnum.hasMoreElements())
  105.         {
  106.             var cmd = cmdEnum.getNext();
  107.             if (!cmd)    break;
  108.             if (nodeIndex == 0)
  109.             {
  110.                 cmdArray[cmdArray.length] = cmd;
  111.             }
  112.             else
  113.             {
  114.                 nextCmdArray[cmdArray.length] = cmd;
  115.             }
  116.         }
  117.         if (nodeIndex > 0)
  118.         {
  119.             // perform command intersection calculation
  120.             for (var cmdIndex = 0; cmdIndex < cmdArray.length; cmdIndex++)
  121.             {
  122.                 var    cmdFound = false;
  123.                 for (var nextCmdIndex = 0; nextCmdIndex < nextCmdArray.length; nextCmdIndex++)
  124.                 {
  125.                     if (nextCmdArray[nextCmdIndex] == cmdArray[cmdIndex])
  126.                     {
  127.                         cmdFound = true;
  128.                         break;
  129.                     }
  130.                 }
  131.                 if ((cmdFound == false) && (cmdArray[cmdIndex]))
  132.                 {
  133.       var cmdResource = cmdArray[cmdIndex].QueryInterface(Components.interfaces.nsIRDFResource);
  134.                   if ((cmdResource) && (cmdResource != separatorResource))
  135.                   {
  136.         cmdArray[cmdIndex] = null;
  137.       }
  138.                 }
  139.             }
  140.         }
  141.     }
  142.  
  143.     // need a resource to ask RDF for each command's name
  144.     var rdfNameResource = rdf.GetResource("http://home.netscape.com/NC-rdf#Name");
  145.     if (!rdfNameResource)        return(false);
  146.  
  147.     // build up menu items
  148.     if (cmdArray.length < 1)    return(false);
  149.  
  150.     var lastWasSep = false;
  151.  
  152.     for (var cmdIndex = 0; cmdIndex < cmdArray.length; cmdIndex++)
  153.     {
  154.         var cmd = cmdArray[cmdIndex];
  155.         if (!cmd)        continue;
  156.         var cmdResource = cmd.QueryInterface(Components.interfaces.nsIRDFResource);
  157.         if (!cmdResource)    break;
  158.  
  159.   // handle separators
  160.   if (cmdResource == separatorResource)
  161.   {
  162.     if (lastWasSep != true)
  163.     {
  164.       lastWasSep = true;
  165.             var menuItem = document.createElement("menuseparator");
  166.             popupNode.appendChild(menuItem);
  167.     }
  168.     continue;
  169.   }
  170.  
  171.   lastWasSep = false;
  172.  
  173.         var cmdNameNode = compositeDB.GetTarget(cmdResource, rdfNameResource, true);
  174.         if (!cmdNameNode)    break;
  175.         cmdNameLiteral = cmdNameNode.QueryInterface(Components.interfaces.nsIRDFLiteral);
  176.         if (!cmdNameLiteral)    break;
  177.         cmdName = cmdNameLiteral.Value;
  178.         if (!cmdName)        break;
  179.  
  180.         var menuItem = document.createElement("menuitem");
  181.         menuItem.setAttribute("label", cmdName);
  182.         popupNode.appendChild(menuItem);
  183.         // work around bug # 26402 by setting "oncommand" attribute AFTER appending menuitem
  184.         menuItem.setAttribute("oncommand", "return doContextCmd('" + cmdResource.Value + "', '" + treeName + "');");
  185.     }
  186.  
  187.   // strip off leading/trailing menuseparators
  188.   while (popupNode.childNodes.length > 0)
  189.   {
  190.     if (popupNode.childNodes[0].tagName != "menuseparator")
  191.       break;
  192.     popupNode.removeChild(popupNode.childNodes[0]);
  193.   }
  194.   while (popupNode.childNodes.length > 0)
  195.   {
  196.     if (popupNode.childNodes[popupNode.childNodes.length - 1].tagName != "menuseparator")
  197.       break;
  198.     popupNode.removeChild(popupNode.childNodes[popupNode.childNodes.length - 1]);
  199.   }
  200.  
  201.   var   searchMode = 0;
  202.   if (pref) searchMode = pref.getIntPref("browser.search.mode");
  203.   if (pref && bundle)
  204.   {
  205.     // then add a menu separator (if necessary)
  206.     if (popupNode.childNodes.length > 0)
  207.     {
  208.       if (popupNode.childNodes[popupNode.childNodes.length - 1].tagName != "menuseparator")
  209.       {
  210.           var menuSep = document.createElement("menuseparator");
  211.           popupNode.appendChild(menuSep);
  212.       }
  213.     }
  214.     // And then add a "Search Mode" menu item
  215.     var propMenuName = (searchMode == 0) ? bundle.GetStringFromName("enableAdvanced") : bundle.GetStringFromName("disableAdvanced");
  216.     var menuItem = document.createElement("menuitem");
  217.     menuItem.setAttribute("label", propMenuName);
  218.     popupNode.appendChild(menuItem);
  219.     // Work around bug # 26402 by setting "oncommand" attribute
  220.     // AFTER appending menuitem
  221.     menuItem.setAttribute("oncommand", "return setSearchMode();");
  222.   }
  223.  
  224.     return(true);
  225. }
  226.  
  227.  
  228.  
  229. function setSearchMode()
  230. {
  231.   var   searchMode = 0;
  232.   if (pref) searchMode = pref.getIntPref("browser.search.mode");
  233.   if (searchMode == 0)  searchMode = 1;
  234.   else      searchMode = 0;
  235.   if (pref) pref.setIntPref("browser.search.mode", searchMode);
  236.   return(true);
  237. }
  238.  
  239.  
  240.  
  241. function doContextCmd(cmdName, treeName)
  242. {
  243.   var treeNode = document.getElementById(treeName);
  244.   if (!treeNode)    return(false);
  245.   var db = treeNode.database;
  246.   if (!db)    return(false);
  247.  
  248.   var compositeDB = db.QueryInterface(Components.interfaces.nsIRDFDataSource);
  249.   if (!compositeDB)    return(false);
  250.  
  251.   var isupports = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService();
  252.   if (!isupports)    return(false);
  253.   var rdf = isupports.QueryInterface(Components.interfaces.nsIRDFService);
  254.   if (!rdf)    return(false);
  255.  
  256.   // need a resource for the command
  257.   var cmdResource = rdf.GetResource(cmdName);
  258.   if (!cmdResource)        return(false);
  259.   cmdResource = cmdResource.QueryInterface(Components.interfaces.nsIRDFResource);
  260.   if (!cmdResource)        return(false);
  261.  
  262.   // set up selection nsISupportsArray
  263.   var selectionInstance = Components.classes["@mozilla.org/supports-array;1"].createInstance();
  264.   var selectionArray = selectionInstance.QueryInterface(Components.interfaces.nsISupportsArray);
  265.  
  266.   // set up arguments nsISupportsArray
  267.   var argumentsInstance = Components.classes["@mozilla.org/supports-array;1"].createInstance();
  268.   var argumentsArray = argumentsInstance.QueryInterface(Components.interfaces.nsISupportsArray);
  269.  
  270.   // get argument (parent)
  271.   var parentArc = rdf.GetResource("http://home.netscape.com/NC-rdf#parent");
  272.   if (!parentArc)        return(false);
  273.  
  274.   var select_list = treeNode.selectedItems;
  275.   if (select_list.length < 1)
  276.   {
  277.     // if nothing is selected, default to using the "ref" on the root of the tree
  278.     var uri = treeNode.getAttribute("ref");
  279.     if (!uri || uri=="")    return(false);
  280.     var rdfNode = rdf.GetResource(uri);
  281.     // add node into selection array
  282.     if (rdfNode)
  283.     {
  284.       selectionArray.AppendElement(rdfNode);
  285.     }
  286.   }
  287.   else for (var nodeIndex=0; nodeIndex<select_list.length; nodeIndex++)
  288.   {
  289.     var node = select_list[nodeIndex];
  290.     if (!node)    break;
  291.     var uri = node.getAttribute("ref");
  292.     if ((uri) || (uri == ""))
  293.     {
  294.       uri = node.getAttribute("id");
  295.     }
  296.     if (!uri)    return(false);
  297.  
  298.     var rdfNode = rdf.GetResource(uri);
  299.     if (!rdfNode)    break;
  300.  
  301.     // add node into selection array
  302.     selectionArray.AppendElement(rdfNode);
  303.  
  304.     // get the parent's URI
  305.     var parentURI="";
  306.     var theParent = node;
  307.     while (theParent)
  308.     {
  309.       theParent = theParent.parentNode;
  310.  
  311.       parentURI = theParent.getAttribute("ref");
  312.       if ((!parentURI) || (parentURI == ""))
  313.       {
  314.         parentURI = theParent.getAttribute("id");
  315.       }
  316.       if (parentURI != "")  break;
  317.     }
  318.     if (parentURI == "")    return(false);
  319.  
  320.     var parentNode = rdf.GetResource(parentURI, true);
  321.     if (!parentNode)  return(false);
  322.  
  323.     // add parent arc and node into arguments array
  324.     argumentsArray.AppendElement(parentArc);
  325.     argumentsArray.AppendElement(parentNode);
  326.   }
  327.  
  328.   // do the command
  329.   compositeDB.DoCommand( selectionArray, cmdResource, argumentsArray );
  330.   return(true);
  331. }
  332.  
  333.  
  334.  
  335. /* Note: doSort() does NOT support natural order sorting, unless naturalOrderResource is valid,
  336.          in which case we sort ascending on naturalOrderResource
  337.  */
  338. function doSort(sortColName, naturalOrderResource)
  339. {
  340.   var node = document.getElementById(sortColName);
  341.   // determine column resource to sort on
  342.   var sortResource = node.getAttribute('resource');
  343.   if (!sortResource) return(false);
  344.  
  345.   var sortDirection="ascending";
  346.   var isSortActive = node.getAttribute('sortActive');
  347.   if (isSortActive == "true")
  348.   {
  349.     sortDirection = "ascending";
  350.  
  351.     var currentDirection = node.getAttribute('sortDirection');
  352.     if (currentDirection == "ascending")
  353.     {
  354.       if (sortResource != naturalOrderResource)
  355.       {
  356.         sortDirection = "descending";
  357.       }
  358.     }
  359.     else if (currentDirection == "descending")
  360.     {
  361.       if (naturalOrderResource != null && naturalOrderResource != "")
  362.       {
  363.         sortResource = naturalOrderResource;
  364.       }
  365.     }
  366.   }
  367.  
  368.   var isupports = Components.classes["@mozilla.org/xul/xul-sort-service;1"].getService();
  369.   if (!isupports)    return(false);
  370.   var xulSortService = isupports.QueryInterface(Components.interfaces.nsIXULSortService);
  371.   if (!xulSortService)    return(false);
  372.   try
  373.   {
  374.     xulSortService.sort(node, sortResource, sortDirection);
  375.   }
  376.   catch(ex)
  377.   {
  378.     debug("Exception calling xulSortService.sort()");
  379.   }
  380.   return(true);
  381. }
  382.  
  383.  
  384.  
  385. function setInitialSort(node, sortDirection)
  386. {
  387.   // determine column resource to sort on
  388.   var sortResource = node.getAttribute('resource');
  389.   if (!sortResource) return(false);
  390.  
  391.   try
  392.   {
  393.     var isupports = Components.classes["@mozilla.org/xul/xul-sort-service;1"].getService();
  394.     if (!isupports)    return(false);
  395.     var xulSortService = isupports.QueryInterface(Components.interfaces.nsIXULSortService);
  396.     if (!xulSortService)    return(false);
  397.     xulSortService.sort(node, sortResource, sortDirection);
  398.   }
  399.   catch(ex)
  400.   {
  401.   }
  402.   return(true);
  403. }
  404.  
  405.  
  406. function switchTab(aPageIndex)
  407. {
  408.   var deck = document.getElementById("advancedDeck");
  409.   if (!deck)
  410.   { // get the search-panel deck the hard way 
  411.     // otherwise the switchTab call from internetresults.xul would fail
  412.     var oSearchPanel = getNavigatorWindow(false).sidebarObj.panels.get_panel_from_id("urn:sidebar:panel:search");
  413.     if (!oSearchPanel)
  414.       return;
  415.     deck = oSearchPanel.get_iframe().contentDocument.getElementById("advancedDeck")
  416.   }
  417.   deck.setAttribute("selectedIndex", aPageIndex);
  418. }
  419.  
  420.  
  421. // retrieves the most recent navigator window
  422. function getNavigatorWindow(aOpenFlag)
  423. {
  424.   var navigatorWindow;
  425.  
  426.   // if this is a browser window, just use it
  427.   if ("document" in top) {
  428.     var possibleNavigator = top.document.getElementById("main-window");
  429.     if (possibleNavigator &&
  430.         possibleNavigator.getAttribute("windowtype") == "navigator:browser")
  431.       navigatorWindow = top;
  432.   }
  433.  
  434.   // if not, get the most recently used browser window
  435.   if (!navigatorWindow) {
  436.     var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  437.                        .getService(Components.interfaces.nsIWindowMediator);
  438.     navigatorWindow = wm.getMostRecentWindow("navigator:browser");
  439.   }
  440.  
  441.   // if no browser window available and it's ok to open a new one, do so
  442.   if (!navigatorWindow && aOpenFlag) {
  443.     var navigatorChromeURL = search_getBrowserURL();
  444.     navigatorWindow = openDialog(navigatorChromeURL, "_blank", "chrome,all,dialog=no");
  445.   }
  446.   return navigatorWindow;
  447. }
  448.  
  449.