// JavaScript File: Sticky Header
// Version: 20171026-001
//
// Writted by: Jason Monds
// Date: Oct 26, 2017
//
// Description: Place the "level" menu under the "SiteHeaderPanel" area
//
// MODIFICATIONS:
// 30-Nov-2017: Jason Monds SDSJBM:25430
// Auto-center the dark-header content
// 26-Feb-2018: Jason Monds SDSJBM:25789
// Remove a sticky header when logged in
// Never change between sticky and static header
// when the menu is clicked, just use ScrollBars class
// ===========================================
var locateLevelMenuTimerStarted = 0;
var scrollManagementTimerStarted = 0;
var isMenuOpen = false;
var isAutoScrollToTopRunning = false;
var isAutoScrollToTopExecuted = false;
var autoScrollToTopTimerStarted = 0;
var dynamicDebugLevel = 1;
// -------------------------------------------
// Resize functions
// -------------------------------------------
// SDSJBM:Make the window resize all the time
bindEvent(window, 'resize', locateLevelMenuTimer);
// Initializes global and proxy default variables.
$(document).ready(function () {
$('#site-debug').append('locateLevelMenu.ready: Loaded
');
scrollManagement();
});
$(document).resize(function () {
$('#site-debug').append('locateLevelMenu.resize: Loaded
');
locateLevelMenuTimer();
});
// Auto Scroll to Top
function autoScrollToTopTimer() {
// SDSJBM:25789: Disable autoScrollToTop
return;
if (
isAutoScrollToTopExecuted == false &&
autoScrollToTopTimerStarted == 0
) {
isAutoScrollToTopRunning = true;
setTimeout(autoScrollToTop, 200);
autoScrollToTopTimerStarted = 1;
}
if (!isAutoScrollToTopRunning) {
// Allow another call to be ran
autoScrollToTopTimerStarted = 0;
}
}
function autoScrollToTop() {
if (isAutoScrollToTopRunning) {
var $levelMenu = $('.TabMain .current.canapps-family > .level');
var levelMenuHeight = 0;
if (!$levelMenu.length) {
levelMenuHeight = $('#SM_SiteHeaderPanel').height();
} else {
levelMenuHeight = $levelMenu.height();
}
var windowScrollAmount = $(window).scrollTop();
//$("#site-debug").append("autoScrollToTop: Menu " + levelMenuHeight.toString() + ", vs Window " + windowScrollAmount.toString() + "
");
if (windowScrollAmount > 0) {
var scrollJump = windowScrollAmount / 10;
$(window).scrollTop(windowScrollAmount - scrollJump);
setTimeout(autoScrollToTop, 30);
} else {
// Stop scrolling
isAutoScrollToTopRunning = false;
isAutoScrollToTopExecuted = true;
autoScrollToTopTimerStarted = 0;
}
}
}
// Small header on scrolling
$(window).scroll(function () {
scrollManagement();
});
function scrollManagementTimer() {
if (scrollManagementTimerStarted == 0) {
// Check if a menu is open
// Wide view uses the level style display: none to hide the menu
var canAppMenuDisplayValue = $(
'.TabMain .current.canapps-family > .level',
).css('visibility');
// Check root tabs if they're open
var rootTabMenuItems = $(
'.TabMain .sdsLevel1 .canAppListItem > .canAppSubMenu',
);
var rootTabMenuItemsVisible = $(rootTabMenuItems).filter(function (
index,
) {
return $(this).css('visibility') == 'visible';
});
var rootTabMenuItemVisible =
clientWidth > 749 && rootTabMenuItemsVisible.length > 0;
// Is either the CANapps menu or a Root tab sub menu open?
var isCanAppMenuOpen =
canAppMenuDisplayValue == 'visible' || rootTabMenuItemVisible;
/**************
if (clientWidth <= 749)
{
// Mobile view uses the [>] class open
var levelListSelector = $(".TabMain .current.canapps-family > .list-selector");
isListSelectorOpen = $(levelListSelector).hasClass("open");
if (isListSelectorOpen && !isCanAppMenuOpen)
{
// Remove the open class, when I do this the mobile menu [=] never opens
$(levelListSelector).removeClass("open");
}
}
**************/
var isSubMenuOpen = null;
isMenuOpen = isCanAppMenuOpen;
// Reset the auto-scroll for the next time the menu is opened
if (!isMenuOpen && isAutoScrollToTopExecuted)
isAutoScrollToTopExecuted = false;
setTimeout(scrollManagement, 200);
scrollManagementTimerStarted = 1;
}
}
function scrollManagement() {
var windowScrollAmount = $(window).scrollTop();
// Allow for a sticky header
// SDSJBM:25789: Remove the sticky header if logged in
var isLoggedIn = $('form').hasClass('LoggedIn');
var isSticky = $('.SiteHeaderPanel').hasClass('sticky');
// if (windowScrollAmount >= 0 && !isMenuOpen && !isLoggedIn)
if (!isLoggedIn) {
$('.SiteHeaderPanel').addClass('sticky');
} else $('.SiteHeaderPanel').removeClass('sticky');
// Allow for the header to shrink
if (windowScrollAmount > 20) $('.SiteHeaderPanel').addClass('small');
else $('.SiteHeaderPanel').removeClass('small');
// When using a mobile screen (<=749px) items go under the first header row
var topHeaderHeight = $('.SiteHeaderPanel .header-dark').height();
var menuButton = $(
'form.NotLoggedIn:not(.cantab-menu) .current.canapps-family > .list-selector',
);
if (topHeaderHeight != null && menuButton != null) {
// Auto center the [=] menu button
var menuButtonHeight = $(menuButton).outerHeight();
$(menuButton).css('position', 'fixed');
// Base the position on the scrollbar until the header is off the page
var menuButtonTop = (topHeaderHeight - menuButtonHeight) / 2;
var windowScrollAmount = $(window).scrollTop();
if (isSticky) windowScrollAmount = 0;
$(menuButton).css('top', menuButtonTop - windowScrollAmount);
} else {
// Stop changing the button location
$(menuButton).css('position', 'inherit');
$(menuButton).css('top', inherit);
}
// If the menu is open and off the page, auto scroll to the top of the page
windowScrollAmount = $(window).scrollTop();
var $levelMenu = $('.TabMain .current.canapps-family > .level');
var levelMenuHeight = 0;
if (!$levelMenu.length) {
levelMenuHeight = $('#SM_SiteHeaderPanel').height();
} else {
levelMenuHeight = $levelMenu.height();
}
if (dynamicDebugLevel > 0) {
$('#site-debug').append(
'scrollManagement[1]: isMenuOpen ' +
isMenuOpen.toString() +
', isAutoScrollToTopRunning ' +
isAutoScrollToTopRunning.toString() +
', isAutoScrollToTopExecuted ' +
isAutoScrollToTopExecuted.toString() +
', levelMenuHeight ' +
levelMenuHeight.toString() +
', windowScrollAmount ' +
windowScrollAmount.toString(),
);
dynamicDebugLevel = dynamicDebugLevel - 1;
}
if (isMenuOpen && !isAutoScrollToTopRunning && !isAutoScrollToTopExecuted) {
$('#site-debug').append(
'scrollManagement[2]: isMenuOpen ' +
isMenuOpen.toString() +
', isAutoScrollToTopRunning ' +
isAutoScrollToTopRunning.toString() +
', isAutoScrollToTopExecuted ' +
isAutoScrollToTopExecuted.toString() +
', levelMenuHeight ' +
levelMenuHeight.toString() +
', windowScrollAmount ' +
windowScrollAmount.toString(),
);
autoScrollToTopTimer();
}
// Re-layout the content
locateLevelMenuTimer();
// Check for open/closed menus
scrollManagementTimerStarted = 0;
scrollManagementTimer();
}
function locateLevelMenuTimer() {
if (locateLevelMenuTimerStarted == 0) {
setTimeout(locateLevelMenu, 200);
locateLevelMenuTimerStarted = 1;
}
}
// Place the "level" menu under the "SiteHeaderPanel" area
// +- SiteHeaderPanel -----------
// | +- TabMenu ---------------------
// | |
// | +-------------------------------
// +---------------------------------
// +- level ------------------------- ... This menu has to be place here
// | menu details...
// +---------------------------------
function locateLevelMenu() {
//$("#site-debug").append("locateLevelMenu: Start, ");
try {
// Fix the SiteMainPanel to appear under the sticky header
// ===========================================
// Get the panels to be updated
// When using the full screen, items go under the full 2-row header
siteHeaderHeight = $('.SiteHeaderPanel').height();
// When using a mobile screen (<=749px) items go under the first header row
var topHeaderHeight = $('.SiteHeaderPanel .header-dark').height();
// ===========================================
// Determine if we're using a sticky header
// 1. If no menu is open, then we can use sticky
// ===========================================
var isSticky = $('.SiteHeaderPanel').hasClass('sticky');
// Center the header items if we are at least xx wide
if (clientWidth >= 570) {
// Auto center the Company Name
// ============================
var companyName = $('.modGroupId-19.moduleId-157');
var companyNameHeight = $(companyName).outerHeight();
$(companyName).css(
'margin-top',
(topHeaderHeight - companyNameHeight) / 2,
);
// Auto center the Phone Number
// ============================
var headerLink = $('.modGroupId-19.moduleId-143');
var headerLinkHeight = $(headerLink).outerHeight();
$(headerLink).css(
'margin-top',
(topHeaderHeight - headerLinkHeight) / 2,
);
// Auto center the [=] menu button
var menuButton = $(
'form.NotLoggedIn:not(.cantab-menu) .current.canapps-family > .list-selector',
);
if (menuButton != null && !isSticky) {
var menuButtonHeight = $(menuButton).outerHeight();
$(menuButton).css('position', 'fixed');
// Base the position on the scrollbar until the header is off the page
var menuButtonTop = (topHeaderHeight - menuButtonHeight) / 2;
var windowScrollAmount = $(window).scrollTop();
$(menuButton).css('top', menuButtonTop);
}
}
// ===========================================
// Determine where the menu and content will be spaced
// based on the header height -- which changes based
// on clientWidth
// ===========================================
var isLoggedIn =
$('form.LoggedIn').length > 0 || $('form.cantab-menu').length > 0;
var headerHeight = topHeaderHeight;
if (clientWidth > 749 || isLoggedIn) headerHeight = siteHeaderHeight;
// Position under the first-row header.
headerPanel = $('.dark-header');
siteMainPanel = $('.SiteMainPanel');
if (headerHeight != null) {
// When logged in and viewing a public tab, use the height of the "canapps-family" menu item
var canAppRootMenuHeight = $(
'.TabMenu .current.canapps-family',
).outerHeight();
// Fix the submenu (level)
$('.sdsDropDown .level').css('position', 'absolute');
if (clientWidth <= 749) $('.sdsDropDown .level').css('top', 'auto');
else if (isLoggedIn)
$('.sdsDropDown .level').css('top', canAppRootMenuHeight);
else $('.sdsDropDown .level').css('top', headerHeight);
// Fix the Site main content
$(siteMainPanel).css('margin-top', 'auto');
if (isSticky) $(siteMainPanel).css('margin-top', headerHeight);
else $(siteMainPanel).css('margin-top', 'auto');
}
} catch (err) {
// Don't worry about the errors for now.
siteDebugMessage('locateLevelMenu: ERROR: ' + err.message + '
');
}
//$("#site-debug").append("
");
// Allow another locate to be called
locateLevelMenuTimerStarted = 0;
}