use a cookie to store sidepage status (#12617)

This commit is contained in:
Frédéric Péters 2016-07-17 14:49:03 +02:00
parent 87d72ccc11
commit 9b62ad77f1
1 changed files with 45 additions and 11 deletions

View File

@ -225,6 +225,48 @@ var gadjo_js = gadjo_js || {};
}
$(function() {
var cookie_domain = window.location.hostname.split('.').slice(1).join('.');
function readCookie(name) { /* http://www.quirksmode.org/js/cookies.html */
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function set_sidepage_status(sidepage_status) {
storage.sidepage_status = sidepage_status;
if (cookie_domain) {
var date = new Date();
date.setTime(date.getTime() + (10 * 86400 * 1000)); /* a long week */
document.cookie = 'gadjo_sidepage_status=' + sidepage_status +
'; expires=' + date.toGMTString() +
'; domain=.' + cookie_domain +
'; path=/';
}
}
function get_sidepage_status() {
if (window.location.protocol == 'file:') {
/* don't open sidepage when loading from a file:// */
return 'collapsed';
}
var sidepage_status = null;
if (cookie_domain) {
sidepage_status = readCookie('gadjo_sidepage_status');
} else {
sidepage_status = storage.sidepage_status;
}
if (!sidepage_status &&
typeof(GADJO_DEFAULT_SIDEPAGE_STATUS) !== "undefined") {
return GADJO_DEFAULT_SIDEPAGE_STATUS;
}
return sidepage_status;
}
$(document).on('click.gadjo', 'a[rel=popup]', displayPopup);
if ($('#sidepage').length) {
var sidepage_button = $('#sidepage #applabel');
@ -232,20 +274,12 @@ var gadjo_js = gadjo_js || {};
$('#sidepage, #main').addClass('enable-transitions');
$('#sidepage, #main').toggleClass('sidepage-expanded');
if ($('#sidepage').hasClass('sidepage-expanded')) {
storage.sidepage_status = 'expanded';
set_sidepage_status('expanded');
} else {
storage.sidepage_status = null;
set_sidepage_status('collasped');
}
});
if (window.location.protocol == 'file:') {
/* don't open sidepage when loading from a file:// */
storage.sidepage_status = null;
}
if (storage.sidepage_status === undefined &&
typeof(GADJO_DEFAULT_SIDEPAGE_STATUS) !== "undefined") {
storage.sidepage_status = GADJO_DEFAULT_SIDEPAGE_STATUS;
}
if (storage.sidepage_status == 'expanded') {
if (get_sidepage_status() == 'expanded') {
$('#sidepage, #main').toggleClass('sidepage-expanded');
}
}