From e7557d46f8c29646782574de7ddbf66c73af8461 Mon Sep 17 00:00:00 2001 From: Liam Appelbe Date: Wed, 17 Jul 2024 19:52:27 +1000 Subject: [PATCH] web: Refactor/simplify pubmedtool.html --- vetupdates/pubmedtool.html | 62 ++++++++++++--------------------- vetupdates/vetupdates_search.js | 19 ++++------ 2 files changed, 29 insertions(+), 52 deletions(-) diff --git a/vetupdates/pubmedtool.html b/vetupdates/pubmedtool.html index d6e6fe6..75ecd85 100644 --- a/vetupdates/pubmedtool.html +++ b/vetupdates/pubmedtool.html @@ -35,7 +35,7 @@ class State { constructor() { this.ids = []; - this.errorIds = []; + this.errorText = ''; this.synths = []; } } @@ -56,18 +56,19 @@ function loadState() { const saved = window.localStorage.getItem("save"); - if (saved == null) { - state = new State(); - } else { - state = JSON.parse(saved); - if (state.ids == null) state.ids = []; - if (state.errorIds == null) state.errorIds = []; - if (state.synths == null) state.synths = []; + state = new State(); + if (saved != null) { + jsonState = JSON.parse(saved); + state.ids = jsonState[0] ?? []; + state.synths = jsonState[1] ?? []; } } function saveState() { - window.localStorage.setItem("save", JSON.stringify(state)); + window.localStorage.setItem("save", JSON.stringify([ + state.ids, + state.synths, + ])); } function clearState() { @@ -76,14 +77,14 @@ function fillDomFromState() { domIds.value = state.ids.join('\n'); - domErrors.value = state.errorIds.join('\n'); + domErrors.value = state.errorText; emptyDiv(domBottom); for (const s of state.synths) addSynth(s); } function fillStateFromDom() { state.ids = domIds.value.split(/[\n\t ,]+/).filter(id => id != ''); - state.errorIds = domErrors.value.split(/[\n\t ,]+/).filter(id => id != ''); + state.errorText = domErrors.value; state.synths = []; for (const synth of domBottom.getElementsByClassName('synth')) { const f = (cls) => synth.getElementsByClassName('synth-' + cls)[0].value; @@ -93,6 +94,13 @@ } } +function addError(msg) { + if (state.errorText != '') { + state.errorText += '\n'; + } + state.errorText += msg; +} + function newButton( parent, classes = [], text = null, onclick = null) { const btn = newDiv(parent, classes, text); @@ -156,39 +164,25 @@ } } - /*function cleanSynthAuthor(a) { - const aa = cleanText(a).split(' '); - let b = ''; - for (let i = 0; i < aa.length - 1; ++i) { - b += aa[i][0].toUpperCase(); - } - return `${b} ${fixCase(aa[aa.length - 1])}`; - } - const cleanSynthAuthors = a => - cleanText(a).split(',').map(cleanSynthAuthor).join(', ');*/ - const cleanSynthAuthors = a => a; - domWrap.classList.add('loading'); domLoad.classList.add('loading'); emptyDiv(domFormatted); domTags.value = ''; fillStateFromDom(); + state.errorText = ''; saveState(); - const fulfilledIds = []; - const rejectedIds = []; const results = await Promise.all(state.ids.map(getArticle)); domWrap.classList.remove('loading'); domLoad.classList.remove('loading'); for (const ar of results) { if (ar.err != null) { - rejectedIds.push(ar.id); + addError(`Invalid ID: ${ar.id}`); console.error(ar.id, ar.err); continue; } - fulfilledIds.push(ar.id); } const articlesByJournal = new Map(); @@ -202,16 +196,10 @@ } for (const [journal, articles] of articlesByJournal) { - // domTags.value += '
\n'; - // domTags.value += `
${journal}
\n`; - // domTags.value += '
diff --git a/vetupdates/vetupdates_search.js b/vetupdates/vetupdates_search.js index fd14662..cffc447 100644 --- a/vetupdates/vetupdates_search.js +++ b/vetupdates/vetupdates_search.js @@ -47,9 +47,7 @@ const kMajorTags = new Set([ 'trauma', ]); -function isMajorTag(tag) { - return kMajorTags.has(tag.toLowerCase().trim()); -} +function isMajorTag(tag) { return kMajorTags.has(tag.toLowerCase().trim()); } async function asyncRequest(url) { let errorCode = null; @@ -217,17 +215,13 @@ function newPubMedTag(parent, pmid) { } const reClean = /[^a-zA-Z0-9]+/g; -function cleanText(t) { - return t.replaceAll(reClean, ' ').trim(); -} +function cleanText(t) { return t.replaceAll(reClean, ' ').trim(); } function fixCase(t) { return t.slice(0, 1).toUpperCase() + t.slice(1).toLowerCase(); } -function cleanTitle(t) { - return t.split(' ').map(fixCase).join(' '); -} +function cleanTitle(t) { return t.split(' ').map(fixCase).join(' '); } function removeThe(t) { if (t.toLowerCase().startsWith('the ')) return t.substring(4); @@ -360,10 +354,9 @@ async function buildVetUpdatesSearch(node) { } class VetUpdatesSearch extends HTMLElement { - constructor() { - super(); - buildVetUpdatesSearch(this); - } + constructor() { super(); } + connectedCallback() { buildVetUpdatesSearch(this); } + attributeChangedCallback() { buildVetUpdatesSearch(this); } } function buildDropdownMenu(node) {