<commit_before><?xml version="1.0" encoding="UTF-8"?>
<!--
    Copyright (C) 2008 Orbeon, Inc.

    This program is free software; you can redistribute it and/or modify it under the terms of the
    GNU Lesser General Public License as published by the Free Software Foundation; either version
    2.1 of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    See the GNU Lesser General Public License for more details.

    The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
-->
<xxforms:dialog xmlns:xhtml="http://www.w3.org/1999/xhtml"
      xmlns:xforms="http://www.w3.org/2002/xforms"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:ev="http://www.w3.org/2001/xml-events"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      xmlns:xxi="http://orbeon.org/oxf/xml/xinclude"
      xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
      xmlns:fr="http://orbeon.org/oxf/xml/form-runner"
      xmlns:saxon="http://saxon.sf.net/"

      id="fb-action-dialog" level="modal" close="true" draggable="true" class="fr-dialog" model="fb-bindings-model">

    <xforms:label>Actions Editor</xforms:label>

    <xxforms:variable name="instance" select="instance('fb-bindings-editor-instance')"/>
    <xhtml:div class="fr-grid">
        <xhtml:div class="fr-grid-content">
            <xforms:input ref="name" id="fb-binding-name-input" incremental="true">
                <xforms:label>Action Name</xforms:label>
                <xforms:hint>Choose a unique name for this action</xforms:hint>
            </xforms:input>
            <xforms:group ref="$instance/xforms:action" appearance="xxforms:internal">
                <xforms:select1 ref="xforms:action[1]/@ev:event">
                    <xforms:label>React to</xforms:label>
                    <xforms:hint>Event (on a control or global) to react to</xforms:hint>
                    <xforms:item>
                        <xforms:label>[Select...]</xforms:label>
                        <xforms:value/>
                    </xforms:item>
                    <xforms:item>
                        <xforms:label>Value Change</xforms:label>
                        <xforms:value>xforms-value-changed</xforms:value>
                    </xforms:item>
                    <xforms:item>
                        <xforms:label>Activation</xforms:label>
                        <xforms:value>DOMActivate</xforms:value>
                    </xforms:item>
                    <xforms:item>
                        <xforms:label>Form Load</xforms:label>
                        <xforms:value>xforms-ready</xforms:value>
                    </xforms:item>
                </xforms:select1>
                <xforms:select1 ref="xforms:action[1]/@ev:observer">
                    <xforms:label>Control</xforms:label>
                    <xforms:item>
                        <xforms:label>[Select...]</xforms:label>
                        <xforms:value/>
                    </xforms:item>
                    <xforms:itemset nodeset="$body//(*[@ref or @bind or self::xforms:trigger])[ends-with(@id, '-control')]">
                        <!-- Try to build a reasonably user-friendly label -->
                        <xforms:label value="for $id in substring-before(@id, '-control') return concat($current-resources/*[name() = $id]/label, ' (', $id, ')')"/>
                        <xforms:value value="@id"/>
                    </xforms:itemset>
                </xforms:select1>
                <xforms:select1 ref="xforms:action[1]/xforms:send/@submission" id="fb-bindings-submission-select">
                    <xforms:label>Service to Call</xforms:label>
                    <xforms:hint>Service to call when the event happens</xforms:hint>
                    <xforms:item>
                        <xforms:label>[Select...]</xforms:label>
                        <xforms:value/>
                    </xforms:item>
                    <!-- Show submissions that are NOT yet used by a service. This is a current limitation. -->
                    <xforms:itemset nodeset="$model/xforms:submission[ends-with(@id, '-submission') and not(@id = ../xforms:action[ends-with(@id, '-binding') and @id != concat($instance/initial-name, '-binding')]/xforms:action[1]/xforms:send/@submission)]">
                        <xforms:label value="substring-before(@id, '-submission')"/>
                        <xforms:value value="@id"/>
                    </xforms:itemset>
                </xforms:select1>
                <!-- Set request values -->
                <xforms:group ref="xforms:action[@ev:event = 'xforms-submit']/xforms:action">
                    <xforms:label>Set Service Request Values</xforms:label>
                    <fr:repeat nodeset="xforms:action[@class = 'fr-set-service-value-action']" id="fb-actions-setvalue-repeat" origin="instance('fb-binding-action-template')/xforms:action[@class = 'fr-set-service-value-action']" columns="2" appearance="xxforms:table">
                        <fr:body>
                            <xhtml:tr>
                                <xhtml:td>
                                    <xforms:select1 ref="xxforms:variable[@name = 'control-name']/@select">
                                        <xforms:label>Source Control</xforms:label>
                                        <xforms:item>
                                            <xforms:label>[Select...]</xforms:label>
                                            <xforms:value/>
                                        </xforms:item>
                                        <xforms:itemset nodeset="$body//*[(@ref or @bind) and ends-with(@id, '-control')]">
                                            <!-- Try to build a reasonably user-friendly label -->
                                            <xforms:label value="for $id in substring-before(@id, '-control') return concat($current-resources/*[name() = $id]/label, ' (', $id, ')')"/>
                                            <xforms:value value="concat('''', substring-before(@id, '-control'), '''')"/>
                                        </xforms:itemset>
                                    </xforms:select1>
                                </xhtml:td>
                                <xhtml:td>
                                    <xforms:input ref="xxforms:variable[@name = 'path']/@select">
                                        <xforms:label>Destination XPath Expression</xforms:label>
                                    </xforms:input>
                                </xhtml:td>
                            </xhtml:tr>
                        </fr:body>
                    </fr:repeat>
                </xforms:group>
                <xforms:group ref="xforms:action[@ev:event = 'xforms-submit']/xforms:action">
                    <xforms:label>Set Database Service Parameters</xforms:label>
                    <fr:repeat nodeset="xforms:action[@class = 'fr-set-database-service-value-action']" id="fb-actions-database-setvalue-repeat" origin="instance('fb-binding-action-template')/xforms:action[@class = 'fr-set-database-service-value-action']" columns="2" appearance="xxforms:table">
                        <fr:body>
                            <xhtml:tr>
                                <xhtml:td>
                                    <xforms:select1 ref="xxforms:variable[@name = 'control-name']/@select">
                                        <xforms:label>Source Control</xforms:label>
                                        <xforms:item>
                                            <xforms:label>[Select...]</xforms:label>
                                            <xforms:value/>
                                        </xforms:item>
                                        <xforms:itemset nodeset="$body//*[(@ref or @bind) and ends-with(@id, '-control')]">
                                            <!-- Try to build a reasonably user-friendly label -->
                                            <xforms:label value="for $id in substring-before(@id, '-control') return concat($current-resources/*[name() = $id]/label, ' (', $id, ')')"/>
                                            <xforms:value value="concat('''', substring-before(@id, '-control'), '''')"/>
                                        </xforms:itemset>
                                    </xforms:select1>
                                </xhtml:td>
                                <xhtml:td>
                                    <xforms:input ref="xxforms:variable[@name = 'parameter']/@select">
                                        <xforms:label>Parameter Number</xforms:label>
                                    </xforms:input>
                                </xhtml:td>
                            </xhtml:tr>
                        </fr:body>
                    </fr:repeat>
                </xforms:group>
                <!-- Set control values -->
                <xforms:group ref="xforms:action[@ev:event = 'xforms-submit-done']">
                    <xforms:label>Set Response Control Values</xforms:label>
                    <fr:repeat nodeset="xforms:action[@class = 'fr-set-control-value-action']" id="fb-bindings-setvalue-repeat" origin="instance('fb-binding-action-template')/xforms:action[@class = 'fr-set-control-value-action']" columns="2" appearance="xxforms:table">
                        <fr:body>
                            <xhtml:tr>
                                <xhtml:td>
                                    <xforms:select1 ref="xxforms:variable[@name = 'control-name']/@select">
                                        <xforms:label>Destination Control</xforms:label>
                                        <xforms:item>
                                            <xforms:label>[Select...]</xforms:label>
                                            <xforms:value/>
                                        </xforms:item>
                                        <xforms:itemset nodeset="$body//*[(@ref or @bind) and ends-with(@id, '-control')]">
                                            <xforms:label value="substring-before(@id, '-control')"/>
                                            <xforms:value value="concat('''', substring-before(@id, '-control'), '''')"/>
                                        </xforms:itemset>
                                    </xforms:select1>
                                </xhtml:td>
                                <xhtml:td>
                                    <xforms:input ref="xxforms:variable[@name = 'control-value']/@select">
                                        <xforms:label>Source XPath Expression</xforms:label>
                                    </xforms:input>
                                </xhtml:td>
                            </xhtml:tr>
                        </fr:body>
                    </fr:repeat>
                </xforms:group>
                <!-- Only show this section if there are selection controls -->
                <xforms:group ref="xforms:action[@ev:event = 'xforms-submit-done' and exists($body//(xforms:select | xforms:select1))]">
                    <xforms:label>Set Response Selection Control Items</xforms:label>
                    <fr:repeat nodeset="xforms:action[@class = 'fr-itemset-action']" id="fb-bindings-itemset-repeat" origin="instance('fb-binding-action-template')/xforms:action[@class = 'fr-itemset-action']" columns="2" appearance="xxforms:table">
                        <fr:body>
                            <xhtml:tr>
                                <xhtml:td>
                                    <!-- Only list selection controls -->
                                    <xforms:select1 ref="xxforms:variable[@name = 'control-name']/@select">
                                        <xforms:label>Destination Control</xforms:label>
                                        <xforms:hint>Selection control</xforms:hint>
                                        <xforms:item>
                                            <xforms:label>[Select...]</xforms:label>
                                            <xforms:value/>
                                        </xforms:item>
                                        <xforms:itemset nodeset="$body//(xforms:select | xforms:select1)[(@ref or @bind) and ends-with(@id, '-control')]">
                                            <!-- Try to build a reasonably user-friendly label -->
                                            <xforms:label value="for $id in substring-before(@id, '-control') return concat($current-resources/*[name() = $id]/label, ' (', $id, ')')"/>
                                            <xforms:value value="concat('''', substring-before(@id, '-control'), '''')"/>
                                        </xforms:itemset>
                                    </xforms:select1>
                                </xhtml:td>
                                <xhtml:td>
                                    <!-- Only display this if a selection control is picked -->
                                    <xforms:group ref=".[xxforms:variable[@name = 'control-name']/@select != '']">
                                        <xforms:label>Itemset</xforms:label>
                                        <xforms:hint>XPath expressions to build a new itemset</xforms:hint>
                                        <xhtml:div class="fb-parameters">
                                            <xforms:input ref="xxforms:variable[@name = 'response-items']/@select">
                                                <xforms:label>Items</xforms:label>
                                                <xforms:hint>XPath expression returning all response item nodes</xforms:hint>
                                            </xforms:input>
                                            <xforms:input ref=".//xxforms:variable[@name = 'item-label']/@select">
                                                <xforms:label>Label</xforms:label>
                                                <xforms:hint>XPath expression relative to an item node</xforms:hint>
                                            </xforms:input>
                                            <xforms:input ref=".//xxforms:variable[@name = 'item-value']/@select">
                                                <xforms:label>Value</xforms:label>
                                                <xforms:hint>XPath expression relative to an item node</xforms:hint>
                                            </xforms:input>
                                        </xhtml:div>
                                    </xforms:group>
                                </xhtml:td>
                            </xhtml:tr>
                        </fr:body>
                    </fr:repeat>
                </xforms:group>
            </xforms:group>
            <!--<xforms:group ref="$instance/validation">-->
                <!--<xforms:label>Validate Control</xforms:label>-->
                <!--<fr:repeat nodeset="validation" id="fb-bindings-validation-repeat" origin="instance('fb-binding-action-template')/validation" columns="2" appearance="xxforms:table">-->
                    <!--<xhtml:tr>-->
                        <!--<xhtml:td>-->
                            <!--<xforms:select1 ref="@control">-->
                                <!--<xforms:label>Control</xforms:label>-->
                                <!--<xforms:item>-->
                                    <!--<xforms:label>[Select...]</xforms:label>-->
                                    <!--<xforms:value/>-->
                                <!--</xforms:item>-->
                                <!--<xforms:itemset nodeset="$body//*[(@ref or @bind) and ends-with(@id, '-control')]">-->
                                    <!--<xforms:label value="substring-before(@id, '-control')"/>-->
                                    <!--<xforms:value value="concat('instance(''fr-form-instance'')/*/*[name() = ''', substring-before(@id, '-control'), ''']')"/>-->
                                <!--</xforms:itemset>-->
                            <!--</xforms:select1>-->
                        <!--</xhtml:td>-->
                        <!--<xhtml:td>-->
                            <!--<xforms:input ref="@constraint">-->
                                <!--<xforms:label>Constraint Expression</xforms:label>-->
                            <!--</xforms:input>-->
                        <!--</xhtml:td>-->
                    <!--</xhtml:tr>-->
                <!--</fr:repeat>-->
            <!--</xforms:group>-->
        </xhtml:div>
    </xhtml:div>

    <xhtml:div class="fr-dialog-buttons">
        <xforms:group>
            <!-- Test button -->
            <xforms:trigger ref="$instance/save-test-trigger[false()]">
                <xforms:label>
                    <xhtml:img src="/forms/orbeon/builder/images/play.png" alt=""/>
                    <xhtml:span>Test</xhtml:span>
                </xforms:label>

                <!-- Test submission -->
                <xforms:action ev:event="DOMActivate">
                    <!-- TODO-->
                </xforms:action>
            </xforms:trigger>
            <!-- Save button -->
            <xforms:trigger ref="$instance/save-test-trigger">

                <xforms:label>
                    <xhtml:img src="/apps/fr/style/images/silk/disk.png" alt=""/>
                    <xhtml:span>Save</xhtml:span>
                </xforms:label>
                <!-- Save changes to form -->
                <xforms:action ev:event="DOMActivate">

                    <!-- Delete existing binding if any -->
                    <xxforms:variable name="binding-id" select="$instance/xforms:action[1]/@id"/>
                    <xforms:delete nodeset="$model/xforms:action[@id = $binding-id]"/>

                    <!-- Insert new binding -->
                    <xforms:insert context="$model" nodeset="*" origin="$instance/xforms:action[1]"/>

                    <!-- Hide dialog -->
                    <xxforms:hide dialog="fb-action-dialog"/>
                </xforms:action>
            </xforms:trigger>
            <xforms:trigger>
                <xforms:label><xhtml:img src="/apps/fr/style/close.gif" alt=""/> <xhtml:span>Cancel</xhtml:span></xforms:label>
                <!-- Hide dialog -->
                <xxforms:hide ev:event="DOMActivate" dialog="fb-action-dialog"/>
            </xforms:trigger>
        </xforms:group>
    </xhtml:div>

    <!-- Initialize dialog -->
    <xforms:action ev:event="xxforms-dialog-open">
        <xxforms:variable name="instance" select="instance('fb-bindings-editor-instance')"/>
        <xxforms:variable name="binding-name" select="event('binding-name')"/>

        <xforms:delete nodeset="$instance/xforms:action"/>

        <!-- Empty binding if no binding id passed -->
        <xforms:action if="not($binding-name)">
            <xforms:insert nodeset="$instance/*" origin="instance('fb-binding-template')"/>
        </xforms:action>
        <!-- Otherwise copy binding -->
        <xforms:action if="$binding-name">
            <xxforms:variable name="binding-id" select="concat($binding-name, '-binding')"/>
            <xforms:setvalue ref="$instance/name" value="$binding-name"/>
            <xforms:setvalue ref="$instance/initial-name" value="$binding-name"/>
            <xforms:insert nodeset="$instance/*" origin="$model/xforms:action[@id = $binding-id]"/>
        </xforms:action>

        <!-- Set focus to first input -->
        <xforms:setfocus control="fb-binding-name-input"/>
    </xforms:action>

    <!-- Model for the bindings editor -->
    <xforms:model id="fb-bindings-model">

        <xxforms:variable name="fb-lang" select="xxforms:instance('fb-language-instance')"/>
        <xxforms:variable name="model" select="xxforms:instance('fr-form-instance')/xhtml:head/xforms:model[@id = 'fr-form-model']"/>
        <xxforms:variable name="resources" select="$model/xforms:instance[@id = 'fr-form-resources']/*"/>
        <xxforms:variable name="current-resources" select="$resources/resource[@xml:lang = $fb-lang]"/>

        <xforms:instance id="fb-bindings-editor-instance">
            <bindings-editor>
                <name/>
                <initial-name/>
                <instance-id/>
                <save-test-trigger/>
                <xforms:action/>
            </bindings-editor>
        </xforms:instance>

        <!-- Enable/disable trigger -->
        <xforms:setvalue ev:event="xxforms-invalid" ev:observer="fb-bindings-editor-instance" ref="save-test-trigger">disabled</xforms:setvalue>
        <xforms:setvalue ev:event="xxforms-valid" ev:observer="fb-bindings-editor-instance" ref="save-test-trigger">enabled</xforms:setvalue>

        <xforms:bind nodeset="instance('fb-bindings-editor-instance')">
            <xforms:bind nodeset="name" type="xs:NCName"
                         constraint="(normalize-space() != '' and normalize-space() = ../initial-name)
                                        or not(../xforms:action[1]/@id = $model/xforms:action/@id)"/>
            <xforms:bind nodeset="instance-id" calculate="concat(../name, '-instance')"/>
            
            <xforms:bind nodeset="xforms:action">
                <!-- Action id -->
                <xforms:bind nodeset="@id" calculate="concat(../../name, '-binding')"/>
                <!-- Action that triggers the submission -->
                <xforms:bind nodeset="xforms:action[1]">
                    <!-- Event triggering the action -->
                    <xforms:bind nodeset="@ev:event" required="true()"/>
                    <xforms:bind name="submission-bind" nodeset="xforms:send/@submission" required="true()"/>
                    <!-- The observer for the event triggering the action is either a control, or the model -->
                    <xforms:bind nodeset="@ev:observer"
                                 required="../@ev:event = ('DOMActivate', 'xforms-value-changed')"
                                 relevant="../@ev:event = ('DOMActivate', 'xforms-value-changed')"
                                 calculate="if (../@ev:event = ('DOMActivate', 'xforms-value-changed')) then . else ('fr-form-model')"
                                 readonly="false()"/>
                </xforms:bind>
                <!-- xforms-submit -->
                <xforms:bind nodeset="xforms:action[@ev:event = 'xforms-submit']">
                    <xforms:bind nodeset="@ev:observer" calculate="$submission-bind"/><commit_msg>Support for "Value Change or Form Load".<commit_after><?xml version="1.0" encoding="UTF-8"?>
