# HG changeset patch # User Paul Boddie # Date 1329673722 -3600 # Node ID a285ae825ba32a1dcb4c739a66a737c259e34bc5 # Parent a5aa61c0be15fac1583756b0195d53b1e1a7283b Changed the sort controls to be a list of columns, with the selected column appearing as an insertion marker alongside the list where the column could be inserted to change the criteria, and with the column appearing as a link in the list if already part of the criteria, enabling the column to be removed from the criteria. If appropriate, a link permitting the column to be added or moved to the end of the list is also shown. diff -r a5aa61c0be15 -r a285ae825ba3 ImprovedTableParser.py --- a/ImprovedTableParser.py Sun Feb 19 13:38:53 2012 +0100 +++ b/ImprovedTableParser.py Sun Feb 19 18:48:42 2012 +0100 @@ -354,68 +354,113 @@ return 0 -def write_sort_control(columnnumber, write, sort_columns, column_types, columns, table_name, data_start, start=0): +def write_sort_control(request, columnnumber, fmt, write, sort_columns, column_types, columns, table_name, start=0): """ - Write a sort control in its own form which provides a list of sort - descriptions, modifying the 'sort_columns' provided by introducing the given - column in different positions. + Write a sort control in a pop-up element which provides a list of links + corresponding to modified sort criteria. """ - option_html = """\ - -""" + _ = request.getText + + write(fmt.div(1, css_class="sortcolumns")) # Start with the existing criteria without this column being involved. - current_sort_columns = [(column + start, suffixes[fn], not ascending and "d" or "") - for (column, fn, ascending) in sort_columns] - revised_sort_columns = [(column + start, suffixes[fn], not ascending and "d" or "") + revised_sort_columns = [(column, fn, ascending) for (column, fn, ascending) in sort_columns if column != columnnumber] - values = [revised_sort_columns] - revised_sort_labels = [columns[column][1].strip() for (column, fn, ascending) in revised_sort_columns] - labels = [revised_sort_labels] + + # Get the specification of this column. + + columnfn, columnascending = column_types.get(columnnumber, (str, True)) + newsortcolumn = columnnumber, columnfn, columnascending + newlabel = columns[columnnumber][1].strip() + + # Show this column in all possible places in the sorting criteria. - # Add this column in all possible places in the sorting criteria. + write(fmt.number_list(1)) + + already_have_this_column = len(sort_columns) != len(revised_sort_columns) + just_had_this_column = False + + for i, (column, fn, ascending) in enumerate(sort_columns): + new_sort_columns = revised_sort_columns[:] + new_sort_columns.insert(i, newsortcolumn) + label = columns[column][1].strip() - i = 0 - while i <= len(revised_sort_columns): - value = revised_sort_columns[:] - label = revised_sort_labels[:] - fn, ascending = column_types.get(columnnumber, (str, True)) - value.insert(i, (columnnumber + start, suffixes[fn], not ascending and "d" or "")) - label.insert(i, columns[columnnumber][1].strip()) - values.append(value) - labels.append(label) - i += 1 + # Pop-up element showing the column inserted before the sort column. + + if column != columnnumber and not just_had_this_column: + write(fmt.listitem(1, css_class="sortcolumn")) + + write(fmt.span(1, css_class="sortcolumn-container")) + write(fmt.span(1, css_class="newsortcolumn")) + write(fmt.text(newlabel)) + write(fmt.span(0)) + write(fmt.span(0)) - # Make the list of options. + # Link for selection of the modified sort criteria. + + write(fmt.url(1, "?%s-sortcolumns=%s" % (table_name, get_sort_column_output(new_sort_columns)))) + write(fmt.text(label)) + write(fmt.url(0)) + + else: + write(fmt.listitem(1)) + + # Either show the column without a link, since the column to be + # inserted is already before the current column. - options_html = [] - for value, label in zip(values, labels): - options_html.append(option_html % { - "value" : ",".join([("%d%s%s" % spec) for spec in value]), - "label" : ", ".join(label), - "selected" : value == current_sort_columns and 'selected="selected"' or "", - }) + if just_had_this_column: + just_had_this_column = False + write(fmt.span(1, css_class="unlinkedcolumn")) + write(fmt.text(label)) + write(fmt.span(0)) + + # Or show the column with a link for its removal. - # Write the form. + else: + just_had_this_column = True + write(fmt.url(1, "?%s-sortcolumns=%s" % (table_name, get_sort_column_output(revised_sort_columns)), + css_class="removecolumn")) + write(fmt.text(label)) + write(fmt.url(0)) + + write(fmt.listitem(0)) + + if not already_have_this_column: + + # Write the sorting criteria with this column at the end. + + new_sort_columns = revised_sort_columns[:] + new_sort_columns.append(newsortcolumn) - d = { - "table_name" : table_name, - "options" : "".join(options_html), - "data_start" : data_start, - } + write(fmt.listitem(1, css_class="sortcolumn", style="list-style-type: none")) + + # Pop-up element showing the column inserted before the sort column. + + write(fmt.span(1, css_class="sortcolumn-container")) + write(fmt.span(1, css_class="newsortcolumn")) + write(fmt.text(newlabel)) + write(fmt.span(0)) + write(fmt.span(0)) - write("""\ -
- - - -
-""" % d) + write(fmt.url(1, "?%s-sortcolumns=%s" % (table_name, get_sort_column_output(new_sort_columns)))) + write(fmt.text(_("..."))) + write(fmt.url(0)) + + write(fmt.listitem(0)) + + write(fmt.number_list(0)) + + write(fmt.div(0)) + +def get_sort_column_output(columns, start=0): + + "Return the output criteria for the given 'columns' indexed from 'start'." + + return ",".join([("%d%s%s" % (column + start, suffixes[fn], not ascending and "d" or "")) + for (column, fn, ascending) in columns]) # Sorting-related tables. @@ -471,21 +516,26 @@ # Write the table. - writing_html = request.page.output_mimetype == "text/html" write = request.write write(fmt.table(1, table_attrs)) for rownumber, (row_attrs, columns) in enumerate(table): write(fmt.table_row(1, row_attrs)) + sortable = sortcolumns and rownumber == data_start - 1 for columnnumber, (column_attrs, column_text) in enumerate(columns): write(fmt.table_cell(1, column_attrs)) + + if sortable: + write(fmt.div(1, css_class="sortablecolumn")) + write(formatText(column_text, request, fmt)) # Add sorting controls, if appropriate. - if writing_html and sortcolumns and rownumber == data_start - 1: - write_sort_control(columnnumber, write, sort_columns, column_types, columns, table_name, data_start) + if sortable: + write_sort_control(request, columnnumber, fmt, write, sort_columns, column_types, columns, table_name) + write(fmt.div(0)) write(fmt.table_cell(0))