MediaWiki:Common.js: відмінності між версіями
Admin (обговорення | внесок) (Common.js: portlet Міста через офіційний mw.util.addPortlet) |
Admin (обговорення | внесок) (Common.js v5: diagnostic + manual fallback) |
||
| Рядок 3: | Рядок 3: | ||
$(function () { | $(function () { | ||
var pageName = mw.config.get('wgPageName') || ''; | var pageName = mw.config.get('wgPageName') || ''; | ||
console.log('[nomadica] common.js v5, wgPageName =', pageName); | |||
console.log('[nomadica] mw.util.addPortlet available:', typeof mw.util.addPortlet === 'function'); | |||
if (pageName === '🇷🇴_Румунія') { | if (pageName === '🇷🇴_Румунія') { | ||
console.log('[nomadica] matched Romania, creating cities portlet'); | |||
var cities = [ | var cities = [ | ||
{ title: 'Бухарест', page: 'Румунія/Бухарест' }, | { title: 'Бухарест', page: 'Румунія/Бухарест' }, | ||
| Рядок 15: | Рядок 18: | ||
]; | ]; | ||
var portlet = mw.util.addPortlet('p-cities', 'Міста'); | if (typeof mw.util.addPortlet === 'function') { | ||
if (portlet) | var portlet = mw.util.addPortlet('p-cities', 'Міста'); | ||
console.log('[nomadica] portlet created:', portlet); | |||
cities.forEach(function (city) { | |||
var link = mw.util.addPortletLink('p-cities', mw.util.getUrl(city.page), city.title); | |||
console.log('[nomadica] added link:', city.title, link); | |||
}); | |||
} else { | |||
console.warn('[nomadica] mw.util.addPortlet NOT available — fallback to manual DOM'); | |||
// Fallback: clone navigation portlet structure | |||
var $nav = $('#p-navigation'); | |||
if (!$nav.length) { | |||
console.warn('[nomadica] #p-navigation also not found'); | |||
return; | |||
} | |||
var $portlet = $('<div>') | |||
.attr('id', 'p-cities') | |||
.addClass('vector-menu mw-portlet mw-portlet-cities'); | |||
var $heading = $('<div>').addClass('vector-menu-heading').text('Міста'); | |||
var $content = $('<div>').addClass('vector-menu-content'); | |||
var $ul = $('<ul>').addClass('vector-menu-content-list'); | |||
cities.forEach(function (city) { | cities.forEach(function (city) { | ||
mw. | var $li = $('<li>').addClass('mw-list-item'); | ||
var $a = $('<a>').attr('href', mw.util.getUrl(city.page)).append($('<span>').text(city.title)); | |||
$li.append($a); | |||
$ul.append($li); | |||
}); | }); | ||
$content.append($ul); | |||
$portlet.append($heading).append($content); | |||
$nav.after($portlet); | |||
console.log('[nomadica] fallback portlet inserted after #p-navigation'); | |||
} | } | ||
} | } | ||
}); | }); | ||
}); | }); | ||
Версія за 23:28, 2 травня 2026
/* Per-page sidebar portlets via official mw.util.addPortlet API */
mw.loader.using(['mediawiki.util'], function () {
$(function () {
var pageName = mw.config.get('wgPageName') || '';
console.log('[nomadica] common.js v5, wgPageName =', pageName);
console.log('[nomadica] mw.util.addPortlet available:', typeof mw.util.addPortlet === 'function');
if (pageName === '🇷🇴_Румунія') {
console.log('[nomadica] matched Romania, creating cities portlet');
var cities = [
{ title: 'Бухарест', page: 'Румунія/Бухарест' },
{ title: 'Клуж-Напока', page: 'Румунія/Клуж-Напока' },
{ title: 'Тімішоара', page: 'Румунія/Тімішоара' },
{ title: 'Брашов', page: 'Румунія/Брашов' },
{ title: 'Ясси', page: 'Румунія/Ясси' },
{ title: 'Сібіу', page: 'Румунія/Сібіу' }
];
if (typeof mw.util.addPortlet === 'function') {
var portlet = mw.util.addPortlet('p-cities', 'Міста');
console.log('[nomadica] portlet created:', portlet);
cities.forEach(function (city) {
var link = mw.util.addPortletLink('p-cities', mw.util.getUrl(city.page), city.title);
console.log('[nomadica] added link:', city.title, link);
});
} else {
console.warn('[nomadica] mw.util.addPortlet NOT available — fallback to manual DOM');
// Fallback: clone navigation portlet structure
var $nav = $('#p-navigation');
if (!$nav.length) {
console.warn('[nomadica] #p-navigation also not found');
return;
}
var $portlet = $('<div>')
.attr('id', 'p-cities')
.addClass('vector-menu mw-portlet mw-portlet-cities');
var $heading = $('<div>').addClass('vector-menu-heading').text('Міста');
var $content = $('<div>').addClass('vector-menu-content');
var $ul = $('<ul>').addClass('vector-menu-content-list');
cities.forEach(function (city) {
var $li = $('<li>').addClass('mw-list-item');
var $a = $('<a>').attr('href', mw.util.getUrl(city.page)).append($('<span>').text(city.title));
$li.append($a);
$ul.append($li);
});
$content.append($ul);
$portlet.append($heading).append($content);
$nav.after($portlet);
console.log('[nomadica] fallback portlet inserted after #p-navigation');
}
}
});
});