From 57451741cfbc4a2f65a3c655c18ef5fc000b09a3 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Fri, 1 Mar 2024 12:49:13 +0100 Subject: [PATCH] Introduce warning topbar menu, allow following crates to see warnings for --- static/warnings.js | 125 +++++++++++++++++++++++++++++++ templates/base.html | 1 + templates/header/topbar_end.html | 2 + templates/header/warnings.html | 7 ++ templates/rustdoc/body.html | 1 + templates/style/_navbar.scss | 16 ++++ 6 files changed, 152 insertions(+) create mode 100644 static/warnings.js create mode 100644 templates/header/warnings.html diff --git a/static/warnings.js b/static/warnings.js new file mode 100644 index 000000000..799f55cad --- /dev/null +++ b/static/warnings.js @@ -0,0 +1,125 @@ +(function() { + function load(key, def = null) { + const value = window.localStorage.getItem(key); + if (value) { + try { + return JSON.parse(value); + } catch (ex) { + console.error(`Failed loading ${key} from local storage`, ex); + return def; + } + } else { + return def; + } + } + + function store(key, value) { + window.localStorage.setItem(key, JSON.stringify(value)); + } + + function create(tagName, attrs = {}, children = [], listeners = {}) { + const el = document.createElement(tagName); + for (const key of Object.keys(attrs)) { + if (typeof attrs[key] === "object") { + for (const subkey of Object.keys(attrs[key])) { + el[key].setProperty(subkey, attrs[key][subkey]); + } + } else { + el.setAttribute(key, attrs[key]); + } + } + el.append(...children); + for (const key of Object.keys(listeners)) { + el.addEventListener(key, listeners[key]); + } + return el; + } + + + if (!load("docs-rs-warnings-enabled", false)) { + return; + } + + const parentEl = document.getElementById("warnings-menu-parent"); + parentEl.removeAttribute("hidden"); + + const current = JSON.parse(document.getElementById("crate-metadata")?.innerText || null); + + const menuEl = document.getElementById("warnings-menu"); + + const followed = load("docs-rs-warnings-followed", []); + + function update() { + const children = []; + + if (followed.length > 0) { + children.push( + create("div", { class: "pure-g" }, [ + create("div", { class: "pure-u-1" }, [ + create("ul", { class: "pure-menu-list", style: { width: "100%" } }, [ + create("li", { class: "pure-menu-heading" }, [ + create("b", {}, ["Followed crates"]), + ]), + ...followed.map(name => ( + create("li", { class: "pure-menu-item followed" }, [ + create("a", { class: "pure-menu-link", href: `/${name}` }, [ + name, + ]), + create( + "a", + { class: "pure-menu-link remove", href: "#" }, + ["🗙"], + { + click: _ => { + const index = followed.indexOf(name); + followed.splice(index, 1); + store("docs-rs-warnings-followed", followed); + update(); + }, + }, + ), + ]) + )), + ]), + ]), + ]), + ); + } + + if (current && !followed.includes(current.name)) { + children.push( + create("div", { class: "pure-g" }, [ + create("div", { class: "pure-u-1" }, [ + create("ul", { class: "pure-menu-list", style: { width: "100%" } }, [ + create("li", { class: "pure-menu-item" }, [ + create("a", { class: "pure-menu-link", href: "#" }, [ + "Follow ", + create("b", {}, [current.name]), + ], { + click: () => { + const i = followed.findIndex(name => name > current.name); + if (i >= 0) { + followed.splice(i, 0, current.name); + } else { + followed.push(current.name); + } + store("docs-rs-warnings-followed", followed); + update(); + }, + }), + ]), + ]), + ]), + ]), + ); + } + + for (const child of children.slice(0, -1)) { + child.classList.add("menu-item-divided"); + } + + menuEl.replaceChildren(...children); + } + + update(); +})(); diff --git a/templates/base.html b/templates/base.html index 8085c8b39..b2ebad5ed 100644 --- a/templates/base.html +++ b/templates/base.html @@ -24,6 +24,7 @@ + diff --git a/templates/header/topbar_end.html b/templates/header/topbar_end.html index 92b7cfa4e..5723f5303 100644 --- a/templates/header/topbar_end.html +++ b/templates/header/topbar_end.html @@ -4,6 +4,8 @@ {# The global alert, if there is one #} {% include "header/global_alert.html" -%} + {% include "header/warnings.html" -%} +