Added functions to set/read cookies; added ability to make the collapsed nav sticky across refreshes

This commit is contained in:
Gregory Ballantine 2022-12-22 13:03:08 -05:00
parent 10345d5238
commit 2b6a222bcf

View File

@ -1,6 +1,8 @@
$(document).ready( -> $(document).ready( ->
$('#nav-toggle').on('click', toggleNav) $('#nav-toggle').on('click', toggleNav)
if getCookie('navCollapsed') == 'true'
$('body').addClass('collapsed')
) )
@ -8,5 +10,26 @@ toggleNav = () ->
bodyElem = $('body') bodyElem = $('body')
if bodyElem.hasClass('collapsed') if bodyElem.hasClass('collapsed')
bodyElem.removeClass('collapsed') bodyElem.removeClass('collapsed')
setCookie('navCollapsed', 'false')
else else
bodyElem.addClass('collapsed') bodyElem.addClass('collapsed')
setCookie('navCollapsed', 'true')
getCookie = (cName) ->
name = cName + '='
cDecoded = decodeURIComponent(document.cookie)
#to be careful
cArr = cDecoded.split('; ')
res = undefined
cArr.forEach (val) ->
if val.indexOf(name) == 0
res = val.substring(name.length)
return
res
setCookie = (cName, cValue, expDays = 30) ->
date = new Date
date.setTime date.getTime() + expDays * 24 * 60 * 60 * 1000
expires = 'expires=' + date.toUTCString()
document.cookie = cName + '=' + cValue + '; ' + expires + '; path=/'
return