diff --git a/index.js b/index.js index a8d5425..ae614a6 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -/* global CodeMirror window document $ Set */ +/* global CodeMirror window document Set */ /* eslint-disable no-implicit-globals */ // CodeMirror @@ -13,7 +13,7 @@ var editor = CodeMirror.fromTextArea(document.getElementById('code'), { // CodeMirror theme selector var input = document.getElementById('select-theme'); -var font_data = {}; +var font_data; var filters = { 'style': false, 'rendering': false, @@ -39,14 +39,14 @@ function selectFont() { var status_msg = document.querySelector('footer .subtitle'); var code_mirror = document.querySelector('.CodeMirror'); - if (font === '') { + if (! font) { font = 'cartograph'; status_msg.innerHTML = 'Test drive all the programming fonts!'; - } else { + } else if (typeof font_data !== 'undefined') { status_msg.innerHTML = 'Test drive ' + font_data[font].name + '!'; } - if (font_data[font].rendering === 'bitmap') { + if (typeof font_data !== 'undefined' && font_data[font].rendering === 'bitmap') { code_mirror.classList.add('no-smooth'); } else { code_mirror.classList.remove('no-smooth'); @@ -150,6 +150,7 @@ function renderSelectList() { var pinIcon = ''; var favoritesMap = {}; var favorites = []; + var ajax = new XMLHttpRequest(); document.getElementById('select-font').innerHTML = ''; @@ -164,28 +165,23 @@ function renderSelectList() { console.error('could not render favorites', err); } - $.getJSON('fonts.json', function(data) { + ajax.onreadystatechange = function() { var fonts = []; var authors = []; - font_data = data; + if (ajax.readyState === 4 && ajax.status === 200) { + font_data = ajax.response; - $.each(font_data, function(k, v) { - var font_props = v; - font_props.alias = k; - fonts.push(v); - if (authors.indexOf(v.author) < 0) { - authors.push(v.author); - } - }); + Object.keys(font_data).forEach(function(key){ + var v= font_data[key]; + v.alias = key; + fonts.push(v); + if (authors.indexOf(v.author) < 0) { + authors.push(v.author); + } + }); + } authors.sort(); - - $.each(authors, function(iteration, author) { - $('#authors-list .other').append( - '' - ); - }); - fonts.sort(function(a, b) { if (favoritesMap[a.alias] && !favoritesMap[b.alias]) {return -1;} if (!favoritesMap[a.alias] && favoritesMap[b.alias]) {return 1;} @@ -194,9 +190,16 @@ function renderSelectList() { return 0; }); - $.each(fonts, function(k, v) { + authors.forEach(function(author) { + var option = document.createElement('option'); + option.innerHTML = author; + document.getElementById('authors-list').querySelector('.other').appendChild(option); + }); + + fonts.forEach(function(v) { var liga_info = ''; var render_info = ''; + var option = document.createElement('div'); if (v.ligatures) { liga_info = ', ligatures'; @@ -204,21 +207,30 @@ function renderSelectList() { if (v.rendering === 'bitmap') { render_info = ', bitmap'; } - $('#select-font').append( - '
' + - '' + - '' + v.name + '' + - '' + v.author + ' (' + v.year + ') — ' + v.style + render_info + liga_info + '' + + + option.classList.add('entry'); + if (favoritesMap[v.alias]) { + option.classList.add('pinned'); + } + option.setAttribute('data-alias', v.alias); + option.innerHTML = '' + + '' + v.name + '' + + '' + v.author + ' (' + v.year + ') — ' + v.style + render_info + liga_info + '' + '' + '' + pinIcon + '' + - ' Info & Download' + icon + '
' - ); + ' Info & Download' + icon + ''; + + document.getElementById('select-font').appendChild(option); }); + selectFont(); applyFilters(); - }); + }; + ajax.responseType = 'json'; + ajax.open('GET', 'fonts.json', true); + ajax.send(); } // eslint-disable-next-line no-unused-vars