From acb9b0dfb06e1cc3c900eecad2eac883c9f77d80 Mon Sep 17 00:00:00 2001 From: Koen Lagveen Date: Sat, 8 Jul 2023 15:46:16 +0200 Subject: [PATCH] get fonts data using fetch --- index.js | 66 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index b31b6e1..9bc6db0 100644 --- a/index.js +++ b/index.js @@ -110,7 +110,6 @@ function renderSelectList () { '' let favoritesMap = {} let favorites = [] - const ajax = new XMLHttpRequest() document.getElementById('select-font').innerHTML = '' @@ -125,23 +124,16 @@ function renderSelectList () { console.error('could not render favorites', err) } - ajax.onreadystatechange = () => { - const fonts = [] - const authors = [] - if (ajax.readyState === 4 && ajax.status === 200) { - fontData = ajax.response - - Object.keys(fontData).forEach((key) => { - const v = fontData[key] - v.alias = key - fonts.push(v) - if (authors.indexOf(v.author) < 0) { - authors.push(v.author) - } - }) - } - + const renderAuthors = (authors) => { authors.sort() + authors.forEach((author) => { + const option = document.createElement('option') + option.innerHTML = author + document.getElementById('authors-list').querySelector('.other').appendChild(option) + }) + } + + const renderFonts = (fonts) => { fonts.sort((a, b) => { if (favoritesMap[a.alias] && !favoritesMap[b.alias]) { return -1 @@ -158,12 +150,6 @@ function renderSelectList () { return 0 }) - authors.forEach((author) => { - const option = document.createElement('option') - option.innerHTML = author - document.getElementById('authors-list').querySelector('.other').appendChild(option) - }) - fonts.forEach((v) => { const option = document.createElement('div') @@ -177,13 +163,33 @@ function renderSelectList () { document.getElementById('select-font').appendChild(option) }) - - selectFont() - new Filters(fontData).init() } - ajax.responseType = 'json' - ajax.open('GET', 'fonts.json', true) - ajax.send() + + fetch('fonts.json') + .then((response) => { + if (response.ok) { + return response.json() + } else { + console.error(response.status + ': ' + response.statusText) + } + }).then((data) => { + const fonts = [] + const authors = [] + + Object.keys(data).forEach((key) => { + const v = data[key] + v.alias = key + fonts.push(v) + if (authors.indexOf(v.author) < 0) { + authors.push(v.author) + } + }) + + renderAuthors(authors) + renderFonts(fonts) + selectFont() + new Filters(data).init() + }) } window.toggleFavorite = (alias) => { @@ -233,8 +239,8 @@ window.onhashchange = () => { window.addEventListener('DOMContentLoaded', () => { renderSelectList() - new Theme().init() fontsize.init() + new Theme().init() new Spacing().init() new Language().init()