home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / toolkit.jar / content / global / selectDialog.js < prev    next >
Encoding:
JavaScript  |  2001-07-31  |  4.1 KB  |  138 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape Communications
  16.  * Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  * Alec Flett <alecf@netscape.com>
  22.  */
  23.  
  24. var elements = [];
  25. var numItems;
  26. var list;
  27. var param;
  28.  
  29. function selectDialogOnLoad() {
  30.   doSetOKCancel( commonDialogOnOK, commonDialogOnCancel );
  31.  
  32.   param = window.arguments[0].QueryInterface( Components.interfaces.nsIDialogParamBlock  );
  33.   if( !param )
  34.   dump( " error getting param block interface\n" );
  35.  
  36.   var messageText = param.GetString( 1 );
  37.   {
  38.     var messageFragment;
  39.  
  40.     // Let the caller use "\n" to cause breaks
  41.     // Translate these into <br> tags
  42.     var messageParent = (document.getElementById("info.txt"));
  43.     var done = false;
  44.     while (!done) {
  45.       var breakIndex = messageText.indexOf('\n');
  46.       if (breakIndex == 0) {
  47.         // Ignore break at the first character
  48.         messageText = messageText.slice(1);
  49.         messageFragment = "";
  50.       } else if (breakIndex > 0) {
  51.         // The fragment up to the break
  52.         messageFragment = messageText.slice(0, breakIndex);
  53.  
  54.         // Chop off fragment we just found from remaining string
  55.         messageText = messageText.slice(breakIndex+1);
  56.       } else {
  57.         // "\n" not found. We're done
  58.         done = true;
  59.         messageFragment = messageText;
  60.       }
  61.       messageParent.setAttribute("value", messageFragment);
  62.     }
  63.   }
  64.  
  65.   var windowTitle = param.GetString( 0 );
  66.   window.title = windowTitle;
  67.  
  68.   list = document.getElementById("list");
  69.   numItems = param.GetInt( 2 )
  70.  
  71.   var i;
  72.   for ( i = 2; i <= numItems+1; i++ ) {
  73.     var newString = param.GetString( i );
  74.     if (newString == "") {
  75.       newString = "<>";
  76.     }
  77.     elements[i-2] = AppendStringToTreelist(list, newString);
  78.   }
  79.   list.selectItem(elements[0]);
  80.   list.focus();
  81.  
  82.   // resize the window to the content
  83.   window.sizeToContent();
  84.  
  85.   // Move to the right location
  86.   moveToAlertPosition();
  87.   param.SetInt(0, 1 );
  88.   centerWindowOnScreen();
  89. }
  90.  
  91. function commonDialogOnOK() {
  92.   for (var i=0; i<numItems; i++) {
  93.     if (elements[i] == list.selectedItems[0]) {
  94.       param.SetInt(2, i );
  95.       break;
  96.     }
  97.   }
  98.   param.SetInt(0, 0 );
  99.   return true;
  100. }
  101.  
  102. function commonDialogOnCancel() {
  103.   for (var i=0; i<numItems; i++) {
  104.     if (elements[i]) {
  105.       param.SetInt(2, i );
  106.       break;
  107.     }
  108.   }
  109.   param.SetInt(0, 1 );
  110.   return true;
  111. }
  112.  
  113. // following routine should really be in a global utilities package
  114.  
  115. function AppendStringToTreelist(tree, string)
  116. {
  117.   if (tree)
  118.   {
  119.     var treechildren = document.getElementById('child');
  120.  
  121.     var treeitem = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "treeitem");
  122.     var treerow = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "treerow");
  123.     var treecell = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "treecell");
  124.     if (treeitem && treerow && treecell)
  125.     {
  126.       treecell.setAttribute("label", string);
  127.       treerow.appendChild(treecell);
  128.       treeitem.appendChild(treerow);
  129.       treechildren.appendChild(treeitem)
  130.       var len = Number(tree.getAttribute("length"));
  131.       if (!len) len = -1;
  132.       tree.setAttribute("length",len+1);
  133.       return treeitem;
  134.     }
  135.   }
  136.   return null;
  137. }
  138.