|
| 1 | +(function() { |
| 2 | + function load(key, def = null) { |
| 3 | + const value = window.localStorage.getItem(key) |
| 4 | + if (value) { |
| 5 | + try { |
| 6 | + return JSON.parse(value) |
| 7 | + } catch (ex) { |
| 8 | + console.error(`Failed loading ${key} from local storage`, ex) |
| 9 | + return def |
| 10 | + } |
| 11 | + } else { |
| 12 | + return def |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + function store(key, value) { |
| 17 | + window.localStorage.setItem(key, JSON.stringify(value)) |
| 18 | + } |
| 19 | + |
| 20 | + function create(tagName, attrs = {}, children = [], listeners = {}) { |
| 21 | + const el = document.createElement(tagName) |
| 22 | + for (const key of Object.keys(attrs)) { |
| 23 | + if (typeof attrs[key] === 'object') { |
| 24 | + for (const subkey of Object.keys(attrs[key])) { |
| 25 | + el[key].setProperty(subkey, attrs[key][subkey]) |
| 26 | + } |
| 27 | + } else { |
| 28 | + el.setAttribute(key, attrs[key]) |
| 29 | + } |
| 30 | + } |
| 31 | + el.append(...children) |
| 32 | + for (const key of Object.keys(listeners)) { |
| 33 | + el.addEventListener(key, listeners[key]) |
| 34 | + } |
| 35 | + return el |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + if (!load('docs-rs-warnings-enabled', false)) { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + const parentEl = document.getElementById('warnings-menu-parent') |
| 44 | + parentEl.removeAttribute('hidden') |
| 45 | + |
| 46 | + const current = JSON.parse(document.getElementById("crate-metadata")?.innerText || null) |
| 47 | + |
| 48 | + const menuEl = document.getElementById('warnings-menu') |
| 49 | + |
| 50 | + const followed = load('docs-rs-warnings-followed', []) |
| 51 | + |
| 52 | + function update() { |
| 53 | + const children = [] |
| 54 | + |
| 55 | + if (followed.length > 0) { |
| 56 | + children.push( |
| 57 | + create('div', { class: 'pure-g' }, [ |
| 58 | + create('div', { class: 'pure-u-1' }, [ |
| 59 | + create('ul', { class: 'pure-menu-list', style: { width: '100%' } }, [ |
| 60 | + create('li', { class: 'pure-menu-heading' }, [ |
| 61 | + create('b', {}, ['Followed crates']), |
| 62 | + ]), |
| 63 | + ...followed.map(name => ( |
| 64 | + create('li', { class: 'pure-menu-item followed' }, [ |
| 65 | + create('a', { class: 'pure-menu-link', href: `/${name}` }, [ |
| 66 | + name, |
| 67 | + ]), |
| 68 | + create('a', { class: 'pure-menu-link remove', href: '#' }, ['🗙'], { |
| 69 | + click: (ev) => { |
| 70 | + const index = followed.indexOf(name) |
| 71 | + followed.splice(index, 1) |
| 72 | + store('docs-rs-warnings-followed', followed) |
| 73 | + update() |
| 74 | + }, |
| 75 | + }), |
| 76 | + ]) |
| 77 | + )), |
| 78 | + ]), |
| 79 | + ]), |
| 80 | + ]), |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + if (current && !followed.includes(current.name)) { |
| 85 | + children.push( |
| 86 | + create('div', { class: 'pure-g' }, [ |
| 87 | + create('div', { class: 'pure-u-1' }, [ |
| 88 | + create('ul', { class: 'pure-menu-list', style: { width: '100%' } }, [ |
| 89 | + create('li', { class: 'pure-menu-item' }, [ |
| 90 | + create('a', { class: 'pure-menu-link', href: '#' }, [ |
| 91 | + "Follow ", |
| 92 | + create('b', {}, [current.name]), |
| 93 | + ], { |
| 94 | + click: () => { |
| 95 | + const index = followed.findIndex((name) => name > current.name) |
| 96 | + if (index >= 0) { |
| 97 | + followed.splice(index, 0, current.name) |
| 98 | + } else { |
| 99 | + followed.push(current.name) |
| 100 | + } |
| 101 | + store('docs-rs-warnings-followed', followed) |
| 102 | + update() |
| 103 | + } |
| 104 | + }), |
| 105 | + ]), |
| 106 | + ]), |
| 107 | + ]), |
| 108 | + ]), |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + for (const child of children.slice(0, -1)) { |
| 113 | + child.classList.add('menu-item-divided') |
| 114 | + } |
| 115 | + |
| 116 | + menuEl.replaceChildren(...children) |
| 117 | + } |
| 118 | + |
| 119 | + update() |
| 120 | +})() |
0 commit comments