home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 May / PCWorld_2003-05_cd.bin / Komunik / phoenix / chrome / toolkit.jar / content / global / consoleBindings.xml < prev    next >
Extensible Markup Language  |  2002-08-05  |  15KB  |  420 lines

  1. <?xml version="1.0"?>
  2.  
  3. <!DOCTYPE window SYSTEM "chrome://global/locale/console.dtd">
  4.  
  5. <bindings id="consoleBindings"
  6.           xmlns="http://www.mozilla.org/xbl"
  7.           xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  8.           xmlns:xbl="http://www.mozilla.org/xbl">
  9.  
  10.   <binding id="console-box" extends="xul:box">
  11.     <content>  
  12.       <xul:stringbundle src="chrome://global/locale/console.properties" role="string-bundle"/>
  13.       <xul:vbox class="console-box-internal">
  14.         <xul:vbox class="console-rows" role="console-rows" xbl:inherits="dir=sortOrder"/>
  15.       </xul:vbox>
  16.     </content>
  17.   
  18.     <implementation>
  19.       <field name="limit" readonly="true">
  20.         250
  21.       </field>
  22.       <field name="_showChromeErrors">-1</field>
  23.       
  24.       <property name="showChromeErrors">
  25.         <getter><![CDATA[
  26.           if (this._showChromeErrors != -1)
  27.             return this._showChromeErrors;
  28.           var pref = Components.classes['@mozilla.org/preferences-service;1'].getService();
  29.           pref = pref.QueryInterface(Components.interfaces.nsIPrefBranch);
  30.  
  31.           try {
  32.             return this._showChromeErrors = pref.getBoolPref("javascript.options.showInConsole");
  33.           }
  34.           catch(ex) {
  35.             return this._showChromeErrors = false;
  36.           }
  37.         ]]></getter>
  38.       </property>          
  39.  
  40.       <property name="count" readonly="true">
  41.         <getter>return this.mCount</getter>
  42.       </property>
  43.     
  44.       <property name="mode">
  45.         <getter>return this.mMode;</getter>
  46.         <setter><![CDATA[
  47.           this.mMode = val || "All";
  48.           this.setAttribute("mode", this.mMode);
  49.           return val;
  50.         ]]></setter>
  51.       </property>
  52.     
  53.       <property name="sortOrder">
  54.         <getter>return this.getAttribute("sortOrder");</getter>
  55.         <setter>this.setAttribute("sortOrder", val); return val;</setter>
  56.       </property>
  57.       <field name="mSelectedItem">null</field>
  58.       <property name="selectedItem">
  59.         <getter>return this.mSelectedItem</getter>
  60.         <setter><![CDATA[
  61.           if (this.mSelectedItem)
  62.             this.mSelectedItem.removeAttribute("selected");
  63.           
  64.           this.mSelectedItem = val;
  65.           val.setAttribute("selected", "true");
  66.         ]]></setter>
  67.       </property>
  68.     
  69.       <method name="init">
  70.         <body><![CDATA[
  71.           this.mCount = 0;
  72.           
  73.           this.mConsoleListener = {
  74.             console: this, 
  75.             observe : function(aObject) { this.console.appendItem(aObject); }
  76.           };
  77.           
  78.           this.mConsoleRowBox = this.getAnonElByAttr("role", "console-rows");
  79.           this.mStrBundle = this.getAnonElByAttr("role", "string-bundle");
  80.           
  81.           try {
  82.             var isupports = Components.classes['@mozilla.org/consoleservice;1'].getService();
  83.             this.mCService = isupports.QueryInterface(Components.interfaces.nsIConsoleService);
  84.             this.mCService.registerListener(this.mConsoleListener);
  85.           } catch (ex) {
  86.             appendItem(
  87.               "Unable to display errors - couldn't get Console Service component. " +
  88.               "(Missing @mozilla.org/consoleservice;1)");
  89.             return;
  90.           }          
  91.                     
  92.           this.mMode = this.getAttribute("mode") || "All";
  93.  
  94.           this.appendInitialItems();
  95.         ]]></body>
  96.       </method>
  97.     
  98.       <method name="destroy">
  99.         <body><![CDATA[
  100.           this.mCService.unregisterListener(this.mConsoleListener);
  101.         ]]></body>
  102.       </method>
  103.     
  104.       <method name="appendInitialItems">
  105.         <body><![CDATA[
  106.           var out = {}; // Throwaway references to support 'out' parameters.
  107.           this.mCService.getMessageArray(out, {});
  108.           var messages = out.value;
  109.       
  110.           // In case getMessageArray returns 0-length array as null
  111.           if (!messages)
  112.             messages = [];
  113.       
  114.           var limit = messages.length - this.limit;
  115.           if (limit < 0) limit = 0;
  116.         
  117.           // Checks if console ever been cleared
  118.           for (var i = messages.length - 1; i >= limit; --i)
  119.             if (!messages[i].message)
  120.               break;
  121.         
  122.           // Populate with messages after latest "clear"
  123.           while (++i < messages.length)
  124.             this.appendItem(messages[i]);
  125.         ]]></body>
  126.       </method>
  127.  
  128.       <method name="appendItem">
  129.         <parameter name="aObject"/>
  130.         <body><![CDATA[
  131.           if (!aObject.message) return;
  132.  
  133.           try {
  134.             // Try to QI it to a script error to get more info
  135.             var scriptError = aObject.QueryInterface(Components.interfaces.nsIScriptError);
  136.             
  137.             // filter chrome urls
  138.             if (!this.showChromeErrors && scriptError.sourceName.substr(0, 9) == "chrome://")
  139.               return;
  140.  
  141.             this.appendError(scriptError);
  142.           } catch (ex) {
  143.             try {
  144.               // Try to QI it to a console message
  145.               var msg = aObject.QueryInterface(Components.interfaces.nsIConsoleMessage);
  146.               this.appendMessage(msg.message);
  147.             } catch (ex2) {
  148.               // Give up and append the object itself as a string
  149.               this.appendMessage(aObject);
  150.             }
  151.           }
  152.         ]]></body>
  153.       </method>
  154.  
  155.       <method name="appendError">
  156.         <parameter name="aObject"/>
  157.         <body><![CDATA[
  158.           var row = this.createConsoleRow();
  159.           var nsIScriptError = Components.interfaces.nsIScriptError;
  160.           
  161.           // Is this error actually just a non-fatal warning?
  162.           var warning = aObject.flags & nsIScriptError.warningFlag != 0;
  163.   
  164.           var typetext = warning ? "typeWarning" : "typeError";
  165.           row.setAttribute("typetext", this.mStrBundle.getString(typetext));
  166.           row.setAttribute("type", warning ? "warning" : "error");
  167.           row.setAttribute("msg", aObject.message);
  168.           if (aObject.lineNumber || aObject.sourceName) {
  169.             row.setAttribute("url", aObject.sourceName);
  170.             row.setAttribute("line", aObject.lineNumber);
  171.           } else {
  172.             row.setAttribute("hideSource", "true");
  173.           }
  174.           if (aObject.sourceLine) {
  175.             row.setAttribute("code", aObject.sourceLine.replace("\n", "", "g"));
  176.             if (aObject.columnNumber) {
  177.               row.setAttribute("col", aObject.columnNumber);
  178.               row.setAttribute("errorDots", this.repeatChar(" ", aObject.columnNumber));
  179.               row.setAttribute("errorCaret", " ");
  180.             } else {
  181.               row.setAttribute("hideCaret", "true");
  182.             }
  183.           } else {
  184.             row.setAttribute("hideCode", "true");
  185.           }
  186.           this.appendConsoleRow(row);
  187.         ]]></body>
  188.       </method>
  189.             
  190.       <method name="appendMessage">
  191.         <parameter name="aMessage"/>
  192.         <parameter name="aType"/>
  193.         <body><![CDATA[
  194.           var row = this.createConsoleRow();
  195.           row.setAttribute("type", aType || "message");
  196.           row.setAttribute("msg", aMessage);
  197.           this.appendConsoleRow(row);
  198.         ]]></body>
  199.       </method>
  200.       
  201.       <method name="clear">
  202.         <body><![CDATA[
  203.           this.mCService.logStringMessage(null);
  204.           this.mCount = 0;
  205.           
  206.           var newRows = this.mConsoleRowBox.cloneNode(false);
  207.           this.mConsoleRowBox.parentNode.replaceChild(newRows, this.mConsoleRowBox);
  208.           this.mConsoleRowBox = newRows;
  209.         ]]></body>
  210.       </method>
  211.             
  212.       <method name="copySelectedItem">
  213.         <body><![CDATA[
  214.           if (this.mSelectedItem)
  215.             this.copyString(this.mSelectedItem.toString());
  216.         ]]></body>
  217.       </method>
  218.                   
  219.       <method name="createConsoleRow">
  220.         <body><![CDATA[
  221.           var row = document.createElement("box");
  222.           row.setAttribute("class", "console-row");
  223.           row._IsConsoleRow = true;
  224.           row._ConsoleBox = this;
  225.           return row;
  226.         ]]></body>
  227.       </method>
  228.             
  229.       <method name="appendConsoleRow">
  230.         <parameter name="aRow"/>
  231.         <body><![CDATA[
  232.           this.mConsoleRowBox.appendChild(aRow);
  233.           if (++this.mCount > this.limit) this.deleteFirst();
  234.         ]]></body>
  235.       </method>
  236.             
  237.       <method name="deleteFirst">
  238.         <body><![CDATA[
  239.           var node = this.mConsoleRowBox.firstChild;
  240.           this.mConsoleRowBox.removeChild(node);
  241.           --this.mCount;
  242.         ]]></body>
  243.       </method>
  244.             
  245.       <!-- UTILITY FUNCTIONS -->
  246.       
  247.       <!-- We need this method only because document.getAnonymousElementByAttribute 
  248.           is crashing (as of 2/26/2001) -->
  249.       <method name="getAnonElByAttr">
  250.         <parameter name="aAttr"/>
  251.         <parameter name="aVal"/>
  252.         <body><![CDATA[
  253.           var kids = document.getAnonymousNodes(this);
  254.           for (var i = 0; i < kids.length; ++i) {
  255.             if (kids[i].getAttribute(aAttr) == aVal)
  256.               return kids[i];
  257.             var kids2 = kids[i].getElementsByAttribute(aAttr, aVal);
  258.             if (kids2.length > 0)
  259.               return kids2[0];
  260.           }
  261.           return null;
  262.         ]]></body>
  263.       </method>
  264.  
  265.       <method name="repeatChar">
  266.         <parameter name="aChar"/>
  267.         <parameter name="aCol"/>
  268.         <body><![CDATA[
  269.           var str = "";
  270.           if (aCol)
  271.             for (var i = 1; i < aCol; ++i)
  272.               str += aChar;
  273.           
  274.           return str;
  275.         ]]></body>
  276.       </method>
  277.           
  278.       <method name="copyString">
  279.         <parameter name="aString"/>
  280.         <body><![CDATA[
  281.           try {
  282.             const clipURI = "@mozilla.org/widget/clipboard;1";
  283.             const clipI = Components.interfaces.nsIClipboard;
  284.             var clipboard = Components.classes[clipURI].getService(clipI);
  285.           
  286.             const transURI = "@mozilla.org/widget/transferable;1";
  287.             var transferable = Components.classes[transURI].createInstance(Components.interfaces.nsITransferable);
  288.           
  289.             transferable.addDataFlavor("text/unicode");
  290.             
  291.             const strURI = "@mozilla.org/supports-string;1";
  292.             var wstring = Components.classes[strURI].createInstance(Components.interfaces.nsISupportsString);
  293.             
  294.             wstring.data = aString;
  295.             transferable.setTransferData("text/unicode", wstring, aString.length * 2);
  296.             clipboard.setData(transferable, null, clipI.kGlobalClipboard);
  297.           } catch (ex) {
  298.             // Unable to copy anything, die quietly
  299.           }
  300.         ]]></body>
  301.       </method>
  302.  
  303.       <constructor> this.init(); </constructor>
  304.       <destructor> this.destroy(); </destructor>
  305.     </implementation>
  306.     
  307.     <handlers>
  308.       <handler event="mousedown"><![CDATA[
  309.         if (event.button == 0 || event.button == 2) {
  310.           var target = event.originalTarget;
  311.   
  312.           while (target && !("_IsConsoleRow" in target))
  313.             target = target.parentNode;
  314.  
  315.           if (target)
  316.             this.selectedItem = target;
  317.         }
  318.       ]]></handler>
  319.     </handlers>
  320.   </binding>
  321.  
  322.   <binding id="error" extends="xul:box">
  323.     <content>
  324.       <xul:box class="console-row-internal-box" flex="1">
  325.         <xul:box class="console-row-icon" align="center" xbl:inherits="selected">
  326.           <xul:image class="console-icon" xbl:inherits="src,type"/>
  327.         </xul:box>
  328.         <xul:vbox class="console-row-content" xbl:inherits="selected" flex="1">
  329.           <xul:box class="console-row-msg" align="start">
  330.             <xul:label class="label" xbl:inherits="value=typetext"/>
  331.             <xul:description class="console-error-msg" xbl:inherits="value=msg" flex="1"/>
  332.           </xul:box>
  333.           <xul:box class="console-row-file" xbl:inherits="hidden=hideSource">
  334.             <xul:label class="label" value="&errFile.label;"/>
  335.             <xul:box class="console-error-source" xbl:inherits="url"/>
  336.             <spacer flex="1"/>
  337.             <xul:label class="label" value="&errLine.label;"/>
  338.             <xul:label class="label" xbl:inherits="value=line" flex="1"/>
  339.           </xul:box>
  340.           <xul:vbox class="console-row-code" xbl:inherits="selected,hidden=hideCode">
  341.             <xul:label class="monospace console-code" xbl:inherits="value=code"/>
  342.             <xul:box xbl:inherits="hidden=hideCaret">
  343.               <xul:label class="monospace console-dots" xbl:inherits="value=errorDots"/>
  344.               <xul:label class="monospace console-caret" xbl:inherits="value=errorCaret"/>
  345.               <xul:spacer flex="1"/>
  346.             </xul:box>
  347.           </xul:vbox>
  348.         </xul:vbox>
  349.       </xul:box>
  350.     </content>
  351.  
  352.     <implementation>
  353.       
  354.       <method name="toString">
  355.         <body><![CDATA[
  356.           var msg = this.getAttribute("typetext") + " " + this.getAttribute("msg");
  357.           
  358.           var strBundle = this._ConsoleBox.mStrBundle;
  359.           
  360.           if (this.hasAttribute("line") && this.hasAttribute("url")) {
  361.             msg += "\n" + strBundle.getFormattedString("errFile", 
  362.                                         [this.getAttribute("url")]) + "\n";
  363.             if (this.hasAttribute("col")) {
  364.               msg += strBundle.getFormattedString("errLineCol",
  365.                          [this.getAttribute("line"), this.getAttribute("col")]);
  366.             } else
  367.               msg += strBundle.getFormattedString("errLine", [this.getAttribute("line")]);
  368.           }
  369.           
  370.           if (this.hasAttribute("code"))
  371.             msg += "\n" + strBundle.getString("errCode") + "\n" + this.getAttribute("code");
  372.           
  373.           return msg;
  374.         ]]></body>
  375.       </method>
  376.       
  377.     </implementation>
  378.     
  379.   </binding>
  380.   
  381.   <binding id="message" extends="xul:box">
  382.     <content>
  383.       <xul:box class="console-internal-box" flex="1">
  384.         <xul:box class="console-row-icon" align="center">
  385.           <xul:image class="console-icon" xbl:inherits="src,type"/>
  386.         </xul:box>
  387.         <xul:vbox class="console-row-content" xbl:inherits="selected" flex="1">
  388.           <xul:vbox class="console-row-msg" flex="1">
  389.             <xul:description class="console-msg-text" xbl:inherits="value=msg" flex="1"/>
  390.           </xul:vbox>
  391.         </xul:vbox>
  392.       </xul:box>
  393.     </content>
  394.  
  395.     <implementation>
  396.       <method name="toString">
  397.         <body><![CDATA[
  398.           return this.getAttribute("msg");
  399.         ]]></body>
  400.       </method>
  401.     </implementation>
  402.   </binding>
  403.  
  404.   <binding id="console-error-source" extends="xul:box">
  405.     <content>
  406.       <xul:label class="text-link" xbl:inherits="value=url" crop="right"/>
  407.     </content>
  408.  
  409.     <handlers>
  410.       <handler event="click"><![CDATA[
  411.           window.openDialog(
  412.             "chrome://navigator/content/viewSource.xul", "_blank", 
  413.             "scrollbars,resizable,chrome,dialog=no", this.getAttribute("url"));
  414.       ]]></handler>
  415.     </handlers>
  416.   </binding>
  417.  
  418. </bindings>
  419.  
  420.