home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / selectbox.js < prev    next >
Encoding:
JavaScript  |  2004-07-12  |  11.5 KB  |  316 lines

  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. //                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. COMPATIBILITY: These are fairly basic functions - they should work on
  31. all browsers that support Javascript.
  32. */
  33.  
  34. // -------------------------------------------------------------------
  35. // selectUnselectMatchingOptions(select_object,regex,select/unselect,true/false)
  36. //  This is a general function used by the select functions below, to
  37. //  avoid code duplication
  38. // -------------------------------------------------------------------
  39. function selectUnselectMatchingOptions(obj,regex,which,only) {
  40.     if (window.RegExp) {
  41.         if (which == "select") {
  42.             var selected1=true;
  43.             var selected2=false;
  44.             }
  45.         else if (which == "unselect") {
  46.             var selected1=false;
  47.             var selected2=true;
  48.             }
  49.         else {
  50.             return;
  51.             }
  52.         var re = new RegExp(regex);
  53.         for (var i=0; i<obj.options.length; i++) {
  54.             if (re.test(obj.options[i].text)) {
  55.                 obj.options[i].selected = selected1;
  56.                 }
  57.             else {
  58.                 if (only == true) {
  59.                     obj.options[i].selected = selected2;
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.     }
  65.         
  66. // -------------------------------------------------------------------
  67. // selectMatchingOptions(select_object,regex)
  68. //  This function selects all options that match the regular expression
  69. //  passed in. Currently-selected options will not be changed.
  70. // -------------------------------------------------------------------
  71. function selectMatchingOptions(obj,regex) {
  72.     selectUnselectMatchingOptions(obj,regex,"select",false);
  73.     }
  74. // -------------------------------------------------------------------
  75. // selectOnlyMatchingOptions(select_object,regex)
  76. //  This function selects all options that match the regular expression
  77. //  passed in. Selected options that don't match will be un-selected.
  78. // -------------------------------------------------------------------
  79. function selectOnlyMatchingOptions(obj,regex) {
  80.     selectUnselectMatchingOptions(obj,regex,"select",true);
  81.     }
  82. // -------------------------------------------------------------------
  83. // unSelectMatchingOptions(select_object,regex)
  84. //  This function Unselects all options that match the regular expression
  85. //  passed in. 
  86. // -------------------------------------------------------------------
  87. function unSelectMatchingOptions(obj,regex) {
  88.     selectUnselectMatchingOptions(obj,regex,"unselect",false);
  89.     }
  90.     
  91. // -------------------------------------------------------------------
  92. // sortSelect(select_object)
  93. //   Pass this function a SELECT object and the options will be sorted
  94. //   by their text (display) values
  95. // -------------------------------------------------------------------
  96. function sortSelect(obj) {
  97.     var o = new Array();
  98.     if (obj.options==null) { return; }
  99.     for (var i=0; i<obj.options.length; i++) {
  100.         o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
  101.         }
  102.     if (o.length==0) { return; }
  103.     o = o.sort( 
  104.         function(a,b) { 
  105.             if ((a.text+"") < (b.text+"")) { return -1; }
  106.             if ((a.text+"") > (b.text+"")) { return 1; }
  107.             return 0;
  108.             } 
  109.         );
  110.  
  111.     for (var i=0; i<o.length; i++) {
  112.         obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
  113.         }
  114.     }
  115.  
  116. // -------------------------------------------------------------------
  117. // selectAllOptions(select_object)
  118. //  This function takes a select box and selects all options (in a 
  119. //  multiple select object). This is used when passing values between
  120. //  two select boxes. Select all options in the right box before 
  121. //  submitting the form so the values will be sent to the server.
  122. // -------------------------------------------------------------------
  123. function selectAllOptions(obj) {
  124.     for (var i=0; i<obj.options.length; i++) {
  125.         obj.options[i].selected = true;
  126.         }
  127.     }
  128.     
  129. // -------------------------------------------------------------------
  130. // moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
  131. //  This function moves options between select boxes. Works best with
  132. //  multi-select boxes to create the common Windows control effect.
  133. //  Passes all selected values from the first object to the second
  134. //  object and re-sorts each box.
  135. //  If a third argument of 'false' is passed, then the lists are not
  136. //  sorted after the move.
  137. //  If a fourth string argument is passed, this will function as a
  138. //  Regular Expression to match against the TEXT or the options. If 
  139. //  the text of an option matches the pattern, it will NOT be moved.
  140. //  It will be treated as an unmoveable option.
  141. //  You can also put this into the <SELECT> object as follows:
  142. //    onDblClick="moveSelectedOptions(this,this.form.target)
  143. //  This way, when the user double-clicks on a value in one box, it
  144. //  will be transferred to the other (in browsers that support the 
  145. //  onDblClick() event handler).
  146. // -------------------------------------------------------------------
  147. function moveSelectedOptions(from,to) {
  148.     // Unselect matching options, if required
  149.     if (arguments.length>3) {
  150.         var regex = arguments[3];
  151.         if (regex != "") {
  152.             unSelectMatchingOptions(from,regex);
  153.             }
  154.         }
  155.     // Move them over
  156.     for (var i=0; i<from.options.length; i++) {
  157.         var o = from.options[i];
  158.         if (o.selected) {
  159.             to.options[to.options.length] = new Option( o.text, o.value, false, false);
  160.             }
  161.         }
  162.     // Delete them from original
  163.     for (var i=(from.options.length-1); i>=0; i--) {
  164.         var o = from.options[i];
  165.         if (o.selected) {
  166.             from.options[i] = null;
  167.             }
  168.         }
  169.     if ((arguments.length<3) || (arguments[2]==true)) {
  170.         sortSelect(from);
  171.         sortSelect(to);
  172.         }
  173.     from.selectedIndex = -1;
  174.     to.selectedIndex = -1;
  175.     }
  176.  
  177. // -------------------------------------------------------------------
  178. // copySelectedOptions(select_object,select_object[,autosort(true/false)])
  179. //  This function copies options between select boxes instead of 
  180. //  moving items. Duplicates in the target list are not allowed.
  181. // -------------------------------------------------------------------
  182. function copySelectedOptions(from,to) {
  183.     var options = new Object();
  184.     for (var i=0; i<to.options.length; i++) {
  185.         options[to.options[i].value] = to.options[i].text;
  186.         }
  187.     for (var i=0; i<from.options.length; i++) {
  188.         var o = from.options[i];
  189.         if (o.selected) {
  190.             if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text) {
  191.                 to.options[to.options.length] = new Option( o.text, o.value, false, false);
  192.                 }
  193.             }
  194.         }
  195.     if ((arguments.length<3) || (arguments[2]==true)) {
  196.         sortSelect(to);
  197.         }
  198.     from.selectedIndex = -1;
  199.     to.selectedIndex = -1;
  200.     }
  201.  
  202. // -------------------------------------------------------------------
  203. // moveAllOptions(select_object,select_object[,autosort(true/false)[,regex]])
  204. //  Move all options from one select box to another.
  205. // -------------------------------------------------------------------
  206. function moveAllOptions(from,to) {
  207.     selectAllOptions(from);
  208.     if (arguments.length==2) {
  209.         moveSelectedOptions(from,to);
  210.         }
  211.     else if (arguments.length==3) {
  212.         moveSelectedOptions(from,to,arguments[2]);
  213.         }
  214.     else if (arguments.length==4) {
  215.         moveSelectedOptions(from,to,arguments[2],arguments[3]);
  216.         }
  217.     }
  218.  
  219. // -------------------------------------------------------------------
  220. // copyAllOptions(select_object,select_object[,autosort(true/false)])
  221. //  Copy all options from one select box to another, instead of
  222. //  removing items. Duplicates in the target list are not allowed.
  223. // -------------------------------------------------------------------
  224. function copyAllOptions(from,to) {
  225.     selectAllOptions(from);
  226.     if (arguments.length==2) {
  227.         copySelectedOptions(from,to);
  228.         }
  229.     else if (arguments.length==3) {
  230.         copySelectedOptions(from,to,arguments[2]);
  231.         }
  232.     }
  233.  
  234. // -------------------------------------------------------------------
  235. // swapOptions(select_object,option1,option2)
  236. //  Swap positions of two options in a select list
  237. // -------------------------------------------------------------------
  238. function swapOptions(obj,i,j) {
  239.     var o = obj.options;
  240.     var i_selected = o[i].selected;
  241.     var j_selected = o[j].selected;
  242.     var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
  243.     var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
  244.     o[i] = temp2;
  245.     o[j] = temp;
  246.     o[i].selected = j_selected;
  247.     o[j].selected = i_selected;
  248.     }
  249.     
  250. // -------------------------------------------------------------------
  251. // moveOptionUp(select_object)
  252. //  Move selected option in a select list up one
  253. // -------------------------------------------------------------------
  254. function moveOptionUp(obj) {
  255.     for (i=0; i<obj.options.length; i++) {
  256.         if (obj.options[i].selected) {
  257.             if (i != 0 && !obj.options[i-1].selected) {
  258.                 swapOptions(obj,i,i-1);
  259.                 obj.options[i-1].selected = true;
  260.                 }
  261.             }
  262.         }
  263.     }
  264.  
  265. // -------------------------------------------------------------------
  266. // moveOptionDown(select_object)
  267. //  Move selected option in a select list down one
  268. // -------------------------------------------------------------------
  269. function moveOptionDown(obj) {
  270.     for (i=obj.options.length-1; i>=0; i--) {
  271.         if (obj.options[i].selected) {
  272.             if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
  273.                 swapOptions(obj,i,i+1);
  274.                 obj.options[i+1].selected = true;
  275.                 }
  276.             }
  277.         }
  278.     }
  279.  
  280. // -------------------------------------------------------------------
  281. // removeSelectedOptions(select_object)
  282. //  Remove all selected options from a list
  283. //  (Thanks to Gene Ninestein)
  284. // -------------------------------------------------------------------
  285. function removeSelectedOptions(from) { 
  286.     for (var i=(from.options.length-1); i>=0; i--) { 
  287.         var o=from.options[i]; 
  288.         if (o.selected) { 
  289.             from.options[i] = null; 
  290.             } 
  291.         } 
  292.     from.selectedIndex = -1; 
  293.     } 
  294.  
  295. // -------------------------------------------------------------------
  296. // removeAllOptions(select_object)
  297. //  Remove all options from a list
  298. // -------------------------------------------------------------------
  299. function removeAllOptions(from) { 
  300.     for (var i=(from.options.length-1); i>=0; i--) { 
  301.         from.options[i] = null; 
  302.         } 
  303.     from.selectedIndex = -1; 
  304.     } 
  305.  
  306. // -------------------------------------------------------------------
  307. // addOption(select_object,display_text,value,selected)
  308. //  Add an option to a list
  309. // -------------------------------------------------------------------
  310. function addOption(obj,text,value,selected) {
  311.     if (obj!=null && obj.options!=null) {
  312.         obj.options[obj.options.length] = new Option(text, value, false, selected);
  313.         }
  314.     }
  315.  
  316.