silverstripe-betternavigator/javascript/betternavigator.js

45 lines
1.4 KiB
JavaScript
Raw Normal View History

2014-08-13 02:41:18 +02:00
//For reading cookies
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
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() {
2015-12-18 00:58:50 +01:00
//Dom elements
var BetterNavigator = document.getElementById("BetterNavigator");
var BetterNavigatorStatus = document.getElementById("BetterNavigatorStatus");
2014-08-13 02:41:18 +02:00
2015-12-18 00:58:50 +01:00
//Toggle visibility of menu by clicking status
BetterNavigatorStatus.onclick = function () {
BetterNavigator.className = BetterNavigator.className === 'collapsed' ? 'open' : 'collapsed';
//Set cookie to remember state
document.cookie = "BetterNavigator=" + BetterNavigator.className + ";path=/";
};
2015-02-08 17:33:26 +01:00
2015-12-18 00:58:50 +01:00
//Restore menu state
if (getCookie('BetterNavigator') === 'open') {
BetterNavigator.className = 'open';
}
2015-02-08 17:33:26 +01:00
}
if (document.addEventListener) {
2015-12-18 00:58:50 +01:00
document.addEventListener("DOMContentLoaded", function () {
//wait til DOM is ready
initialiseBetterNavigator();
});
2015-02-08 17:33:26 +01:00
} else {
2015-12-18 00:58:50 +01:00
//This is the case for IE8 and below
//initialise straight away - fine if script is loaded after BN dom element
initialiseBetterNavigator();
}