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 / OptionTransfer.js < prev    next >
Encoding:
JavaScript  |  2004-07-12  |  8.0 KB  |  188 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. /* 
  22. OptionTransfer.js
  23. Last Modified: 11/18/2002
  24.  
  25. DESCRIPTION: This widget is used to easily and quickly create an interface
  26. where the user can transfer choices from one select box to another. For
  27. example, when selecting which columns to show or hide in search results.
  28. This object adds value by automatically storing the values that were added
  29. or removed from each list, as well as the state of the final list. 
  30.  
  31. COMPATIBILITY: Should work on all Javascript-compliant browsers.
  32.  
  33. USAGE:
  34. // Create a new OptionTransfer object. Pass it the field names of the left
  35. // select box and the right select box.
  36. var ot = new OptionTransfer("from","to");
  37.  
  38. // Optionally tell the lists whether or not to auto-sort when options are 
  39. // moved. By default, the lists will be sorted.
  40. ot.setAutoSort(true);
  41.  
  42. // Optionally set the delimiter to be used to separate values that are
  43. // stored in hidden fields for the added and removed options, as well as
  44. // final state of the lists. Defaults to a comma.
  45. ot.setDelimiter("|");
  46.  
  47. // These functions assign the form fields which will store the state of
  48. // the lists. Each one is optional, so you can pick to only store the
  49. // new options which were transferred to the right list, for example.
  50. // Each function takes the name of a HIDDEN or TEXT input field.
  51.  
  52. // Store list of options removed from left list into an input field
  53. ot.saveRemovedLeftOptions("removedLeft");
  54. // Store list of options removed from right list into an input field
  55. ot.saveRemovedRightOptions("removedRight");
  56. // Store list of options added to left list into an input field
  57. ot.saveAddedLeftOptions("addedLeft");
  58. // Store list of options radded to right list into an input field
  59. ot.saveAddedRightOptions("addedRight");
  60. // Store all options existing in the left list into an input field
  61. ot.saveNewLeftOptions("newLeft");
  62. // Store all options existing in the right list into an input field
  63. ot.saveNewRightOptions("newRight");
  64.  
  65. // IMPORTANT: This step is required for the OptionTransfer object to work
  66. // correctly.
  67. // Add a call to the BODY onLoad="" tag of the page, and pass a reference to
  68. // the form which contains the select boxes and input fields.
  69. BODY onLoad="ot.init(document.forms[0])"
  70.  
  71. // ADDING ACTIONS INTO YOUR PAGE
  72. // Finally, add calls to the object to move options back and forth, either
  73. // from links in your page or from double-clicking the options themselves.
  74. // See example page, and use the following methods:
  75. ot.transferRight();
  76. ot.transferAllRight();
  77. ot.transferLeft();
  78. ot.transferAllLeft();
  79.  
  80.  
  81. NOTES:
  82. 1) Requires the functions in selectbox.js
  83.  
  84. */ 
  85. function OT_transferLeft() { moveSelectedOptions(this.right,this.left,this.autoSort); this.update(); }
  86. function OT_transferRight() { moveSelectedOptions(this.left,this.right,this.autoSort); this.update(); }
  87. function OT_transferAllLeft() { moveAllOptions(this.right,this.left,this.autoSort); this.update(); }
  88. function OT_transferAllRight() { moveAllOptions(this.left,this.right,this.autoSort); this.update(); }
  89. function OT_saveRemovedLeftOptions(f) { this.removedLeftField = f; }
  90. function OT_saveRemovedRightOptions(f) { this.removedRightField = f; }
  91. function OT_saveAddedLeftOptions(f) { this.addedLeftField = f; }
  92. function OT_saveAddedRightOptions(f) { this.addedRightField = f; }
  93. function OT_saveNewLeftOptions(f) { this.newLeftField = f; }
  94. function OT_saveNewRightOptions(f) { this.newRightField = f; }
  95. function OT_update() {
  96.     var removedLeft = new Object();
  97.     var removedRight = new Object();
  98.     var addedLeft = new Object();
  99.     var addedRight = new Object();
  100.     var newLeft = new Object();
  101.     var newRight = new Object();
  102.     for (var i=0;i<this.left.options.length;i++) {
  103.         var o=this.left.options[i];
  104.         newLeft[o.value]=1;
  105.         if (typeof(this.originalLeftValues[o.value])=="undefined") {
  106.             addedLeft[o.value]=1;
  107.             removedRight[o.value]=1;
  108.             }
  109.         }
  110.     for (var i=0;i<this.right.options.length;i++) {
  111.         var o=this.right.options[i];
  112.         newRight[o.value]=1;
  113.         if (typeof(this.originalRightValues[o.value])=="undefined") {
  114.             addedRight[o.value]=1;
  115.             removedLeft[o.value]=1;
  116.             }
  117.         }
  118.     if (this.removedLeftField!=null) { this.removedLeftField.value = OT_join(removedLeft,this.delimiter); }
  119.     if (this.removedRightField!=null) { this.removedRightField.value = OT_join(removedRight,this.delimiter); }
  120.     if (this.addedLeftField!=null) { this.addedLeftField.value = OT_join(addedLeft,this.delimiter); }
  121.     if (this.addedRightField!=null) { this.addedRightField.value = OT_join(addedRight,this.delimiter); }
  122.     if (this.newLeftField!=null) { this.newLeftField.value = OT_join(newLeft,this.delimiter); }
  123.     if (this.newRightField!=null) { this.newRightField.value = OT_join(newRight,this.delimiter); }
  124.     }
  125. function OT_join(o,delimiter) {
  126.     var val; var str="";
  127.     for(val in o){
  128.         if (str.length>0) { str=str+delimiter; }
  129.         str=str+val;
  130.         }
  131.     return str;
  132.     }
  133. function OT_setDelimiter(val) { this.delimiter=val; }
  134. function OT_setAutoSort(val) { this.autoSort=val; }
  135. function OT_init(theform) {
  136.     this.form = theform;
  137.     if(!theform[this.left]){alert("OptionTransfer init(): Left select list does not exist in form!");return false;}
  138.     if(!theform[this.right]){alert("OptionTransfer init(): Right select list does not exist in form!");return false;}
  139.     this.left=theform[this.left];
  140.     this.right=theform[this.right];
  141.     for(var i=0;i<this.left.options.length;i++) {
  142.         this.originalLeftValues[this.left.options[i].value]=1;
  143.         }
  144.     for(var i=0;i<this.right.options.length;i++) {
  145.         this.originalRightValues[this.right.options[i].value]=1;
  146.         }
  147.     if(this.removedLeftField!=null) { this.removedLeftField=theform[this.removedLeftField]; }
  148.     if(this.removedRightField!=null) { this.removedRightField=theform[this.removedRightField]; }
  149.     if(this.addedLeftField!=null) { this.addedLeftField=theform[this.addedLeftField]; }
  150.     if(this.addedRightField!=null) { this.addedRightField=theform[this.addedRightField]; }
  151.     if(this.newLeftField!=null) { this.newLeftField=theform[this.newLeftField]; }
  152.     if(this.newRightField!=null) { this.newRightField=theform[this.newRightField]; }
  153.     this.update();
  154.     }
  155. // -------------------------------------------------------------------
  156. // OptionTransfer()
  157. //  This is the object interface.
  158. // -------------------------------------------------------------------
  159. function OptionTransfer(l,r) {
  160.     this.form = null;
  161.     this.left=l;
  162.     this.right=r;
  163.     this.autoSort=true;
  164.     this.delimiter=",";
  165.     this.originalLeftValues = new Object();
  166.     this.originalRightValues = new Object();
  167.     this.removedLeftField = null;
  168.     this.removedRightField = null;
  169.     this.addedLeftField = null;
  170.     this.addedRightField = null;
  171.     this.newLeftField = null;
  172.     this.newRightField = null;
  173.     this.transferLeft=OT_transferLeft;
  174.     this.transferRight=OT_transferRight;
  175.     this.transferAllLeft=OT_transferAllLeft;
  176.     this.transferAllRight=OT_transferAllRight;
  177.     this.saveRemovedLeftOptions=OT_saveRemovedLeftOptions;
  178.     this.saveRemovedRightOptions=OT_saveRemovedRightOptions;
  179.     this.saveAddedLeftOptions=OT_saveAddedLeftOptions;
  180.     this.saveAddedRightOptions=OT_saveAddedRightOptions;
  181.     this.saveNewLeftOptions=OT_saveNewLeftOptions;
  182.     this.saveNewRightOptions=OT_saveNewRightOptions;
  183.     this.setDelimiter=OT_setDelimiter;
  184.     this.setAutoSort=OT_setAutoSort;
  185.     this.init=OT_init;
  186.     this.update=OT_update;
  187.     }
  188.