<?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.
-->
<!-- This file is similar to form1_template_action.xml. They differ in group layout,
     form's action attribute (to use continuations) and, this one is a dynamic form
     template that changes the 'contacts' repeater layout depending on its size -->
<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">
      
  <!-- Import the macros that define CForms template elements -->
  <jx:import uri="resource://org/apache/cocoon/forms/generation/jx-macros.xml"/>
  <h4 class="samplesGroup">Repeater widget</h4>
  <title>Dynamic template</title>
  <style type="text/css">
    /* Additional styles for alternating row colors */
    .forms-row-0 td {
       padding: 5px;
       background-color: #E0E0E0;
    }

    .forms-row-1 td {
       padding: 5px;
    }
  </style>
  
  <content>
    <ft:form-template action="#{$cocoon/continuation/id}.continue" method="POST" ajax="true">
    
    <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>
    <p>This sample has a <a href="do-dojoRepeater.flow">variation using Dojo</a> that allows drag-and-drop reordering
       of rows and clicking anywhere in a row to select it.</p>
    <p>Here is another <a href="do-dojoRepeaters.flow">Dojo sample</a> that extends the above dojo sample by
    allowing drag-and-drop support between different repeaters.</p>
      <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>
              <tbody>
                  <!-- 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="resources/forms/img/blank_btn.gif"/>
                          </jx:when>
                          <jx:otherwise>
                            <ft:widget id="up">
                              <fi:styling type="image" src="resources/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="resources/forms/img/blank_btn.gif"/>
                          </jx:when>
                          <jx:otherwise>
                            <ft:widget id="down">
                              <fi:styling type="image" src="resources/forms/img/move_down.gif"/>
                            </ft:widget>
                          </jx:otherwise>
                        </jx:choose>
                      <ft:widget id="select"/>
                      </td>
                    </tr>
                  </ft:repeater-rows>
                </tbody>
            </table>
                </jx:otherwise>
              </jx:choose>
             <p>
                  <ft:widget id="../addcontact"/>
                  <jx:if test="${widget.getSize() > 0}">
                    <ft:widget id="../removecontacts"/>
                  </jx:if>
              </p>
              </div>
            </ft:repeater>
            <ft:widget id="ok"/><br/>
            <a href="./do-dynaRepeater.flow">Restart this sample</a> - <a href="./">Back to samples</a>

    </ft:form-template>
  </content>
</page>
