[%# The contents of this file are subject to the Mozilla Public
  # License Version 1.1 (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.mozilla.org/MPL/
  #
  # Software distributed under the License is distributed on an "AS
  # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  # implied. See the License for the specific language governing
  # rights and limitations under the License.
  #
  # The Original Code is the Bugzilla Bug Tracking System.
  #
  # The Initial Developer of the Original Code is Netscape Communications
  # Corporation. Portions created by Netscape are
  # Copyright (C) 1998 Netscape Communications Corporation. All
  # Rights Reserved.
  #
  # Contributor(s): Myk Melez <myk@mozilla.org>
  #                 Jesse Clark <jjclark1982@gmail.com>
  #%]

[%############################################################################%]
[%# Initialization                                                           #%]
[%############################################################################%]

[%# Don't display the table or do any processing if there are no bugs
  # to display %]
[% RETURN IF !bugs.size %]

[%# Columns whose titles or values should be abbreviated to make the list
  # more compact.  For columns whose titles should be abbreviated,
  # the shortened title is included.  For columns whose values should be
  # abbreviated, a maximum length is provided along with the ellipsis that
  # should be added to an abbreviated value, if any.
  # wrap is set if a column's contents should be allowed to be word-wrapped
  # by the browser.
  #%]

[% field_descs.short_short_desc     = field_descs.short_desc  %]
[% field_descs.assigned_to_realname = field_descs.assigned_to %]
[% field_descs.reporter_realname    = field_descs.reporter    %]
[% field_descs.qa_contact_realname  = field_descs.qa_contact  %]
[% PROCESS bug/time.html.tmpl %]

[% Hook.process("before_table") %]

[%############################################################################%]
[%# Table Header                                                             #%]
[%############################################################################%]

[% tableheader = BLOCK %]
<table class="bz_buglist sortable" cellspacing="0" cellpadding="4" width="100%">
   [% IF template_format != "simple" %]
   <tr>
      [% i = lsearch(order_columns, 'bug_id') %]
      <td><input type="button" value="Apply" id="usr_filter_submit" /></td>
      [% FOREACH id = displaycolumns %]
        [% column = columns.$id %]
        [% PROCESS columnheaderfilter key=loop.count() %]
      [% END %]
  </tr>
  [% END %]
  <thead>
    <tr class="bz_buglist_header bz_first_buglist_header">
      [% IF dotweak %]
      <th>&nbsp;</th>
      [% END %]
      [% i = lsearch(order_columns, 'bug_id') %]
      <th colspan="[% splitheader ? 2 : 1 %]" class="first-child0 sortable_column_0 sorttable_numeric
        [%- IF i>=0 %] sorted_[% i %] sorttable_sorted[% "_reverse" IF order_dir.$i %][% END %]">
        <a href="buglist.cgi?
                  [% urlquerypart FILTER html %]&amp;order=
                  [% PROCESS new_order id='bug_id' %]
                  [%-#%]&amp;query_based_on=
                  [% defaultsavename OR searchname FILTER url_quote %]">ID
        </a>
      </th>

      [% IF splitheader %]

        [% FOREACH id = displaycolumns %]
          [% NEXT UNLESS loop.count() % 2 == 0 %]
          [% column = columns.$id %]
          [% PROCESS columnheader key=loop.count() %]
        [% END %]

        [%# vitalif@mail.ru --- A "hook" into the bug row for building custom views based on table view %]
        [% IF bug_header_callback %][% bug_header_callback FILTER process %][% END %]

        </tr><tr class="bz_buglist_header">
        [% IF dotweak %]
          <th>&nbsp;</th>
        [% END %]
        <th>&nbsp;</th>

        [% FOREACH id = displaycolumns %]
          [% NEXT IF loop.count() % 2 == 0 %]
          [% column = columns.$id %]
          [% PROCESS columnheader key=loop.count() %]
        [% END %]

        [%# vitalif@mail.ru --- A "hook" into the bug row for building custom views based on table view %]
        [% IF bug_header_callback %][% bug_header_callback FILTER process %][% END %]

      [% ELSE %]

        [% FOREACH id = displaycolumns %]
          [% column = columns.$id %]
          [% PROCESS columnheader key=loop.count() %]
        [% END %]

        [%# vitalif@mail.ru --- A "hook" into the bug row for building custom views based on table view %]
        [% IF bug_header_callback %][% bug_header_callback FILTER process %][% END %]

      [% END %]

    </tr>
  </thead>
[% END %]

[% BLOCK columnheader %]
  [% i = lsearch(order_columns, id) %]
  [% type = bug_fields.$id.type %]
  <th colspan="[% splitheader ? 2 : 1 %]" class="sortable_column_[% key %]
    [%- IF i>=0 || type == constants.FIELD_TYPE_NUMERIC || type == constants.FIELD_TYPE_BUG_ID %] sorttable_numeric[% END %]
    [%- IF i>=0 %] sorted_[% i %] sorttable_sorted[% "_reverse" IF order_dir.$i %][% END %]">
    <a href="buglist.cgi?[% urlquerypart FILTER html %]&amp;order=
      [% PROCESS new_order %]
      [%-#%]&amp;query_based_on=
      [% defaultsavename OR searchname FILTER url_quote %]">
        [%- abbrev.$id.title || field_descs.$id || column.title -%]
    </a>
  </th>
[% END %]


[% BLOCK columnheaderfilter %]
  [% i = lsearch(order_columns, id) %]
  [% idd = id %]
  [% type = bug_fields.$id.type %]
  [% IF id == "bug_severity" || id == "priority" || id == "bug_status" || id == "resolution" %]
  <td>
    <input type="text" value="[% list_params.$id.join(",") %]" id="usr_filter_[% id %]"
        style="width: 1px; height: 1px; border: 0px; margin-top: -5px; display: block;" />

        <script type="text/javascript">
        var emptyOptions[% id %] = [];
        var filter_[% id %] = null;
        addListener(window, 'load', function() {
            filter_[% id %] = new SimpleAutocomplete("usr_filter_[% id %]",
                function(h) { fieldBuglistAutocomplete(h, "[% id %]", emptyOptions[% id %]); },
                { emptyText: 'No keywords found', multipleDelimiter: "," }
            );
        });
        </script>
    <a href="javascript:void(0)" onclick="filter_[% id %].show(); return false;">Filter</a>
  </td>
  [% ELSE %]
  <td>&nbsp;</td>
  [% END %]
[% END %]


[% BLOCK new_order %]
  [% desc = '' %]
  [% IF (om = order.match("\\b$id( DESC)?")) %]
    [% desc = ' DESC' IF NOT om.0 %]
  [% END %]
  [% id _ desc FILTER url_quote %]
  [% IF id != 'bug_id' AND order %]
    [% ',' _ order.remove("\\b$id( DESC)?(,\\s*|\$)") FILTER url_quote %]
  [% END %]
[% END %]

[%############################################################################%]
[%# Bug Table                                                                #%]
[%############################################################################%]

[% tableheader %]

<tbody class="sorttable_body">
[% FOREACH bug = bugs %]
  [% count = loop.count() %]

  <tr class="bz_bugitem
             bz_[% bug.bug_severity FILTER css_class_quote -%]
             bz_[% bug.priority FILTER css_class_quote -%]
             bz_[% bug.bug_status FILTER css_class_quote -%]
             [%+ "bz_$bug.resolution" FILTER css_class_quote IF bug.resolution -%]
             [%+ "bz_secure" IF bug.secure_mode -%]
             [%+ "bz_secure_mode_$bug.secure_mode" FILTER css_class_quote IF bug.secure_mode -%]
             [%+ count % 2 == 1 ? "bz_row_odd" : "bz_row_even" -%]
             ">

    [% IF dotweak %]
    <td class="bz_checkbox_column">
      <input type="checkbox" id="id_[% bug.bug_id %]" name="id_[% bug.bug_id %]" />
    </td>
    [% END %]

    <td class="first-child bz_id_column">
      <a name="b[% bug.bug_id %]"
         href="show_bug.cgi?id=[% bug.bug_id %]">[% bug.bug_id %]</a>
      <span style="display: none">[%+ '[SEC]' IF bug.secure_mode %]</span>
    </td>

    [% FOREACH column = displaycolumns %]
    <td class="bz_[% column FILTER css_class_quote %]_column bz_f[% bug_fields.$column.type %]
        [%- IF COLUMNS.$column.subid %] bz_[% COLUMNS.$column.subid | css_class_quote %]_column[% END %]"
        [%- IF abbrev.$column.maxlength %] title="[%- bug.$column | html %]"[% END %]>
      [% IF abbrev.$column.format_value %]
        [%- bug.$column FILTER format(abbrev.$column.format_value) FILTER html -%]
      [% ELSIF bug_fields.$column.type == constants.FIELD_TYPE_BUG_ID %]
        [% bug.$column FILTER bug_link(bug.$column) IF bug.$column %]
      [% ELSIF bug_fields.$column.type == constants.FIELD_TYPE_EXTURL %]
        [% IF bug.$column %]
          [% SET url_quoted = bug.$column | url_quote_ns %]
          <a href="[% bug_fields.$column.url.replace('\$1', url_quoted) %]">[% bug.$column | html %]</a>
        [% END %]
      [% ELSIF column == 'work_time' ||
               column == 'remaining_time' ||
               column == 'estimated_time' ||
               column == 'interval_time' %]
        [% PROCESS formattimeunit time_unit=bug.$column %]
      [%# Display the login name of the user if their real name is empty. %]
      [% ELSIF column.match('_realname$') %]
        [% SET login_column = column.remove('_realname$') %]
        [% IF bug.$column == '' %]
          [% bug.${login_column}.truncate(
               abbrev.$column.maxlength,
               abbrev.$column.ellipsis
             ) FILTER html %]
        [% ELSE %]
          <a href="buglist.cgi?quicksearch=[% 'OPEN ' _ login_column _ ':' _ bug.$login_column | url_quote %]">
            [%- bug.$column.truncate(abbrev.$column.maxlength, abbrev.$column.ellipsis) FILTER html -%]
          </a>
        [% END %]
      [% ELSIF column == 'assigned_to_short' || column == 'qa_contact_short' || column == 'reporter_short' %]
        [% SET login_column = column.remove('_short$') %]
        <a href="buglist.cgi?quicksearch=[% 'OPEN ' _ login_column _ ':' _ bug.$login_column | url_quote %]">
          [%- bug.$column.truncate(abbrev.$column.maxlength, abbrev.$column.ellipsis) FILTER html -%]
        </a>
      [% ELSIF column == 'product' %]
        <a href="buglist.cgi?quicksearch=[% "OPEN product:" _ bug.$column | url_quote %]">
          [%- bug.$column.truncate(abbrev.$column.maxlength, abbrev.$column.ellipsis) FILTER html -%]
        </a>
      [% ELSIF column == 'assigned_to' || column == 'qa_contact' || column == 'reporter' %]
        <a href="buglist.cgi?quicksearch=[% 'OPEN ' _ column _ ':' _ bug.$column | url_quote %]">
          [%- bug.$column.truncate(abbrev.$column.maxlength, abbrev.$column.ellipsis) FILTER html -%]
        </a>
      [% ELSIF column == 'blocked' OR column == 'dependson' %]
        [% bug.$column | bug_list_link %]
      [% ELSIF column == 'short_desc' %]
        <a href="show_bug.cgi?id=[% bug.bug_id %]">
          [%- bug.$column.truncate(abbrev.$column.maxlength, abbrev.$column.ellipsis) FILTER html -%]
        </a>
      [% ELSIF column == 'bug_file_loc' && bug.$column.search('http:') %]
        <a href="[%- bug.$column.truncate(abbrev.$column.maxlength, abbrev.$column.ellipsis) FILTER html -%]" target="_blank">
        [%- bug.$column.truncate(abbrev.$column.maxlength, abbrev.$column.ellipsis) FILTER html -%]
        </a>
      [% ELSE %]
        [%- bug.$column.truncate(abbrev.$column.maxlength, abbrev.$column.ellipsis) FILTER html -%]
      [% END %]
    </td>
    [% END %]

    [%# vitalif@mail.ru --- A "hook" into the bug row for building custom views based on table view %]
    [% IF bug_table_callback %][% bug_table_callback FILTER process %][% END %]

  </tr>

  [% IF loop.last() && time_info.time_present == 1 %]
    [% PROCESS time_summary_line %]
  [% END %]

[% END %]
</tbody>
</table>

[% IF template_format != "simple" %]
<script type="text/javascript">
    addListener('usr_filter_submit', 'click', function (e) {
        var inputs = [ "bug_severity", "priority", "bug_status", "resolution" ];
        [% editqueryname = searchname OR defaultsavename OR '' %]
        var href = "buglist.cgi?[% urlquerypart %][% IF editqueryname != '' %]&known_name=[% editqueryname | url_quote %][% END %]&order=[% order %]";
        for (var i = 0; i < inputs.length; i++)
        {
            var field_el = document.getElementById("usr_filter_" + inputs[i]);
            if (field_el)
            {
                var removeOldParams = new RegExp("(^|/?|&)" + inputs[i] + "=[^&]*", 'ig');
                href = href.replace(removeOldParams, "");
                var new_values = field_el.value.split(",");
                for(var j = 0; j < new_values.length; j++)
                {
                    href += "&" + encodeURIComponent(inputs[i].trim()) + "=" + encodeURIComponent(new_values[j].trim());
                }
            }
        }
        window.location.href = href;
    });
</script>
[% END %]

[% BLOCK time_summary_line %]
  <tr class="bz_time_summary_line">
    [% columns_to_span = 1 %]  [%# bugID %]
    [% IF dotweak %]
      [% columns_to_span = columns_to_span + 1 %]
    [% END %]
    [% FOREACH column = displaycolumns %]
      [% IF column == 'work_time' ||
            column == 'remaining_time' ||
            column == 'estimated_time' ||
            column == 'percentage_complete' ||
            column == 'interval_time' %]
        [% IF columns_to_span > 0 %]
          <td class="bz_total bz_total_label" colspan="
              [%- columns_to_span FILTER html %]"><b>Totals</b></td>
          [% columns_to_span = 0 %]
        [% END %]
        [% IF column == 'percentage_complete' %]
          <td class="bz_total">[% time_info.percentage_complete
              FILTER format(abbrev.$column.format_value) FILTER html %]</td>
        [% ELSE %]
          <td class="bz_total">
            [%- PROCESS formattimeunit time_unit=time_info.$column %]</td>
        [% END %]
      [% ELSIF columns_to_span == 0 %] [%# A column following the first total %]
        <td class="bz_total">&nbsp;</td>
      [% ELSE %] [%# We haven't gotten to a time column yet, keep computing span %]
        [% columns_to_span = columns_to_span + 1 %]
      [% END %]
    [% END %]

    [%# vitalif@mail.ru --- A "hook" into the bug row for building custom views based on table view %]
    [% IF bug_time_summary_line_callback %][% bug_time_summary_line_callback FILTER process %][% END %]
  </tr>
[% END %]
