| 1 |
(function ($) { |
| 2 |
'use strict'; |
| 3 |
$(function () { |
| 4 |
var body = document.getElementById('wpbody-content'); |
| 5 |
if (!body) return; |
| 6 |
var roots = '#bkbg-template-catalog,#bkbg-dashboard,.bkbg-ai-settings'; |
| 7 |
var headers = '.bkbg-catalog-header,.bkbg-header,.bkbg-ai-settings-header'; |
| 8 |
var notices = '.notice,.updated,.error,.update-nag,.fs-notice'; |
| 9 |
var slot = document.createElement('div'); |
| 10 |
slot.className = 'bkbg-admin-notices'; |
| 11 |
slot.setAttribute('role', 'region'); |
| 12 |
slot.setAttribute('aria-label', bkbgAdminNotices.label); |
| 13 |
slot.hidden = true; |
| 14 |
var queued = false; |
| 15 |
|
| 16 |
function placeNotices() { |
| 17 |
queued = false; |
| 18 |
var root = body.querySelector(roots); |
| 19 |
var header = root && root.querySelector(headers); |
| 20 |
if (!header) return; |
| 21 |
// Retain the original nodes and their dismissal handlers when catalog |
| 22 |
// navigation replaces the entire root through AJAX. |
| 23 |
if (header.nextElementSibling !== slot) header.after(slot); |
| 24 |
body.querySelectorAll(notices).forEach(function (notice) { |
| 25 |
if (slot.contains(notice)) return; |
| 26 |
var parent = notice.parentElement; |
| 27 |
if (!parent || parent.closest(notices)) return; |
| 28 |
if (header.contains(notice) || parent === root || parent === body) { |
| 29 |
slot.appendChild(notice); |
| 30 |
} |
| 31 |
}); |
| 32 |
slot.hidden = !slot.children.length; |
| 33 |
} |
| 34 |
function schedule() { |
| 35 |
if (queued) return; |
| 36 |
queued = true; |
| 37 |
window.requestAnimationFrame(placeNotices); |
| 38 |
} |
| 39 |
placeNotices(); |
| 40 |
// Only child changes matter: no attribute polling or recurring timer. |
| 41 |
new MutationObserver(schedule).observe(body, { childList: true, subtree: true }); |
| 42 |
}); |
| 43 |
})(jQuery); |
| 44 |
|