User:PerfektesChaos/js/tableXpander

From Wikipedia, the free encyclopedia
|}===={|

JavaScript gadget – calculations and auxilary functions are offered for data tables.

Purpose[edit]

Within data tables in wiki pages there is sometimes a need to

  • calculate sum of all figures in a column or row
  • calculate average, respectively
  • check a ranking or retrieve the number of cells in a column or row
  • retrieve contents of a column or row

Usage[edit]

  • If your project has registered this as a gadget, just activate on your Preferences page.
  • Otherwise there are two approaches:
    • Provide a toolbox link and run the full tool on demand.
    • Start the entire tool for the current page.

Just a trigger[edit]

Include the following line into your common.js, global.js etc.:

mw.loader.load( "https://en.wikipedia.org/w/index.php?title=User:PerfektesChaos/js/tableXpander/scout/r.js&action=raw&maxage=86400&ctype=text/javascript" );

In the (desktop) toolbox a |}===={| will appear on all pages, and you can start the full gadget on demand.

Full tool[edit]

Run the following code wherever you want:

mw.loader.load( "https://en.wikipedia.org/w/index.php?title=User:PerfektesChaos/js/tableXpander/r.js&action=raw&maxage=86400&ctype=text/javascript" );

Non-MediaWiki pages[edit]

Basically, the (full) tool may be executed with any MediaWiki version. However, MediaWiki is not really required, but jQuery is necessary. The gadget can be loaded by appropriate means, if there is at window.jQuery such a library (any version) present.

Workflow[edit]

Once the full tool has been requested, it will take a few seconds.

  • Then in the top region of the page a box will be inserted.
  • The box will contain
    • a link to this documentation
    • a version number
    • a set of number formats
    • a list of links to data tables detected in current page.

All appropriate data tables (at a first glance) will be equipped with a handle button |}===={| in the caption.

If a data table shall be expanded, the handle button may be clicked. Then either:

  • on top and left hand side stripes of cells will be inserted, or
  • it is not an appropriate table and there is no content that can be processed.

The handle will be removed anyway.

The appearance and decoration within the page may be changed, and sorting might not work any longer. There is no way to return to original state than reloading the page when investigation has been completed. By tableXpander equipment page and tables might look a bit messy until reloaded.

The tool is not limited to any type of page, namespace, domain, area within page.

Kind of tables[edit]

Only data tables will be equipped.

  • Layout tables do nothing more than arranging decorative elements.
  • This is in conflict with accessibility and an approach of last century.
  • Those should not be touched.

A data table does need:

  • A headline row, or
  • at least some header cells in first column.

It might happen that a table has been classified first as data table, but no data content has been found on more close investigation. For those the feature will be revocated.

Particular data table[edit]

Once a table has been required to be equipped, on top and in front of each row a stripe with |}===={| buttons will be inserted.

  • If none is inserted, there was no data content detected.
  • When a handle is clicked, the related column or row is selected and will get a background colour matching the button background.
  • The activation button is replaced by a selection of functions: # Σ Ø % ↕
  • All data cells will be equipped with a toggle button [-] and resetting [+] to remove or add a data cell to current selection.
  • In the top first corner a X gives opportunity to clear all selections.
  • Once a column or row is selected a previous selection is discarded.

The function buttons have the following meaning:

Symbol Functionality Answer
# Ranking and counting 1 within cell content
Σ Sum 12345 below heading menu
Ø Average 5.45789012 below heading menu
% Percentage 3,14% within cell content

Copy & Paste
(column or row, respectively)
Additional box at page top (scrollable)

Example for an expanded table (simulation):

X |}===={| # Σ ø % ↕
91
|}===={|
This That Those
|}===={| 10 20  [-] 30
|}===={| 5 15  [-] 25
|}===={| 2 56  [-] 3
|}===={| 17 91  [+] 58

Merged cells and sorting[edit]

If there is a span of cells aside or below, tableXpander will consider them once only.

  • They are regarded as part of any column or row to which they extend.

Note that if MediaWiki sorting is requested for a table, merged cells will be split and duplicated within columns and rows. If taken a sum later or the number of cells will be counted those will occur multiple times now.

Cell contents[edit]

Basically, only content of each cell will be taken into account.

  • That does mean: No markup will influence the analysis.
  • On the other hand: If parts of content are made invisible, they are still part of contents, whether it is visible or not.
  • Empty content does matter for counting and copy & paste operations.
  • An entire cell can be removed from selection by toggle button [-] and by [+] toggled back.

Numerical contents[edit]

For arithmetics some more effort will be taken:

  • If any data-sort-value is attached to a cell that value will be used rather than cell contents.
  • That does mean: It may be specified data-sort-value="5" but cell content Five – however, the numerical value will be used instead of text.
  • After first numerical evaluation contents in effect of such data-sort-value will be shown as tooltip.

Numbers will be parsed from assignment in effect, be it data-sort-value or cell contents, by the following rules:

  • All known types of leading spaces will be ignored if preceding the figure or between sign and first digit; might be HTML entities as well.
  • Signed numbers may be used. Those cover:
    • ASCII minus sign - (U+002D).
    • ASCII plus sign + (U+002B).
    • Typographic minus sign (U+2212).
    • Fullwidth plus/minus signs (U+FF0B / U+FF0D) may used if followed by digits from the same system.
  • Digits might be taken from common ASCII set U+0030...U+0039 (0-9).
    • Several dozens of asian scripting systems may be used as well, as long as they maintain a sequence from zero to nine (only those ten digits are recognized).
  • Scientific or exponential number formatting may be used; that is: 1.234E-34 (or downcased e).
  • Number parsing will be stopped if any character, e.g. a regular space, is found that does not match the number formatting in effect.
  • Remember that any content of the cell will be taken into account. If the method which has been obsoleted in 2012 is used to precede numbers with invisible content to enforce sorting that is part of the significant content.

Number formatting[edit]

There are various systems to mark the beginning of decimal fraction in the world, as well as grouping digits by three (thousands), sometimes four, to improve readability.

  • Depending on knowledge about interpretation the value of a figure might change dramatically. 1,234 is regarded as thousand two hundred in the English world, but less than one and a half in e.g. German where the comma , is used as decimal separator.

The following number formatting schemes are offered on top of the page. They may be changed at any time and will be used for next interpretation on arithmetics.

Available number formatting schemes
Pattern Decimal Grouping Remarks
0,000.00 period . U+002E comma , U+002C common in English and standard in programming languages
0.000,00 comma , U+002C period . U+002E e.g. German
0'000,00 comma , U+002C apostrophe ' U+0027 Switzerland style
0▯000.00 period . U+002E nbsp U+00A0 non breaking space for grouping
0□000.00 period . U+002E nnbsp U+202F narrow non breaking space for grouping
0▯000,00 comma , U+002C nbsp U+00A0 non breaking space for grouping
0□000,00 comma , U+002C nnbsp U+202F narrow non breaking space for grouping

In a MediaWiki page it will be tried to retrieve the project default number formatting on first page loading.

If successful that one is preselected for this page.

The default is the first one (English and programming).

To improve performance it is attempred to store that scheme as localStorage for this domain.

Functionalities[edit]

The # Σ Ø % ↕ toolbar is offering the following activities for selected column or row and the active cells.

Ranking and counting[edit]

# will cause numbering of active selected cells.

  • Each cell is preceded by the sequence number in a small box: 123
  • The total number of active cells is displayed in the toolbar.
  • When removing from active cells by [-] or adding to again by [+] the numbering and total count will be updated immediately.

Arithmetics[edit]

Sum[edit]

Σ will trigger addition of all numerical values of all active cells which are recognized as numerical data.

  • Cells not recognized as numerical will be removed from set of active cells automatically.
  • The current sum is displayed in the toolbar.
  • When removing from active cells by [-] or adding to again by [+] the total amount will be updated immediately.

Average[edit]

Ø will calculate the average.

  • The functionality is the same as the sum but divided by the number of active cells, if any (zero if none).

Percentage[edit]

% calculates the percentual part of that cell at the total amount.

  • Within every data cell the percentage will be prepended in a small box: 3,14%
  • Furthermore the total amont will be displayed in the heading cell.
  • On negative sum and/or alternating content negative values may yield respectively. Basically the feature is dedicated to positive figures.

The function is supposed to check existing percentages in (another) column.

  • Examples:
    • In first column the number of citizens or votes by party might be provided. In the second column the percentage will be present already. Now the percentage function may be applied to first column and the distribution can be compared.
    • Or run the percentage function on second column. The total is to be 100 and all values should match. If not then somewhere is a bug.

Copy & Paste[edit]

(for a column) or (for a row) will insert a text field in the top of the page when used first.

  • In this area the contents of all active cells will be listed.
  • Every cell will be inserted as a single line.
  • Line breaks and some whitespace might be normalized.
  • That may be taken by copy&paste and used wherever needed.

Cookies[edit]

localStorage is attempted to memorize project number formatting scheme.

Internationalization[edit]

Not really required for gadget itself, since deliberately any text has been avoided.

A translation of this page may be offered.

Behaviour does not depend on scripting systems nor writing direction and does support non-ASCII digits.

Codes[edit]

Source code
ResourceLoader
Namespaces
  • Every
Cookies localStorage
mw.libs tableXpander (reserved, but currently unused)
mw.hook
  • tableXpander.ready
    When page has been equipped.
MediaWiki any, or none.
JavaScript 1.5 (2000)

A test page tells how to check for proper functionality.