123 lines
3.1 KiB
JavaScript
123 lines
3.1 KiB
JavaScript
|
|
(function (window, document) {
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
function toArray(nodeList) {
|
||
|
|
return Array.prototype.slice.call(nodeList || []);
|
||
|
|
}
|
||
|
|
|
||
|
|
function dayTokenLocal() {
|
||
|
|
var now = new Date();
|
||
|
|
var year = String(now.getFullYear());
|
||
|
|
var month = String(now.getMonth() + 1).padStart(2, '0');
|
||
|
|
var day = String(now.getDate()).padStart(2, '0');
|
||
|
|
return year + '-' + month + '-' + day;
|
||
|
|
}
|
||
|
|
|
||
|
|
function storageKey(viewerId, dayToken) {
|
||
|
|
return 'taskCertEscOverlayAck:v1:' + viewerId + ':' + dayToken;
|
||
|
|
}
|
||
|
|
|
||
|
|
function hasAck(key) {
|
||
|
|
try {
|
||
|
|
return window.localStorage.getItem(key) === '1';
|
||
|
|
} catch (error) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function setAck(key) {
|
||
|
|
try {
|
||
|
|
window.localStorage.setItem(key, '1');
|
||
|
|
return true;
|
||
|
|
} catch (error) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function closeAndCleanup(root, overlay) {
|
||
|
|
if (overlay) {
|
||
|
|
overlay.hidden = true;
|
||
|
|
}
|
||
|
|
document.body.classList.remove('task-cert-user-escalation-overlay-open');
|
||
|
|
if (root && root.parentNode) {
|
||
|
|
root.parentNode.removeChild(root);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function initEscalationOverlay(root) {
|
||
|
|
if (!(root instanceof window.HTMLElement)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (root.dataset.overlayInit === '1') {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
root.dataset.overlayInit = '1';
|
||
|
|
|
||
|
|
var overlay = root.querySelector('.task-cert-user-escalation-overlay');
|
||
|
|
if (!(overlay instanceof window.HTMLElement)) {
|
||
|
|
closeAndCleanup(root, null);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var viewerId = parseInt(root.getAttribute('data-viewer-id') || '0', 10);
|
||
|
|
if (!viewerId || viewerId <= 0) {
|
||
|
|
closeAndCleanup(root, overlay);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var key = storageKey(viewerId, dayTokenLocal());
|
||
|
|
if (hasAck(key)) {
|
||
|
|
closeAndCleanup(root, overlay);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var myTasksUrl = root.getAttribute('data-my-tasks-url') || '';
|
||
|
|
var myTasksLink = root.querySelector('[data-overlay-my-tasks]');
|
||
|
|
var ackButton = root.querySelector('[data-overlay-ack]');
|
||
|
|
|
||
|
|
if (myTasksLink instanceof window.HTMLAnchorElement && myTasksUrl !== '') {
|
||
|
|
myTasksLink.href = myTasksUrl;
|
||
|
|
}
|
||
|
|
|
||
|
|
overlay.hidden = false;
|
||
|
|
document.body.classList.add('task-cert-user-escalation-overlay-open');
|
||
|
|
|
||
|
|
if (ackButton instanceof window.HTMLButtonElement) {
|
||
|
|
ackButton.focus();
|
||
|
|
ackButton.addEventListener('click', function () {
|
||
|
|
setAck(key);
|
||
|
|
closeAndCleanup(root, overlay);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
if (myTasksLink instanceof window.HTMLAnchorElement) {
|
||
|
|
myTasksLink.addEventListener('click', function () {
|
||
|
|
setAck(key);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function autoInit() {
|
||
|
|
var roots = toArray(document.querySelectorAll('[data-task-cert-escalation-overlay]'));
|
||
|
|
if (roots.length === 0) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
var primary = roots[0];
|
||
|
|
for (var i = 1; i < roots.length; i += 1) {
|
||
|
|
if (roots[i] && roots[i].parentNode) {
|
||
|
|
roots[i].parentNode.removeChild(roots[i]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
initEscalationOverlay(primary);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (document.readyState === 'loading') {
|
||
|
|
document.addEventListener('DOMContentLoaded', autoInit);
|
||
|
|
} else {
|
||
|
|
autoInit();
|
||
|
|
}
|
||
|
|
})(window, document);
|
||
|
|
|