home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 March / PCWorld_2005-03_cd.bin / komunikace / kmeleon / kmeleon09.exe / toolkit.jar / content / global / console.js < prev    next >
Text File  |  2005-01-08  |  6KB  |  230 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *          Joe Hewitt <hewitt@netscape.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the NPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the NPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. var gConsole, gConsoleBundle;
  40.  
  41. /* :::::::: Console Initialization ::::::::::::::: */
  42.  
  43. window.onload = function()
  44. {
  45.   // 1 line added K-Meleon
  46.   document.title="Javascript Console";
  47.  
  48.   gConsole = document.getElementById("ConsoleBox");
  49.   gConsoleBundle = document.getElementById("ConsoleBundle");
  50.   
  51.   top.controllers.insertControllerAt(0, ConsoleController);
  52.   
  53.   updateSortCommand(gConsole.sortOrder);
  54.   updateModeCommand(gConsole.mode);
  55.  
  56.   var iframe = document.getElementById("Evaluator");
  57.   iframe.addEventListener("load", displayResult, true);
  58. }
  59.  
  60. /* :::::::: Console UI Functions ::::::::::::::: */
  61.  
  62. function changeMode(aMode)
  63. {
  64.   switch (aMode) {
  65.     case "Errors":
  66.     case "Warnings":
  67.     case "Messages":
  68.       gConsole.mode = aMode;
  69.       break;
  70.     case "All":
  71.       gConsole.mode = null;
  72.   }
  73.   
  74.   document.persist("ConsoleBox", "mode");
  75. }
  76.  
  77. function clearConsole()
  78. {
  79.   gConsole.clear();
  80. }
  81.  
  82. function changeSortOrder(aOrder)
  83. {
  84.   updateSortCommand(gConsole.sortOrder = aOrder);
  85. }
  86.  
  87. function updateSortCommand(aOrder)
  88. {
  89.   var orderString = aOrder == 'reverse' ? "Descend" : "Ascend";
  90.   var bc = document.getElementById("Console:sort"+orderString);
  91.   bc.setAttribute("checked", true);  
  92.  
  93.   orderString = aOrder == 'reverse' ? "Ascend" : "Descend";
  94.   bc = document.getElementById("Console:sort"+orderString);
  95.   bc.setAttribute("checked", false);
  96. }
  97.  
  98. function updateModeCommand(aMode)
  99. {
  100.   var bc = document.getElementById("Console:mode" + aMode);
  101.   bc.setAttribute("checked", true);
  102. }
  103.  
  104. function toggleToolbar(aEl)
  105. {
  106.   var bc = document.getElementById(aEl.getAttribute("observes"));
  107.   var truth = bc.getAttribute("checked");
  108.   bc.setAttribute("checked", truth != "true");
  109.   var toolbar = document.getElementById(bc.getAttribute("_toolbar"));
  110.   toolbar.setAttribute("hidden", truth);
  111.  
  112.   document.persist(toolbar.id, "hidden");
  113.   document.persist(bc.id, "checked");
  114. }
  115.  
  116. function copyItemToClipboard()
  117. {
  118.   gConsole.copySelectedItem();
  119. }
  120.  
  121. function isItemSelected()
  122. {
  123.   return gConsole.selectedItem != null;
  124. }
  125.  
  126. function UpdateCopyMenu()
  127. {
  128.   goUpdateCommand("cmd_copy");
  129. }
  130.  
  131. function onEvalKeyPress(aEvent)
  132. {
  133.   if (aEvent.keyCode == 13)
  134.     evaluateTypein();
  135. }
  136.  
  137. function evaluateTypein()
  138. {
  139.   var code = document.getElementById("TextboxEval").value;
  140.   var iframe = document.getElementById("Evaluator");
  141.   iframe.setAttribute("src", "javascript: " + code);
  142. }
  143.  
  144. function displayResult()
  145. {
  146.   var resultRange = Evaluator.document.createRange();
  147.   resultRange.selectNode(Evaluator.document.documentElement);
  148.   var result = resultRange.toString();
  149.   if (result)
  150.     gConsole.mCService.logStringMessage(result);
  151.     // or could use appendMessage which doesn't persist
  152.   var iframe = document.getElementById("Evaluator");
  153.   if (iframe.getAttribute("src") != "about:blank")
  154.     iframe.setAttribute("src", "about:blank");
  155. }
  156.  
  157. /* :::::::: Command Controller for the Window ::::::::::::::: */
  158.  
  159. var ConsoleController = 
  160. {
  161.   isCommandEnabled: function (aCommand)
  162.   {
  163.     switch (aCommand) {
  164.       case "cmd_copy":
  165.         return isItemSelected();
  166.       default:
  167.         return false;
  168.     }
  169.   },
  170.   
  171.   supportsCommand: function (aCommand) 
  172.   {
  173.     switch (aCommand) {
  174.       case "cmd_copy":
  175.         return true;
  176.       default:
  177.         return false;
  178.     }
  179.   },
  180.   
  181.   doCommand: function (aCommand)
  182.   {
  183.     switch (aCommand) {
  184.       case "cmd_copy":
  185.         copyItemToClipboard();
  186.         break;
  187.       default:
  188.         break;
  189.     }
  190.   },
  191.   
  192.   onEvent: function (aEvent) 
  193.   {
  194.   }
  195. };
  196.  
  197. // XXX DEBUG
  198.  
  199. function debug(aText)
  200. {
  201.   var csClass = Components.classes['@mozilla.org/consoleservice;1'];
  202.   var cs = csClass.getService(Components.interfaces.nsIConsoleService);
  203.   cs.logStringMessage(aText);
  204. }
  205.  
  206. function getStackTrace()
  207. {
  208.   var frame = Components.stack.caller;
  209.   var str = "";
  210.   while (frame) {
  211.     if (frame.filename)
  212.       str += frame.filename + ", Line " + frame.lineNumber;
  213.     else
  214.       str += "[" + gConsoleBundle.getString("noFile") + "]";
  215.     
  216.     str += " --> ";
  217.     
  218.     if (frame.functionName)
  219.       str += frame.functionName;
  220.     else
  221.       str += "[" + gConsoleBundle.getString("noFunction") + "]";
  222.       
  223.     str += "\n";
  224.     
  225.     frame = frame.caller;
  226.   }
  227.   
  228.   return str;
  229. }
  230.