User:Alex Smotrov/mw/sortable

Possible improvements to Sortable Tables Javascript code used by MediaWiki.


 * The code is in wikibits.js in SVN


 * Original code: sortable_us.js from http://www.joostdevalk.nl/code/sortable-table/

SharkD's changes can be found here (note: the diffs may no longer be up-to-date):
 * User:SharkD/Sandbox/wikibits_1
 * User:SharkD/Sandbox/wikibits_2
 * User:SharkD/Sandbox/wikibits_3
 * User:SharkD/Sandbox/wikibits_4 - (Note: there are several "disabled" bits of code (formatting of alternate rows, detection of THEAD elements, auto-sorting of fractions/ratios) that can be removed if it is determined they will never be used.)
 * Diff: Table sorting section
 * Diff: Entire file

Fix: remove alternate rows code
This code marks table rows with classes "odd" and "even":
 * It is apparently not used by anyone
 * It executes on the page load, slowing loading time

The code in question is:
 * and two calls to this function
 * and two calls to this function
 * and two calls to this function

Comments 1
it could be used by introducing another class into site CSS, something like
 * I would also prefer if it were enabled instead of removed. The fact that people don't know about it is because it isn't advertised. Infobox VG for instance could feasably switch from using templates to using JavaScript for alternate row coloring. SharkD (talk) 00:50, 18 August 2008 (UTC)

Simplification: remove THEAD check
As MediaWiki does not support THEAD, nor through wiki-syntax, neither as HTML tag.

can be replaced with

and the line

in  can be replaced with

Comments 2
The THEAD element may make an appearance in the future, so a link to some record of this code block's former existence would at least be warranted. SharkD (talk) 04:11, 28 August 2008 (UTC)
 * Also, in theory, a table could be constructed purely from HTML elements, and thus possibly have a THEAD element. SharkD (talk) 04:27, 28 August 2008 (UTC)
 * Nevermind. The THEAD is not supported at all, even if the table is constructed using HTML. SharkD (talk) 07:08, 28 August 2008 (UTC)

Simplification: month conversion
can be replaced with

Comments 3
In either case, it is not clear to me what format the input dates take. I.e., what do the input dates look like? SharkD (talk) 04:14, 28 August 2008 (UTC)
 * Nevermind. I'll have to take a closer look at those regexps. SharkD (talk) 04:26, 28 August 2008 (UTC)

It might speed things up if only the ISO 8601 YYYY-MM-DD format were supported, leaving it up to editors to convert dates to this format (either visibly, or invisibly using a template such as dts). SharkD (talk) 07:43, 28 August 2008 (UTC)

This line: can be removed from your proposal, as the condition will never be met. SharkD (talk) 19:02, 29 August 2008 (UTC)

I started a bugzilla report on the locale-specific issues. SharkD (talk) 03:30, 1 September 2008 (UTC)

Proposal: input date format localization
Sortable Tables code determines the sorting mode by analyzing the top non-empty cell. The first RegExp used for possible dates is

Proposal: accept arbitrary months names, something like

and then introduce another parameter ts_months_names, which is set in local Common.js and is equivalent to English "janfebmaraprmayjunjulaugsepoctnovdec"

Then the function  (see above section) is modified to use  ts_months_names if month name was not found in the English string of month names.

Comments 4
Is the above supposed to replace the latter two date regexprs as well, or just the first? Also, what if the day value is only a single digit long? SharkD (talk) 04:40, 28 August 2008 (UTC)
 * Also, is this change supposed to make the script language-agnostic? What if a month name in another language has more than 12 characters or has spaces in it (I have no idea if this will actually ever happen)? SharkD (talk) 05:07, 28 August 2008 (UTC)
 * The Wiktionary article on "January" lists some translations that use spaces or other weird characters, but there aren't many of them. A few of them are longer than 12 characters, too. SharkD (talk) 06:05, 28 August 2008 (UTC)

The  in   matches any character. Is this a good idea? Doesn't that make searching for the other characters redundant? Is it a mistake, and is it supposed to be  instead? SharkD (talk) 05:55, 28 August 2008 (UTC)
 * Nevermind. A period in square brackets doesn't match any character as it normally does--it only matches other periods. SharkD (talk) 06:56, 28 August 2008 (UTC)

Proposal: comma/dot localization
The JavaScript hack which can be found in de:MediaWiki:Common.js or ru:MediaWiki:Common.js, should be replaced with another configuration variable set in local Common.js.

Of course, if MediaWiki can automatically set this parameter depending on wgContentLanguage, this would be even better (this applies to the previous proposal as well).

Comments 5
I'm not sure the above will work. For instance, if the number has decimal values, removing the period will turn them into integers. I suggest the following:

Regardless if using  is the wrong way of going about doing it, some fix must be put into place to detect and handle this scenario. SharkD (talk) 13:36, 28 August 2008 (UTC)

Change variables in statements so that the DOM is not traversed each time the block is looped
Change:

To:

Or (if the above is not possible):

Use a local variable instead of global, and use conditions to eliminate cases
Change:

To:

Store DOM nodes in variables so that the DOM is not traversed each time
Change:

To:

Change:

To:

Bug: the first (header) row is included in the row count, so only enable sorting when there are three or more rows
Change:

To:

Arrange conditions/switches in order of greatest to least likelihood (SharkD (talk))
Change:

To:

Proposal: "sortreverse" header cell class for default reverse sorting of a column (SharkD (talk))
This proposal is meant to make it possible that a certain column should be sorted in reverse order by default. This works similarly to the "unsortable" class, in that the class is added to the column header, not the table.

Change:

To:

Change:

To:

Change:

To:

Remove all brackets from conditions with only a single line of code
Change:

To:

Use literal definitions for arrays
Change:

To:

Change:

To:

Remove all semicolons
Change:

To:

Proposal: Put clickable icon in its own cell. (SharkD (talk))
The reason is so that it aligns more nicely and doesn't break onto new lines like inline elements tend to do. This involves wrapping each header cell within its own table. The existing header text is placed in the first cell of the new table; the clickable icon is placed in the second cell. All style/class attributes are copied to the new tables/cells, except for the "sortable" class which should not be copied.

Change the  function to this:

Change:

To:

Fix: include Yen in currency search (SharkD (talk))
Change:

To:

Proposal: sort by fraction or ratio (SharkD (talk))
I'm not as enthusiastic about this one. There's no real sense in beginning to evaluate expressions and then not continuing to handle all types of them. Anyway, the change amounts to adding the following:

And:

Note that the above code interprets the colon in addition to the forward slash as signifying a fraction or ratio. This may present a problem with certain dates or times.

Proposal: Increase the number of detected date formats; split them into different functions. (SharkD (talk))
In its current status, the script uses a lot of code to detect only a few date formats. This change is meant to open the sorting function up to more date formats as well as split them the different formate into separate functions, thereby reducing the number of conditional statements required.

Change:

To:

Change:

To:

Change:

To:

Proposal: change "sortable" from classname to attribute; add column attributes to force sorting function (SharkD (talk))
This proposal is to change the "sortable" table class from a class to an attribute and add a "sortorder" attribute to column headers. The first part would involve the following:

Change: class="sortable"

To: sortable="true"

The second part would allow users to force the use of a particular sorting function for a column. The "unsortable" class could also be merged with this item. I.e.:

Change:

To something like this (a rough sketch):