<tal:block
  xmlns:tal="http://xml.zope.org/namespaces/tal"
  xmlns:metal="http://xml.zope.org/namespaces/metal"
  xmlns:i18n="http://xml.zope.org/namespaces/i18n"
  tal:define="field_id here/getId;
              form_id python: here.getForm().id;
              selection_name here/getSelectionName;
              selection here/getSelection;
              real_context here/getContext;
              global portal_url_string here/getPortalUrlString;
              context_url real_context/absolute_url;
              md5_string here/getMD5Checksum;
              hide_rows_on_no_search_criterion here/isHideRowsOnNoSearchCriterion;
              is_domain_tree_mode here/isDomainTreeMode;
              is_report_tree_mode here/isReportTreeMode;
              global is_domain_tree_supported here/isDomainTreeSupported;
              global is_report_tree_supported here/isReportTreeSupported;
              global is_gadget_mode request/is_gadget_mode | nothing;
              show_select_column here/showSelectColumn;
              show_anchor_column here/showAnchorColumn;
              show_search_line here/showSearchLine;
              is_web_mode real_context/isWebMode | nothing;
              is_dialog_mode request/dialog_mode | nothing;
              display_style_list here/getDisplayStyleList;
              list_style here/getListboxDisplayStyle;
              global_search_column here/getGlobalSearchColumn;
              global_search_column_script string:Base_doSelect;
              show_global_search python: global_search_column not in ('', None);
              line_list here/query;
              listbox_max_lines  python: int(here.getMaxLineNumber());
              total_line         python: int(here.total_size);
              is_sample          python: getattr(here, 'is_sample', False);
              current_page       python: int(here.current_page) + 1;
              current_page_max   python: listbox_max_lines * current_page;
              current_page_start python: (listbox_max_lines * (current_page - 1)) + 1;
              current_page_stop  python: (total_line < current_page_max) and total_line or current_page_max;
              form_url string:${context_url}/${form_id};
              need_pagination python: total_line > listbox_max_lines;
              show_list_style_selection python: len(display_style_list) > 1;
              show_listbox_tree_mode_selection python: not is_gadget_mode and 
                                                       (is_domain_tree_supported or is_report_tree_supported);
              show_list_action_link python: here.field.get_value('list_action');
              page_navigation_template python: request.get('page_navigation_template', here.getPageNavigationTemplate());
              is_slider_mode python:  'Slider' in page_navigation_template;
              is_default_listbox_field python: field_id=='listbox';
              field_prefix python: '';">

  <!-- Define hidden input.  -->
  <input type="hidden" 
         name="list_selection_name" 
         value="default" 
         tal:attributes="value selection_name" />
  <input type="hidden" 
         name="list_selection_name" 
         value="default"
         tal:attributes="value selection_name;
                         name string:${field_id}_list_selection_name" />
  <input tal:condition="md5_string" 
         type="hidden" 
         name="md5_object_uid_list" 
         value="checksum" 
         tal:attributes="value md5_string" />
  <input tal:condition="form_id" 
         type="hidden" 
         name="form_id" 
         tal:attributes="value form_id" 
         tal:replace="nothing"/>
  <input tal:condition="field_id" 
         type="hidden" 
         name="field_id" 
         tal:attributes="value field_id" 
         tal:replace="nothing"/>

  <tal:block tal:condition="is_gadget_mode">
    <tal:block tal:define="global box_relative_url python: request.get('box_relative_url', '');
                           global box python: real_context.restrictedTraverse(box_relative_url); 
                           global box_id python: '%s_content' %box_relative_url.replace('/', '_');
                           global dom_id python: request.get('dom_id',None) or box_id;
                           global field_prefix string:${box_id}_">
      <input tal:condition="python:form_id" 
             type="hidden" 
             name="gadget_form_id"
             tal:attributes="value form_id" />
    </tal:block>
  </tal:block>

