MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 746: | Строка 746: | ||
if (queue.length > 0) processQueue(); | if (queue.length > 0) processQueue(); | ||
} | } | ||
// Для "Шаблон:CheckboxCreator" | |||
( function () { | |||
if ( window.checkboxGeneratorInit ) return; | |||
window.checkboxGeneratorInit = true; | |||
function parseBool(v) { | |||
if (!v && v !== 0) return false; | |||
v = String(v).trim().toLowerCase(); | |||
return v === '1' || v === 'true' || v === 'yes' || v === 'checked'; | |||
} | |||
function makeSingle(container, spec) { | |||
var input = document.createElement('input'); | |||
input.type = 'checkbox'; | |||
if (spec.cls) input.className = spec.cls; | |||
if (spec.name) input.name = spec.name; | |||
input.checked = !!spec.checked; | |||
if (spec.id) input.id = spec.id; | |||
else input.id = 'chk-' + Math.random().toString(36).slice(2,9); | |||
if (spec.disabled) input.disabled = true; | |||
var label = document.createElement('label'); | |||
label.htmlFor = input.id; | |||
label.textContent = spec.label || ''; | |||
var wrapper = document.createElement('span'); | |||
wrapper.className = 'js-checkbox-gen-item'; | |||
wrapper.appendChild(input); | |||
wrapper.appendChild(label); | |||
container.appendChild(wrapper); | |||
} | |||
function buildFromContainer(container) { | |||
var itemsRaw = container.dataset.cboxItems; | |||
if (itemsRaw) { | |||
try { | |||
var arr = JSON.parse(itemsRaw); | |||
if (Array.isArray(arr)) { | |||
arr.forEach(function (it, idx) { | |||
if (typeof it === 'string') { | |||
makeSingle(container, { label: it }); | |||
} else if (typeof it === 'object' && it !== null) { | |||
makeSingle(container, { | |||
label: it.label || '', | |||
cls: it["class"] || it.cls || container.dataset.cboxClass || '', | |||
checked: parseBool(it.checked) || false, | |||
name: it.name || '', | |||
id: it.id || '', | |||
disabled: parseBool(it.disabled) || false | |||
}); | |||
} | |||
}); | |||
return; | |||
} | |||
} catch (e) { | |||
} | |||
} | |||
var spec = { | |||
label: container.dataset.cboxLabel || '', | |||
cls: container.dataset.cboxClass || '', | |||
checked: parseBool(container.dataset.cboxChecked), | |||
name: container.dataset.cboxName || '', | |||
id: container.dataset.cboxId || '', | |||
disabled: parseBool(container.dataset.cboxDisabled) | |||
}; | |||
makeSingle(container, spec); | |||
} | |||
function init() { | |||
var nodes = document.querySelectorAll('.js-checkbox-generator'); | |||
nodes.forEach(function (node) { | |||
if (node.dataset.checkboxInitialized) return; | |||
node.innerHTML = ''; | |||
buildFromContainer(node); | |||
node.dataset.checkboxInitialized = '1'; | |||
}); | |||
} | |||
if (document.readyState === 'loading') { | |||
document.addEventListener('DOMContentLoaded', init); | |||
} else { | |||
init(); | |||
} | |||
}() ); | |||
const currentPageTitle = document.title; | const currentPageTitle = document.title; | ||
$(document).ready(function() { | $(document).ready(function() { | ||