class Navigation { static initialize() { Navigation.allSubviews = [ ]; Navigation.element = document.body.querySelector("tab-bar"); //Process the tab bar like a view controller. UIKit.processViewControllerElement(Navigation.element, Navigation); Navigation.barItems = document.body.querySelectorAll("tab-bar bar-button-item:not(.user-menu-button)"); for (var i = 0; i < Navigation.barItems.length; i++) { Navigation.barItems[i].addEventListener("click", Navigation.onBarItemSelected); } //Bind user listener, which will then bind a role listener. TimeCards.dataManager.addDataListener("user", { is_session_user: true }, Navigation.onSessionUserUpdate); } /** * Hides all elements from the navigation that should not be visible on the login screen. */ static displayForLogin() { Navigation.element.classList.add("login-view"); } /** * Shows all elements again that were not visible on the login screen. */ static endDisplayForLogin() { Navigation.element.classList.remove("login-view"); } /** * Called whenever the session user changes. * Will make sure we have a data listener on the user's role. * @param {number} userId * @param {object} user */ static onSessionUserUpdate(userId, user) { //Remove existing role listener. TimeCards.dataManager.removeDataListener("user_role", Navigation.onRoleUpdate); //Add new role listener. TimeCards.dataManager.addDataListener("user_role", { role_id: user.id_role }, Navigation.onRoleUpdate); } /** * Called whenever the session user's role changes. * @param {number} roleId * @param {object} role */ static onRoleUpdate(roleId, role) { Navigation.onHashChanged(); } static onHashChanged(event) { const hash = window.location.hash; let foundItem = false; for (let i = 0; i < Navigation.barItems.length; i++) { const item = Navigation.barItems[i]; if (item.getAttribute("target") == hash && Navigation.hasPermissionForItem(item)) { if (item.className.indexOf("active") == -1) { item.classList.add("active"); } foundItem = true; } else { item.classList.remove("active"); } } //If no item was found (either invalid hash or missing permission), select first available item. //NOTE: This also overrides UIKit's initial view controller behavior. if (!foundItem) { for (let i = 0; i < Navigation.barItems.length; i++) { const item = Navigation.barItems[i]; //Do not select if no permission. if (!Navigation.hasPermissionForItem(item)) { continue; } //Do not select if no target attribute. if (!item.hasAttribute("target")) { continue; } const redirectHash = item.getAttribute("target"); //Do not select if we are already trying to select this one. if (redirectHash == hash) { continue; } //Do not select if no hash target. if (!redirectHash.startsWith("#")) { continue; } //Redirect to this item's target. window.location.hash = redirectHash; break; } } } static hasPermissionForItem(item) { for (const className of item.classList) { if (!className.startsWith("require_")) { continue; } if ((className.startsWith("require_w__") && !Authentication.isResourceAvailable(className.replace("require_w__", ""), "w")) || (className.startsWith("require__") && !Authentication.isResourceAvailable(className.replace("require__", ""), "r"))) { return false; } } return true; } static onBarItemSelected(event) { var target = event.currentTarget.getAttribute("target"); if (!target) { return; } if (target.startsWith("#")) { window.location.hash = target; return; } } static onUserIconClicked(event) { var userMenu = UIKit.getPopoverById("user-menu"); //Inform the context menu about the summoning view controller and view. userMenu.setViewController(Navigation); //userMenu.setView(Navigation); userMenu.showForElement(event.currentTarget, "right"); event.stopPropagation(); } static onLogOutMenuItemPressed(event) { Authentication.logOut(); } } window.addEventListener("hashchange", Navigation.onHashChanged);