Skip to content

Commit

Permalink
web: Refactor/simplify pubmedtool.html
Browse files Browse the repository at this point in the history
  • Loading branch information
liamappelbe committed Jul 18, 2024
1 parent eaa85c8 commit e7557d4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 52 deletions.
62 changes: 23 additions & 39 deletions vetupdates/pubmedtool.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
class State {
constructor() {
this.ids = [];
this.errorIds = [];
this.errorText = '';
this.synths = [];
}
}
Expand All @@ -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() {
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -202,16 +196,10 @@
}

for (const [journal, articles] of articlesByJournal) {
// domTags.value += '<div>\n';
// domTags.value += ` <div class="sticky_heading">${journal}</div>\n`;
// domTags.value += ' <ul>\n';

// const heading = newElement('h2', domFormatted, [], journal);

const ul = newElement('ul', domFormatted);
for (const ai of articles) {
const aid = ai.aid;
const authors = cleanSynthAuthors(ai.authors);
const authors = ai.authors;
if (aid != null) {
domTags.value += `<li><pub-med ${
aid.pmid != null ? `pmid="${aid.pmid}"` : `pmcid="${aid.pmcid}"`
Expand All @@ -233,12 +221,8 @@
const cite = newDiv(li, ['cite'], cleanText(ai.date, '. '));
newLink(cite, ['doi'], ai.url, maybePrefix(ai.doi, 'doi: '));
}
// domTags.value += ' </ul>\n';
// domTags.value += '</div>\n\n';
}

state.ids = fulfilledIds;
state.errorIds = state.errorIds.concat(rejectedIds);
saveState();
fillDomFromState();

Expand Down Expand Up @@ -533,7 +517,7 @@ <h1>PubMed Formatter</h1>
<textarea id="idin" rows="20" cols="40"></textarea>
<br/>
<br/>
Bad IDs:
Errors:
<br/>
<textarea id="errors" rows="6" cols="40"></textarea>
</div>
Expand Down
19 changes: 6 additions & 13 deletions vetupdates/vetupdates_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e7557d4

Please sign in to comment.