home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / chrome / chatzilla.jar / content / chatzilla / lib / xul / listbox.js next >
Text File  |  2000-05-08  |  5KB  |  212 lines

  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Mozilla 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/MPL/
  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 JSIRC Library
  14.  *
  15.  * The Initial Developer of the Original Code is New Dimensions Consulting,
  16.  * Inc. Portions created by New Dimensions Consulting, Inc. are
  17.  * Copyright (C) 1999 New Dimenstions Consulting, Inc. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s): 
  21.  *
  22.  *
  23.  * Contributor(s):
  24.  *  Robert Ginda, rginda@ndcico.com, original author
  25.  */
  26.  
  27. function CListBox ()
  28. {
  29.  
  30.     this.listContainer = document.createElement ("box");
  31.     this.listContainer.setAttribute ("class", "list");
  32.     this.listContainer.setAttribute ("align", "vertical");
  33.  
  34. }
  35.  
  36. CListBox.prototype.clear =
  37. function lbox_clear (e)
  38. {
  39.     var obj = this.listContainer.firstChild;
  40.  
  41.     while (obj)
  42.     {
  43.         this.listContainer.removeChild (obj);
  44.         obj = obj.nextSibling;    
  45.     }    
  46.  
  47. }
  48.  
  49. CListBox.prototype.clickHandler =
  50. function lbox_chandler (e)
  51. {
  52.     var lm = this.listManager;
  53.     
  54.     e.target = this;
  55.  
  56.     if (lm && typeof lm.onClick == "function")
  57.         lm.onClick ({realTarget: this, event: e});
  58.     
  59. }   
  60.  
  61. CListBox.prototype.onClick =
  62. function lbox_chandler (e)
  63.  
  64.     /* Check for the button number first */
  65.     /* FIXME: are there constants for this stuff? */
  66.     if (e.event.which == 3)
  67.     {
  68.         return;
  69.     }
  70.  
  71.     /* 
  72.      * If the ctrlKey is _not_ set, unselect all currently 
  73.      * selected items 
  74.      */    
  75.  
  76.     if (!e.event.ctrlKey) 
  77.     {
  78.         this.enumerateElements(this._unselectCallback, e.realTarget);
  79.     }
  80.  
  81.     /* Check to see whether the item is currently selected */
  82.     var isSelected = e.realTarget.getAttribute("selected");
  83.  
  84.     /* toggle the value */
  85.     if ("true" == isSelected)
  86.         e.realTarget.setAttribute("selected", "false");    
  87.     else
  88.         e.realTarget.setAttribute("selected", "true");
  89.     
  90. }
  91.  
  92. CListBox.prototype._unselectCallback =
  93. function (element, ndx, realTarget)
  94. {
  95.     if (element != realTarget)
  96.             element.setAttribute("selected", "false");    
  97. }
  98.  
  99. CListBox.prototype.add =
  100. function lbox_add (stuff, tag)
  101. {
  102.     /* NOTE: JG using a div here makes the item span the full 
  103.        length and looks better when selected */
  104.     
  105.     var option = document.createElement ("box");
  106.     option.setAttribute ("class", "list-option");
  107.     option.setAttribute ("align", "horizontal");
  108.     option.appendChild (stuff);
  109.     option.onclick = this.clickHandler;
  110.     option.listManager = this;
  111.     option.tag = tag;
  112.     this.listContainer.appendChild (option);
  113.     
  114.     return true;    
  115. }
  116.  
  117. CListBox.prototype.prepend =
  118. function lbox_prepend (stuff, oldstuff, tag)
  119. {
  120.     
  121.     if (!this.listContainer.firstChild)
  122.         return this.add (stuff, tag);
  123.  
  124.     var nextOption = this._findOption (oldstuff);
  125.     if (!nextOption)
  126.         return false;
  127.     
  128.     var option = document.createElement ("box");
  129.     option.setAttribute ("align", "horizontal");
  130.  
  131.     option.appendChild (stuff);
  132.     option.onclick = this.clickHandler;
  133.     option.listManager = this;
  134.     option.tag = tag;
  135.     this.listContainer.insertBefore (option, nextOption);
  136.     
  137. }
  138.  
  139. CListBox.prototype.insert =
  140. function lbox_Insert (stuff, tag)
  141. {
  142.     this.prepend (stuff, this.listContainer.firstChild, tag);
  143. }
  144.  
  145. CListBox.prototype.remove =
  146. function lbox_remove (stuff)
  147. {
  148.     var option = this._findOption (stuff);
  149.  
  150.     if (option)
  151.         this.listContainer.removeChild (option);
  152.  
  153.     return;
  154. }
  155.  
  156. CListBox.prototype._findOption =
  157. function lbox_remove (stuff)
  158. {
  159.     var option = this.listContainer.firstChild;
  160.  
  161.     while (option)
  162.     {
  163.         if (option.firstChild == stuff)
  164.             return option;
  165.         option = option.nextSibling;
  166.     }
  167.  
  168.     return;
  169. }
  170.  
  171. CListBox.prototype.enumerateElements =
  172. /* NOTE: JG: added data param so arbitrary data can be passed. */
  173. function lbox_enum (callback, data)
  174. {
  175.     var i = 0;
  176.     var current = this.listContainer.firstChild;
  177.     
  178.     while (current)
  179.     {
  180.         callback (current, i++, data);
  181.         current = current.nextSibling;
  182.     }
  183.     
  184. }
  185.  
  186. /**
  187.  * Using enumerateElements, this is used just to fill an array
  188.  * with selected nick names.
  189.  * @returns an array of selected nicknames
  190.  */
  191. CListBox.prototype.getSelectedItems =
  192. function lbox_getselecteditems () 
  193. {
  194.     var ary = [];
  195.  
  196.     this.enumerateElements(this.selectedItemCallback, ary);
  197.  
  198.     return ary;
  199. }
  200.  
  201. /**
  202.  * used to build the array of values returned by getSelectedItems.
  203.  */
  204. CListBox.prototype.selectedItemCallback =
  205. function (item, idx, data) 
  206. {
  207.     return item;
  208. }
  209.  
  210.  
  211.