68 lines
2.2 KiB
JavaScript
68 lines
2.2 KiB
JavaScript
function runMapInjectionScript() {
|
|
const targetTitle = "Active Map Chart";
|
|
const iframeHTML = `
|
|
<div style="height:500px; width:95%; overflow:hidden; border-radius: 8px; box-shadow: 0 0 4px #ccc;">
|
|
<iframe src="/standalone-active-map" style="width:100%; height:100%; border:none;"></iframe>
|
|
</div>
|
|
`;
|
|
|
|
function injectMap(retryCount = 0) {
|
|
let injected = false;
|
|
|
|
$('div.widget.dashboard-widget-box').each(function () {
|
|
const $widget = $(this);
|
|
const title = $widget.find('.widget-title span.ellipsis').text().trim();
|
|
|
|
console.log("Found widget title:", title);
|
|
|
|
if (title === targetTitle) {
|
|
const $chartContainer = $widget.find('.chart-container, .frappe-chart');
|
|
|
|
console.log("→ Checking chart container for:", title);
|
|
console.log("→ Chart container found:", $chartContainer.length);
|
|
console.log("→ Already injected:", $chartContainer.hasClass("map-injected"));
|
|
|
|
if ($chartContainer.length && !$chartContainer.hasClass("map-injected")) {
|
|
$chartContainer.addClass("map-injected").html(iframeHTML);
|
|
console.log("✅ Map injected into:", title);
|
|
injected = true;
|
|
}
|
|
}
|
|
});
|
|
|
|
if (!injected && retryCount < 10) {
|
|
setTimeout(() => injectMap(retryCount + 1), 500);
|
|
}
|
|
}
|
|
|
|
injectMap();
|
|
|
|
// Optionally reinject on dashboard refresh
|
|
$(document).on('dashboard-refresh', injectMap);
|
|
}
|
|
|
|
// Role-based access control: redirect non-Admin users to ASM dashboard
|
|
function checkRoleAndRedirect() {
|
|
if (!frappe.user_roles) return;
|
|
|
|
const isAdmin = frappe.user_roles.includes('Admin');
|
|
|
|
if (!isAdmin && window.location.pathname.startsWith("/app")) {
|
|
window.location.href = "/asm_app/dashboard";
|
|
}
|
|
}
|
|
|
|
// Run on first load
|
|
$(document).ready(function () {
|
|
runMapInjectionScript();
|
|
checkRoleAndRedirect();
|
|
});
|
|
|
|
// Also run when navigating via search or sidebar
|
|
frappe.router.on('change', function () {
|
|
// Small delay to let new page render
|
|
setTimeout(() => {
|
|
runMapInjectionScript();
|
|
checkRoleAndRedirect();
|
|
}, 300);
|
|
}); |