/*!
* @license GNU GPL v2+
* @since 3.0
*
* @author mwjames
* @author thomas-topway-it
*/
/* global jQuery, mediaWiki, mw */
( function ( $, mw ) {
'use strict';
var dataTable = {
/**
* Adds the initial sort/order from the #ask request that is available as
* `data-column-sort` attribute with something like:
*
* {
* "list":["","Foo","Bar"]
* "sort":["Foo"],
* "order":["asc"]
* }
*
* on
*
* {{#ask: ...
* |?Foo
* |?Bar
* |sort=Foo
* |order=asc
* ...
* }}
*
* @since 3.0
*
* @private
* @static
*
* @param {Object} context
*/
initColumnSort: function ( context ) {
var column = context.data( 'column-sort' );
var order = [];
// SMW allows descending and ascending but those are unknown to DataTables
var orderMap = {
'descending' : 'desc',
'ascending': 'asc',
'asc': 'asc',
'desc': 'desc'
};
// In case of a transposed table, don't try to match a column or its order
if ( column === undefined || !column.hasOwnProperty( 'sort' ) || column.sort.length === 0 || context.attr( 'data-transpose' ) ) {
return;
};
// https://datatables.net/reference/api/order()
// [1, 'asc'], [2, 'desc']
$.map( column.sort, function( val, i ) {
if ( val === '' ) {
i = 0;
};
if ( $.inArray( val, column.list ) < 0 ) {
return
};
order.push( [
$.inArray( val, column.list ), // Find matchable index from the list
column.order[i] === undefined ? 'asc' : orderMap[column.order[i]]
] );
} );
if ( order.length > 0 ) {
context.data( 'order', order );
};
},
/**
* @since 3.0
*
* @private
* @static
*
* @param {Object} context
*/
addHeader: function ( context ) {
// Copy the thead to a position the DataTable plug-in can transform
// and display
if ( context.find( 'thead' ).length === 0 ) {
var head = context.find( 'tbody tr' );
context.prepend( '' + head.html() + '' );
head.eq(0).remove();
// In case of a transposed, turn any td into a th
context.find( 'thead td' ).wrapInner( '
' ).contents().unwrap();
}
// Ensure that any link in the header stops the propagation of the
// click sorting event
context.find( 'thead tr a' ).on( 'click.sorting', function ( event ) {
event.stopPropagation();
} );
},
/**
* @since 3.0
*
* @private
* @static
*
* @param {Object} context
*/
addFooter: function ( context ) {
// As a transposed table, move the footer column to the bottom
// and remove any footer-cell from the table matrix to
// ensure a proper formatted table
if ( context.data( 'transpose' ) === 1 && context.find( 'tbody .sortbottom' ).length === 1 ) {
var footer = context.find( 'tbody .sortbottom' );
context.append( '
' + footer.html() + '
' );
footer.eq(0).remove();
// Remove remaining footer cells to avoid an uneven table
context.find( 'tbody .footer-cell' ).each( function() {
$( this ).remove();
} );
};
// Copy the tbody to a position the DataTable plug-in can transform
// and display
if ( context.find( 'tbody .smwfooter' ).length == 1 ) {
var footer = context.find( 'tbody .smwfooter' );
context.append( '' + footer.html() + '' );
footer.eq(0).remove();
}
context.find( '.sortbottom' ).addClass( 'plainlinks' );
},
/**
* @since 3.0
*
* @param {Object} context
*/
addToolbarExportLinks: function ( context ) {
// @see https://datatables.net/examples/layout/custom-nodes.html
var toolbar = $('