~hexmode/+junk/main

« back to all changes in this revision

Viewing changes to install-files/apps/sqlitemanager1.2.0/plugins/MySQL_Import/select.js

  • Committer: Mark A. Hershberger
  • Date: 2008-01-05 19:38:56 UTC
  • Revision ID: hershberger@spawn-xp-20080105193856-6rnzgwa4nehue3qj
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ===================================================================
 
2
// Author: Matt Kruse <matt@mattkruse.com>
 
3
// WWW: http://www.mattkruse.com/
 
4
//
 
5
// NOTICE: You may use this code for any purpose, commercial or
 
6
// private, without any further permission from the author. You may
 
7
// remove this notice from your final code if you wish, however it is
 
8
// appreciated by the author if at least my web site address is kept.
 
9
//
 
10
// You may *NOT* re-distribute this code in any way except through its
 
11
// use. That means, you can include it in your product, or your web
 
12
// site, or any other form where the code is actually being used. You
 
13
// may not put the plain javascript up on your site for download or
 
14
// include it in your javascript libraries for download. 
 
15
// If you wish to share this code with others, please just point them
 
16
// to the URL instead.
 
17
// Please DO NOT link directly to my .js files from your site. Copy
 
18
// the files to your server and use them there. Thank you.
 
19
// ===================================================================
 
20
 
 
21
// HISTORY
 
22
// ------------------------------------------------------------------
 
23
// June 12, 2003: Modified up and down functions to support more than
 
24
//                one selected option
 
25
/*
 
26
DESCRIPTION: These are general functions to deal with and manipulate
 
27
select boxes. Also see the OptionTransfer library to more easily 
 
28
handle transferring options between two lists
 
29
 
 
30
COMPATABILITY: These are fairly basic functions - they should work on
 
31
all browsers that support Javascript.
 
32
*/
 
33
 
 
34
 
 
35
// -------------------------------------------------------------------
 
36
// hasOptions(obj)
 
37
//  Utility function to determine if a select object has an options array
 
38
// -------------------------------------------------------------------
 
39
function hasOptions(obj) {
 
40
        if (obj!=null && obj.options!=null) { return true; }
 
41
        return false;
 
42
        }
 
43
 
 
44
// -------------------------------------------------------------------
 
45
// selectUnselectMatchingOptions(select_object,regex,select/unselect,true/false)
 
46
//  This is a general function used by the select functions below, to
 
47
//  avoid code duplication
 
48
// -------------------------------------------------------------------
 
49
function selectUnselectMatchingOptions(obj,regex,which,only) {
 
50
        if (window.RegExp) {
 
51
                if (which == "select") {
 
52
                        var selected1=true;
 
53
                        var selected2=false;
 
54
                        }
 
55
                else if (which == "unselect") {
 
56
                        var selected1=false;
 
57
                        var selected2=true;
 
58
                        }
 
59
                else {
 
60
                        return;
 
61
                        }
 
62
                var re = new RegExp(regex);
 
63
                if (!hasOptions(obj)) { return; }
 
64
                for (var i=0; i<obj.options.length; i++) {
 
65
                        if (re.test(obj.options[i].text)) {
 
66
                                obj.options[i].selected = selected1;
 
67
                                }
 
68
                        else {
 
69
                                if (only == true) {
 
70
                                        obj.options[i].selected = selected2;
 
71
                                        }
 
72
                                }
 
73
                        }
 
74
                }
 
75
        }
 
76
                
 
77
// -------------------------------------------------------------------
 
78
// selectMatchingOptions(select_object,regex)
 
79
//  This function selects all options that match the regular expression
 
80
//  passed in. Currently-selected options will not be changed.
 
81
// -------------------------------------------------------------------
 
82
function selectMatchingOptions(obj,regex) {
 
83
        selectUnselectMatchingOptions(obj,regex,"select",false);
 
84
        }
 
85
// -------------------------------------------------------------------
 
86
// selectOnlyMatchingOptions(select_object,regex)
 
87
//  This function selects all options that match the regular expression
 
88
//  passed in. Selected options that don't match will be un-selected.
 
89
// -------------------------------------------------------------------
 
90
function selectOnlyMatchingOptions(obj,regex) {
 
91
        selectUnselectMatchingOptions(obj,regex,"select",true);
 
92
        }
 
93
// -------------------------------------------------------------------
 
94
// unSelectMatchingOptions(select_object,regex)
 
95
//  This function Unselects all options that match the regular expression
 
96
//  passed in. 
 
97
// -------------------------------------------------------------------
 
98
function unSelectMatchingOptions(obj,regex) {
 
99
        selectUnselectMatchingOptions(obj,regex,"unselect",false);
 
100
        }
 
101
        
 
102
// -------------------------------------------------------------------
 
103
// sortSelect(select_object)
 
104
//   Pass this function a SELECT object and the options will be sorted
 
