Subversion Repositories wimsdev

Rev

Rev 12784 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1.  
  2. /**
  3.  * chooselist, inspired by qfamsHAndler HTML_QuickForm_advmultiselect
  4.  
  5.  * JavaScript functions to handle a multiselect element (Move elements between 2 select boxes)
  6.  * @author     Laurent Laville <pear@laurent-laville.org>
  7.  * @copyright  2007-2009 Laurent Laville
  8.  * @license    http://www.opensource.org/licenses/bsd-license.php  New BSD License
  9.  * @website    https://github.com/pear/HTML_QuickForm_advmultiselect
  10.  */
  11.  
  12.  
  13. /**
  14.  * QFAMS.moveSelection
  15.  * in double select box mode, to move current selection and update live counter
  16.  *
  17.  * @param      dom element   selectLeft     Data source list
  18.  * @param      dom element   selectRight    Target data list
  19.  * @param      dom element   selectHidden   Full data source (selected, unselected)
  20.  *                                          private usage
  21.  * @param      string        action         Action name (add, remove, all, none, toggle)
  22.  */
  23. function moveSelections(selectLeft, selectRight, selectHidden, action) {
  24.   var isIE = /*@cc_on!@*/false; //IE detector
  25.   var source = null;
  26.   var target = null;
  27.   var option;
  28.   var c      = null;
  29.   var s      = null;
  30.   var i;
  31.   var maxFrom, maxTo;
  32.  
  33.   if (action === 'add' || action === 'all' || action === 'toggle') {
  34.     source = selectLeft;
  35.     target = selectRight;
  36.   } else {
  37.     source = selectRight;
  38.     target = selectLeft;
  39.   }
  40.   // Don't do anything if nothing selected. Otherwise we throw javascript errors.
  41.   if (source.selectedIndex === -1 && (action === 'add' || action === 'remove')) {
  42.     return;
  43.   }
  44.   maxFrom = source.options.length;
  45.   maxTo   = target.options.length;
  46.  
  47.   // check if target list is empty and remove fake empty option (tip to be XHTML compliant)
  48.   if (maxTo > 0 && target.options[0].value === "") {
  49.     target.removeAttribute("disabled");
  50.     target.options[0] = null;
  51.   }
  52.  
  53.   // Add items to the 'TO' list.
  54.   for (i = (maxFrom - 1); i >= 0; i--) {
  55.     if (action === 'all' || action === 'none' || action === 'toggle' || source.options[i].selected === true) {
  56.       if (source.options[i].disabled === false) {
  57.         if (isIE) {
  58.           option = source.options[i].removeNode(true);
  59.           //option.selected = env.persistantSelection;
  60.           target.appendChild(option);
  61.         } else {
  62.           option = source.options[i].cloneNode(true);
  63.           //option.selected = env.persistantSelection;
  64.           target.options[target.options.length] = option;
  65.         }
  66.       }
  67.     }
  68.   }
  69.  
  70.   // Remove items from the 'FROM' list.
  71.   if (!isIE) {
  72.     for (i = (maxFrom - 1); i >= 0; i--) {
  73.       if (action === 'all' || action === 'none' || action === 'toggle' || source.options[i].selected === true) {
  74.         if (source.options[i].disabled === false) {
  75.           source.options[i] = null;
  76.         }
  77.       }
  78.     }
  79.   }
  80.  
  81.   // Add items to the 'FROM' list for toggle function
  82.   if (action === 'toggle') {
  83.     for (i = (maxTo - 1); i >= 0; i--) {
  84.       if (target.options[i].disabled === false) {
  85.         if (isIE) {
  86.           option = target.options[i].removeNode(true);
  87.           //option.selected = env.persistantSelection;
  88.           source.appendChild(option);
  89.         } else {
  90.           option = target.options[i].cloneNode(true);
  91.           //option.selected = env.persistantSelection;
  92.           source.options[source.options.length] = option;
  93.         }
  94.       }
  95.     }
  96.     if (!isIE) {
  97.       for (i = (maxTo - 1); i >= 0; i--) {
  98.         if (target.options[i].disabled === false) {
  99.           target.options[i] = null;
  100.         }
  101.       }
  102.     }
  103.   }
  104.  
  105.   // Set the appropriate items as 'selected in the hidden select.
  106.   // These are the values that will actually be posted with the form.
  107.   updateHidden(selectHidden, selectRight);
  108. }
  109.  
  110. /*
  111.  * QFAMS.updateHidden
  112.  * updates the private list that handle selection of all elements (selected and unselected)
  113.  * @param      dom element   h              hidden list (contains all elements)
  114.  * @param      dom element   r              selection list (contains only elements selected)
  115.  */
  116. function updateHidden(h, r) {
  117.   var i;
  118.  
  119.   for(i = h.options.length - 1 ; i >= 0 ; i--)
  120.   {
  121.     //h.options[i].selected = false;
  122.     h.remove(i);
  123.   }
  124.  
  125.   for (i = 0; i < r.length; i++) {
  126.     h.options[h.length] = new Option(r.options[i].text, r.options[i].value);
  127.     h.options[h.length - 1].selected = true;
  128.   }
  129. }
  130.  
  131. /**
  132.  * QFAMS.moveUp
  133.  * end-user may arrange and element up to the selection list
  134.  *
  135.  * @param      dom element   l              selection list (contains only elements selected)
  136.  * @param      dom element   h              hidden list (contains all elements)
  137.  *
  138.  */
  139. function moveUp(l, h) {
  140.   var indice = l.selectedIndex;
  141.   if (indice < 0) {
  142.     return;
  143.   }
  144.   if (indice > 0) {
  145.     moveSwap(l, indice, indice - 1);
  146.     updateHidden(h, l);
  147.   }
  148. }
  149.  
  150. /**
  151.  * QFAMS.moveDown
  152.  * end-user may arrange and element down to the selection list
  153.  *
  154.  * @param      dom element   l              selection list (contains only elements selected)
  155.  * @param      dom element   h              hidden list (contains all elements)
  156.  *
  157.  */
  158. function moveDown(l, h) {
  159.   var indice = l.selectedIndex;
  160.   if (indice < 0) {
  161.     return;
  162.   }
  163.   if (indice < l.options.length - 1) {
  164.     moveSwap(l, indice, indice + 1);
  165.     updateHidden(h, l);
  166.   }
  167. }
  168.  
  169. /**
  170.  * QFAMS.moveSwap
  171.  * end-user may invert two elements position in the selection list
  172.  *
  173.  * @param      dom element   l              selection list (contains only elements selected)
  174.  * @param      integer       i              element source indice
  175.  * @param      integer       j              element target indice
  176.  *
  177.  */
  178. function moveSwap(l,i,j) {
  179.   var valeur = l.options[i].value;
  180.   var texte = l.options[i].text;
  181.   l.options[i].value = l.options[j].value;
  182.   l.options[i].text = l.options[j].text;
  183.   l.options[j].value = valeur;
  184.   l.options[j].text = texte;
  185.   l.selectedIndex = j
  186. }
  187.  
  188.  
  189. /**
  190.  * filterSelectExoSheet
  191.  * Display only exo corresponding to specific sheet Id in a select box
  192.  * The Sheet Id is the value selected by selectFilter (it can be a list like 1,2,3)
  193.  *
  194.  * @param      dom element   selectbox            selection list
  195.  * @param      dom element   selectFilter         WIMS Sheet Id
  196.  *
  197.  */
  198. function filterSelectExoSheet(selectTarget, selectFilter) {
  199.   var i,current;
  200.   // Converts selectFilter to an array
  201.   var sheetIds = selectFilter.value.split(',');
  202.  
  203.   for(i = selectTarget.options.length - 1 ; i >= 0 ; i--){
  204.     current = selectTarget.options[i]
  205.     if (sheetIds.indexOf(current.dataset.sheetid) >= 0){
  206.       selectTarget.options[i].style.display = "block";
  207.     }
  208.     else{
  209.       selectTarget.options[i].style.display = "none";
  210.       selectTarget.options[i].selected = false;
  211.     }
  212.   }
  213. }
  214.  
  215.  
  216.