User:PerfektesChaos/js/tableXpander

|}===={| Deutsch JavaScript gadget – calculations and auxilary functions are offered for data tables.

Purpose
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

 * 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
Include the following line into your common.js, global.js etc.: In the (desktop) toolbox a  will appear on all pages, and you can start the full gadget on demand.

Full tool
Run the following code wherever you want:

Non-MediaWiki pages
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  such a library (any version) present.

Workflow
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: The handle will be removed anyway.
 * 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 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
Only data tables will be equipped. A data table does need: 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.
 * 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 headline row, or
 * at least some header cells in first column.

Particular data table
Once a table has been required to be equipped, on top and in front of each row a stripe with |}===={| buttons will be inserted. The function buttons have the following meaning:
 * 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.

Example for an expanded table (simulation):

Merged cells and sorting
If there is a span of cells aside or below, tableXpander will consider them once only. 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.
 * They are regarded as part of any column or row to which they extend.

Cell contents
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
For arithmetics some more effort will be taken:
 * If any  is attached to a cell that value will be used rather than cell contents.
 * That does mean: It may be specified  but cell content   – however, the numerical value will be used instead of text.
 * After first numerical evaluation contents in effect of such  will be shown as tooltip.

Numbers will be parsed from assignment in effect, be it  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.
 * 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:  (or downcased  ).
 * 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
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.  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.

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
The # Σ Ø % ↕ toolbar is offering the following activities for selected column or row and the active cells.

Ranking and counting

 * 1) 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.

Sum
Σ 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
Ø 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
% calculates the percentual part of that cell at the total amount. The function is supposed to check existing percentages in (another) column.
 * 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.
 * 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  and all values should match. If not then somewhere is a bug.

Copy & Paste
↕ (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
localStorage is attempted to memorize project number formatting scheme.

Internationalization
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
A test page tells how to check for proper functionality.

de:User:PerfektesChaos/js/tableXpander