MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 988: | Строка 988: | ||
} | } | ||
} | } | ||
// | // Для "Шаблон:ProjectSelectionGenerator" | ||
function initProjectSelectionGenerator() { | function initProjectSelectionGenerator() { | ||
var containers = document.getElementsByClassName('js-project-selection-generator'); | var containers = document.getElementsByClassName('js-project-selection-generator'); | ||
| Строка 997: | Строка 997: | ||
.replace(/\s+/g, '-') | .replace(/\s+/g, '-') | ||
.replace(/[^A-Za-z0-9_\-\u0400-\u04FF]/g, ''); | .replace(/[^A-Za-z0-9_\-\u0400-\u04FF]/g, ''); | ||
} | |||
function getContentRoot() { | |||
return document.querySelector('#mw-content-text .mw-parser-output') || document.querySelector('#mw-content-text'); | |||
} | |||
function getCurrentPageWikitext() { | |||
var title = mw.config.get('wgPageName'); | |||
var apiUrl = mw.util.wikiScript('api') + | |||
'?action=query' + | |||
'&prop=revisions' + | |||
'&rvslots=main' + | |||
'&rvprop=content' + | |||
'&titles=' + encodeURIComponent(title) + | |||
'&format=json' + | |||
'&formatversion=2' + | |||
'&origin=*'; | |||
return $.ajax({ | |||
url: apiUrl, | |||
method: 'GET', | |||
dataType: 'json' | |||
}).then(function (data) { | |||
if (!data || !data.query || !data.query.pages || !data.query.pages.length) { | |||
return ''; | |||
} | |||
var page = data.query.pages[0]; | |||
if (!page.revisions || !page.revisions.length) { | |||
return ''; | |||
} | |||
var rev = page.revisions[0]; | |||
if (rev.slots && rev.slots.main && typeof rev.slots.main.content === 'string') { | |||
return rev.slots.main.content; | |||
} | |||
if (typeof rev.content === 'string') { | |||
return rev.content; | |||
} | |||
return ''; | |||
}); | |||
} | |||
function stripProjectSelectionTemplate(text) { | |||
if (!text) return ''; | |||
return text.replace(/\{\{\s*projectSelectionGenerator\b[\s\S]*?\}\}/ig, ''); | |||
} | |||
function buildWikitextForProject(text, projectName) { | |||
var cleanedText = stripProjectSelectionTemplate(text); | |||
return '{{#vardefine:Path|' + projectName + '}}\n' + cleanedText; | |||
} | |||
function parseWikiText(wikiText) { | |||
var apiUrl = mw.util.wikiScript('api') + | |||
'?action=parse' + | |||
'&format=json' + | |||
'&formatversion=2' + | |||
'&prop=text' + | |||
'&text=' + encodeURIComponent(wikiText) + | |||
'&disablelimitreport=1' + | |||
'&disableeditsection=1' + | |||
'&disabletoc=1' + | |||
'&origin=*'; | |||
return $.ajax({ | |||
url: apiUrl, | |||
method: 'GET', | |||
dataType: 'json' | |||
}); | |||
} | |||
function applyParsedHTML(parsedHTML, preservedMenus) { | |||
var contentRoot = getContentRoot(); | |||
if (!contentRoot) return; | |||
contentRoot.innerHTML = parsedHTML; | |||
if (preservedMenus && preservedMenus.length > 0) { | |||
for (var i = 0; i < preservedMenus.length; i++) { | |||
contentRoot.insertBefore(preservedMenus[i], contentRoot.firstChild); | |||
} | |||
} | |||
mw.hook('wikipage.content').fire(contentRoot); | |||
} | |||
function refreshPageForProject(projectName, preservedMenus) { | |||
getCurrentPageWikitext() | |||
.then(function (sourceText) { | |||
var newText = buildWikitextForProject(sourceText, projectName); | |||
return parseWikiText(newText); | |||
}) | |||
.then(function (data) { | |||
if (data && data.parse && data.parse.text) { | |||
var parsedHTML = data.parse.text["*"] || data.parse.text || ""; | |||
applyParsedHTML(parsedHTML, preservedMenus); | |||
} | |||
}) | |||
.fail(function () { | |||
console.error('Не удалось перепарсить страницу для проекта:', projectName); | |||
}); | |||
} | } | ||
for (var ci = 0; ci < containers.length; ci++) { | for (var ci = 0; ci < containers.length; ci++) { | ||
var container = containers[ci]; | var container = containers[ci]; | ||
if (container.getAttribute('data-project-selection-initialized') === '1') { | |||
continue; | |||
} | |||
var rawText = container.textContent || ''; | var rawText = container.textContent || ''; | ||
| Строка 1018: | Строка 1128: | ||
var form = document.createElement('form'); | var form = document.createElement('form'); | ||
form.className = 'project-menu'; | form.className = 'js-project-menu'; | ||
for (var j = 0; j < projects.length; j++) { | for (var j = 0; j < projects.length; j++) { | ||
| Строка 1028: | Строка 1138: | ||
var input = document.createElement('input'); | var input = document.createElement('input'); | ||
input.type = 'radio'; | input.type = 'radio'; | ||
input.name = 'project-selection'; | input.name = 'js-project-selection'; | ||
input.value = projectName; | input.value = projectName; | ||
input.id = projectId; | input.id = projectId; | ||
| Строка 1047: | Строка 1157: | ||
container.textContent = ''; | container.textContent = ''; | ||
container.appendChild(form); | container.appendChild(form); | ||
(function (menuContainer) { | |||
var radios = menuContainer.querySelectorAll('input[type="radio"][name="js-project-selection"]'); | |||
for (var r = 0; r < radios.length; r++) { | |||
radios[r].addEventListener('change', function () { | |||
if (!this.checked) return; | |||
var projectName = this.value; | |||
var preservedMenus = []; | |||
var menuNode = menuContainer; | |||
if (menuNode && menuNode.parentNode) { | |||
preservedMenus.push(menuNode); | |||
} | |||
refreshPageForProject(projectName, preservedMenus); | |||
}); | |||
} | |||
})(container); | |||
container.setAttribute('data-project-selection-initialized', '1'); | |||
} | } | ||
} | } | ||