~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/irc/xul/lib/listbox.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

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