105
//   by their text (display) values
 
106
// -------------------------------------------------------------------
 
107
function sortSelect(obj) {
 
108
        var o = new Array();
 
109
        if (!hasOptions(obj)) { return; }
 
110
        for (var i=0; i<obj.options.length; i++) {
 
111
                o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
 
112
                }
 
113
        if (o.length==0) { return; }
 
114
        o = o.sort( 
 
115
                function(a,b) { 
 
116
                        if ((a.text+"") < (b.text+"")) { return -1; }
 
117
                        if ((a.text+"") > (b.text+"")) { return 1; }
 
118
                        return 0;
 
119
                        } 
 
120
                );
 
121
 
 
122
        for (var i=0; i<o.length; i++) {
 
123
                obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
 
124
                }
 
125
        }
 
126
 
 
127
// -------------------------------------------------------------------
 
128
// selectAllOptions(select_object)
 
129
//  This function takes a select box and selects all options (in a 
 
130
//  multiple select object). This is used when passing values between
 
131
//  two select boxes. Select all options in the right box before 
 
132
//  submitting the form so the values will be sent to the server.
 
133
// -------------------------------------------------------------------
 
134
function selectAllOptions(obj) {
 
135
        if (!hasOptions(obj)) { return; }
 
136
        for (var i=0; i<obj.options.length; i++) {
 
137
                obj.options[i].selected = true;
 
138
                }
 
139
        }
 
140
        
 
141
// -------------------------------------------------------------------
 
142
// moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
 
143
//  This function moves options between select boxes. Works best with
 
144
//  multi-select boxes to create the common Windows control effect.
 
145
//  Passes all selected values from the first object to the second
 
146
//  object and re-sorts each box.
 
147
//  If a third argument of 'false' is passed, then the lists are not
 
148
//  sorted after the move.
 
149
//  If a fourth string argument is passed, this will function as a
 
150
//  Regular Expression to match against the TEXT or the options. If 
 
151
//  the text of an option matches the pattern, it will NOT be moved.
 
152
//  It will be treated as an unmoveable option.
 
153
//  You can also put this into the <SELECT> object as follows:
 
154
//    onDblClick="moveSelectedOptions(this,this.form.target)
 
155
//  This way, when the user double-clicks on a value in one box, it
 
156
//  will be transferred to the other (in browsers that support the 
 
157
//  onDblClick() event handler).
 
158
// -------------------------------------------------------------------
 
159
function moveSelectedOptions(from,to) {
 
160
        // Unselect matching options, if required
 
161
        if (arguments.length>3) {
 
162
                var regex = arguments[3];
 
163
                if (regex != "") {
 
164
                        unSelectMatchingOptions(from,regex);
 
165
                        }
 
166
                }
 
167
        // Move them over
 
168
        if (!hasOptions(from)) { return; }
 
169
        for (var i=0; i<from.options.length; i++) {
 
170
                var o = from.options[i];
 
171
                if (o.selected) {
 
172
                        if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
 
173
                        to.options[index] = new Option( o.text, o.value, false, false);
 
174
                        }
 
175
                }
 
176
        // Delete them from original
 
177
        for (var i=(from.options.length-1); i>=0; i--) {
 
178
                var o = from.options[i];
 
179
                if (o.selected) {
 
180
                        from.options[i] = null;
 
181
                        }
 
182
                }
 
183
        if ((arguments.length<3) || (arguments[2]==true)) {
 
184
                sortSelect(from);
 
185
                sortSelect(to);
 
186
                }
 
187
        from.selectedIndex = -1;
 
188
        to.selectedIndex = -1;
 
189
        }
 
190
 
 
191
// -------------------------------------------------------------------
 
192
// copySelectedOptions(select_object,select_object[,autosort(true/false)])
 
193
//  This function copies options between select boxes instead of 
 
194
//  moving items. Duplicates in the target list are not allowed.
 
195
// -------------------------------------------------------------------
 
