/[drupal]/contributions/modules/FixedDataDropdown/jquery-select.js
ViewVC logotype

Contents of /contributions/modules/FixedDataDropdown/jquery-select.js

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Tue May 29 07:20:27 2007 UTC (2 years, 6 months ago) by markfoodyburton
Branch: MAIN
CVS Tags: DRUPAL-5--1-3, DRUPAL-5--1-2, HEAD
Branch point for: DRUPAL-5
File MIME type: text/javascript
Initial check in of FixedDependentDropdown, now hopefuly in the right place!
1 /*
2 *
3 * Copyright (c) 2006/2007 Sam Collett (http://www.texotela.co.uk)
4 * Licensed under the MIT License:
5 * http://www.opensource.org/licenses/mit-license.php
6 *
7 * 20 February 2007
8 * removeOption can now take a regular expression
9 * (useful if you want to remove multiple options in one go)
10 *
11 * 13 February 2007
12 * addOption can also replace options that already exist with the same value
13 * selectOptions can clear previously selected options
14 * new copyOptions now allows you to copy options from one select to another
15 *
16 * 2 February 2007
17 * Fix for Safari 2.0 - couldn't add option
18 *
19 * 5 December 2006
20 * Select option(s) by value with 'selectOptions'
21 * Based on code by Mathias Bank (http://www.mathias-bank.de)
22 *
23 */
24
25 (function($) {
26
27 /**
28 * Adds (single/multiple) options to a select box (or series of select boxes)
29 *
30 * @name addOption
31 * @author Sam Collett (http://www.texotela.co.uk)
32 * @type jQuery
33 * @example $("#myselect").addOption("Value", "Text"); // add single value (will be selected)
34 * @example $("#myselect").addOption("Value 2", "Text 2", false); // add single value (won't be selected)
35 * @example $("#myselect").addOption({"foo":"bar","bar":"baz"}, false); // add multiple values, but don't select
36 *
37 */
38 $.fn.addOption = function()
39 {
40 var a = arguments;
41 if(a.length == 0) return this;
42 // select option when added? default is true
43 var sO = true;
44 // multiple items
45 var m = false;
46 if(typeof a[0] == "object")
47 {
48 m = true;
49 var items = a[0];
50 }
51 if(a.length >= 2)
52 {
53 if(typeof a[1] == "boolean") sO = a[1];
54 else if(typeof a[2] == "boolean") sO = a[2];
55 if(!m)
56 {
57 var v = a[0];
58 var t = a[1];
59 }
60 }
61 this.each(
62 function()
63 {
64 if(this.nodeName.toLowerCase() != "select") return;
65 if(m)
66 {
67
68 for(var i in items)
69 {
70 $(this).addOption(i, items[i], sO);
71 }
72 }
73 else
74 {
75 var option = document.createElement("option");
76 option.value = v;
77 option.text = t;
78 var i;
79 var r = false;
80 // get options
81 var o = this.options;
82 // get number of options
83 var oL = o.length;
84 // loop through existing options
85 for(i = 0; i < oL; i++)
86 {
87 // replace existing option
88 if(o[i].value == option.value)
89 {
90 r = true;
91 break;
92 }
93 }
94 if(i < oL && !r) i = oL;
95 this.options[i] = option;
96 if(sO)
97 {
98 o[i].selected = true;
99 }
100 }
101 }
102 )
103 return this;
104 }
105
106 /**
107 * Add options via ajax
108 *
109 * @name ajaxAddOption
110 * @author Sam Collett (http://www.texotela.co.uk)
111 * @type jQuery
112 * @param String url Page to get options from (must be valid JSON)
113 * @param Object params (optional) Any parameters to send with the request
114 * @param Boolean select (optional) Select the added options, default true
115 * @example $("#myselect").ajaxAddOption("myoptions.php");
116 *
117 */
118 $.fn.ajaxAddOption = function(url, params, select)
119 {
120 if(typeof url != "string") return this;
121 if(typeof params != "object") params = {};
122 if(typeof select != "boolean") select = true;
123 this.each(
124 function()
125 {
126 var self = this;
127 $.getJSON(url,
128 params,
129 function(r)
130 {
131 $(self).addOption(r, select);
132 }
133 );
134 }
135 )
136 return this;
137 }
138
139 /**
140 * Removes an option (by value or index) from a select box (or series of select boxes)
141 *
142 * @name removeOption
143 * @author Sam Collett (http://www.texotela.co.uk)
144 * @type jQuery
145 * @param String|RegExp|Number Option to remove
146 * @example $("#myselect").removeOption("Value"); // remove by value
147 * @example $("#myselect").removeOption(/^val/i); // remove options with a value starting with 'val'
148 * @example $("#myselect").removeOption(/./); // remove all options
149 * @example $("#myselect").removeOption(0); // remove by index
150 *
151 */
152 $.fn.removeOption = function()
153 {
154 var a = arguments;
155 if(a.length == 0) return this;
156 var ta = typeof a[0];
157 if(ta == "string") var v = a[0];
158 else if(ta == "object" || ta == "function") var v = a[0]; /* regular expression */
159 else if(ta == "number") var i = a[0];
160 else return this;
161 this.each(
162 function()
163 {
164 if(this.nodeName.toLowerCase() != "select") return;
165 if(v)
166 {
167 // get options
168 var o = this.options;
169 // get number of options
170 var oL = o.length;
171 for(var i=oL-1; i>=0; i--)
172 {
173 if(v.constructor == RegExp)
174 {
175 if (o[i].value.match(v))
176 {
177 o[i] = null;
178 }
179 }
180 else if(o[i].value == v)
181 {
182 o[i] = null;
183 }
184 }
185 }
186 else
187 {
188 this.remove(i);
189 }
190 }
191 )
192 return this;
193 }
194
195 /**
196 * Sort options (ascending or descending) in a select box (or series of select boxes)
197 *
198 * @name sortOptions
199 * @author Sam Collett (http://www.texotela.co.uk)
200 * @type jQuery
201 * @param Boolean ascending (optional) Sort ascending (true/undefined), or descending (false)
202 * @example // ascending
203 * $("#myselect").sortOptions(); // or $("#myselect").sortOptions(true);
204 * @example // descending
205 * $("#myselect").sortOptions(false);
206 *
207 */
208 $.fn.sortOptions = function(ascending)
209 {
210 var a = typeof ascending == "undefined" ? true : ascending;
211 this.each(
212 function()
213 {
214 if(this.nodeName.toLowerCase() != "select") return;
215
216 // get options
217 var o = this.options;
218 // get number of options
219 var oL = o.length;
220 // create an array for sorting
221 var sA = [];
222 // loop through options, adding to sort array
223 for(var i = 0; i<oL; i++)
224 {
225 sA[i] =
226 {
227 v: o[i].value,
228 t: o[i].text
229 };
230 }
231 // sort items in array
232 sA.sort(
233 function(o1, o2)
234 {
235 // option text is made lowercase for case insensitive sorting
236 o1t = o1.t.toLowerCase();
237 o2t = o2.t.toLowerCase();
238 // if options are the same, no sorting is needed
239 if(o1t == o2t) return 0;
240 if(a)
241 {
242 return o1t < o2t ? -1 : 1;
243 }
244 else
245 {
246 return o1t > o2t ? -1 : 1;
247 }
248 }
249 );
250 // change the options to match the sort array
251 for(var i = 0; i<oL; i++)
252 {
253 o[i].text = sA[i].t;
254 o[i].value = sA[i].v;
255 }
256 }
257 )
258 return this;
259 }
260 /**
261 * Selects an option by value
262 *
263 * @name selectOptions
264 * @author Mathias Bank (http://www.mathias-bank.de), original function
265 * @author Sam Collett (http://www.texotela.co.uk), addition of regular expression matching
266 * @type jQuery
267 * @param String|RegExp value which options should be selected
268 * can be a string or regular expression
269 * @param Boolean clear clear existing selected options, default false
270 * @example $("#myselect").selectOptions("val1"); // with the value 'val1'
271 * @example $("#myselect").selectOptions(/^val/i); // with the value starting with 'val', case insensitive
272 *
273 */
274 $.fn.selectOptions = function(value, clear)
275 {
276 var v = value;
277 var vT = typeof value;
278 var c = clear || false;
279 // has to be a string or regular expression (object in IE, function in Firefox)
280 if(vT != "string" && vT != "function" && vT != "object") return this;
281 this.each(
282 function()
283 {
284 if(this.nodeName.toLowerCase() != "select") return this;
285
286 // get options
287 var o = this.options;
288 // get number of options
289 var oL = o.length;
290
291 for(var i = 0; i<oL; i++)
292 {
293 if(v.constructor == RegExp)
294 {
295 if (o[i].value.match(v))
296 {
297 o[i].selected = true;
298 }
299 else if(c)
300 {
301 o[i].selected = false;
302 }
303 }
304 else
305 {
306 if (o[i].value == v)
307 {
308 o[i].selected = true;
309 }
310 else if(c)
311 {
312 o[i].selected = false;
313 }
314 }
315 }
316 }
317 )
318 return this;
319 }
320
321 /**
322 * Copy options to another select
323 *
324 * @name copyOptions
325 * @author Sam Collett (http://www.texotela.co.uk)
326 * @type jQuery
327 * @param String to Element to copy to
328 * @param String which (optional) Specifies which options should be copied - 'all' or 'selected'. Default is 'selected'
329 * @example $("#myselect").copyOptions("#myselect2"); // copy selected options from 'myselect' to 'myselect2'
330 * @example $("#myselect").copyOptions("#myselect2","selected"); // same as above
331 * @example $("#myselect").copyOptions("#myselect2","all"); // copy all options from 'myselect' to 'myselect2'
332 *
333 */
334 $.fn.copyOptions = function(to, which)
335 {
336 var w = which || "selected";
337 if($(to).size() == 0) return this;
338 this.each(
339 function()
340 {
341 if(this.nodeName.toLowerCase() != "select") return this;
342
343 // get options
344 var o = this.options;
345 // get number of options
346 var oL = o.length;
347
348 for(var i = 0; i<oL; i++)
349 {
350 if(w == "all" ||
351 (w == "selected" && o[i].selected)
352 )
353 {
354 $(to).addOption(o[i].value, o[i].text);
355 }
356 }
357 }
358 )
359 return this;
360 }
361
362 })(jQuery);

  ViewVC Help
Powered by ViewVC 1.1.2