home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 March / PCWorld_2005-03_cd.bin / komunikace / kmeleon / kmeleon09.exe / comm.jar / content / navigator / urlbarBindings.xml < prev    next >
Extensible Markup Language  |  2004-01-31  |  18KB  |  467 lines

  1. <?xml version="1.0"?>
  2.  
  3. <bindings id="urlbarBindings"
  4.           xmlns="http://www.mozilla.org/xbl"
  5.           xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  6.           xmlns:xbl="http://www.mozilla.org/xbl">
  7.  
  8.   <binding id="urlbar" extends="chrome://global/content/autocomplete.xml#autocomplete">
  9.     <implementation>
  10.       <constructor><![CDATA[
  11.         var pbi = this.mPrefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
  12.         if (pbi)
  13.           pbi.addObserver("browser.urlbar", this.mPrefObserver, false);
  14.         
  15.         this.updatePref("browser.urlbar.showPopup");
  16.         this.updatePref("browser.urlbar.autoFill");
  17.       ]]></constructor>
  18.  
  19.       <destructor><![CDATA[
  20.         var pbi = this.mPrefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
  21.         if (pbi)
  22.           pbi.removeObserver("browser.urlbar", this.mPrefObserver);
  23.       ]]></destructor>
  24.  
  25.       <field name="mPrefs">
  26.         var svc = Components.classes["@mozilla.org/preferences-service;1"]
  27.                             .getService(Components.interfaces.nsIPrefService);
  28.         svc.getBranch(null);
  29.       </field>
  30.  
  31.       <field name="mPrefObserver"><![CDATA[
  32.         ({
  33.           urlbar: this,
  34.           
  35.           observe: function(aObserver, aBlah, aPref) {
  36.             if (!aPref.indexOf("browser.urlbar"))
  37.               this.urlbar.updatePref(aPref);
  38.           }
  39.         });
  40.       ]]></field>
  41.  
  42.       <method name="updatePref">
  43.         <parameter name="aPref"/>
  44.         <body><![CDATA[
  45.           if (!aPref.indexOf("browser.urlbar.showPopup")) {
  46.             this.showPopup = this.mPrefs.getBoolPref("browser.urlbar.showPopup");
  47.           } else if (!aPref.indexOf("browser.urlbar.autoFill")) {
  48.             this.autoFill = this.mPrefs.getBoolPref("browser.urlbar.autoFill");
  49.           } 
  50.         ]]></body>
  51.       </method>
  52.       
  53.     </implementation>
  54.   </binding>
  55.   
  56.   <binding id="autocomplete-result-popup" extends="chrome://global/content/autocomplete.xml#autocomplete-result-popup">
  57.     <content>
  58.       <xul:tree anonid="tree" class="autocomplete-tree plain" flex="1">
  59.         <xul:treecols anonid="treecols"/>
  60.         <xul:treechildren anonid="treebody" class="autocomplete-treebody" flex="1"/>
  61.       </xul:tree>
  62.       <xul:box role="search-box" class="autocomplete-search-box"/>
  63.     </content>
  64.     
  65.     <implementation>
  66.       <constructor><![CDATA[
  67.         // listen for changes to default search engine
  68.         var pbi = this.mPrefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
  69.         if (pbi) {
  70.           pbi.addObserver("browser.search", this.mPrefObserver, false);
  71.           pbi.addObserver("browser.urlbar", this.mPrefObserver, false);
  72.         }
  73.       ]]></constructor>
  74.  
  75.       <destructor><![CDATA[
  76.         var pbi = this.mPrefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
  77.         if (pbi) {
  78.           pbi.removeObserver("browser.search", this.mPrefObserver);
  79.           pbi.removeObserver("browser.urlbar", this.mPrefObserver);
  80.         }
  81.       ]]></destructor>
  82.  
  83.       <property name="selectedIndex"
  84.                 onget="return this.textbox.view.selectedIndex;">
  85.         <setter>
  86.           this.mSelectedIndex = val;
  87.           if (val == null)
  88.             this.mSearchBox.selectedIndex = null;
  89.             
  90.           return val;
  91.         </setter>
  92.      </property>
  93.  
  94.       <property name="showSearch" onget="return this.mShowSearch;">
  95.         <setter><![CDATA[
  96.           this.mShowSearch = val;
  97.           if (val) {
  98.             this.updateEngines();
  99.             this.mSearchBox.removeAttribute("hidden");
  100.           } else {
  101.             this.clearEngines();
  102.             this.mSearchBox.setAttribute("hidden", "true");
  103.           }
  104.         ]]></setter>
  105.       </property>
  106.       
  107.       <property name="mSelectedIndex">
  108.         <setter>
  109.           this.textbox.view.selectedIndex = val;
  110.           return val;
  111.         </setter>
  112.      </property>
  113.  
  114.       <property name="defaultSearchEngine"
  115.                 onget="return this.textbox.getAttribute('defaultSearchEngine') == 'true';"
  116.                 onset="this.textbox.setAttribute('defaultSearchEngine', val); return val;"/>
  117.  
  118.       <field name="mSearchBox">
  119.          document.getAnonymousElementByAttribute(this, "role", "search-box");
  120.       </field>
  121.  
  122.       <field name="mPrefs">
  123.         var svc = Components.classes["@mozilla.org/preferences-service;1"]
  124.                             .getService(Components.interfaces.nsIPrefService);
  125.         svc.getBranch(null);
  126.       </field>
  127.  
  128.       <field name="mPrefObserver"><![CDATA[
  129.         ({
  130.           resultsPopup: this,
  131.           
  132.           observe: function(aObserver, aBlah, aPref) {
  133.             if (!aPref.indexOf("browser.search"))
  134.               this.resultsPopup.updateEngines();
  135.             else if (!aPref.indexOf("browser.urlbar"))
  136.               this.resultsPopup.updatePref(aPref);
  137.           }
  138.         });
  139.       ]]></field>
  140.  
  141.       <field name="mInputListener"><![CDATA[
  142.         (function(aEvent) {
  143.           // "this" is the textbox, not the popup
  144.           if (this.mSearchInputTO)
  145.             window.clearTimeout(this.mSearchInputTO);
  146.           this.mSearchInputTO = window.setTimeout(this.resultsPopup.mInputTimeout, this.timeout, this);
  147.         });
  148.       ]]></field>
  149.  
  150.       <field name="mInputTimeout"><![CDATA[
  151.         (function(me) {
  152.           me.resultsPopup.mSearchBox.searchValue = me.value;
  153.           me.mSearchInputTO = 0;
  154.         });
  155.       ]]></field>
  156.  
  157.       <field name="mEnginesReady">false</field>
  158.       
  159.       <method name="getOverrideValue">
  160.         <body><![CDATA[
  161.           if (this.mSearchBox.selectedIndex != null)
  162.             return this.mSearchBox.getOverrideValue();
  163.           return null;
  164.         ]]></body>
  165.       </method>
  166.  
  167.       <method name="updatePref">
  168.         <parameter name="aPref"/>
  169.         <body><![CDATA[
  170.           if (!aPref.indexOf("browser.urlbar.showSearch"))
  171.             this.showSearch = this.mPrefs.getBoolPref("browser.urlbar.showSearch");
  172.         ]]></body>
  173.       </method>
  174.       
  175.       <method name="addEngine">
  176.         <parameter name="aEngineId"/>
  177.         <parameter name="aName"/>
  178.         <parameter name="aIcon"/>
  179.         <parameter name="aSearchBarUrl"/>
  180.         <body><![CDATA[
  181.           var box = document.createElement("box");
  182.           box.setAttribute("class", "autocomplete-search-engine");
  183.           box.setAttribute("searchEngine", aEngineId);
  184.           box.setAttribute("name", aName);
  185.           box.setAttribute("icon", aIcon);
  186.           box.setAttribute("searchBarUrl", aSearchBarUrl);
  187.           box.setAttribute("engineIndex", this.childNodes.length);
  188.           this.mSearchBox.appendChild(box);
  189.         ]]></body>
  190.       </method>
  191.  
  192.       <method name="clearEngines">
  193.         <body><![CDATA[
  194.           var kids = this.mSearchBox.childNodes;
  195.           for (var i = kids.length-1; i >= 0; --i)
  196.             this.mSearchBox.removeChild(kids[i]);
  197.         ]]></body>
  198.       </method>
  199.    
  200.       <method name="updateEngines">
  201.         <body><![CDATA[
  202.           var rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  203.                       .getService(Components.interfaces.nsIRDFService);
  204.           try {
  205.             var ds = rdf.GetDataSource("rdf:internetsearch");
  206.           } catch (ex) {
  207.             // sometimes bad profiles cause this error, which horks the hold urlbar
  208.             return;
  209.           }
  210.                               
  211.           const kNC_Name = rdf.GetResource("http://home.netscape.com/NC-rdf#Name");
  212.           const kNC_Icon = rdf.GetResource("http://home.netscape.com/NC-rdf#Icon");
  213.           const kNC_searchBarUrl = rdf.GetResource("http://home.netscape.com/NC-rdf#actionBar");
  214.         
  215.           var defaultEngine = null;
  216.           try {
  217.             defaultEngine = this.mPrefs.getComplexValue("browser.search.defaultengine",
  218.                                                         Components.interfaces.nsISupportsString).data;
  219.           } catch(ex) {
  220.             this.ensureDefaultEnginePrefs(rdf, ds);
  221.             defaultEngine = this.mPrefs.getComplexValue("browser.search.defaultengine",
  222.                                                         Components.interfaces.nsISupportsString).data;
  223.           }
  224.           
  225.           if (defaultEngine) {
  226.             this.clearEngines();
  227.             
  228.             if (ds) {
  229.               var res = rdf.GetResource(defaultEngine);
  230.               try {
  231.                 var searchBarUrl = this.readRDFString(ds, res, kNC_searchBarUrl);
  232.               } catch(ex) {
  233.                 searchBarUrl = null;
  234.               }
  235.                 this.addEngine(res.Value, 
  236.                               this.readRDFString(ds, res, kNC_Name),
  237.                               this.readRDFString(ds, res, kNC_Icon),
  238.                               searchBarUrl);                                                    
  239.             }
  240.           }
  241.           
  242.           this.mEnginesReady = true;
  243.         ]]></body>
  244.       </method>
  245.  
  246.       <method name="ensureDefaultEnginePrefs">
  247.         <parameter name="aRDF"/>
  248.         <parameter name="aDS"/>
  249.         <body><![CDATA[
  250.           var defaultName = this.mPrefs.getComplexValue("browser.search.defaultenginename",
  251.                                                         Components.interfaces.nsIPrefLocalizedString).data;
  252.           const kNC_Root = aRDF.GetResource("NC:SearchEngineRoot");
  253.           const kNC_child = aRDF.GetResource("http://home.netscape.com/NC-rdf#child");
  254.           const kNC_Name = aRDF.GetResource("http://home.netscape.com/NC-rdf#Name");
  255.           
  256.           var arcs = aDS.GetTargets(kNC_Root, kNC_child, true);
  257.           while (arcs.hasMoreElements()) {
  258.             var engineRes = arcs.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  259.             var name = this.readRDFString(aDS, engineRes, kNC_Name);
  260.             if (name == defaultName) {
  261.               var str = Components.classes["@mozilla.org/supports-string;1"]
  262.                                   .createInstance(Components.interfaces.nsISupportsString);
  263.               str.data = engineRes.Value;
  264.               this.mPrefs.setComplexValue("browser.search.defaultengine",
  265.                                           Components.interfaces.nsISupportsString,
  266.                                           str);
  267.             }
  268.           }
  269.         ]]></body>
  270.       </method>
  271.  
  272.       <method name="readRDFString">
  273.         <parameter name="aDS"/>
  274.         <parameter name="aRes"/>
  275.         <parameter name="aProp"/>
  276.         <body><![CDATA[
  277.           var n = aDS.GetTarget(aRes, aProp, true);
  278.           return n ? n.QueryInterface(Components.interfaces.nsIRDFLiteral).Value : null;
  279.         ]]></body>
  280.       </method>
  281.  
  282.       <method name="selectBy">
  283.         <parameter name="aDir"/>
  284.         <parameter name="aAmount"/>
  285.         <body><![CDATA[
  286.           var bx = this.tree.treeBoxObject;
  287.           var view = bx.view;
  288.           var sel;
  289.           if (this.selectedIndex == null && aDir < 0) {
  290.             sel = this.mSearchBox.selectBy(aDir, aAmount);
  291.             if (sel != null)
  292.               return null;
  293.           } 
  294.           
  295.           sel = this.getNextIndex(aDir, aAmount, this.selectedIndex, view.rowCount-1);
  296.           this.mSelectedIndex = sel;
  297.  
  298.           if (sel == null && aDir > 0)
  299.             this.mSearchBox.selectBy(aDir, aAmount);
  300.           else if (this.mSearchBox.selectedIndex != null)
  301.             this.mSearchBox.selectedIndex = null;
  302.             
  303.           return sel;
  304.         ]]></body>
  305.       </method>
  306.     </implementation>
  307.  
  308.     <handlers>
  309.       <handler event="popupshowing"><![CDATA[
  310.         // sync up with prefs about showing search in the URL bar if
  311.         // the URL bar hasn't finished initializing the default engine info
  312.         //   -or-
  313.         // the search sidebar tab was displayed already triggering a change
  314.         // notification to the browser.search pref branch listener here which 
  315.         // calls updateEngines but doesn't cause the ``Search for ...'' 
  316.         // display string to be updated
  317.         if ( (!this.mEnginesReady && this.defaultSearchEngine) ||
  318.              !("mShowSearch" in this) ) 
  319.           this.updatePref("browser.urlbar.showSearch");
  320.  
  321.         if (this.mShowSearch) {
  322.           this.textbox.mSearchInputTO = 0;
  323.           this.textbox.addEventListener("input", this.mInputListener, false);
  324.           if ("searchValue" in this.mSearchBox)
  325.             this.mSearchBox.searchValue = this.textbox.currentSearchString;
  326.           else
  327.             this.mSearchBox.setAttribute("searchvalue", this.textbox.currentSearchString);
  328.         }
  329.       ]]></handler>
  330.       
  331.       <handler event="popuphiding"><![CDATA[
  332.         if (this.mShowSearch)
  333.           this.textbox.removeEventListener("input", this.mInputListener, false);
  334.       ]]></handler>      
  335.     </handlers>
  336.   </binding>
  337.  
  338.   <binding id="autocomplete-search-box">
  339.     <content orient="vertical"/>
  340.     
  341.     <implementation>
  342.       <constructor><![CDATA[
  343.         var text = this.getAttribute("searchvalue");
  344.         if (text)
  345.           this.searchValue = text;
  346.         
  347.         this.mSelectedIndex = null;
  348.       ]]></constructor>
  349.       
  350.       <field name="parentMouseoverListener">
  351.         // ensure that if a result menuitem is moused-over, any
  352.         // search selection is cleared
  353.         (function(aEvent) {
  354.           if (aEvent.target.nodeName == "menuitem")
  355.             this.mSearchBox.selectedIndex = null;
  356.          })
  357.       </field>
  358.       
  359.       <field name="parentDestroyListener">
  360.         // ensure that if the popup closes, any search selection is cleared
  361.         (function(aEvent) {
  362.            this.mSearchBox.selectedIndex = null;
  363.          })
  364.       </field>
  365.       
  366.       <property name="activeChild" 
  367.                 onget="return this.childNodes[this.mSelectedIndex]"/>
  368.  
  369.       <property name="selectedIndex">
  370.         <getter>return this.mSelectedIndex;</getter>
  371.         
  372.         <setter><![CDATA[
  373.           if (this.mSelectedIndex != null)
  374.             this.activeChild.removeAttribute("menuactive");
  375.           
  376.           this.mSelectedIndex = val;
  377.  
  378.           if (val != null) {
  379.             this.parentNode.mSelectedIndex = null;
  380.             this.parentNode.addEventListener("mouseover", this.parentMouseoverListener, false);
  381.             this.parentNode.addEventListener("popuphiding", this.parentDestroyListener, false);
  382.             if (this.activeChild)
  383.               this.activeChild.setAttribute("menuactive", "true");
  384.           } else {
  385.             this.parentNode.removeEventListener("mouseover", this.parentMouseoverListener, false);
  386.             this.parentNode.removeEventListener("popuphiding", this.parentDestroyListener, false);
  387.           }
  388.         ]]></setter>
  389.       
  390.       </property>
  391.       
  392.       <property name="searchValue">
  393.         <getter><![CDATA[
  394.           return this.mSearchValue;
  395.         ]]></getter>
  396.         <setter><![CDATA[
  397.           this.mSearchValue = val;
  398.           var kids = this.childNodes;
  399.           var searchForStrBundle = srGetStrBundle("chrome://navigator/locale/navigator.properties");
  400.           for (var i = 0; i < kids.length; ++i) {
  401.             var name = kids[i].getAttribute("name");
  402.             var searchForStr = searchForStrBundle.formatStringFromName("searchFor",  [name, val], 2);
  403.             kids[i].setAttribute("label", searchForStr);
  404.           }
  405.         ]]></setter>
  406.       </property>
  407.       
  408.       <method name="selectBy">
  409.         <parameter name="aDir"/>
  410.         <parameter name="aAmount"/>
  411.         <body><![CDATA[
  412.           var sel = this.parentNode.getNextIndex(aDir, aAmount, this.selectedIndex, this.childNodes.length-1);
  413.           this.selectedIndex = sel;
  414.           return sel;
  415.         ]]></body>
  416.       </method>
  417.  
  418.       <method name="getOverrideValue">
  419.         <body><![CDATA[
  420.           var item = this.activeChild;
  421.           if (item) {
  422.             const ISEARCH_CONTRACTID = "@mozilla.org/rdf/datasource;1?name=internetsearch";
  423.             const nsIInternetSearchService = Components.interfaces.nsIInternetSearchService;           
  424.             var searchService = Components.classes[ISEARCH_CONTRACTID].getService(nsIInternetSearchService);
  425.             var searchEng = item.getAttribute("searchEngine");
  426.             var searchEngUrl = item.getAttribute("searchBarUrl");
  427.             var escapedSearch = encodeURIComponent(this.mSearchValue)
  428.             if (searchEngUrl) {
  429.                 searchEngUrl += escapedSearch;
  430.                 return searchEngUrl;
  431.             } else {
  432.               return searchService.GetInternetSearchURL(item.getAttribute("searchEngine"),escapedSearch, 0, 0, {value:0});
  433.             }
  434.           }
  435.           return null;
  436.         ]]></body>
  437.       </method>
  438.  
  439.     </implementation>
  440.  
  441.     <handlers>
  442.       <handler event="mouseup">
  443.         this.parentNode.textbox.onResultClick();
  444.       </handler>
  445.     </handlers>
  446.  
  447.   </binding>
  448.  
  449.   <binding id="autocomplete-search-engine">
  450.     <content>
  451.       <xul:image class="autocomplete-search-engine-img" xbl:inherits="src=icon"/>
  452.       <xul:label class="autocomplete-search-engine-text" xbl:inherits="value=label" crop="right" flex="1"/>
  453.     </content>
  454.     
  455.     <handlers>
  456.       <handler event="mouseover">
  457.         this.parentNode.selectedIndex = this.getAttribute("engineIndex");
  458.       </handler>
  459.  
  460.       <handler event="mouseout">
  461.         this.parentNode.selectedIndex = null;
  462.       </handler>
  463.     </handlers>
  464.   </binding>
  465.   
  466. </bindings>
  467.