<?xml version="1.0"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<page:page xmlns:ft="http://apache.org/cocoon/forms/1.0#template"
      xmlns:fi="http://apache.org/cocoon/forms/1.0#instance"
      xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
      xmlns:i18n="http://apache.org/cocoon/i18n/2.1"
      xmlns:page="http://apache.org/cocoon/lenya/cms-page/1.0">
      
  <!-- Import the macros that define CForms template elements -->
  <jx:import uri="resource://org/apache/cocoon/forms/generation/jx-macros.xml"/>

  <page:head>
  </page:head>

  <page:title>
    <i18n:text>CForm Example</i18n:text>
  </page:title>

  <page:body>
    <ft:form-template method="POST" ajax="false">
      <input type="hidden" name="lenya.continuation" value="${continuation.id}"/>
      <input type="hidden" name="lenya.usecase"
        value="${request.getParameter('lenya.usecase')}"/>
      <input type="hidden" id="lenya.submit" name="lenya.submit" value="ok"/>
      <p>
        This is an example of how dynamic templates (using the JX macros) allow to change the
        page structure depending on the form contents. By adding or removing rows in the repeater
        below you will see:
      </p>
      <ul>
        <li>when there are no contacts, only the "Add contact" button is displayed and the table isn't shown,</li>
        <li>when there are some contacts, the "move up" and "move down" buttons are not shown on the first and last lines, respectively.</li>
      </ul>
    <ft:repeater id="contacts">
      <div>
        <jx:choose>
          <jx:when test="${repeater.getSize() == 0}">
            <p><strong><em>There are no contacts to display</em></strong></p>
          </jx:when>
          <jx:otherwise>

            <table border="1">
              <tr>
                <th><ft:repeater-widget-label widget-id="firstname"/></th>
                <th><ft:repeater-widget-label widget-id="lastname"/></th>
                <th><ft:repeater-widget-label widget-id="ID"/></th>
                <th><ft:repeater-widget-label widget-id="select"/></th>
              </tr>
              <!-- The contents of the repeater-widget element is a template that will
                   be applied to each row in the repeater. -->
              <ft:repeater-rows>
                <tr class="forms-row-${repeaterLoop.index % 2}">
                  <td><ft:widget id="firstname"/></td>
                  <td><ft:widget id="lastname"/></td>
                  <td><ft:widget id="ID"/></td>
                  <td>
                    <!-- Don't display the "move up" button on the first row -->
                    <jx:choose>
                      <jx:when test="${repeaterLoop.first}">
                        <img src="/cforms/forms/img/blank_btn.gif"/>
                      </jx:when>
                      <jx:otherwise>
                        <ft:widget id="up">
                          <fi:styling type="image" src="/cforms/forms/img/move_up.gif"/>
                        </ft:widget>
                      </jx:otherwise>
                    </jx:choose>
                    <!-- Don't display the "move down" button on the first row -->
                    <jx:choose>
                      <jx:when test="${repeaterLoop.last}">
                        <img src="/cforms/forms/img/blank_btn.gif"/>
                      </jx:when>
                      <jx:otherwise>
                        <ft:widget id="down">
                          <fi:styling type="image" src="/cforms/forms/img/move_down.gif"/>
                        </ft:widget>
                      </jx:otherwise>
                    </jx:choose>
                  <ft:widget id="select"/>
                  </td>
                </tr>
              </ft:repeater-rows>
            </table>
          </jx:otherwise>
        </jx:choose>
        <p>
          <ft:widget id="../addcontact"/>
          <jx:if test="${widget.getChild('contacts').getSize() > 0}">
            <ft:widget id="../removecontacts"/>
          </jx:if>
        </p>
      </div>
      </ft:repeater>
      <ft:widget id="ok"/>
      <!-- If you want a cancel button you beware to change the lenya.submit value!!! like: -->
      <br/>
      <input i18n:attr="value" type="submit" name="cancel" value="Cancel" onClick="document.getElementById('lenya.submit').value='cancel'"/>
    </ft:form-template>
  </page:body>
</page:page>
