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 / forms-lib.js < prev    next >
Encoding:
JavaScript  |  2004-07-12  |  5.9 KB  |  186 lines

  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *     http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17.  * Runtime JavaScript library for Cocoon forms.
  18.  *
  19.  * @author <a href="http://www.apache.org/~sylvain/">Sylvain Wallez</a>
  20.  * @version CVS $Id: forms-lib.js,v 1.4 2004/05/11 22:50:25 joerg Exp $
  21.  */
  22.  
  23. // Handlers that are to be called in the document's "onload" event
  24. var forms_onloadHandlers = new Array();
  25.  
  26. function forms_onload() {
  27.     for (var i = 0; i < forms_onloadHandlers.length; i++) {
  28.         forms_onloadHandlers[i].forms_onload();
  29.     }
  30.     // Clear it (we no more need them)
  31.     forms_onloadHandlers = null;
  32. }
  33.  
  34. // Handlers that are to be called in form's "onsubmit" event
  35. //FIXME: this single var implies only one form per page, and needs to be
  36. //       visited if we decide to support several forms per page.
  37. var forms_onsubmitHandlers = new Array();
  38.  
  39. function forms_onsubmit() {
  40.     if (forms_onsubmitHandlers == null) {
  41.         alert("onsubmit called twice!");
  42.     }
  43.  
  44.     for (var i = 0; i < forms_onsubmitHandlers.length; i++) {
  45.         forms_onsubmitHandlers[i].forms_onsubmit();
  46.     }
  47.     // clear it
  48.     forms_onsubmitHandlers = null;
  49. }
  50.  
  51. /**
  52.  * Submit the form containing an element, also storing in the hidden
  53.  * 'forms_submit_id' field the name of the element which triggered the submit.
  54.  */
  55. function forms_submitForm(element, name) {
  56.     if (name == undefined) {
  57.         name = element.name;
  58.     }
  59.     
  60.     var form = forms_getForm(element);
  61.     if (form == null) {
  62.         alert("Cannot find form for " + element);
  63.     } else {
  64.         form["forms_submit_id"].value = name;
  65.         // FIXME: programmatically submitting the form doesn't trigger onsubmit ? (both in IE and Moz)
  66.         forms_onsubmit();
  67.         form.submit();
  68.     }
  69. }
  70.  
  71. /**
  72.  * Crawl the parents of an element up to finding a form.
  73.  */
  74. function forms_getForm(element) {
  75.     while(element != null && element.tagName != "FORM") {
  76.         element = element.parentNode;
  77.     }
  78.     return element;
  79. }
  80.  
  81. /**
  82.  * Move a named element as an immediate child of the <body> element.
  83.  * This is required for help popups inside <wi:group> tabs. The reason is that CSS positioning
  84.  * properties ("left" and "top") on a block with a "position: absolute" are actually relative to
  85.  * the nearest ancestor that has a position of "absolute", "relative" or "fixed".
  86.  * See http://www.w3.org/TR/CSS21/visudet.html#containing-block-details $4
  87.  */
  88.  
  89. function forms_moveInBody(element) {
  90.     element.parentNode.removeChild(element);
  91.     document.body.appendChild(element);
  92. }
  93.  
  94. /**
  95.  * Create a popup window for a named element.
  96.  *
  97.  * @param id the ID of the element to make a popup with.
  98.  */
  99. function forms_createPopupWindow(id) {
  100.     var result = new PopupWindow(id);
  101.     result.autoHide();
  102.     // add to onload handlers
  103.     result.forms_id = id;
  104.     result.forms_onload = function() {
  105.         forms_moveInBody(document.getElementById(this.forms_id));
  106.     }
  107.     forms_onloadHandlers.push(result);
  108.     return result;
  109. }
  110.  
  111.  
  112. function forms_createOptionTransfer(id, submitOnChange) {
  113.     var result = new OptionTransfer(id + ".unselected", id);
  114.     result.setAutoSort(true);
  115.     // add to onload handlers
  116.     result.forms_id = id;
  117.     result.forms_onload = function() {
  118.         var form = forms_getForm(document.getElementById(this.forms_id));
  119.         this.init(form);
  120.         sortSelect(this.left);
  121.         sortSelect(this.right);
  122.     }
  123.     result.submitOnChange = submitOnChange;
  124.     result.forms_transferLeft = function() {
  125.         this.transferLeft();
  126.         if (this.submitOnChange) {
  127.             forms_submitForm(document.getElementById(this.forms_id));
  128.         }
  129.     }
  130.     result.forms_transferRight = function() {
  131.         this.transferRight();
  132.         if (this.submitOnChange) {
  133.             forms_submitForm(document.getElementById(this.forms_id));
  134.         }
  135.     }
  136.     result.forms_transferAllLeft = function() {
  137.         this.transferAllLeft();
  138.         if (this.submitOnChange) {
  139.             forms_submitForm(document.getElementById(this.forms_id));
  140.         }
  141.     };
  142.     result.forms_transferAllRight = function() {
  143.         this.transferAllRight();
  144.         if (this.submitOnChange) {
  145.             forms_submitForm(document.getElementById(this.forms_id));
  146.         }
  147.     };
  148.     forms_onloadHandlers.push(result);
  149.     
  150.     // add to onsubmit handlers
  151.     result.forms_onsubmit = function() {
  152.         // Select all options in the "selected" list to that
  153.         // its values are sent.
  154.         selectAllOptions(this.right);
  155.     }
  156.     forms_onsubmitHandlers.push(result);
  157.     return result;
  158. }
  159.  
  160.  
  161. /**
  162.  * Show a tab in a <wi:group>
  163.  *
  164.  * @param tabgroup (string) name of the <wi:group>
  165.  * @param idx (integer) index of the selected tab
  166.  * @param length (integer) total number of tabs
  167.  * @param state (string, optional) name of the input storing the tabgroup state
  168.  */
  169. function forms_showTab(tabgroup, idx, length, state) {
  170.     for (var i = 0; i < length; i++) {
  171.         // Change tab status (selected/unselected)
  172.         var tab = document.getElementById(tabgroup + "_tab_" + i);
  173.         if (tab != null) {
  174.             tab.className = (i == idx) ? 'forms-tab forms-activeTab': 'forms-tab';
  175.         }
  176.         // Change tab content visibilty
  177.         var tabitems = document.getElementById(tabgroup + "_items_" + i);
  178.         if (tabitems != null) {
  179.             tabitems.style.display = (i == idx) ? '' : 'none';
  180.         }
  181.     }
  182.     // Change state value
  183.     if (state.length > 0) {
  184.         document.forms[0][state].value = idx;
  185.     }
  186. }