// Haddock JavaScript utilities import {getCookie, setCookie, clearCookie} from "./cookies"; const rspace = /\s\s+/g, rtrim = /^\s+|\s+$/g; function spaced(s: string) { return (" " + s + " ").replace(rspace, " "); } function trim(s: string) { return s.replace(rtrim, ""); } function hasClass(elem: Element, value: string) { const className = spaced(elem.className || ""); return className.indexOf( " " + value + " " ) >= 0; } function addClass(elem: Element, value: string) { const className = spaced(elem.className || ""); if ( className.indexOf( " " + value + " " ) < 0 ) { elem.className = trim(className + " " + value); } } function removeClass(elem: Element, value: string) { let className = spaced(elem.className || ""); className = className.replace(" " + value + " ", " "); elem.className = trim(className); } function toggleClass(elem: Element, valueOn: string, valueOff: string, bool?: boolean): boolean { if (bool == null) { bool = ! hasClass(elem, valueOn); } if (bool) { removeClass(elem, valueOff); addClass(elem, valueOn); } else { removeClass(elem, valueOn); addClass(elem, valueOff); } return bool; } function makeClassToggle(valueOn: string, valueOff: string): (elem: Element, bool?: boolean) => boolean { return function(elem, bool) { return toggleClass(elem, valueOn, valueOff, bool); } } const toggleShow = makeClassToggle("show", "hide"); function addMenuItem(html: string) { const menu = document.getElementById("page-menu"); if (menu && menu.firstChild) { const btn = menu.firstChild.cloneNode(false) as Element; btn.innerHTML = html; menu.appendChild(btn); } } function styles(): HTMLLinkElement[] { const es = Array.prototype.slice.call(document.getElementsByTagName("link")); return es.filter((a: HTMLLinkElement) => a.rel.indexOf("style") != -1 && a.title); } function addStyleMenu() { const as = styles(); let btns = ""; as.forEach((a) => { btns += "