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

Нет описания правки
Нет описания правки
Строка 690: Строка 690:


return container;
return container;
}
function collectWikiVars(root) {
root = root || document;
var vars = {};
var nodes = root.querySelectorAll('[data-wiki-var]');
nodes.forEach(function (node) {
var key = (node.getAttribute('data-wiki-var') || '').trim();
if (!key) return;
var value = node.getAttribute('data-wiki-value');
if (value === null || value === undefined) {
value = node.textContent || '';
}
vars[key] = value;
});
return vars;
}
function resolveWikiVars(wikiText, vars) {
vars = vars || {};
return String(wikiText || '').replace(
/\{\{\s*#var\s*:\s*([^}|]+)\s*(?:\|[^}]*)?\}\}/gi,
function (match, name) {
name = String(name || '').trim();
if (Object.prototype.hasOwnProperty.call(vars, name)) {
return String(vars[name]);
}
return match;
}
);
}
}


function parseAndInjectWikiText(wikiText, options) {
function parseAndInjectWikiText(wikiText, options) {
return fetchParsedData(wikiText)
options = options || {};
 
var contextVars = options.contextVars || collectWikiVars(options.contextRoot || document);
var resolvedWikiText = resolveWikiVars(wikiText, contextVars);
 
return fetchParsedData(resolvedWikiText)
.done(function (data) {
.done(function (data) {
if (data && data.parse && data.parse.text) {
if (data && data.parse && data.parse.text) {
Строка 711: Строка 754:
function createPlaceholderWithSpinner(text) {
function createPlaceholderWithSpinner(text) {
if (!text) {
if (!text) {
loadingImgHTML = '<img alt="Файл:Silva-loading.gif" src="/images/4/45/Silva-loading.gif" decoding="async" width="16" height="16" style="vertical-align:middle;margin-right:6px">';
var loadingImgHTML = '<img alt="Файл:Silva-loading.gif" src="/images/4/45/Silva-loading.gif" decoding="async" width="16" height="16" style="vertical-align:middle;margin-right:6px">';
text = loadingImgHTML + '<span class="ajax-load-text">Пожалуйста, подождите, содержимое загружается...</span>';
text = loadingImgHTML + '<span class="ajax-load-text">Пожалуйста, подождите, содержимое загружается...</span>';
}
}
Строка 745: Строка 788:


batch.forEach(function (job) {
batch.forEach(function (job) {
parseAndInjectWikiText(job.wikiText, { placeholder: job.placeholder })
parseAndInjectWikiText(job.wikiText, {
.always(function () {
placeholder: job.placeholder,
remaining--;
contextRoot: job.contextRoot || document,
if (remaining === 0) nextBatch();
contextVars: job.contextVars || null
});
}).always(function () {
remaining--;
if (remaining === 0) nextBatch();
});
});
});
}
}
Строка 770: Строка 816:
loadingEl.parentNode.removeChild(loadingEl);
loadingEl.parentNode.removeChild(loadingEl);
}
}
var contextVars = collectWikiVars(container);


if (container.classList.contains('ajax-load-link')) {
if (container.classList.contains('ajax-load-link')) {
Строка 786: Строка 834:
}
}


queue.push({ wikiText: wikiText, placeholder: placeholder });
queue.push({
wikiText: wikiText,
placeholder: placeholder,
contextRoot: container,
contextVars: contextVars
});
processQueue();
processQueue();
}, { once: true });
}, { once: true });
Строка 796: Строка 849:
}
}


queue.push({ wikiText: wikiText, placeholder: placeholder });
queue.push({
wikiText: wikiText,
placeholder: placeholder,
contextRoot: container,
contextVars: contextVars
});
}
}
});
});
Строка 1069: Строка 1127:
function stripProjectSelectionTemplate(text) {
function stripProjectSelectionTemplate(text) {
return String(text || '').replace(/\{\{\s*переключатель проекта\b[\s\S]*?\}\}/ig, '');
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 + '}}\n' + stripProjectSelectionTemplate(text);
preparedText = injectJsonPathIntoAjax(preparedText);
 
return '{{#vardefine:JsonPath|' + projectName + '}}\n' + preparedText;
}
}