home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / communicator / wallet / WalletPreview.js < prev    next >
Text File  |  2003-06-08  |  5KB  |  173 lines

  1.  
  2. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  3.  *
  4.  * The contents of this file are subject to the Netscape Public
  5.  * License Version 1.1 (the "License"); you may not use this file
  6.  * except in compliance with the License. You may obtain a copy of
  7.  * the License at http://www.mozilla.org/NPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS
  10.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11.  * implied. See the License for the specific language governing
  12.  * rights and limitations under the License.
  13.  *
  14.  * The Original Code is Mozilla Communicator client code, released March
  15.  * 31, 1998.
  16.  *
  17.  * The Initial Developer of the Original Code is Netscape Communications
  18.  * Corporation. Portions created by Netscape are
  19.  * Copyright (C) 1998 Netscape Communications Corporation. All
  20.  * Rights Reserved.
  21.  *
  22.  * Contributor(s): 
  23.  * Jason Kersey (kerz@netscape.com)
  24.  */
  25.  
  26. /* for xpconnect */
  27. var walletpreview =
  28.     Components.classes
  29.       ["@mozilla.org/walletpreview/walletpreview-world;1"].createInstance();
  30. walletpreview = walletpreview.QueryInterface(Components.interfaces.nsIWalletPreview);
  31.  
  32. var prefillList = [];
  33. var fieldCount = 0;
  34.  
  35. function Startup() {
  36.  
  37.   /* fetch the input */
  38.  
  39.   var list = walletpreview.GetPrefillValue();
  40.   var BREAK = list[0];
  41.   prefillList = list.split(BREAK);
  42.  
  43.   var menuPopup;
  44.   var count;
  45.  
  46.   /* create the fill-in entries */
  47.  
  48.   for (var i=1; i<prefillList.length-1; i+=3) {
  49.  
  50.     if(prefillList[i] != 0) {
  51.       count = prefillList[i];
  52.       menuPopup = document.createElement("menupopup");
  53.     }
  54.     count--;
  55.     var menuItem = document.createElement("menuitem");
  56.     if (count == (prefillList[i]-1)) {
  57.       menuItem.setAttribute("selected", "true");
  58.     }
  59.     menuItem.setAttribute("label", prefillList[i+2]);
  60.  
  61.     // Avoid making duplicate entries in the same menulist
  62.     var child = menuPopup.firstChild;
  63.     var alreadyThere = false;
  64.     while (child) {
  65.       if (child.getAttribute("label") == prefillList[i+2]) {
  66.         alreadyThere = true;
  67.         break;
  68.       }
  69.       child = child.nextSibling;
  70.     }
  71.     if (!alreadyThere) {
  72.       menuPopup.appendChild(menuItem);
  73.     }
  74.  
  75.     if(count == 0) {
  76.       //create the menulist of form data options.
  77.       var menuList = document.createElement("menulist");
  78.       menuList.setAttribute("data", prefillList[i+1]);
  79.       menuList.setAttribute("id", "xx"+(++fieldCount));
  80.       menuList.setAttribute("allowevents", "true");
  81.       menuList.appendChild(menuPopup);
  82.  
  83.       //create the checkbox for the menulist.
  84.       var localCheckBox = document.createElement("checkbox");
  85.       localCheckBox.setAttribute("id", "x"+fieldCount);
  86.       // Note: menulist name is deliberately chosen to be x + checkbox name in
  87.       //       order to make it easy to get to menulist from associated checkbox
  88.       localCheckBox.setAttribute("oncommand", "UpdateMenuListEnable(this)");
  89.       localCheckBox.setAttribute("checked", "true");
  90.       localCheckBox.setAttribute("crop", "right");
  91.       
  92.       //fix label so it only shows title of field, not any of the url.
  93.       var colonPos = prefillList[i+1].indexOf(':');
  94.       var checkBoxLabel;
  95.       if (colonPos != -1)
  96.         checkBoxLabel = prefillList[i+1].slice(colonPos + 1)
  97.       else
  98.         checkBoxLabel = prefillList[i+1];
  99.       localCheckBox.setAttribute("label", checkBoxLabel );
  100.  
  101.       //append all the items into the row.
  102.       var row = document.createElement("row");
  103.       row.appendChild(localCheckBox);
  104.       row.appendChild(menuList);
  105.  
  106.       var rows = document.getElementById("rows");
  107.       rows.appendChild(row);
  108.  
  109.       // xul bug: if this is done earlier, it will result in a crash (???)
  110.       menuList.setAttribute("editable", "true");
  111.  
  112.     }
  113.   }
  114. }
  115.  
  116. function UpdateMenuListEnable(checkBox) {
  117.   var id = checkBox.getAttribute("id");
  118.   var menuList = document.getElementById("x" + id);
  119.   var menuItem = menuList.selectedItem;
  120.   if (checkBox.checked) {
  121.     menuList.removeAttribute("disabled");
  122.     menuList.setAttribute("editable", "true");
  123.   } else {
  124.     menuList.setAttribute("disabled", "true");
  125.     menuList.removeAttribute("editable");
  126.   }
  127.   menuList.selectedItem = menuItem;
  128. }
  129.  
  130. function EncodeVerticalBars(s) {
  131.   s = s.replace(/\^/g, "^1");
  132.   s = s.replace(/\|/g, "^2");
  133.   return s;
  134. }
  135.  
  136. function Save() {
  137.   var url = prefillList[prefillList.length-1];
  138.   var fillins = "";
  139.  
  140.   for (var i=1; i<=fieldCount; i++) { 
  141.     var menuList = document.getElementById("xx" + i);
  142.     if (menuList)
  143.     {
  144.       fillins += menuList.getAttribute("data")  + "#*%$";
  145.       var localCheckBox = document.getElementById("x" + i);
  146.       if (localCheckBox.checked) {
  147.         fillins += menuList.value;
  148.       }
  149.       fillins += "#*%$";
  150.     }
  151.   }
  152.  
  153.   var checkBox = document.getElementById("checkbox");
  154.   var checked = checkBox.checked;
  155.  
  156.   var result = "|fillins|"+EncodeVerticalBars(fillins)+
  157.                "|url|"+EncodeVerticalBars(url)+"|skip|"+checked+"|";
  158.   walletpreview.SetValue(result, window);
  159.   return true;
  160. }
  161.  
  162. function Cancel() {
  163.   var list = prefillList[prefillList.length-2];
  164.   var result = "|list|"+list+"|fillins||url||skip|false|";
  165.   walletpreview.SetValue(result, window);
  166.   return true;
  167. }
  168.  
  169. function doHelpButton() {
  170.   openHelp("forms_prefill");
  171.   return true;
  172. }
  173.