Changeset 6336


Ignore:
Timestamp:
Oct 28, 2013, 12:57:45 PM (10 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1774: Independent sort direction for columns when sorting on multiple colums

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/src/docbook/appendix/incompatible.xml

    r6281 r6336  
    4545      that expect the parent chain of biomaterials to always be fully initialized
    4646      without explicitely having told the BASE core to do so.
     47    </para>
     48   
     49    <bridgehead>Tables can have columns with different sort order</bridgehead>
     50    <para>
     51      A new feature has been implemented which allows columns in a table to
     52      have different sort order. This is implemented by allowing '+' or '-'
     53      as a prefix to properties returned by the <methodname>ItemContext.getSortProperty()</methodname>
     54      method. Properties without a prefix still use the global sort order as returned
     55      by <methodname>ItemContext.getSortDirection()</methodname>.
     56    </para>
     57   
     58    <para>
     59      Code that is not aware of the prefixes may fail since '+' and '-' are not
     60      allowed in property names.
    4761    </para>
    4862  </sect1>
  • trunk/src/clients/web/net/sf/basedb/clients/web/taglib/table/ColumnDef.java

    r6190 r6336  
    679679        else
    680680        {
    681           boolean sortOnThis = table.isSortedby(getSortproperty());
    682           String image = sortOnThis ? table.getDirectionImage() : "";
    683           ItemContext.SortDirection direction = sortOnThis ? table.getReverseDirection() : table.getDirection();
     681          // Current sort direction
     682          ItemContext.SortDirection sortDirection = table.getSortDirection(getSortproperty());
     683          String image = table.getDirectionImage(sortDirection);
     684         
     685          // Next sort direction if clicking on this column
     686          ItemContext.SortDirection nextDirection =
     687              sortDirection == null ? table.getDirection() : sortDirection.getReverseDirection();
     688
    684689          String theTooltip = getTooltip() != null ?
    685690            getTooltip() : "Sort by "+getTitle() + "; hold CTRL, ALT or SHIFT to sort by multiple columns";
     
    687692          sb.append(" id=\"").append(idPrefix).append(".sort\"");
    688693          sb.append(" data-sort-property=\"").append(HTML.encodeTags(getSortproperty())).append("\"");
    689           sb.append(" data-sort-direction=\"").append(direction.name()).append("\"");
     694          sb.append(" data-sort-direction=\"").append(nextDirection.name()).append("\"");
    690695          sb.append(" title=\"").append(theTooltip).append("\">");
    691696          sb.append(getTitle()).append("</span>").append(image);
  • trunk/src/clients/web/net/sf/basedb/clients/web/taglib/table/Table.java

    r6190 r6336  
    229229  */
    230230  private String sortby = null;
    231   private Set<String> sortbyMultiple = null;
     231  private Map<String, ItemContext.SortDirection> sortColumns = null;
    232232 
    233233  private String action = null;
     
    313313  {
    314314    this.sortby = sortby;
    315     if (sortby != null) sortbyMultiple = new HashSet<String>(Arrays.asList(sortby.split(",")));
     315    if (sortby != null)
     316    {
     317      sortColumns = new HashMap<String, ItemContext.SortDirection>();
     318      for (String col : sortby.split(","))
     319      {
     320        if (col.startsWith("-"))
     321        {
     322          sortColumns.put(col.substring(1), ItemContext.SortDirection.DESC);
     323        }
     324        else if (col.startsWith("+"))
     325        {
     326          sortColumns.put(col.substring(1), ItemContext.SortDirection.ASC);
     327        }
     328        else
     329        {
     330          sortColumns.put(col, null); // NOTE! Default direction may not be set yet
     331        }
     332      }
     333    }
    316334  }
    317335  public String getSortby()
     
    467485    return visibleColumns.size();
    468486  }
    469   boolean isSortedby(String sortcolumn)
    470   {
    471     return (sortbyMultiple != null) && sortbyMultiple.contains(sortcolumn);
    472   }
    473   ItemContext.SortDirection getReverseDirection()
    474   {
    475     return sortDirection == ItemContext.SortDirection.ASC ? ItemContext.SortDirection.DESC : ItemContext.SortDirection.ASC;
    476   }
    477   String getDirectionImage()
    478   {
    479     return "<img src=\""+getPage().getRoot()+"images/" +
    480       (sortDirection == ItemContext.SortDirection.ASC ? "sort_asc.png" : "sort_desc.png") +
    481       "\" alt=\"" + (sortDirection == ItemContext.SortDirection.ASC ? "▼" : "▲") + "\">";
     487  /**
     488    Get the sort direction on the given column. If it is not
     489    used for sorting, null is returned.
     490    @since 3.3
     491  */
     492  ItemContext.SortDirection getSortDirection(String colum)
     493  {
     494    ItemContext.SortDirection direction = null;
     495    if (sortColumns != null && sortColumns.containsKey(colum))
     496    {
     497      direction = sortColumns.get(colum);
     498      if (direction == null) direction = getDirection();
     499    }
     500    return direction;
     501  }
     502 
     503  /**
     504    Get sort direction <img> element for the given direction.
     505    Return an empty string if direction = null.
     506    @since 3.3
     507  */
     508  String getDirectionImage(ItemContext.SortDirection direction)
     509  {
     510    String image = "";
     511    if (direction != null)
     512    {
     513      image = "<img src=\""+getPage().getRoot()+"images/" +
     514          (direction == ItemContext.SortDirection.ASC ? "sort_asc.png" : "sort_desc.png") +
     515          "\" alt=\"" + (direction == ItemContext.SortDirection.ASC ? "▼" : "▲") + "\">";
     516    }
     517    return image;
    482518  }
    483519
  • trunk/src/core/net/sf/basedb/core/ItemContext.java

    r6127 r6336  
    382382    Get the name of the property the current listing is sorted by.
    383383    If multiple properties are used for sorting they are separated by a comma.
     384    Each property may also be prefixed by a '+' or '-' to override the
     385    global sort direction.
    384386  */
    385387  public String getSortProperty()
     
    391393    Set the name of the property that the current listing is sorted by.
    392394    If you want to sort by multiple properties, separate them with a
    393     comma.
     395    comma. By default, each property uses the same sort direction ({@link #getSortDirection()},
     396    but this can be overridden by using '+' or '-' as a prefix for
     397    a property.
    394398    This property is stored in the database.
    395399    @param sortProperty The name of the property
     
    434438      String sortProperty = sp[0];
    435439      String alias = null;
     440      SortDirection sortDirection = getSortDirection();
     441     
     442      if (sortProperty.startsWith("+"))
     443      {
     444        sortDirection = SortDirection.ASC;
     445        sortProperty = sortProperty.substring(1);
     446      }
     447      else if (sortProperty.startsWith("-"))
     448      {
     449        sortDirection = SortDirection.DESC;
     450        sortProperty = sortProperty.substring(1);
     451      }
    436452      if (sortProperty.startsWith("$"))
    437453      {
     
    452468      }
    453469      Expression hqlSortby = Hql.property(alias, sortProperty);
    454       sortOrder = (SortDirection.ASC == getSortDirection() ? Orders.asc(hqlSortby) : Orders.desc(hqlSortby));
     470      sortOrder = sortDirection.sortBy(hqlSortby);
    455471    }
    456472    return sortOrder;
     
    11761192      sign it is interpreted as a <code>alias.property</code> value, ignoring
    11771193      the root alias of the query. The sort property may contain multiple properties
    1178       if they are separated by a comma.
     1194      if they are separated by a comma. Each property may also be prefixed by
     1195      a '+' (ASC) or '-' (DESC) to override the global sort direction.
    11791196     
    11801197    <li>{@link #getInclude()} is used to set {@link EntityQuery#include(Include[])}
     
    12161233        if (propertyInspector == null || propertyInspector.evaluate(sortProperty))
    12171234        {
     1235          SortDirection sortDirection = getSortDirection();
     1236         
    12181237          Expression hqlSortby = null;
    12191238          String alias = null;
     1239         
     1240          // Check if sort direction has been overridden
     1241          if (sortProperty.startsWith("-"))
     1242          {
     1243            sortDirection = SortDirection.DESC;
     1244            sortProperty = sortProperty.substring(1);
     1245          }
     1246          else if (sortProperty.startsWith("+"))
     1247          {
     1248            sortDirection = SortDirection.ASC;
     1249            sortProperty = sortProperty.substring(1);
     1250          }
     1251         
    12201252          // Extract the 'alias' part if there is one
    12211253          if (sortProperty.startsWith("$"))
     
    12611293          if (hqlSortby != null)
    12621294          {
    1263             query.order(SortDirection.ASC == getSortDirection() ? Orders.asc(hqlSortby) : Orders.desc(hqlSortby));
     1295            query.order(sortDirection.sortBy(hqlSortby));
    12641296          }
    12651297        }
     
    14771509        if (propertyInspector == null || propertyInspector.evaluate(sortProperty))
    14781510        {
     1511          SortDirection sortDirection = getSortDirection();
     1512         
     1513          // Check if sort direction has been overridden
     1514          if (sortProperty.startsWith("-"))
     1515          {
     1516            sortDirection = SortDirection.DESC;
     1517            sortProperty = sortProperty.substring(1);
     1518          }
     1519          else if (sortProperty.startsWith("+"))
     1520          {
     1521            sortDirection = SortDirection.ASC;
     1522            sortProperty = sortProperty.substring(1);
     1523          }
     1524
    14791525          Expression sortby = getDynamicExpression(dc, sortProperty);
    14801526          if (sortby != null)
    14811527          {
    1482             query.order(SortDirection.ASC == getSortDirection() ? Orders.asc(sortby) : Orders.desc(sortby));
     1528            query.order(sortDirection.sortBy(sortby));
    14831529            if (selectionList == null || !selectionList.contains(sortProperty))
    14841530            {
     
    17081754        return Orders.asc(e);
    17091755      }
     1756      @Override
     1757      public SortDirection getReverseDirection()
     1758      {
     1759        return DESC;
     1760      }
    17101761    },
    17111762   
     
    17191770      {
    17201771        return Orders.desc(e);
     1772      }
     1773      @Override
     1774      public SortDirection getReverseDirection()
     1775      {
     1776        return ASC;
    17211777      }
    17221778    };
     
    17791835    public abstract Order sortBy(Expression e);
    17801836   
     1837    /**
     1838      Get the reverse sort direction.
     1839      @since 3.3
     1840    */
     1841    public abstract SortDirection getReverseDirection();
     1842   
    17811843    @Override
    17821844    public String toString()
  • trunk/www/include/scripts/table.js

    r6315 r6336  
    151151    var tableId = Data.get(column, 'table-id');
    152152    var frm = document.forms[tableId];
    153    
    154     if (frm.sortby.value != sortProperty || frm.direction.value != sortDirection)
     153    var mainDirection = frm.direction.value;
     154    if (frm.sortby.value != sortProperty || mainDirection != sortDirection)
    155155    {
    156156      if (event.altKey || event.ctrlKey || event.shiftKey)
    157157      {
    158158        // Append the current sort property to the list of already sorted columns
    159         // First, if the sort property is already used we remove it
     159        // If the sort property is already used we keep it, but use the new sort direction
    160160        var sortArray = frm.sortby.value.split(',');
    161161        var index = sortArray.indexOf(sortProperty);
    162         if (index >= 0) sortArray.splice(index, 1);
     162        // Check for '+' and '-' prefix if the sort order is different from main direction
     163        if (index == -1) index = sortArray.indexOf('+'+sortProperty);
     164        if (index == -1) index = sortArray.indexOf('-'+sortProperty);
     165        // Currently not sorted, insert at end
     166        if (index == -1) index = sortArray.length;
    163167        // Add the sort property as the last property
    164         sortArray[sortArray.length] = sortProperty;
     168        if (sortDirection != mainDirection)
     169        {
     170          sortProperty = (sortDirection == 'ASC' ? '+' : '-') + sortProperty;
     171          sortDirection = mainDirection;
     172        }
     173        sortArray[index] = sortProperty;
    165174        sortProperty = sortArray.join(',');
    166175      }
Note: See TracChangeset for help on using the changeset viewer.