import rt 3.8.9
[freeside.git] / rt / html / NoAuth / js / combobox.js
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %#  
5 %# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC 
6 %#                                          <jesse@bestpractical.com>
7 %# 
8 %# (Except where explicitly superseded by other copyright notices)
9 %# 
10 %# 
11 %# LICENSE:
12 %# 
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
16 %# from www.gnu.org.
17 %# 
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.
22 %# 
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/licenses/old-licenses/gpl-2.0.html.
28 %# 
29 %# 
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %# 
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.)
37 %# 
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.
46 %# 
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));
53     } else {
54         ComboBox_Init(n)();
55     }
56 }
57 function ComboBox_Init(n) {
58     return function () {
59         if ( ComboBox_UplevelBrowser( n ) ) {
60             ComboBox_Load( n );
61         }
62     }
63 }
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;
70     return true;
71 }
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 );
77     
78     
79     combo.List = list;
80     combo.Button = button;
81     combo.Text = text;
82     
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";
91     
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";
97     
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;
114     list.Hide();
115     
116 }
117 function ComboBox_InitEvent( e ) {
118     if( typeof( e ) == "undefined" && typeof( window.event ) != "undefined" ) e = window.event;
119     if( e == null ) e = new Object();
120     return e;
121 }
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) ;
126     rNew.select();
127 }
128 function ComboBox_GetListSize( theList ) {
129     ComboBox_EnsureListSize( theList );
130     return theList.listSize;
131 }
132 function ComboBox_EnsureListSize( theList ) {
133     if ( typeof( theList.listSize ) == "undefined" ) {
134         if( typeof( theList.getAttribute ) != "undefined" ) {
135             if( theList.getAttribute( "size" ) != null && theList.getAttribute( "size" ) != "" ) {
136                 theList.listSize = theList.getAttribute( "size" );
137                 return;
138             }
139         }
140         if( theList.options.length > 0 ) {
141             theList.listSize = theList.options.length;
142             return;
143         }
144         theList.listSize = 4;
145     }
146 }
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 ) {
150         this.Select();
151         return;
152     }
153     if( e.keyCode == 27 ) {
154         this.Hide();
155         this.Container.Text.focus();
156         return;
157     }
158 }
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();
163     }
164 }
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;
172     }
173     for( var i = 0; i < items.length; i++ ) {
174         var item = items[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();
179                             }
180             break;
181         }
182     }
183 }
184 function ComboBox_ListFireTextChange() {
185     var textOnChange = this.Container.Text.onchange;
186             if ( textOnChange != null && typeof(textOnChange) == "function" ) {
187                     textOnChange();
188             }
189 }
190 function ComboBox_ListEnableBlur(e) {
191     this.onblur = this.Hide;
192 }
193 function ComboBox_ListDisableBlur(e) {
194     this.onblur = null;
195 }
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) ;
206                 rNew.select();
207             }
208         }
209     }
210     this.Hide();
211     this.Container.Text.focus();
212 }
213 function ComboBox_ToggleList(e) {
214     if( this.Container.List.IsShowing == true ) {
215         this.Container.List.Hide();
216     } else {
217         this.Container.List.Show();
218     }
219 }
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);
226         this.focus();
227         this.IsShowing = true;
228     }
229 }
230 function ComboBox_HideList(e) {
231     if( this.IsShowing ) {
232                     ComboBox_SetVisibility(this,false);
233         this.IsShowing = false;
234     }
235 }
236 function ComboBox_SetVisibility(theList, isVisible) {
237     setVisibility(theList, isVisible);
238 }
239 function ComboBox_RecursiveOffsetTop(thisObject,isFirst) {
240     if(thisObject.offsetParent) {
241         if ( thisObject.style.position == "absolute" && !isFirst && typeof(document.designMode) != "undefined" ) {
242             return 0;
243         }
244         return (thisObject.offsetTop + ComboBox_RecursiveOffsetTop(thisObject.offsetParent,false));
245     } else {
246         return thisObject.offsetTop;
247     }
248 }
249 function ComboBox_RecursiveOffsetLeft(thisObject,isFirst) {
250     if(thisObject.offsetParent) {
251         if ( thisObject.style.position == "absolute" && !isFirst && typeof(document.designMode) != "undefined" ) {
252             return 0;
253         }
254         return (thisObject.offsetLeft + ComboBox_RecursiveOffsetLeft(thisObject.offsetParent,false));
255     } else {
256         return thisObject.offsetLeft;
257     }
258 }
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" ) {
263         textOnChange();
264     }
265 }