MediaWiki:Common.js
From MalveraWiki
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
mw.hook('wikipage.content').add(function ($content) {
$content.find('.tooltip-wrapper[data-tooltip-tables]').each(function () {
var $wrapper = $(this);
var tablesStr = $wrapper.data('tooltip-tables');
if (!tablesStr) return;
var $tooltip = $wrapper.find('.custom-tooltip').empty();
// --- Pobieramy obrazek i nazwę z wiersza tabeli ---
var $tr = $wrapper.closest('tr');
var $imgCell = $tr.find('td:first-child img').first();
var $nameCell = $tr.find('td:nth-child(2)').first();
if ($imgCell.length || $nameCell.length) {
var $header = $('<div class="tooltip-header"></div>');
if ($imgCell.length) {
var $imgClone = $imgCell.clone();
$header.append($('<div class="tooltip-item-image"></div>').append($imgClone));
}
if ($nameCell.length) {
var nameText = $nameCell.text().trim();
$header.append($('<div class="tooltip-item-name"></div>').text(nameText));
}
$tooltip.append($header);
}
// --- Kontener tabel ---
var $tablesContainer = $('<div class="tooltip-tables-container"></div>');
var tables = tablesStr.split('##');
tables.forEach(function(tableDef) {
var parts = tableDef.split('{{!}}');
if (parts.length < 2) return;
var header = parts[0].trim();
var rowsStr = parts[1].trim();
if (!rowsStr) return;
var rows = rowsStr.split(';');
var $tableContainer = $('<div class="tooltip-table-container"></div>');
if (header) {
$tableContainer.append('<div class="tooltip-table-header">'+header+'</div>');
}
var $table = $('<table class="wikitable sortable"></table>');
$table.append('<thead><tr><th>Amount</th><th>Chance</th></tr></thead>');
var $tbody = $('<tbody></tbody>');
rows.forEach(function(row) {
var cols = row.split(',');
if (cols.length < 2) return;
var $trRow = $('<tr></tr>');
cols.forEach(function(c){ $trRow.append('<td>'+c.trim()+'</td>'); });
$tbody.append($trRow);
});
$table.append($tbody);
$tableContainer.append($table);
$tablesContainer.append($tableContainer);
});
$tooltip.append($tablesContainer);
});
});
