silverstripe-betternavigator/javascript/betternavigator.js

62 lines
2.2 KiB
JavaScript
Raw Normal View History

// For reading cookies
2014-08-13 02:41:18 +02:00
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
2014-08-13 02:41:18 +02:00
var c = ca[i];
while (c.charAt(0) === ' ') {
2015-12-18 00:58:50 +01:00
c = c.substring(1);
2014-08-13 02:41:18 +02:00
}
if (c.indexOf(name) !== -1) {
2015-12-18 00:58:50 +01:00
return c.substring(name.length,c.length);
2014-08-13 02:41:18 +02:00
}
}
return "";
}
2015-02-08 17:33:26 +01:00
function initialiseBetterNavigator() {
// Dom elements
2015-12-18 00:58:50 +01:00
var BetterNavigator = document.getElementById("BetterNavigator");
var BetterNavigatorStatus = document.getElementById("BetterNavigatorStatus");
var BetterNavigatorLogoutLink = document.getElementById("BetterNavigatorLogoutLink");
var BetterNavigatorLogoutForm = document.getElementById("LogoutForm_BetterNavigatorLogoutForm");
2014-08-13 02:41:18 +02:00
// Toggle visibility of menu by clicking status
2015-12-18 00:58:50 +01:00
BetterNavigatorStatus.onclick = function () {
if (BetterNavigator.classList.contains('collapsed')) {
BetterNavigator.classList.add('open');
BetterNavigator.classList.remove('collapsed');
document.cookie = "BetterNavigator=open;path=/";
} else {
BetterNavigator.classList.add('collapsed');
BetterNavigator.classList.remove('open');
document.cookie = "BetterNavigator=collapsed;path=/";
}
2015-12-18 00:58:50 +01:00
};
2015-02-08 17:33:26 +01:00
// Restore menu state
2015-12-18 00:58:50 +01:00
if (getCookie('BetterNavigator') === 'open') {
BetterNavigator.classList.add('open');
BetterNavigator.classList.remove('collapsed');
2015-12-18 00:58:50 +01:00
}
if (BetterNavigatorLogoutForm) {
// Upgrade logout link to directly log users out instead of redirecting to logout form
BetterNavigatorLogoutLink.onclick = function (e) {
e.preventDefault();
BetterNavigatorLogoutForm.submit();
};
}
2015-02-08 17:33:26 +01:00
}
if (document.addEventListener && document.readyState === 'loading') {
2015-12-18 00:58:50 +01:00
document.addEventListener("DOMContentLoaded", function () {
// wait til DOM finishes loading
2015-12-18 00:58:50 +01:00
initialiseBetterNavigator();
});
2015-02-08 17:33:26 +01:00
} else {
// This is the case for IE8 and below OR already loaded document (e.g. when using async)
// initialise straight away - fine if script is loaded after BN dom element
2015-12-18 00:58:50 +01:00
initialiseBetterNavigator();
}