1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC
6 %# <jesse@bestpractical.com>
8 %# (Except where explicitly superseded by other copyright notices)
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 %# General Public License for more details.
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/copyleft/gpl.html.
30 %# CONTRIBUTION SUBMISSION POLICY:
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
47 %# END BPS TAGGED BLOCK }}}
48 function ComboBox_InitWith(n) {
49 if ( typeof( window.addEventListener ) != "undefined" ) {
50 window.addEventListener("load", ComboBox_Init(n), false);
51 } else if ( typeof( window.attachEvent ) != "undefined" ) {
52 window.attachEvent("onload", ComboBox_Init(n));
57 function ComboBox_Init(n) {
59 if ( ComboBox_UplevelBrowser( n ) ) {
64 function ComboBox_UplevelBrowser( n ) {
65 if( typeof( document.getElementById ) == "undefined" ) return false;
66 var combo = document.getElementById( n + "_Container" );
67 if( combo == null || typeof( combo ) == "undefined" ) return false;
68 if( typeof( combo.style ) == "undefined" ) return false;
69 if( typeof( combo.innerHTML ) == "undefined" ) return false;
72 function ComboBox_Load( comboId ) {
73 var combo = document.getElementById( comboId + "_Container" );
74 var button = document.getElementById( comboId + "_Button" );
75 var list = document.getElementById( comboId + "_List" );
76 var text = document.getElementById( comboId );
80 combo.Button = button;
83 button.Container = combo;
84 button.Toggle = ComboBox_ToggleList;
85 button.onclick = button.Toggle;
86 button.onmouseover = function(e) { this.Container.List.DisableBlur(e); };
87 button.onmouseout = function(e) { this.Container.List.EnableBlur(e); };
88 button.innerHTML = "\u25BC";
89 button.onselectstart = function(e){ return false; };
90 button.style.height = ( list.offsetHeight - 4 ) + "px";
92 text.Container = combo;
93 text.TypeDown = ComboBox_TextTypeDown;
94 text.KeyAccess = ComboBox_TextKeyAccess;
95 text.onkeyup = function(e) { this.KeyAccess(e); this.TypeDown(e); };
96 text.style.width = ( list.offsetWidth ) + "px";
98 list.Container = combo;
99 list.Show = ComboBox_ShowList;
100 list.Hide = ComboBox_HideList;
101 list.EnableBlur = ComboBox_ListEnableBlur;
102 list.DisableBlur = ComboBox_ListDisableBlur;
103 list.Select = ComboBox_ListItemSelect;
104 list.ClearSelection = ComboBox_ListClearSelection;
105 list.KeyAccess = ComboBox_ListKeyAccess;
106 list.FireTextChange = ComboBox_ListFireTextChange;
107 list.onchange = null;
108 list.onclick = function(e){ this.Select(e); this.ClearSelection(); this.FireTextChange(); };
109 list.onkeyup = function(e) { this.KeyAccess(e); };
110 list.EnableBlur(null);
111 list.style.position = "absolute";
112 list.size = ComboBox_GetListSize( list );
113 list.IsShowing = true;
117 function ComboBox_InitEvent( e ) {
118 if( typeof( e ) == "undefined" && typeof( window.event ) != "undefined" ) e = window.event;
119 if( e == null ) e = new Object();
122 function ComboBox_ListClearSelection() {
123 if ( typeof( this.Container.Text.createTextRange ) == "undefined" ) return;
124 var rNew = this.Container.Text.createTextRange();
125 rNew.moveStart('character', this.Container.Text.value.length) ;
128 function ComboBox_GetListSize( theList ) {
129 ComboBox_EnsureListSize( theList );
130 return theList.listSize;
132 function ComboBox_EnsureListSize( theList ) {
133 if ( typeof( theList.listSize ) == "undefined" ) {
134 if( typeof( theList.getAttribute ) != "undefined" ) {
135 if( theList.getAttribute( "listSize" ) != null && theList.getAttribute( "listSize" ) != "" ) {
136 theList.listSize = theList.getAttribute( "listSize" );
140 if( theList.options.length > 0 ) {
141 theList.listSize = theList.options.length;
144 theList.listSize = 4;
147 function ComboBox_ListKeyAccess(e) { //Make enter/space and escape do the right thing :)
148 e = ComboBox_InitEvent( e );
149 if( e.keyCode == 13 || e.keyCode == 32 ) {
153 if( e.keyCode == 27 ) {
155 this.Container.Text.focus();
159 function ComboBox_TextKeyAccess(e) { //Make alt+arrow expand the list
160 e = ComboBox_InitEvent( e );
161 if( e.altKey && (e.keyCode == 38 || e.keyCode == 40) ) {
162 this.Container.List.Show();
165 function ComboBox_TextTypeDown(e) { //Make the textbox do a type-down on the list
166 e = ComboBox_InitEvent( e );
167 var items = this.Container.List.options;
168 if( this.value == "" ) return;
169 var ctrlKeys = Array( 8, 46, 37, 38, 39, 40, 33, 34, 35, 36, 45, 16, 20 );
170 for( var i = 0; i < ctrlKeys.length; i++ ) {
171 if( e.keyCode == ctrlKeys[i] ) return;
173 for( var i = 0; i < items.length; i++ ) {
175 if( item.text.toLowerCase().indexOf( this.value.toLowerCase() ) == 0 ) {
176 this.Container.List.selectedIndex = i;
177 if ( typeof( this.Container.Text.createTextRange ) != "undefined" ) {
178 this.Container.List.Select();
184 function ComboBox_ListFireTextChange() {
185 var textOnChange = this.Container.Text.onchange;
186 if ( textOnChange != null && typeof(textOnChange) == "function" ) {
190 function ComboBox_ListEnableBlur(e) {
191 this.onblur = this.Hide;
193 function ComboBox_ListDisableBlur(e) {
196 function ComboBox_ListItemSelect(e) {
197 if( this.options.length > 0 ) {
198 var text = this.Container.Text;
199 var oldValue = text.value;
200 var newValue = this.options[ this.selectedIndex ].text;
201 text.value = newValue;
202 if ( typeof( text.createTextRange ) != "undefined" ) {
203 if (newValue != oldValue) {
204 var rNew = text.createTextRange();
205 rNew.moveStart('character', oldValue.length) ;
211 this.Container.Text.focus();
213 function ComboBox_ToggleList(e) {
214 if( this.Container.List.IsShowing == true ) {
215 this.Container.List.Hide();
217 this.Container.List.Show();
220 function ComboBox_ShowList(e) {
221 if ( !this.IsShowing && !this.disabled ) {
222 this.style.width = ( this.Container.offsetWidth ) + "px";
223 this.style.top = ( this.Container.offsetHeight + ComboBox_RecursiveOffsetTop(this.Container,true) ) + "px";
224 this.style.left = ( ComboBox_RecursiveOffsetLeft(this.Container,true) + 1 ) + "px";
225 ComboBox_SetVisibility(this,true);
227 this.IsShowing = true;
230 function ComboBox_HideList(e) {
231 if( this.IsShowing ) {
232 ComboBox_SetVisibility(this,false);
233 this.IsShowing = false;
236 function ComboBox_SetVisibility(theList, isVisible) {
237 setVisibility(theList, isVisible);
239 function ComboBox_RecursiveOffsetTop(thisObject,isFirst) {
240 if(thisObject.offsetParent) {
241 if ( thisObject.style.position == "absolute" && !isFirst && typeof(document.designMode) != "undefined" ) {
244 return (thisObject.offsetTop + ComboBox_RecursiveOffsetTop(thisObject.offsetParent,false));
246 return thisObject.offsetTop;
249 function ComboBox_RecursiveOffsetLeft(thisObject,isFirst) {
250 if(thisObject.offsetParent) {
251 if ( thisObject.style.position == "absolute" && !isFirst && typeof(document.designMode) != "undefined" ) {
254 return (thisObject.offsetLeft + ComboBox_RecursiveOffsetLeft(thisObject.offsetParent,false));
256 return thisObject.offsetLeft;
259 function ComboBox_SimpleAttach(selectElement,textElement) {
260 textElement.value = selectElement.options[ selectElement.options.selectedIndex ].text;
261 var textOnChange = textElement.onchange;
262 if ( textOnChange != null && typeof( textOnChange ) == "function" ) {