MediaWiki:Common.js: Difference between revisions

From MalveraWiki
No edit summary
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 7: Line 7:
         var $tooltip = $wrapper.find('.custom-tooltip').empty();
         var $tooltip = $wrapper.find('.custom-tooltip').empty();


         // 1️⃣ Dodaj grafikę i nazwę przedmiotu
         // --- Pobieramy obrazek i nazwę z wiersza tabeli ---
         var itemImage = $wrapper.data('tooltip-image'); // np. "File:Crown.png"
         var $tr = $wrapper.closest('tr');
         var itemName = $wrapper.data('tooltip-name');   // np. "Crown"
        var $imgCell = $tr.find('td:first-child img').first();
         var $nameCell = $tr.find('td:nth-child(2)').first();


         if (itemImage) {
         if ($imgCell.length || $nameCell.length) {
             var $img = $('<div style="text-align:center;margin-bottom:4px;">' +
             var $header = $('<div class="tooltip-header"></div>');
                        '<img src="/images/' + itemImage + '" style="max-width:64px;display:block;margin:0 auto;">' +
            if ($imgCell.length) {
                        '</div>');
                var $imgClone = $imgCell.clone();
            $tooltip.append($img);
                $header.append($('<div class="tooltip-item-image"></div>').append($imgClone));
        }
            }
 
            if ($nameCell.length) {
        if (itemName) {
                var nameText = $nameCell.text().trim();
            var $name = $('<div style="text-align:center;font-weight:bold;margin-bottom:6px;">' + itemName + '</div>');
                $header.append($('<div class="tooltip-item-name"></div>').text(nameText));
             $tooltip.append($name);
            }
             $tooltip.append($header);
         }
         }


         // 2️⃣ Dodaj tabele
         // --- Kontener tabel ---
         var tables = tablesStr.split('##'); // separator między tabelami
         var $tablesContainer = $('<div class="tooltip-tables-container"></div>');


        var tables = tablesStr.split('##');
         tables.forEach(function(tableDef) {
         tables.forEach(function(tableDef) {
             var parts = tableDef.split('{{!}}'); // separator nagłówek | wiersze
             var parts = tableDef.split('{{!}}');
             if (parts.length < 2) return;
             if (parts.length < 2) return;


Line 37: Line 40:


             var $tableContainer = $('<div class="tooltip-table-container"></div>');
             var $tableContainer = $('<div class="tooltip-table-container"></div>');
             if (header) {
             if (header) {
                 $tableContainer.append('<div class="tooltip-table-header">'+header+'</div>');
                 $tableContainer.append('<div class="tooltip-table-header">'+header+'</div>');
Line 49: Line 51:
                 var cols = row.split(',');
                 var cols = row.split(',');
                 if (cols.length < 2) return;
                 if (cols.length < 2) return;
                 var $tr = $('<tr></tr>');
                 var $trRow = $('<tr></tr>');
                 cols.forEach(function(c){ $tr.append('<td>'+c.trim()+'</td>'); });
                 cols.forEach(function(c){ $trRow.append('<td>'+c.trim()+'</td>'); });
                 $tbody.append($tr);
                 $tbody.append($trRow);
             });
             });


             $table.append($tbody);
             $table.append($tbody);
             $tableContainer.append($table);
             $tableContainer.append($table);
             $tooltip.append($tableContainer);
             $tablesContainer.append($tableContainer);
         });
         });
        $tooltip.append($tablesContainer);
     });
     });
});
});

Latest revision as of 21:22, 2 February 2026

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);
    });
});