196
function copySelectedOptions(from,to) {
 
197
        var options = new Object();
 
198
        if (hasOptions(to)) {
 
199
                for (var i=0; i<to.options.length; i++) {
 
200
                        options[to.options[i].value] = to.options[i].text;
 
201
                        }
 
202
                }
 
203
        if (!hasOptions(from)) { return; }
 
204
        for (var i=0; i<from.options.length; i++) {
 
205
                var o = from.options[i];
 
206
                if (o.selected) {
 
207
                        if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {
 
208
                                if (!hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
 
209
                                to.options[index] = new Option( o.text, o.value, false, false);
 
210
                                }
 
211
                        }
 
212
                }
 
213
        if ((arguments.length<3) || (arguments[2]==true)) {
 
214
                sortSelect(to);
 
215
                }
 
216
        from.selectedIndex = -1;
 
217
        to.selectedIndex = -1;
 
218
        }
 
219
 
 
220
// -------------------------------------------------------------------
 
221
// moveAllOptions(select_object,select_object[,autosort(true/false)[,regex]])
 
222
//  Move all options from one select box to another.
 
223
// -------------------------------------------------------------------
 
224
function moveAllOptions(from,to) {
 
225
        selectAllOptions(from);
 
226
        if (arguments.length==2) {
 
227
                moveSelectedOptions(from,to);
 
228
                }
 
229
        else if (arguments.length==3) {
 
230
                moveSelectedOptions(from,to,arguments[2]);
 
231
                }
 
232
        else if (arguments.length==4) {
 
233
                moveSelectedOptions(from,to,arguments[2],arguments[3]);
 
234
                }
 
235
        }
 
236
 
 
237
// -------------------------------------------------------------------
 
238
// copyAllOptions(select_object,select_object[,autosort(true/false)])
 
239
//  Copy all options from one select box to another, instead of
 
240
//  removing items. Duplicates in the target list are not allowed.
 
241
// -------------------------------------------------------------------
 
242
function copyAllOptions(from,to) {
 
243
        selectAllOptions(from);
 
244
        if (arguments.length==2) {
 
245
                copySelectedOptions(from,to);
 
246
                }
 
247
        else if (arguments.length==3) {
 
248
                copySelectedOptions(from,to,arguments[2]);
 
249
                }
 
250
        }
 
251
 
 
252
// -------------------------------------------------------------------
 
253
// swapOptions(select_object,option1,option2)
 
254
//  Swap positions of two options in a select list
 
255
// -------------------------------------------------------------------
 
256
function swapOptions(obj,i,j) {
 
257
        var o = obj.options;
 
258
        var i_selected = o[i].selected;
 
259
        var j_selected = o[j].selected;
 
260
        var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
 
261
        var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
 
262
        o[i] = temp2;
 
263
        o[j] = temp;
 
264
        o[i].selected = j_selected;
 
265
        o[j].selected = i_selected;
 
266
        }
 
267
        
 
268
// -------------------------------------------------------------------
 
269
// moveOptionUp(select_object)
 
270
//  Move selected option in a select list up one
 
271
// -------------------------------------------------------------------
 
272
function moveOptionUp(obj) {
 
273
        if (!hasOptions(obj)) { return; }
 
274
        for (i=0; i<obj.options.length; i++) {
 
275
                if (obj.options[i].selected) {
 
276
                        if (i != 0 && !obj.options[i-1].selected) {
 
277
                                swapOptions(obj,i,i-1);
 
278
                                obj.options[i-1].selected = true;
 
279
                                }
 
280
                        }
 
281
                }
 
282
        }
 
283
 
 
284
// -------------------------------------------------------------------
 
285
// moveOptionDown(select_object)
 
286
//  Move selected option in a select list down one
 
287
// -------------------------------------------------------------------
 
288
function moveOptionDown(obj) {
 
289
        if (!hasOptions(obj)) { return; }
 
290
        for (i=obj.options.length-1; i>=0; i--) {
 
291
                if (obj.options[i].selected) {
 
292
                        if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
 
293
                                swapOptions(obj,i,i+1);
 
294
                                obj.options[i+1].selected = true;
 
295
                                }
 
296
                        }
 
297
                }
 
298
        }
 
299
 
 
300
// -------------------------------------------------------------------
 
301
// removeSelectedOptions(select_object)
 
302
//  Remove all selected options from a list
 
303
//  (Thanks to Gene Ninestein)
 
304
// -------------------------------------------------------------------
 
305
function removeSelectedOptions(from) { 
 
306
        if (!hasOptions(from)) { return; }
 
307
        for (var i=(from.options.length-1); i>=0; i--) { 
 
308
                var o=from.options[i]; 
 
309
                if (o.selected) { 
 
310
                        from.options[i] = null; 
 
311
                        } 
 
312
                } 
 
313
        from.selectedIndex = -1; 
 
314
        } 
 
315
 
 
316
// -------------------------------------------------------------------
 
317
// removeAllOptions(select_object)
 
318
//  Remove all options from a list
 
319
// -------------------------------------------------------------------
 
320
function removeAllOptions(from) { 
 
321
        if (!hasOptions(from)) { return; }
 
322
        for (var i=(from.options.length-1); i>=0; i--) { 
 
323
                from.options[i] = null; 
 
324
                } 
 
325
        from.selectedIndex = -1; 
 
326
        } 
 
327
 
 
328
// -------------------------------------------------------------------
 
329
// addOption(select_object,display_text,value,selected)
 
330
//  Add an option to a list
 
331
// -------------------------------------------------------------------
 
332
function addOption(obj,text,value,selected) {
 
333
        if (obj!=null && obj.options!=null) {
 
334
                obj.options[obj.options.length] = new Option(text, value, false, selected);
 
335
                }
 
336
        }
 
337