140 lines
3.8 KiB
JavaScript
140 lines
3.8 KiB
JavaScript
|
|
(function (window, document) {
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
function toArray(nodeList) {
|
||
|
|
return Array.prototype.slice.call(nodeList || []);
|
||
|
|
}
|
||
|
|
|
||
|
|
function initTaskCertTabs(root) {
|
||
|
|
if (!(root instanceof window.HTMLElement)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (root.dataset.tabsInit === '1') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var tabs = toArray(root.querySelectorAll('[data-tabs-tab]'));
|
||
|
|
var panels = toArray(root.querySelectorAll('[data-tabs-panel]'));
|
||
|
|
if (tabs.length === 0 || panels.length === 0) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
root.dataset.tabsInit = '1';
|
||
|
|
|
||
|
|
function activate(targetKey) {
|
||
|
|
var key = String(targetKey);
|
||
|
|
var activeTabKey = key;
|
||
|
|
|
||
|
|
tabs.forEach(function (tab) {
|
||
|
|
var isActive = tab.getAttribute('data-tabs-tab') === key;
|
||
|
|
tab.classList.toggle('is-active', isActive);
|
||
|
|
tab.setAttribute('aria-selected', isActive ? 'true' : 'false');
|
||
|
|
tab.setAttribute('tabindex', isActive ? '0' : '-1');
|
||
|
|
|
||
|
|
if (isActive) {
|
||
|
|
activeTabKey = tab.getAttribute('data-tab-key') || key;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
panels.forEach(function (panel) {
|
||
|
|
var isActive = panel.getAttribute('data-tabs-panel') === key;
|
||
|
|
panel.classList.toggle('is-active', isActive);
|
||
|
|
panel.hidden = !isActive;
|
||
|
|
});
|
||
|
|
|
||
|
|
toArray(root.querySelectorAll('[data-tabs-active-input]')).forEach(function (input) {
|
||
|
|
if (input instanceof window.HTMLInputElement) {
|
||
|
|
input.value = activeTabKey;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
tabs.forEach(function (tab) {
|
||
|
|
tab.addEventListener('click', function () {
|
||
|
|
activate(tab.getAttribute('data-tabs-tab'));
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
var initial = root.getAttribute('data-tabs-active');
|
||
|
|
var initialTab = null;
|
||
|
|
if (initial !== null) {
|
||
|
|
for (var i = 0; i < tabs.length; i += 1) {
|
||
|
|
if (tabs[i].getAttribute('data-tabs-tab') === initial) {
|
||
|
|
initialTab = tabs[i];
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (initialTab) {
|
||
|
|
activate(initialTab.getAttribute('data-tabs-tab'));
|
||
|
|
} else {
|
||
|
|
activate(tabs[0].getAttribute('data-tabs-tab'));
|
||
|
|
}
|
||
|
|
|
||
|
|
toArray(root.querySelectorAll('form[data-cycle-switch-form]')).forEach(function (form) {
|
||
|
|
if (!(form instanceof window.HTMLFormElement)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (form.dataset.cycleSwitchInit === '1') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
form.dataset.cycleSwitchInit = '1';
|
||
|
|
|
||
|
|
form.addEventListener('submit', function (event) {
|
||
|
|
var method = String(form.getAttribute('method') || 'get').toUpperCase();
|
||
|
|
if (method !== 'GET') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
event.preventDefault();
|
||
|
|
|
||
|
|
var action = form.getAttribute('action') || window.location.href;
|
||
|
|
var actionUrl;
|
||
|
|
try {
|
||
|
|
actionUrl = new window.URL(action, window.location.origin);
|
||
|
|
} catch (err) {
|
||
|
|
form.submit();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var params = new window.URLSearchParams(actionUrl.search || '');
|
||
|
|
var formData = new window.FormData(form);
|
||
|
|
formData.forEach(function (value, key) {
|
||
|
|
if (typeof value !== 'string') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
params.set(key, value);
|
||
|
|
});
|
||
|
|
|
||
|
|
params.delete('load_guard');
|
||
|
|
var query = params.toString();
|
||
|
|
var redirectUrl = actionUrl.pathname;
|
||
|
|
if (query !== '') {
|
||
|
|
redirectUrl += '?' + query + '&load_guard=/';
|
||
|
|
} else {
|
||
|
|
redirectUrl += '?load_guard=/';
|
||
|
|
}
|
||
|
|
|
||
|
|
if (actionUrl.hash) {
|
||
|
|
redirectUrl += actionUrl.hash;
|
||
|
|
}
|
||
|
|
|
||
|
|
window.location.assign(redirectUrl);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
function autoInit() {
|
||
|
|
toArray(document.querySelectorAll('[data-task-cert-tabs]')).forEach(initTaskCertTabs);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (document.readyState === 'loading') {
|
||
|
|
document.addEventListener('DOMContentLoaded', autoInit);
|
||
|
|
} else {
|
||
|
|
autoInit();
|
||
|
|
}
|
||
|
|
})(window, document);
|