1 function ComboBox_InitWith(n) {
2 if ( typeof( window.addEventListener ) != "undefined" ) {
3 window.addEventListener("load", ComboBox_Init(n), false);
4 } else if ( typeof( window.attachEvent ) != "undefined" ) {
5 window.attachEvent("onload", ComboBox_Init(n));
10 function ComboBox_Init(n) {
12 if ( ComboBox_UplevelBrowser( n ) ) {
17 function ComboBox_UplevelBrowser( n ) {
18 if( typeof( document.getElementById ) == "undefined" ) return false;
19 var combo = document.getElementById( n + "_Container" );
20 if( combo == null || typeof( combo ) == "undefined" ) return false;
21 if( typeof( combo.style ) == "undefined" ) return false;
22 if( typeof( combo.innerHTML ) == "undefined" ) return false;
25 function ComboBox_Load( comboId ) {
26 var combo = document.getElementById( comboId + "_Container" );
27 var button = document.getElementById( comboId + "_Button" );
28 var list = document.getElementById( comboId + "_List" );
29 var text = document.getElementById( comboId );
33 combo.Button = button;
36 button.Container = combo;
37 button.Toggle = ComboBox_ToggleList;
38 button.onclick = button.Toggle;
39 button.onmouseover = function(e) { this.Container.List.DisableBlur(e); };
40 button.onmouseout = function(e) { this.Container.List.EnableBlur(e); };
41 button.onselectstart = function(e){ return false; };
42 button.style.height = ( list.offsetHeight - 4 ) + "px";
44 text.Container = combo;
45 text.TypeDown = ComboBox_TextTypeDown;
46 text.KeyAccess = ComboBox_TextKeyAccess;
47 text.onkeyup = function(e) { this.KeyAccess(e); this.TypeDown(e); };
49 list.Container = combo;
50 list.Show = ComboBox_ShowList;
51 list.Hide = ComboBox_HideList;
52 list.EnableBlur = ComboBox_ListEnableBlur;
53 list.DisableBlur = ComboBox_ListDisableBlur;
54 list.Select = ComboBox_ListItemSelect;
55 list.ClearSelection = ComboBox_ListClearSelection;
56 list.KeyAccess = ComboBox_ListKeyAccess;
57 list.FireTextChange = ComboBox_ListFireTextChange;
59 list.onclick = function(e){ this.Select(e); this.ClearSelection(); this.FireTextChange(); };
60 list.onkeyup = function(e) { this.KeyAccess(e); };
61 list.EnableBlur(null);
62 list.style.position = "absolute";
63 list.size = ComboBox_GetListSize( list );
64 list.IsShowing = true;
68 function ComboBox_InitEvent( e ) {
69 if( typeof( e ) == "undefined" && typeof( window.event ) != "undefined" ) e = window.event;
70 if( e == null ) e = new Object();
73 function ComboBox_ListClearSelection() {
74 if ( typeof( this.Container.Text.createTextRange ) == "undefined" ) return;
75 var rNew = this.Container.Text.createTextRange();
76 rNew.moveStart('character', this.Container.Text.value.length) ;
79 function ComboBox_GetListSize( theList ) {
80 ComboBox_EnsureListSize( theList );
81 return theList.listSize;
83 function ComboBox_EnsureListSize( theList ) {
84 if ( typeof( theList.listSize ) == "undefined" ) {
85 if( typeof( theList.getAttribute ) != "undefined" ) {
86 if( theList.getAttribute( "size" ) != null && theList.getAttribute( "size" ) != "" ) {
87 theList.listSize = theList.getAttribute( "size" );
91 if( theList.options.length > 0 ) {
92 theList.listSize = theList.options.length;
98 function ComboBox_ListKeyAccess(e) { //Make enter/space and escape do the right thing :)
99 e = ComboBox_InitEvent( e );
100 if( e.keyCode == 13 || e.keyCode == 32 ) {
104 if( e.keyCode == 27 ) {
106 this.Container.Text.focus();
110 function ComboBox_TextKeyAccess(e) { //Make alt+arrow expand the list
111 e = ComboBox_InitEvent( e );
112 if( e.altKey && (e.keyCode == 38 || e.keyCode == 40) ) {
113 this.Container.List.Show();
116 function ComboBox_TextTypeDown(e) { //Make the textbox do a type-down on the list
117 e = ComboBox_InitEvent( e );
118 var items = this.Container.List.options;
119 if( this.value == "" ) return;
120 var ctrlKeys = Array( 8, 46, 37, 38, 39, 40, 33, 34, 35, 36, 45, 16, 20 );
121 for( var i = 0; i < ctrlKeys.length; i++ ) {
122 if( e.keyCode == ctrlKeys[i] ) return;
124 for( var i = 0; i < items.length; i++ ) {
126 if( item.text.toLowerCase().indexOf( this.value.toLowerCase() ) == 0 ) {
127 this.Container.List.selectedIndex = i;
128 if ( typeof( this.Container.Text.createTextRange ) != "undefined" ) {
129 this.Container.List.Select();
135 function ComboBox_ListFireTextChange() {
136 var textOnChange = this.Container.Text.onchange;
137 if ( textOnChange != null && typeof(textOnChange) == "function" ) {
141 function ComboBox_ListEnableBlur(e) {
142 this.onblur = this.Hide;
144 function ComboBox_ListDisableBlur(e) {
147 function ComboBox_ListItemSelect(e) {
148 if( this.options.length > 0 ) {
149 var text = this.Container.Text;
150 var oldValue = text.value;
151 var newValue = this.options[ this.selectedIndex ].text;
152 text.value = newValue;
153 if ( typeof( text.createTextRange ) != "undefined" ) {
154 if (newValue != oldValue) {
155 var rNew = text.createTextRange();
156 rNew.moveStart('character', oldValue.length) ;
162 this.Container.Text.focus();
164 function ComboBox_ToggleList(e) {
165 if( this.Container.List.IsShowing == true ) {
166 this.Container.List.Hide();
168 this.Container.List.Show();
171 function ComboBox_ShowList(e) {
172 if ( !this.IsShowing && !this.disabled ) {
173 this.style.top = '1.2em';//( this.Container.offsetHeight + ComboBox_RecursiveOffsetTop(this.Container,true) ) + "px";
174 this.style.left = '0px';// ( ComboBox_RecursiveOffsetLeft(this.Container,true) + 1 ) + "px";
175 ComboBox_SetVisibility(this,true);
177 this.IsShowing = true;
180 function ComboBox_HideList(e) {
181 if( this.IsShowing ) {
182 ComboBox_SetVisibility(this,false);
183 this.IsShowing = false;
186 function ComboBox_SetVisibility(theList, isVisible) {
187 setVisibility(theList, isVisible);
189 function ComboBox_RecursiveOffsetTop(thisObject,isFirst) {
190 if(thisObject.offsetParent) {
191 if ( thisObject.style.position == "absolute" && !isFirst && typeof(document.designMode) != "undefined" ) {
194 return (thisObject.offsetTop + ComboBox_RecursiveOffsetTop(thisObject.offsetParent,false));
196 return thisObject.offsetTop;
199 function ComboBox_RecursiveOffsetLeft(thisObject,isFirst) {
200 if(thisObject.offsetParent) {
201 if ( thisObject.style.position == "absolute" && !isFirst && typeof(document.designMode) != "undefined" ) {
204 return (thisObject.offsetLeft + ComboBox_RecursiveOffsetLeft(thisObject.offsetParent,false));
206 return thisObject.offsetLeft;
209 function ComboBox_SimpleAttach(selectElement,textElement) {
210 textElement.value = selectElement.options[ selectElement.options.selectedIndex ].text;
211 var textOnChange = textElement.onchange;
212 if ( textOnChange != null && typeof( textOnChange ) == "function" ) {