From 2b6a222bcfe497d97fb7cf4aaf438d7be22fcc3e Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Thu, 22 Dec 2022 13:03:08 -0500 Subject: [PATCH] Added functions to set/read cookies; added ability to make the collapsed nav sticky across refreshes --- assets/coffee/gargoyle.coffee | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/assets/coffee/gargoyle.coffee b/assets/coffee/gargoyle.coffee index b048205..32818cc 100644 --- a/assets/coffee/gargoyle.coffee +++ b/assets/coffee/gargoyle.coffee @@ -1,6 +1,8 @@ $(document).ready( -> $('#nav-toggle').on('click', toggleNav) + if getCookie('navCollapsed') == 'true' + $('body').addClass('collapsed') ) @@ -8,5 +10,26 @@ toggleNav = () -> bodyElem = $('body') if bodyElem.hasClass('collapsed') bodyElem.removeClass('collapsed') + setCookie('navCollapsed', 'false') else 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