1
// Adds to the target select object all elements in array that
2
// correspond to the elements selected in source.
3
// - array should be a array of arrays, indexed by product name. the
4
// array should contain the elements that correspont to that
6
// var array = Array();
7
// array['ProductOne'] = [ 'ComponentA', 'ComponentB' ];
8
// updateSelect(array, source, target);
9
// - sel is a list of selected items, either whole or a diff
10
// depending on sel_is_diff.
11
// - sel_is_diff determines if we are sending in just a diff or the
12
// whole selection. a diff is used to optimize adding selections.
13
// - target should be the target select object.
14
// - single specifies if we selected a single item. if we did, no
17
function updateSelect( array, sel, target, sel_is_diff, single, blank ) {
21
// if single, even if it's a diff (happens when you have nothing
22
// selected and select one item alone), skip this.
25
// array merging/sorting in the case of multiple selections
28
// merge in the current options with the first selection
29
comp = merge_arrays( array[sel[0]], target.options, 1 );
31
// merge the rest of the selection with the results
32
for ( i = 1 ; i < sel.length ; i++ ) {
33
comp = merge_arrays( array[sel[i]], comp, 0 );
36
// here we micro-optimize for two arrays to avoid merging with a
38
comp = merge_arrays( array[sel[0]],array[sel[1]], 0 );
40
// merge the arrays. not very good for multiple selections.
41
for ( i = 2; i < sel.length; i++ ) {
42
comp = merge_arrays( comp, array[sel[i]], 0 );
46
// single item in selection, just get me the list
50
// save the selection in the target select so we can restore it later
51
var selections = new Array();
52
for ( i = 0; i < target.options.length; i++ )
53
if (target.options[i].selected) selections.push(target.options[i].value);
56
target.options.length = 0;
58
// add empty "Any" value back to the list
59
if (blank) target.options[0] = new Option( blank, "" );
61
// load elements of list into select
62
for ( i = 0; i < comp.length; i++ ) {
63
target.options[target.options.length] = new Option( comp[i], comp[i] );
66
// restore the selection
67
for ( i=0 ; i<selections.length ; i++ )
68
for ( j=0 ; j<target.options.length ; j++ )
69
if (target.options[j].value == selections[i]) target.options[j].selected = true;
73
// Returns elements in a that are not in b.
74
// NOT A REAL DIFF: does not check the reverse.
75
// - a,b: arrays of values to be compare.
77
function fake_diff_array( a, b ) {
78
var newsel = new Array();
80
// do a boring array diff to see who's new
84
if ( a[ia] == b[ib] ) {
89
newsel[newsel.length] = a[ia];
96
// takes two arrays and sorts them by string, returning a new, sorted
97
// array. the merge removes dupes, too.
98
// - a, b: arrays to be merge.
99
// - b_is_select: if true, then b is actually an optionitem and as
100
// such we need to use item.value on it.
102
function merge_arrays( a, b, b_is_select ) {
105
var ret = new Array();
108
// iterate through both arrays and add the larger item to the return
109
// list. remove dupes, too. Use toLowerCase to provide
110
// case-insensitivity.
112
while ( ( pos_a < a.length ) && ( pos_b < b.length ) ) {
115
bitem = b[pos_b].value;
121
// smaller item in list a
122
if ( aitem.toLowerCase() < bitem.toLowerCase() ) {
123
ret[ret.length] = aitem;
126
// smaller item in list b
127
if ( aitem.toLowerCase() > bitem.toLowerCase() ) {
128
ret[ret.length] = bitem;
131
// list contents are equal, inc both counters.
132
ret[ret.length] = aitem;
139
// catch leftovers here. these sections are ugly code-copying.
140
if ( pos_a < a.length ) {
141
for ( ; pos_a < a.length ; pos_a++ ) {
142
ret[ret.length] = a[pos_a];
146
if ( pos_b < b.length ) {
147
for ( ; pos_b < b.length; pos_b++ ) {
149
bitem = b[pos_b].value;
153
ret[ret.length] = bitem;
159
// selectProduct reads the selection from f[productfield] and updates
160
// f.version, component and target_milestone accordingly.
161
// - f: a form containing product, component, varsion and
162
// target_milestone select boxes.
164
// - cpts, vers, tms: array of arrays, indexed by product name. the
165
// subarrays contain a list of names to be fed to the respective
166
// selectboxes. For bugzilla, these are generated with perl code
168
// - usetms: this is a global boolean that is defined if the
169
// bugzilla installation has it turned on. generated in perl too.
170
// - first_load: boolean, specifying if it's the first time we load
172
// - last_sel: saves our last selection list so we know what has
173
// changed, and optimize for additions.
175
function selectProduct( f , productfield, componentfield, blank ) {
177
// this is to avoid handling events that occur before the form
178
// itself is ready, which happens in buggy browsers.
180
if ( ( !f ) || ( ! f[productfield] ) ) {
184
// Do nothing if no products are defined (this avoids the
185
// "a has no properties" error from merge_arrays function)
186
if (f[productfield].length == blank ? 1 : 0) {
190
// if this is the first load and nothing is selected, no need to
191
// merge and sort all components; perl gives it to us sorted.
193
if ( ( first_load ) && ( f[productfield].selectedIndex == -1 ) ) {
198
// turn first_load off. this is tricky, since it seems to be
199
// redundant with the above clause. It's not: if when we first load
200
// the page there is _one_ element selected, it won't fall into that
201
// clause, and first_load will remain 1. Then, if we unselect that
202
// item, selectProduct will be called but the clause will be valid
203
// (since selectedIndex == -1), and we will return - incorrectly -
204
// without merge/sorting.
208
// - sel keeps the array of products we are selected.
209
// - is_diff says if it's a full list or just a list of products that
210
// were added to the current selection.
211
// - single indicates if a single item was selected
212
// - selectedIndex is the index of the first selected item
213
// - selectedValue is the value of the first selected item
217
var selectedIndex = f[productfield].selectedIndex;
218
var selectedValue = f[productfield].options[selectedIndex].value;
220
// If nothing is selected, or the selected item is the "blank" value
221
// at the top of the list which represents all products on drop-down menus,
222
// then pick all products so we show all components.
223
if ( selectedIndex == -1 || !cpts[selectedValue])
225
for ( var i = blank ? 1 : 0 ; i < f[productfield].length ; i++ ) {
226
sel[sel.length] = f[productfield].options[i].value;
228
// If there is only one product, then only one product can be selected
229
single = ( sel.length == 1 );
232
for ( i = blank ? 1 : 0 ; i < f[productfield].length ; i++ ) {
233
if ( f[productfield].options[i].selected ) {
234
sel[sel.length] = f[productfield].options[i].value;
238
single = ( sel.length == 1 );
240
// save last_sel before we kill it
244
// this is an optimization: if we've added components, no need
245
// to remerge them; just merge the new ones with the existing
248
if ( ( tmp ) && ( tmp.length < sel.length ) ) {
249
sel = fake_diff_array(sel, tmp);
254
// do the actual fill/update
255
updateSelect( cpts, sel, f[componentfield], is_diff, single, blank );