「MediaWiki:Gadget-tablesort.js」の版間の差分
ナビゲーションに移動
検索に移動
(一部のページで無限ループに陥る問題を修正) |
(rowspanで不具合があったので分割するように処置(とりあえず)) |
||
23行目: | 23行目: | ||
'わ': 'ワ', 'を': 'ヲ', 'ん': 'ン' | 'わ': 'ワ', 'を': 'ヲ', 'ん': 'ン' | ||
}); | }); | ||
/* ソートキー */ | |||
function getElementSortKey(node) { | |||
var $node = $(node), | |||
data = $node.data('sortValue'); | |||
if (data !== null && data !== undefined) { | |||
return String(data); | |||
} else if (node.tagName.toLowerCase() === 'img') { | |||
return $node.attr('alt') || ''; | |||
} else { | |||
return $.map(node.childNodes, function(el) { | |||
return el.nodeType === 1 ? getElementSortKey(el) : $.text(el); | |||
}).join(''); | |||
} | |||
} | |||
/* sortbottom相当の機能を追加する */ | /* sortbottom相当の機能を追加する */ | ||
67行目: | 82行目: | ||
$tfoot.appendTo(this); | $tfoot.appendTo(this); | ||
/* .sortmiddle, .sortbottom */ | |||
var $rows = $(rows); | var $rows = $(rows); | ||
this.$sortbottoms = $rows.filter('.sortmiddle, .sortbottom').each(function(i) { | this.$sortbottoms = $rows.filter('.sortmiddle, .sortbottom').each(function(i) { | ||
this.insertPosition = $rows.index(this) - i; | this.insertPosition = $rows.index(this) - i; | ||
}).addClass('remove-me').remove(); | }).addClass('remove-me').remove(); | ||
/* rowspanを持つセルを分割する(暫定) */ | |||
$rows.each(function(i) { | |||
var cells = this.cells; | |||
for (var j = 0, len = cells.length; j < len; j++) { | |||
var rowspan = cells[j].rowSpan; | |||
cells[j].rowSpan = 1; | |||
for (var k = 1; k < rowspan; k++) { | |||
$(cells[j]).clone().insertBefore($rows.get(i + k).cells[j]); | |||
} | |||
} | |||
}); | |||
} | }) | ||
} | .tablesorter({ | ||
} | |||
sortLocaleCompare: true, | sortLocaleCompare: true, | ||
sortRestart: true, | sortRestart: true, |
2014年4月9日 (水) 18:30時点における版
/**
* 標準のソート機能を置換するスクリプト
* table.sortableがあるページでMediaWiki標準のtablesorterの読み込みを阻止し、
* 別のソートライブラリでソート機能を実現します。
*/
mw.hook('wikipage.content').add(function($content) {
var $sortableTables = $content.find('table.sortable');
if ($sortableTables.length) {
$sortableTables.removeClass('sortable');
$.getScript(mw.util.wikiScript() + '?title=MediaWiki:Jquery.tablesorter.min.js&action=raw&ctype=text/javascript', function() {
/* ソートでカタカナ・濁音・半濁音を清音(ひらがな)として評価させる */
$.extend($.tablesorter.characterEquivalents, {
'あ': 'アァぁ', 'い': 'イィぃ', 'う': 'ウゥぅ', 'え': 'エェぇ', 'お': 'オォぉ',
'か': 'カガが', 'き': 'キギぎ', 'く': 'クグぐ', 'け': 'ケゲげ', 'こ': 'コゴご',
'さ': 'サザざ', 'し': 'シジじ', 'す': 'スズず', 'せ': 'セゼぜ', 'そ': 'ソゾぞ',
'た': 'タダだ', 'ち': 'チヂぢ', 'つ': 'ツヅづ', 'て': 'テデで', 'と': 'トドど',
'な': 'ナ', 'に': 'ニ', 'ぬ': 'ヌ', 'ね': 'ネ', 'の': 'ノ',
'は': 'ハバパばぱ', 'ひ': 'ヒビピびぴ', 'ふ': 'フブプぶぷ', 'へ': 'ヘベペべぺ', 'ほ': 'ホボポぼぽ',
'ま': 'マ', 'み': 'ミ', 'む': 'ム', 'め': 'メ', 'も': 'モ',
'や': 'ヤャゃ', 'ゆ': 'ユュゅ', 'よ': 'ヨョょ',
'ら': 'ラ', 'り': 'リ', 'る': 'ル', 'れ': 'レ', 'ろ': 'ロ',
'わ': 'ワ', 'を': 'ヲ', 'ん': 'ン'
});
/* ソートキー */
function getElementSortKey(node) {
var $node = $(node),
data = $node.data('sortValue');
if (data !== null && data !== undefined) {
return String(data);
} else if (node.tagName.toLowerCase() === 'img') {
return $node.attr('alt') || '';
} else {
return $.map(node.childNodes, function(el) {
return el.nodeType === 1 ? getElementSortKey(el) : $.text(el);
}).join('');
}
}
/* sortbottom相当の機能を追加する */
$.tablesorter.addWidget({
id: 'sortbottom',
priority: 80,
format: function(table) {
var $rows = $('> tbody > tr:not(.remove-me)', table);
table.$sortbottoms.each(function() {
var $before = $rows.eq(this.insertPosition);
if ($before.length) {
$before.before(this);
} else {
$('> tbody', table).append(this);
}
});
}
});
/* tablesorterに必要なthead要素とtfoot要素を生成する */
$sortableTables.each(function() {
var $thead = $('<thead></thead>'),
$tfoot = $('<tfoot></tfoot>'),
rows = this.tBodies[0].rows;
while (rows.length) {
var $tr = $(rows[0]);
if ($tr.children('td').length) break;
$tr.children().each(function() {
var $cell = $(this),
sortType = $cell.data('sortType');
if ($cell.hasClass('unsortable')) {
$cell.data('sorter', false);
} else if (sortType === 'number') {
$cell.data('sorter', 'digit');
}
});
$thead.append($tr);
}
$thead.insertBefore(this.tBodies[0]);
for (var i = rows.length - 1; 0 < i; i--) {
if ($(rows[i]).children('td').length) break;
$tfoot.prepend(rows[i]);
}
$tfoot.appendTo(this);
/* .sortmiddle, .sortbottom */
var $rows = $(rows);
this.$sortbottoms = $rows.filter('.sortmiddle, .sortbottom').each(function(i) {
this.insertPosition = $rows.index(this) - i;
}).addClass('remove-me').remove();
/* rowspanを持つセルを分割する(暫定) */
$rows.each(function(i) {
var cells = this.cells;
for (var j = 0, len = cells.length; j < len; j++) {
var rowspan = cells[j].rowSpan;
cells[j].rowSpan = 1;
for (var k = 1; k < rowspan; k++) {
$(cells[j]).clone().insertBefore($rows.get(i + k).cells[j]);
}
}
});
})
.tablesorter({
sortLocaleCompare: true,
sortRestart: true,
textExtraction: getElementSortKey,
widgets: ['sortbottom']
});
});
}
});