<!--
    Copyright (C) 2008 Orbeon, Inc.

    This program is free software; you can redistribute it and/or modify it under the terms of the
    GNU Lesser General Public License as published by the Free Software Foundation; either version
    2.1 of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    See the GNU Lesser General Public License for more details.

    The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
-->
<xxforms:dialog xmlns:xhtml="http://www.w3.org/1999/xhtml"
      xmlns:xforms="http://www.w3.org/2002/xforms"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:ev="http://www.w3.org/2001/xml-events"
      xmlns:xi="http://www.w3.org/2001/XInclude"
      xmlns:xxi="http://orbeon.org/oxf/xml/xinclude"
      xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
      xmlns:fr="http://orbeon.org/oxf/xml/form-runner"
      xmlns:saxon="http://saxon.sf.net/"

      id="fb-action-dialog" level="modal" close="true" draggable="true" class="fr-dialog" model="fb-bindings-model">

    <xforms:label>Actions Editor</xforms:label>

    <xxforms:variable name="instance" select="instance('fb-bindings-editor-instance')"/>
    <xhtml:div class="fr-grid">
        <xhtml:div class="fr-grid-content">
            <xforms:input ref="name" id="fb-binding-name-input" incremental="true">
                <xforms:label>Action Name</xforms:label>
                <xforms:hint>Choose a unique name for this action</xforms:hint>
            </xforms:input>
            <xforms:group ref="$instance/xforms:action" appearance="xxforms:internal">
                <xforms:select1 ref="xforms:action[1]/@ev:event">
                    <xforms:label>React to</xforms:label>
                    <xforms:hint>Event (on a control or global) to react to</xforms:hint>
                    <xforms:item>
                        <xforms:label>[Select...]</xforms:label>
                        <xforms:value/>
                    </xforms:item>
                    <!-- Just a value change -->
                    <xforms:item>
                        <xforms:label>Value Change</xforms:label>
                        <xforms:value>xforms-value-changed</xforms:value>
                    </xforms:item>
                    <!-- Value change AND the first time the control shows -->
                    <xforms:item>
                        <xforms:label>Value Change or Form Load</xforms:label>
                        <xforms:value>xforms-value-changed xforms-enabled</xforms:value>
                    </xforms:item>
                    <!-- Control activation -->
                    <xforms:item>
                        <xforms:label>Activation</xforms:label>
                        <xforms:value>DOMActivate</xforms:value>
                    </xforms:item>
                    <!-- Form load -->
                    <xforms:item>
                        <xforms:label>Form Load</xforms:label>
                        <xforms:value>xforms-ready</xforms:value>
                    </xforms:item>
                </xforms:select1>
                <xforms:select1 ref="xforms:action[1]/@ev:observer">
                    <xforms:label>Control</xforms:label>
                    <xforms:item>
                        <xforms:label>[Select...]</xforms:label>
                        <xforms:value/>
                    </xforms:item>
                    <xforms:itemset nodeset="$body//(*[@ref or @bind or self::xforms:trigger])[ends-with(@id, '-control')]">
                        <!-- Try to build a reasonably user-friendly label -->
                        <xforms:label value="for $id in substring-before(@id, '-control') return concat($current-resources/*[name() = $id]/label, ' (', $id, ')')"/>
                        <xforms:value value="@id"/>
                    </xforms:itemset>
                </xforms:select1>
                <xforms:select1 ref="xforms:action[1]/xforms:send/@submission" id="fb-bindings-submission-select">
                    <xforms:label>Service to Call</xforms:label>
                    <xforms:hint>Service to call when the event happens</xforms:hint>
                    <xforms:item>
                        <xforms:label>[Select...]</xforms:label>
                        <xforms:value/>
                    </xforms:item>
                    <!-- Show submissions that are NOT yet used by a service. This is a current limitation. -->
                    <xforms:itemset nodeset="$model/xforms:submission[ends-with(@id, '-submission') and not(@id = ../xforms:action[ends-with(@id, '-binding') and @id != concat($instance/initial-name, '-binding')]/xforms:action[1]/xforms:send/@submission)]">
                        <xforms:label value="substring-before(@id, '-submission')"/>
                        <xforms:value value="@id"/>
                    </xforms:itemset>
                </xforms:select1>
                <!-- Set request values -->
                <xforms:group ref="xforms:action[@ev:event = 'xforms-submit']/xforms:action">
                    <xforms:label>Set Service Request Values</xforms:label>
                    <fr:repeat nodeset="xforms:action[@class = 'fr-set-service-value-action']" id="fb-actions-setvalue-repeat" origin="instance('fb-binding-action-template')/xforms:action[@class = 'fr-set-service-value-action']" columns="2" appearance="xxforms:table">
                        <fr:body>
                            <xhtml:tr>
                                <xhtml:td>
                                    <xforms:select1 ref="xxforms:variable[@name = 'control-name']/@select">
                                        <xforms:label>Source Control</xforms:label>
                                        <xforms:item>
                                            <xforms:label>[Select...]</xforms:label>
                                            <xforms:value/>
                                        </xforms:item>
                                        <xforms:itemset nodeset="$body//*[(@ref or @bind) and ends-with(@id, '-control')]">
                                            <!-- Try to build a reasonably user-friendly label -->
                                            <xforms:label value="for $id in substring-before(@id, '-control') return concat($current-resources/*[name() = $id]/label, ' (', $id, ')')"/>
                                            <xforms:value value="concat('''', substring-before(@id, '-control'), '''')"/>
                                        </xforms:itemset>
                                    </xforms:select1>
                                </xhtml:td>
                                <xhtml:td>
                                    <xforms:input ref="xxforms:variable[@name = 'path']/@select">
                                        <xforms:label>Destination XPath Expression</xforms:label>
                                    </xforms:input>
                                </xhtml:td>
                            </xhtml:tr>
                        </fr:body>
                    </fr:repeat>
                </xforms:group>
                <xforms:group ref="xforms:action[@ev:event = 'xforms-submit']/xforms:action">
                    <xforms:label>Set Database Service Parameters</xforms:label>
                    <fr:repeat nodeset="xforms:action[@class = 'fr-set-database-service-value-action']" id="fb-actions-database-setvalue-repeat" origin="instance('fb-binding-action-template')/xforms:action[@class = 'fr-set-database-service-value-action']" columns="2" appearance="xxforms:table">
                        <fr:body>
                            <xhtml:tr>
                                <xhtml:td>
                                    <xforms:select1 ref="xxforms:variable[@name = 'control-name']/@select">
                                        <xforms:label>Source Control</xforms:label>
                                        <xforms:item>
                                            <xforms:label>[Select...]</xforms:label>
                                            <xforms:value/>
                                        </xforms:item>
                                        <xforms:itemset nodeset="$body//*[(@ref or @bind) and ends-with(@id, '-control')]">
                                            <!-- Try to build a reasonably user-friendly label -->
                                            <xforms:label value="for $id in substring-before(@id, '-control') return concat($current-resources/*[name() = $id]/label, ' (', $id, ')')"/>
                                            <xforms:value value="concat('''', substring-before(@id, '-control'), '''')"/>
                                        </xforms:itemset>
                                    </xforms:select1>
                                </xhtml:td>
                                <xhtml:td>
                                    <xforms:input ref="xxforms:variable[@name = 'parameter']/@select">
                                        <xforms:label>Parameter Number</xforms:label>
                                    </xforms:input>
                                </xhtml:td>
                            </xhtml:tr>
                        </fr:body>
                    </fr:repeat>
                </xforms:group>
                <!-- Set control values -->
                <xforms:group ref="xforms:action[@ev:event = 'xforms-submit-done']">
                    <xforms:label>Set Response Control Values</xforms:label>
                    <fr:repeat nodeset="xforms:action[@class = 'fr-set-control-value-action']" id="fb-bindings-setvalue-repeat" origin="instance('fb-binding-action-template')/xforms:action[@class = 'fr-set-control-value-action']" columns="2" appearance="xxforms:table">
                        <fr:body>
                            <xhtml:tr>
                                <xhtml:td>
                                    <xforms:select1 ref="xxforms:variable[@name = 'control-name']/@select">
                                        <xforms:label>Destination Control</xforms:label>
                                        <xforms:item>
                                            <xforms:label>[Select...]</xforms:label>
                                            <xforms:value/>
                                        </xforms:item>
                                        <xforms:itemset nodeset="$body//*[(@ref or @bind) and ends-with(@id, '-control')]">
                                            <xforms:label value="substring-before(@id, '-control')"/>
                                            <xforms:value value="concat('''', substring-before(@id, '-control'), '''')"/>
                                        </xforms:itemset>
                                    </xforms:select1>
                                </xhtml:td>
                                <xhtml:td>
                                    <xforms:input ref="xxforms:variable[@name = 'control-value']/@select">
                                        <xforms:label>Source XPath Expression</xforms:label>
                                    </xforms:input>
                                </xhtml:td>
                            </xhtml:tr>
                        </fr:body>
                    </fr:repeat>
                </xforms:group>
                <!-- Only show this section if there are selection controls -->
                <xforms:group ref="xforms:action[@ev:event = 'xforms-submit-done' and exists($body//(xforms:select | xforms:select1))]">
                    <xforms:label>Set Response Selection Control Items</xforms:label>
                    <fr:repeat nodeset="xforms:action[@class = 'fr-itemset-action']" id="fb-bindings-itemset-repeat" origin="instance('fb-binding-action-template')/xforms:action[@class = 'fr-itemset-action']" columns="2" appearance="xxforms:table">
                        <fr:body>
                            <xhtml:tr>
                                <xhtml:td>
                                    <!-- Only list selection controls -->
                                    <xforms:select1 ref="xxforms:variable[@name = 'control-name']/@select">
                                        <xforms:label>Destination Control</xforms:label>
                                        <xforms:hint>Selection control</xforms:hint>
                                        <xforms:item>
                                            <xforms:label>[Select...]</xforms:label>
                                            <xforms:value/>
                                        </xforms:item>
                                        <xforms:itemset nodeset="$body//(xforms:select | xforms:select1)[(@ref or @bind) and ends-with(@id, '-control')]">
                                            <!-- Try to build a reasonably user-friendly label -->
                                            <xforms:label value="for $id in substring-before(@id, '-control') return concat($current-resources/*[name() = $id]/label, ' (', $id, ')')"/>
                                            <xforms:value value="concat('''', substring-before(@id, '-control'), '''')"/>
                                        </xforms:itemset>
                                    </xforms:select1>
                                </xhtml:td>
                                <xhtml:td>
                                    <!-- Only display this if a selection control is picked -->
                                    <xforms:group ref=".[xxforms:variable[@name = 'control-name']/@select != '']">
                                        <xforms:label>Itemset</xforms:label>
                                        <xforms:hint>XPath expressions to build a new itemset</xforms:hint>
                                        <xhtml:div class="fb-parameters">
                                            <xforms:input ref="xxforms:variable[@name = 'response-items']/@select">
                                                <xforms:label>Items</xforms:label>
                                                <xforms:hint>XPath expression returning all response item nodes</xforms:hint>
                                            </xforms:input>
                                            <xforms:input ref=".//xxforms:variable[@name = 'item-label']/@select">
                                                <xforms:label>Label</xforms:label>
                                                <xforms:hint>XPath expression relative to an item node</xforms:hint>
                                            </xforms:input>
                                            <xforms:input ref=".//xxforms:variable[@name = 'item-value']/@select">
                                                <xforms:label>Value</xforms:label>
                                                <xforms:hint>XPath expression relative to an item node</xforms:hint>
                                            </xforms:input>
                                        </xhtml:div>
                                    </xforms:group>
                                </xhtml:td>
                            </xhtml:tr>
                        </fr:body>
                    </fr:repeat>
                </xforms:group>
            </xforms:group>
            <!--<xforms:group ref="$instance/validation">-->
                <!--<xforms:label>Validate Control</xforms:label>-->
                <!--<fr:repeat nodeset="validation" id="fb-bindings-validation-repeat" origin="instance('fb-binding-action-template')/validation" columns="2" appearance="xxforms:table">-->
                    <!--<xhtml:tr>-->
                        <!--<xhtml:td>-->
                            <!--<xforms:select1 ref="@control">-->
                                <!--<xforms:label>Control</xforms:label>-->
                                <!--<xforms:item>-->
                                    <!--<xforms:label>[Select...]</xforms:label>-->
                                    <!--<xforms:value/>-->
                                <!--</xforms:item>-->
                                <!--<xforms:itemset nodeset="$body//*[(@ref or @bind) and ends-with(@id, '-control')]">-->
                                    <!--<xforms:label value="substring-before(@id, '-control')"/>-->
                                    <!--<xforms:value value="concat('instance(''fr-form-instance'')/*/*[name() = ''', substring-before(@id, '-control'), ''']')"/>-->
                                <!--</xforms:itemset>-->
                            <!--</xforms:select1>-->
                        <!--</xhtml:td>-->
                        <!--<xhtml:td>-->
                            <!--<xforms:input ref="@constraint">-->
                                <!--<xforms:label>Constraint Expression</xforms:label>-->
                            <!--</xforms:input>-->
                        <!--</xhtml:td>-->
                    <!--</xhtml:tr>-->
                <!--</fr:repeat>-->
            <!--</xforms:group>-->
        </xhtml:div>
    </xhtml:div>

    <xhtml:div class="fr-dialog-buttons">
        <xforms:group>
            <!-- Test button -->
            <xforms:trigger ref="$instance/save-test-trigger[false()]">
                <xforms:label>
                    <xhtml:img src="/forms/orbeon/builder/images/play.png" alt=""/>
                    <xhtml:span>Test</xhtml:span>
                </xforms:label>

                <!-- Test submission -->
                <xforms:action ev:event="DOMActivate">
                    <!-- TODO-->
                </xforms:action>
            </xforms:trigger>
            <!-- Save button -->
            <xforms:trigger ref="$instance/save-test-trigger">

                <xforms:label>
                    <xhtml:img src="/apps/fr/style/images/silk/disk.png" alt=""/>
                    <xhtml:span>Save</xhtml:span>
                </xforms:label>
                <!-- Save changes to form -->
                <xforms:action ev:event="DOMActivate">

                    <!-- Delete existing binding if any -->
                    <xxforms:variable name="binding-id" select="$instance/xforms:action[1]/@id"/>
                    <xforms:delete nodeset="$model/xforms:action[@id = $binding-id]"/>

                    <!-- Insert new binding -->
                    <xforms:insert context="$model" nodeset="*" origin="$instance/xforms:action[1]"/>

                    <!-- Hide dialog -->
                    <xxforms:hide dialog="fb-action-dialog"/>
                </xforms:action>
            </xforms:trigger>
            <xforms:trigger>
                <xforms:label><xhtml:img src="/apps/fr/style/close.gif" alt=""/> <xhtml:span>Cancel</xhtml:span></xforms:label>
                <!-- Hide dialog -->
                <xxforms:hide ev:event="DOMActivate" dialog="fb-action-dialog"/>
            </xforms:trigger>
        </xforms:group>
    </xhtml:div>

    <!-- Initialize dialog -->
    <xforms:action ev:event="xxforms-dialog-open">
        <xxforms:variable name="instance" select="instance('fb-bindings-editor-instance')"/>
        <xxforms:variable name="binding-name" select="event('binding-name')"/>

        <xforms:delete nodeset="$instance/xforms:action"/>

        <!-- Empty binding if no binding id passed -->
        <xforms:action if="not($binding-name)">
            <xforms:insert nodeset="$instance/*" origin="instance('fb-binding-template')"/>
        </xforms:action>
        <!-- Otherwise copy binding -->
        <xforms:action if="$binding-name">
            <xxforms:variable name="binding-id" select="concat($binding-name, '-binding')"/>
            <xforms:setvalue ref="$instance/name" value="$binding-name"/>
            <xforms:setvalue ref="$instance/initial-name" value="$binding-name"/>
            <xforms:insert nodeset="$instance/*" origin="$model/xforms:action[@id = $binding-id]"/>
        </xforms:action>

        <!-- Set focus to first input -->
        <xforms:setfocus control="fb-binding-name-input"/>
    </xforms:action>

    <!-- Model for the bindings editor -->
    <xforms:model id="fb-bindings-model">

        <xxforms:variable name="fb-lang" select="xxforms:instance('fb-language-instance')"/>
        <xxforms:variable name="model" select="xxforms:instance('fr-form-instance')/xhtml:head/xforms:model[@id = 'fr-form-model']"/>
        <xxforms:variable name="resources" select="$model/xforms:instance[@id = 'fr-form-resources']/*"/>
        <xxforms:variable name="current-resources" select="$resources/resource[@xml:lang = $fb-lang]"/>

        <xforms:instance id="fb-bindings-editor-instance">
            <bindings-editor>
                <name/>
                <initial-name/>
                <instance-id/>
                <save-test-trigger/>
                <xforms:action/>
            </bindings-editor>
        </xforms:instance>

        <!-- Enable/disable trigger -->
        <xforms:setvalue ev:event="xxforms-invalid" ev:observer="fb-bindings-editor-instance" ref="save-test-trigger">disabled</xforms:setvalue>
        <xforms:setvalue ev:event="xxforms-valid" ev:observer="fb-bindings-editor-instance" ref="save-test-trigger">enabled</xforms:setvalue>

        <xforms:bind nodeset="instance('fb-bindings-editor-instance')">
            <xforms:bind nodeset="name" type="xs:NCName"
                         constraint="(normalize-space() != '' and normalize-space() = ../initial-name)
                                        or not(../xforms:action[1]/@id = $model/xforms:action/@id)"/>
            <xforms:bind nodeset="instance-id" calculate="concat(../name, '-instance')"/>
            
            <xforms:bind nodeset="xforms:action">
                <!-- Action id -->
                <xforms:bind nodeset="@id" calculate="concat(../../name, '-binding')"/>
                <!-- Action that triggers the submission -->
                <xforms:bind nodeset="xforms:action[1]">
                    <!-- Event triggering the action -->
                    <xforms:bind nodeset="@ev:event" required="true()"/>
                    <xforms:bind name="submission-bind" nodeset="xforms:send/@submission" required="true()"/>
                    <!-- The observer for the event triggering the action is either a control, or the model -->
                    <xforms:bind nodeset="@ev:observer"
                                 required="tokenize(../@ev:event, '\s') = ('DOMActivate', 'xforms-value-changed', 'xforms-enabled')"
                                 relevant="tokenize(../@ev:event, '\s') = ('DOMActivate', 'xforms-value-changed', 'xforms-enabled')"
                                 calculate="if (tokenize(../@ev:event, '\s') = ('DOMActivate', 'xforms-value-changed', 'xforms-enabled')) then . else ('fr-form-model')"
                                 readonly="false()"/>
                </xforms:bind>
                <!-- xforms-submit -->
                <xforms:bind nodeset="xforms:action[@ev:event = 'xforms-submit']">
                    <xforms:bind nodeset="@ev:observer" calculate="$submission-bind"/>
{'fraction_non_alphanumeric': 0.1497102172827715, 'fraction_numerical': 0.0028218541752588508, 'mean_word_length': 1.3640188834154352, 'pattern_counts': {'":': 0, '<': 785, '<?xml version=': 2, '>': 785, 'https://': 0, 'lorem ipsum': 0, 'www.': 12, 'xml': 30}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 240, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5009724', 'n_tokens_mistral': 11754, 'n_tokens_neox': 11063, 'n_words': 1845}