<div class="listbox-container">

  <div class="listbox-tree">

    <!-- Domain Report Tree mode -->
    <div class="listbox-domain-tree-container" 
         tal:condition="is_domain_tree_mode">
      <tal:block tal:define="selected_domain_path here/getSelectedDomainPath">

        <!-- Select domain node -->
        <select name="domain_root_url"
                tal:attributes="onChange string:submitAction(this.form, '${context_url}/setDomainRoot')">
          <tal:block tal:repeat="c here/getDomainRootList">
            <option value="base_domain"
                    tal:define="path python: c[0]; title python: c[1]"
                    tal:attributes="selected python: path == selected_domain_path; value path"
                    tal:content="title"
                    i18n:translate="" i18n:domain="ui"/>
          </tal:block>
        </select>

        <!-- Domain node contents -->
        <table cellpadding="0"
               summary="This table contains the domain tree"
               class="listbox-table-domain-tree"
               tal:attributes="class string:${field_id}-table-domain-tree"
               tal:define="report_tree_list python: here.makeReportTreeList(report_path = selected_domain_path, unfolded_list = selection.getDomainList(), is_report_opened = False, sort_on=(('int_index', 'ASC'),));
                           total_depth python: max([report_tree.depth for report_tree in report_tree_list] + [-1])">
          <tr tal:repeat="report_tree report_tree_list">
            <tal:block tal:repeat="i python: range(report_tree.depth)">
              <td width="12" nowrap="nowrap">&nbsp;</td>
            </tal:block>
            <td colspan="1" 
                class="listbox-table-domain-tree-cell"
                tal:attributes="colspan python: total_depth - report_tree.depth + 1">
              <button type="submit"
                      name="foldDomain:method"
                      class="tree-open"
                      tal:condition="report_tree/is_open"
                      tal:content="report_tree/obj/getCompactTranslatedTitle"
                      tal:attributes="value string:${report_tree/domain_url}.${report_tree/depth}"/>
              <button type="submit"
                      name="unfoldDomain:method"
                      class="tree-closed"
                      tal:condition="not: report_tree/is_open"
                      tal:content="report_tree/obj/getCompactTranslatedTitle"
                      tal:attributes="value string:${report_tree/domain_url}.${report_tree/depth}"/>
            </td>
          </tr>
        </table>
      </tal:block>
    </div>
  </div>

  <div class="listbox-content"  
       tal:attributes="class python: test(not is_domain_tree_mode, 'listbox-content maximal-width', 'listbox-content listbox-content-fixed-width')">

    <div class="listbox-head">

      <div class="listbox-head-spacer"></div>

      <div class="listbox-head-content">

        <!-- Listbox head (in left) -->
        <div class="listbox-head-title">

          <!-- List tree mode choice -->
          <div class="listbox-header-box"
               tal:condition="python: show_listbox_tree_mode_selection and not is_web_mode">
            <tal:block metal:use-macro="container/ListBox_asHTMLLibrary/macros/listbox_tree_mode_selection"/>
          </div>

          <!-- Listbox title -->
          <div class="listbox-header-box">
            <tal:block metal:use-macro="container/ListBox_asHTMLLibrary/macros/listbox_title"/>
          </div>

          <!-- Number of rows in ERP5 mode -->
          <div class="listbox-header-box"
               tal:condition="python: not is_web_mode">
            <tal:block metal:use-macro="container/ListBox_asHTMLLibrary/macros/listbox_number_of_records"/>
          </div>

          <!-- List style display mode -->
          <div class="listbox-header-box"
               tal:condition="python: show_list_style_selection and not is_web_mode">
            <tal:block metal:use-macro="container/ListBox_asHTMLLibrary/macros/listbox_display_style_selection"/>
          </div>

        </div>

      <!-- Listbox nagivation (in right) -->
      <div class="listbox-head-navigation">

        <!--Show search result in web mode-->
        <div class="listbox-header-box"
             tal:condition="python: is_web_mode">
          <tal:block metal:use-macro="container/ListBox_asHTMLLibrary/macros/listbox_number_of_records"/>
        </div>

        <!--Page navigation -->
        <div class="listbox-header-box"
             tal:condition="python: need_pagination and not is_web_mode">
          <tal:block metal:use-macro="container/ListBox_asHTMLLibrary/macros/listbox_navigation"/>
        </div>

      </div>


      </div>


    </div>

    <div class="listbox-body">
      <table class="listbox"
             tal:attributes="class python: 'listbox %s %s-%s' %(field_id, field_id, list_style)">      
        <thead>
          <!--Column title -->
          <tr class="listbox-label-line">  

            <!--Report tree-->
            <th tal:condition="is_report_tree_mode"
                class="listbox-table-report-tree-selection-cell">
              <select name="report_root_url"
                      tal:attributes="onChange string:submitAction(this.form, '${context_url}/setReportRoot')">
                <tal:block tal:repeat="c here/getReportRootList">
                  <option value="base_domain"
                          tal:define="path python: c[0]; title python: c[1]"
                          tal:attributes="selected python: path == here.getSelectedReportPath(); value path"
                          tal:content="title" i18n:domain="ui" i18n:translate="">Domain</option>
                </tal:block>
              </select>
            </th>

            <!-- Anchor cell -->
            <th class="listbox-table-anchor-cell" tal:condition="show_anchor_column">&nbsp;</th>

            <!-- Select cell -->
            <th tal:condition="python: show_select_column"
                class="listbox-table-select-cell">
           
              <input class="listbox-check-all"
                     type="image"
                     name="checkAll:method" value="1"
                     alt="Check All" title="Check All"
                     tal:attributes="name string:${field_id}_checkAll:method;
                                     src string:${portal_url_string}/images/checkall.png"
                     i18n:domain="ui" i18n:attributes="title" />
              &nbsp;
              <input class="listbox-uncheck-all"
                     type="image" 
                     name="uncheckAll:method" value="1"
                     alt="Uncheck All" title="Uncheck All"
                     tal:attributes="src string:${portal_url_string}/images/decheckall.png;
                                     name string:${field_id}_uncheckAll:method;"
                     i18n:domain="ui" i18n:attributes="title" />         
                  
              </th>

              <!-- Label column row -->
              <tal:block tal:repeat="value here/getLabelValueList">
                <tal:block tal:define="sql        python: value[0];
                                       title      python: value[1];
                                       sort_order python: value[2]">

                  <th tal:condition="sql" class="listbox-table-header-cell"
                      tal:define="bt_class python:  sort_order=='ascending' and 'sort-button sort-button-asc' 
                                                    or sort_order == 'descending' and 'sort-button sort-button-desc' 
                                                    or 'sort-button';
                                  bt_title python: sort_order=='ascending' and 'Ascending Display'
                                                   or sort_order=='descending' and 'Descending Display'
                                                   or 'Sort';
                                  listbox_field_id string:${field_id}.${sql};">                 
                    <!-- Button in normal view -->
                    <button tal:condition="not:is_gadget_mode"
                            type="submit"
                            name="setSelectionQuickSortOrder:method"
                            tal:attributes="title title;
                                            value listbox_field_id;
                                            class bt_class;"
                            i18n:domain="ui" i18n:attributes="title">
                      <span i18n:translate="" i18n:domain="ui" tal:content="title"/>
                    </button>

                    <!-- Button in gadget mode -->
                    <button tal:condition="is_gadget_mode" 
                              tal:define ="params python: {'setSelectionQuickSortOrder:method':listbox_field_id};"
                              type="button"  
                              tal:attributes="title title; 
                                              onclick python: real_context.KnowledgePad_generateAjaxCall(context_url+'/'+form_id,box,dom_id,params);
                                              class bt_class;"
                              i18n:domain="ui" i18n:attributes="title">
                      <span i18n:translate="" i18n:domain="ui" tal:content="title"/>
                    </button>
  
                    <!-- Icon showing sort order -->
                    <img src="images/transparent-image.gif"
                         tal:attributes="alt   bt_title;
                                         title   bt_title;
                                         class bt_class;
                                         src string:${portal_url_string}/images/transparent-image.gif"
                         i18n:domain="ui" i18n:attributes="title;alt" />
                    
                  </th>

                  <th class="listbox-table-header-cell"
                      tal:condition="not: sql" 
                      tal:content="title" 
                      i18n:domain="ui" i18n:translate=""/>

                </tal:block>
              </tal:block>
            </tr>

            <!--Search column input -->
            <tr tal:condition="python: show_search_line or is_report_tree_mode"
                class="listbox-search-line">

            <!--Report Tree -->
            <tal:block tal:condition="is_report_tree_mode">
              <th class="listbox-table-report-tree-selection-cell"
                  colspan="1"
                  tal:attributes="colspan python: show_search_line and 1 or (len(here.getSelectedColumnList()) + show_select_column + show_anchor_column + 1)"
                  tal:define="selection_index here/getSelectionIndex;
                              index python: selection_index is not None and '&amp;selection_index=%s' % selection_index or '';
                              is_report_opened python: int(not here.getSelection().isReportOpened());
                              requested_selection_name here/getRequestedSelectionName;
                              url here/getUrl;
                              report_depth python: selection.getParams().get('report_depth', request.get('report_depth', 0))">
                <tal:block tal:repeat="i python: range(0, 6)">&nbsp;
                  <a href="?selection_name=default&amp;selection_index=0&amp;report_depth:int=0"
                     tal:attributes="href string:${url}?selection_name=${requested_selection_name}${index}&amp;report_depth:int=${i};
                                     class python: test(i==report_depth, 'selected', '');"
                     tal:content="i"/>
                </tal:block>&nbsp;-&nbsp;
                <a 
                  href="?selection_name=default&amp;selection_index=0&amp;is_report_opened:int=0"
                  tal:attributes="href string:${url}?selection_name=${requested_selection_name}${index}&amp;is_report_opened:int=${is_report_opened}"
                  tal:content="python: is_report_opened and 'Show' or 'Hide'"
                  i18n:domain="ui" i18n:translate="">Show</a>
              </th>
            </tal:block>

            <!--Anchor cell -->
            <th class="listbox-table-anchor-cell" tal:condition="show_anchor_column">&nbsp;</th>

            <!--Select cell -->
            <th tal:condition="show_select_column"
                class="listbox-table-select-cell">
              <input class="listbox-select-action" type="image"
                    title="Action" alt="Action" name="Base_doSelect:method"
                    tal:attributes="class string:${field_id}-select-action;
                                    src string:${portal_url_string}/images/exec16.png"
                    i18n:domain="ui" i18n:attributes="title" />
            </th>

            <!-- Real search columns headers -->
            <th class="listbox-table-filter-cell"
                tal:condition="show_search_line"
                tal:repeat="value here/getSearchValueList">
              <tal:block tal:define="alias        python: value[0];
                                     param        python: value[1];
                                     search_field python: value[2]"
                         tal:condition="alias">
                <!-- Render search field -->
                <tal:block tal:condition="python: search_field is not None"
                           tal:replace="structure python: search_field.render(value=param, key=alias)"/>

                <tal:block tal:condition="python: search_field is None">
                  <input tal:condition="python: not is_gadget_mode" 
                         size="5"
                         type="text" 
                         tal:attributes="name string:${field_id}_${alias}; 
                                         value param"
                         onkeypress="submitFormOnEnter(event, this.form, 'Base_doSelect');"/>
                  <!-- Search for gadget mode -->
                  <input tal:condition="python: is_gadget_mode" 
                         tal:define ="params python: {alias:'this.value'};"
                         size="8"
                         type="text" 
                         tal:attributes='value python: selection.getParams().get(alias,"");
                                         onkeypress python:"if(event.keyCode==13){" + real_context.KnowledgePad_generateAjaxCall(context_url+"/"+form_id,box,dom_id,params).replace("\"this.value\"","this.value")+ "return false;;}"'/>
                </tal:block>
              </tal:block>
            </th>
          </tr>
        </thead>

        <!-- Stats -->
        <tfoot tal:condition="python:here.showStat() and not hide_rows_on_no_search_criterion">
            
          <tr class="listbox_stat_line"
              tal:attributes="class string:${field_id}_stat_line listbox-stat-line">
            <td tal:condition="is_report_tree_mode" >&nbsp;</td>
            <td class="listbox-table-anchor-cell" tal:condition="show_anchor_column">&nbsp;</td>
            <td class="listbox-table-select-cell" tal:condition="show_select_column">&nbsp;</td>
            <tal:block tal:repeat="value here/getStatValueList">
            <td align="left"
               class="listbox-table-data-cell"
               tal:define="original_value python: value[0]; processed_value python: value[1]"
               tal:content="structure processed_value" />
            </tal:block>
          </tr>
        </tfoot>

        <tbody>

          <!-- Render listbox data-->
          <tal:block tal:condition="line_list"
                     tal:define="checked_uid_set here/getCheckedUidSet">
            <tr tal:repeat="line line_list"  
                tal:attributes="                
                  class python: line.getRowCSSClassName() or '%s %s' %('%s-data-line-%s' %(field_id, repeat['line'].index) ,test(repeat['line'].index % 2, 'DataB', 'DataA'));">
              
              <tal:block tal:define="render_result line/render">

                  <!--Report tree column -->
                  <td tal:condition="is_report_tree_mode"
                      class="listbox-table-report-tree-selection-cell"                   
                      tal:define="section_name python: line.getDomainTitle()">
                    <a  tal:condition="section_name"
                        tal:define="method_id python: line.isOpen() and 'foldReport' or 'unfoldReport'"
                        tal:attributes="href string:${method_id}?report_url=${line/getDomainUrl}&amp;form_id=${form_id}&amp;list_selection_name=${selection_name};
                                        class python:test(line.isOpen(), 'tree-open', 'tree-closed');
                                        style python:'white-space: nowrap;; margin-left: %spx' % (line.getDepth() * 15)"
                        tal:content="section_name"/>
                  </td>

                  <!--Anchor cell -->
                  <td tal:condition="show_anchor_column"
                      class="listbox-table-anchor-cell">
                    <!--Use [0][4] ? :(-->
                    <a href="#" tal:attributes="href python:render_result[0][4]">
                      <img src="document_icon.gif" alt="document" 
                           tal:attributes="src string:${portal_url_string}/images/line_clickable.png" />
                    </a>
                  </td>
                              
                  <!--Select cell -->
                  <td tal:condition="show_select_column"
                      class="listbox-table-select-cell">
                    <input tal:condition="python: not line.isSummary()"
                          type="radio" id="listbox_cb_1" name="uids:list"
                          tal:attributes="checked python: line.getUid() in checked_uid_set;
                                          value line/getUid;
                                          id string:${field_id}_cb_${line/getUid}" />         
                  </td>

                  <!-- Data cells -->
                  <tal:block tal:repeat="value render_result">
                    <td class="listbox-table-data-cell"
                        tal:define="html  python: value[0];">
                        <input tal:condition="not: repeat/value/index"
                               type="hidden" value="1" name="listbox_uid:list"
                               tal:attributes="value python: line.getUid() or '';
                                               name string:${field_prefix}${field_id}_uid:list" />
                      <tal:block tal:replace="structure html"/>
                    </td>
                  </tal:block>
              </tal:block>
            </tr>
            </tal:block>

            <!-- Hide row on no search criterion-->
            <tr tal:condition="hide_rows_on_no_search_criterion"
                class="listbox_missing_search_criterion">
              <td tal:attributes="colspan python: len(here.getSearchValueList()) + 1">
                <span i18n:translate="" i18n:domain="ui">
                  To display actual content, please fill in one or more search criterion.
                </span>
              </td>
            </tr>
            
            <!-- No results. -->
            <tr tal:condition="python: total_line == 0 and not hide_rows_on_no_search_criterion">
              <td tal:attributes="colspan python: len(here.getSearchValueList()) + 1"
                  class="listbox-table-no-result-row">
                <span i18n:translate="" i18n:domain="ui">
                  No result.
                </span>
              </td>
            </tr>
          </tbody>

      </table>
    </div>

    <div class="listbox-footer">
      
      <!-- List tree mode choice -->
      <div class="listbox-footer-box"
           tal:condition="python: show_listbox_tree_mode_selection and is_web_mode">
        <tal:block metal:use-macro="container/ListBox_asHTMLLibrary/macros/listbox_tree_mode_selection"/>
      </div>
 
      <!-- List style display mode -->
      <div class="listbox-footer-box"
           tal:condition="python: show_list_style_selection and is_web_mode">
        <tal:block metal:use-macro="container/ListBox_asHTMLLibrary/macros/listbox_display_style_selection"/>
      </div>

      <!-- Full text search -->
      <div class="listbox-footer-box"
           tal:condition="show_global_search">
        <tal:block metal:use-macro="container/ListBox_asHTMLLibrary/macros/listbox_global_search"/>
      </div>

      <!--Page navigation in web mode floating in right (slider) or whole width (text) -->
      <div class="listbox-footer-box"
           tal:condition="python: need_pagination and is_web_mode"
           tal:attributes="style python: test(is_slider_mode, 'float:right', 'width:100%')">
        <tal:block metal:use-macro="container/ListBox_asHTMLLibrary/macros/listbox_navigation"/>
      </div>

    </div>
  </div>
</div>

</tal:block>