rt 4.2.16
[freeside.git] / rt / share / static / js / combobox.js
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));
6     } else {
7         ComboBox_Init(n)();
8     }
9 }
10 function ComboBox_Init(n) {
11     return function () {
12         if ( ComboBox_UplevelBrowser( n ) ) {
13             ComboBox_Load( n );
14         }
15     }
16 }
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;
23     return true;
24 }
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 );
30     
31     
32     combo.List = list;
33     combo.Button = button;
34     combo.Text = text;
35     
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";
43     
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); };
48     
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;
58     list.onchange = null;
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;
65     list.Hide();
66     
67 }
68 function ComboBox_InitEvent( e ) {
69     if( typeof( e ) == "undefined" && typeof( window.event ) != "undefined" ) e = window.event;
70     if( e == null ) e = new Object();
71     return e;
72 }
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) ;
77     rNew.select();
78 }
79 function ComboBox_GetListSize( theList ) {
80     ComboBox_EnsureListSize( theList );
81     return theList.listSize;
82 }
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" );
88                 return;
89             }
90         }
91         if( theList.options.length > 0 ) {
92             theList.listSize = theList.options.length;
93             return;
94         }
95         theList.listSize = 4;
96     }
97 }
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 ) {
101         this.Select();
102         return;
103     }
104     if( e.keyCode == 27 ) {
105         this.Hide();
106         this.Container.Text.focus();
107         return;
108     }
109 }
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();
114     }
115 }
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;
123     }
124     for( var i = 0; i < items.length; i++ ) {
125         var item = items[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();
130                             }
131             break;
132         }
133     }
134 }
135 function ComboBox_ListFireTextChange() {
136     var textOnChange = this.Container.Text.onchange;
137             if ( textOnChange != null && typeof(textOnChange) == "function" ) {
138                     textOnChange();
139             }
140 }
141 function ComboBox_ListEnableBlur(e) {
142     this.onblur = this.Hide;
143 }
144 function ComboBox_ListDisableBlur(e) {
145     this.onblur = null;
146 }
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) ;
157                 rNew.select();
158             }
159         }
160     }
161     this.Hide();
162     this.Container.Text.focus();
163 }
164 function ComboBox_ToggleList(e) {
165     if( this.Container.List.IsShowing == true ) {
166         this.Container.List.Hide();
167     } else {
168         this.Container.List.Show();
169     }
170 }
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);
176         this.focus();
177         this.IsShowing = true;
178     }
179 }
180 function ComboBox_HideList(e) {
181     if( this.IsShowing ) {
182                     ComboBox_SetVisibility(this,false);
183         this.IsShowing = false;
184     }
185 }
186 function ComboBox_SetVisibility(theList, isVisible) {
187     setVisibility(theList, isVisible);
188 }
189 function ComboBox_RecursiveOffsetTop(thisObject,isFirst) {
190     if(thisObject.offsetParent) {
191         if ( thisObject.style.position == "absolute" && !isFirst && typeof(document.designMode) != "undefined" ) {
192             return 0;
193         }
194         return (thisObject.offsetTop + ComboBox_RecursiveOffsetTop(thisObject.offsetParent,false));
195     } else {
196         return thisObject.offsetTop;
197     }
198 }
199 function ComboBox_RecursiveOffsetLeft(thisObject,isFirst) {
200     if(thisObject.offsetParent) {
201         if ( thisObject.style.position == "absolute" && !isFirst && typeof(document.designMode) != "undefined" ) {
202             return 0;
203         }
204         return (thisObject.offsetLeft + ComboBox_RecursiveOffsetLeft(thisObject.offsetParent,false));
205     } else {
206         return thisObject.offsetLeft;
207     }
208 }
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" ) {
213         textOnChange();
214     }
215 }