<?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.
-->

<!-- An implementation of the CForms template engine as a JXTemplate tag library -->
<!-- $Id$ -->

<jx:template xmlns:jx="http://apache.org/cocoon/templates/jx/1.0"
             xmlns:ft="http://apache.org/cocoon/forms/1.0#template"
             xmlns:fi="http://apache.org/cocoon/forms/1.0#instance"
             xmlns:bu="http://apache.org/cocoon/browser-update/1.0">

    <!--
        ft:form
    -->
    <jx:macro name="form" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:set var="localeSitemapParameter" value="${cocoon.parameters.getParameter('locale','')}"/>
      <jx:set var="cformsHelper" value="#{org.apache.cocoon.forms.generation.JXMacrosHelper.createHelper($cocoon/consumer,$cocoon/request,$localeSitemapParameter)}"/>
      <jx:set var="form" value="${cformsHelper.getForm(CocoonFormsInstance, cocoon.parameters['attribute-name'])}"/>
      <jx:if test="${cformsHelper.isVisible(form)}">
        <jx:set var="cformsDummy" value="${cformsHelper.startForm(form, macro.arguments)}"/>
        <!-- the form is also the current widget -->
        <jx:set var="widget" value="${form}"/>
        <jx:evalBody/>
        <jx:set var="cformsDummy" value="${cformsHelper.endForm()}"/>
      </jx:if>
    </jx:macro>

    <!--
        ft:form-template: preferred syntax is now "ft:form" (we know we're in a template)
    -->
    <jx:macro name="form-template" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:set var="localeSitemapParameter" value="${cocoon.parameters.getParameter('locale', '')}"/>
      <jx:set var="cformsHelper" value="#{org.apache.cocoon.forms.generation.JXMacrosHelper.createHelper($cocoon/consumer,$cocoon/request,$localeSitemapParameter)}"/>
      <jx:set var="form" value="${cformsHelper.getForm(CocoonFormsInstance, cocoon.parameters['attribute-name'])}"/>
      <jx:if test="${cformsHelper.isVisible(form)}">
        <jx:set var="cformsDummy" value="${cformsHelper.startForm(form, macro.arguments)}"/>
        <!-- the form is also the current widget -->
        <jx:set var="widget" value="${form}"/>
        <jx:evalBody/>
        <jx:set var="cformsDummy" value="${cformsHelper.endForm()}"/>
      </jx:if>
    </jx:macro>

    <!--
        ft:widget
    -->
    <jx:macro name="widget" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>

      <jx:if test="${cformsHelper.pushWidget(id)}">
        <jx:set var="widget" value="${cformsHelper.peekWidget()}"/>
        <jx:set var="cformsDummy" value="${cformsHelper.generateWidget(widget, macro.arguments)}"/>
        <jx:evalBody/>
        <jx:set var="cformsDummy" value="${cformsHelper.flushRootAndPop()}"/>
      </jx:if>
    </jx:macro>

    <!--
        ft:repeater-widget-label
    -->
    <jx:macro name="repeater-widget-label" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>
      <jx:parameter name="widget-id"/>

      <jx:set var="cformsDummy" value="${cformsHelper.generateRepeaterWidgetLabel(widget, id, this['widget-id'])}"/>
    </jx:macro>

    <!--
        ft:widget-label
    -->
    <jx:macro name="widget-label" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>

      <jx:set var="cformsDummy" value="${cformsHelper.generateWidgetLabel(widget, id)}"/>
    </jx:macro>

    <!--
        ft:repeater-size
    -->
    <jx:macro name="repeater-size" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>

      <jx:set var="cformsDummy" value="${cformsHelper.generateRepeaterSize(widget, id)}"/>
    </jx:macro>

    <!--
        ft:repeater-widget, the old non-ajax capable way
    -->
    <jx:macro name="repeater-widget" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>

      <jx:if test="${cformsHelper.pushRepeater(id, false)}">
        <jx:set var="repeater" value="${cformsHelper.peekWidget()}"/>
        <jx:forEach varStatus="repeaterLoop" begin="0" end="${repeater.getSize() - 1}">
          <jx:if test="#{pushContainer($cformsHelper, java.lang.Integer.toString($repeaterLoop/index))}">
            <jx:set var="widget" value="${cformsHelper.peekWidget()}"/>
            <jx:evalBody/>
            <jx:set var="cformsDummy" value="${cformsHelper.popWidget()}"/>
          </jx:if>
        </jx:forEach>
        <jx:set var="cformsDummy" value="${cformsHelper.popWidget()}"/>
      </jx:if>
    </jx:macro>

    <!--
        ft:continuation-id
    -->
    <jx:macro name="continuation-id" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <fi:continuation-id>${cocoon.continuation.id}</fi:continuation-id>
    </jx:macro>

    <!--
        ft:class
    -->
    <jx:macro name="class" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>

      <jx:set var="cformsDummy" value="${cformsHelper.defineClassBody(form, id, macro.body)}"/>
    </jx:macro>

    <!--
        ft:group : just increase the nesting level
    -->
    <jx:macro name="group" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>

      <jx:if test="${cformsHelper.pushContainer(id)}">
        <jx:set var="widget" value="${cformsHelper.peekWidget()}"/>
        <fi:group id="${widget.fullName}">
          <jx:evalBody/>
        </fi:group>
        <jx:set var="cformsDummy" value="${cformsHelper.popWidget()}"/>
      </jx:if>
    </jx:macro>

    <!--
        ft:new
    -->
    <jx:macro name="new" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>

      <jx:eval select="${cformsHelper.getClassBody(id)}"/>
    </jx:macro>

    <!--
        ft:struct : just increase the nesting level
    -->
    <jx:macro name="struct" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>

      <jx:if test="${cformsHelper.pushContainer(id)}">
        <jx:set var="widget" value="${cformsHelper.peekWidget()}"/>
        <fi:struct id="${widget.fullName}">
          <jx:evalBody/>
        </fi:struct>
        <jx:set var="cformsDummy" value="${cformsHelper.popWidget()}"/>
      </jx:if>
    </jx:macro>

    <!--
        ft:union
    -->
    <jx:macro name="union" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>

      <jx:if test="${cformsHelper.pushContainer(id)}">
        <jx:set var="widget" value="${cformsHelper.peekWidget()}"/>
        <fi:union id="${widget.getRequestParameterName()}">
          <jx:evalBody/>
        </fi:union>
        <jx:set var="cformsDummy" value="${cformsHelper.popWidget()}"/>
      </jx:if>
    </jx:macro>

    <!--
        ft:case
    -->
    <jx:macro name="case" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>

      <jx:if test="${cformsHelper.isSelectedCase(widget, id)}">
        <jx:set widget="${cformsHelper.getWidget(widget, id)}"/>
        <jx:evalBody/>
      </jx:if>
    </jx:macro>

    <!--
        ft:validation-error
    -->
    <jx:macro name="validation-error" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>

      <jx:set var="widget" value="${cformsHelper.getWidget(widget, id)}"/>
      <jx:set var="validationError" value="${widget.getValidationError()}"/>
      <jx:if test="${cformsHelper.isValidationError(validationError)}">
          <jx:set var="cformsDummy" value="${cformsHelper.generateValidationError(validationError)}"/>
            <jx:evalBody/>
          <jx:set var="cformsDummy" value="${cformsHelper.flushRoot()}"/>
      </jx:if>
    </jx:macro>

    <!--
        ft:aggregate-widget
    -->
    <jx:macro name="aggregate-widget" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>

      <jx:if test="${cformsHelper.pushContainer(id)}">
        <jx:set var="widget" value="${cformsHelper.peekWidget()}"/>
        <jx:evalBody/>
        <jx:set var="cformsDummy" value="${cformsHelper.popWidget()}"/>
      </jx:if>
    </jx:macro>

    <jx:macro name="repeater" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>
      <jx:if test="${cformsHelper.pushRepeater(id, true)}">
        <jx:set var="repeater" value="${cformsHelper.peekWidget()}"/>
        <jx:set var="widget" value="${cformsHelper.peekWidget()}"/>
        <jx:evalBody/>
        <jx:set var="cformsDummy" value="${cformsHelper.popWidget()}"/>
      </jx:if>
    </jx:macro>

    <jx:macro name="repeater-rows" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
        <jx:forEach varStatus="repeaterLoop" begin="0" end="${repeater.getSize() - 1}">
          <jx:if test="#{pushContainer($cformsHelper, java.lang.Integer.toString($repeaterLoop/index))}">
            <jx:set var="widget" value="${cformsHelper.peekWidget()}"/>
            <jx:evalBody/>
            <jx:set var="cformsDummy" value="${cformsHelper.popWidget()}"/>
          </jx:if>
        </jx:forEach>
    </jx:macro>

    <jx:macro name="tree" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:parameter name="id"/>
      <jx:if test="${cformsHelper.pushContainer(id)}">
        <jx:set var="widget" value="${cformsHelper.peekWidget()}"/>
          <jx:set var="treeWidget" value="${widget}"/>
	      <jx:set var="treeNode" value="${cformsHelper.createWalker()}"/>
	      <jx:evalBody/>
          <jx:set var="cformsDummy" value="${cformsHelper.popWidget()}"/>
	  </jx:if>
    </jx:macro>

    <jx:macro name="tree-nodes" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:set var="treeNodesBody" value="${macro.body}"/>
      <!-- Generate tree-management hidden fields here, i.e. inside the block element
           that should have been produced by <ft:tree> (used by bu:replace) -->
      <fi:field id="${widget.fullName}:action">
        <fi:styling type="hidden"/>
      </fi:field>
      <fi:field id="${widget.fullName}:path">
        <fi:styling type="hidden"/>
      </fi:field>
      <jx:choose>
        <jx:when test="${treeWidget.isRootVisible()}">
          <!-- set current widget for root node -->
          <jx:eval select="${treeNodesBody}"/>
        </jx:when>
        <jx:otherwise>
          <ft:tree-children/>
        </jx:otherwise>
      </jx:choose>
    </jx:macro>

    <jx:macro name="tree-children" targetNamespace="http://apache.org/cocoon/forms/1.0#template">
      <jx:if test="${!treeNode.isLeaf() &amp;&amp; treeNode.isExpanded()}">
        <jx:forEach varStatus="treeLoop" items="${treeNode.enterChildren()}">
          <!-- also set widget when we'll have a container for each node -->
          <jx:eval select="${treeNodesBody}"/>
        </jx:forEach>
        <jx:set var="cformsDummy" value="${treeNode.leave()}"/>
      </jx:if>
    </jx:macro>
</jx:template>
