MediaWiki:Common.js: различия между версиями

Нет описания правки
Нет описания правки
 
(не показаны 4 промежуточные версии этого же участника)
Строка 627: Строка 627:
});
});
}
}
var ProjectSelectionState = {
jsonPath: ''
};
function trimText(value) {
return String(value === undefined || value === null ? '' : value).trim();
}
function normalizeJsonPath(value) {
return trimText(value);
}
function getFirstProjectFromPage() {
var generator = document.querySelector('.js-project-selection-generator');
if (!generator) return '';
var rawText = generator.textContent || '';
var items = rawText.split(',');
for (var i = 0; i < items.length; i++) {
var item = trimText(items[i]);
if (item) return item;
}
return '';
}
function setCurrentJsonPath(value) {
ProjectSelectionState.jsonPath = normalizeJsonPath(value);
}
function getCurrentJsonPath() {
if (ProjectSelectionState.jsonPath) {
return ProjectSelectionState.jsonPath;
}
var fromPage = getFirstProjectFromPage();
if (fromPage) {
ProjectSelectionState.jsonPath = fromPage;
return fromPage;
}
return '';
}
function wrapWikiTextWithJsonPath(wikiText) {
var jsonPath = getCurrentJsonPath();
if (!jsonPath) return String(wikiText || '');
return '{{#vardefine:JsonPath|' + jsonPath + '}}\n' + String(wikiText || '');
}
function fetchParsedData(wikiText) {
function fetchParsedData(wikiText) {
var apiUrl = "https://station14.ru/api.php?action=parse&format=json&prop=text&text=" + encodeURIComponent(wikiText) + "&origin=*";
var apiUrl = "https://station14.ru/api.php?action=parse&format=json&prop=text&text=" + encodeURIComponent(wikiText) + "&origin=*";
Строка 693: Строка 745:


function parseAndInjectWikiText(wikiText, options) {
function parseAndInjectWikiText(wikiText, options) {
wikiText = wrapWikiTextWithJsonPath(wikiText);
return fetchParsedData(wikiText)
return fetchParsedData(wikiText)
.done(function (data) {
.done(function (data) {
Строка 1065: Строка 1119:
return '';
return '';
});
});
}
function stripProjectSelectionTemplate(text) {
return String(text || '').replace(/\{\{\s*переключатель проекта\b[\s\S]*?\}\}/ig, '');
}
function injectJsonPathIntoAjax(text) {
return String(text || '').replace(
/(\{\{\s*ajax\s*\|\s*)(?!\{\{\s*#var\s*:\s*JsonPath\s*\}\})(<nowiki>)/ig,
'$1{{#var:JsonPath}}$2'
);
}
}


function buildWikitext(text, projectName) {
function buildWikitext(text, projectName) {
var preparedText = stripProjectSelectionTemplate(text);
return '{{#vardefine:JsonPath|' + projectName + '}}' + text;
preparedText = injectJsonPathIntoAjax(preparedText);
 
return '{{#vardefine:JsonPath|' + projectName + '}}\n' + preparedText;
}
}


function refreshForProject(projectName) {
function refreshForProject(projectName) {
setCurrentJsonPath(projectName);
var root = getContentRoot();
var root = getContentRoot();
if (!root) return;
if (!root) return;
Строка 1124: Строка 1166:
if (projects.length <= 1) {
if (projects.length <= 1) {
continue;
continue;
}
if (!getCurrentJsonPath()) {
setCurrentJsonPath(projects[0]);
}
}