/* global CodeMirror window document $ Set */ /* eslint-disable no-implicit-globals */ // CodeMirror var editor = CodeMirror.fromTextArea(document.getElementById('code'), { lineNumbers: true, styleActiveLine: true, matchBrackets: true, theme: 'pastel-on-dark', lineWrapping: true }); // CodeMirror theme selector var input = document.getElementById('select-theme'); var font_data = {}; var filters = { 'style': false, 'rendering': false, 'liga': false, 'zerostyle': false, 'author': 'all', 'name': '' }; function selectTheme() { var theme = 'monokai'; if (input.selectedIndex > -1) { theme = input.options[input.selectedIndex].innerHTML; } editor.setOption('theme', theme); document.cookie = 'theme=' + theme + ';max-age=172800'; } // ProgrammingFonts font selector function selectFont() { var font = window.location.hash.substring(1); if (font === '') { font = 'cartograph'; $('footer .subtitle').text('Test drive all the programming fonts!'); } else { $('footer .subtitle').html('Test drive ' + font_data[font].name + '!'); } if (font_data[font].rendering === 'bitmap') { $('.CodeMirror').addClass('no-smooth'); } else { $('.CodeMirror').removeClass('no-smooth'); } if (font === 'input') { $('pre').css({ fontFamily: 'Input Mono, monospace' }); $('textarea').css({ fontFamily: 'Input Mono, monospace' }); $('.CodeMirror').css({ fontFamily: 'Input Mono, monospace' }); } else { $('pre').css({ fontFamily: font + ', monospace' }); $('textarea').css({ fontFamily: font + ', monospace' }); $('.CodeMirror').css({ fontFamily: font + ', monospace' }); } $('#select-font [data-alias]').removeClass('active'); $('#select-font [data-alias=\'' + font + '\']').addClass('active'); document.cookie = 'font=' + font + ';max-age=172800'; } window.onhashchange = selectFont; function setSize() { var size = $('#size').val(); $('.CodeMirror').css({ fontSize: size + 'px' }); document.cookie = 'size=' + size + ';max-age=172800'; } function setSpacing() { var spacing = $('#spacing').val(); $('.CodeMirror').css({ lineHeight: spacing }); document.cookie = 'spacing=' + spacing + ';max-age=172800'; } function selectLanguage() { var lang = $('#select-language').val(); editor.setOption('mode', lang.toLowerCase()); document.cookie = 'language=' + lang + ';max-age=172800'; } function setCounter(amount) { if (amount === 1) { $('h1 a:first-child').text(amount + ' Programming Font'); } else { $('h1 a:first-child').text(amount + ' Programming Fonts'); } } function applyFilters() { var count = 0; Object.keys(filters).forEach(function(filter) { var button = document.querySelector('button[value="' + filter + '"]'); if (! button) { return; } if (filters[filter]) { button.classList.add('selected'); button.querySelectorAll('svg').forEach(function(image){ image.classList.remove('selected'); }); button.querySelector('svg[alt="' + filters[filter] + '"]').classList.add('selected'); } else { button.classList.remove('selected'); button.querySelectorAll('svg').forEach(function(image){ image.classList.remove('selected'); }); } }); $('.entry[data-alias]').each(function(iteration, element) { var data = font_data[$(element).data().alias]; if ( (!filters.style || data.style === filters.style) && (!filters.rendering || data.rendering === filters.rendering) && (!filters.liga || data.ligatures === false && filters.liga === 'no' || data.ligatures === true && filters.liga === 'yes') && (!filters.zerostyle || data.zerostyle === filters.zerostyle) && (filters.author === 'all' || data.author === filters.author) && (!filters.name || data.name.toLowerCase().indexOf(filters.name) > -1) ) { $(element).removeClass('filtered-out'); count++; } else { $(element).addClass('filtered-out'); } }); setCounter(count); } function renderSelectList() { var icon = ''; var pinIcon = ''; var favoritesMap = {}; var favorites = []; $('#select-font').empty(); try { favorites = JSON.parse(localStorage.getItem('favorites')) || []; favoritesMap = favorites.reduce(function(acc, alias) { acc[alias] = true; return acc; }, {}); } catch (err) { // eslint-disable-next-line no-console console.error('could not render favorites', err); } $.getJSON('fonts.json', function(data) { var fonts = []; var authors = []; font_data = data; $.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); } }); 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;} if (a.name.toLowerCase() < b.name.toLowerCase()) {return -1;} if (a.name.toLowerCase() > b.name.toLowerCase()) {return 1;} return 0; }); $.each(fonts, function(k, v) { var liga_info = ''; var render_info = ''; if (v.ligatures) { liga_info = ', ligatures'; } if (v.rendering === 'bitmap') { render_info = ', bitmap'; } $('#select-font').append( '
' + '' + '' + v.name + '' + '' + v.author + ' (' + v.year + ') — ' + v.style + render_info + liga_info + '' + '' + '' + pinIcon + '' + ' Info & Download' + icon + '
' ); }); selectFont(); applyFilters(); }); } // eslint-disable-next-line no-unused-vars function toggleFavorite(alias) { var favorites = []; try { favorites = JSON.parse(localStorage.getItem('favorites')) || []; if (favorites.indexOf(alias) > -1) { favorites = favorites.filter(function(v) { return v !== alias; }); } else { favorites.push(alias); } localStorage.setItem('favorites', JSON.stringify(Array.from(new Set(favorites)))); } catch (err) { // eslint-disable-next-line no-console console.error('could not save favorite', err); } renderSelectList(); return false; } function walk(direction) { var activeEntry = document.querySelector('.entry.active'); var target = null; var next = direction === 'up' ? activeEntry.previousSibling : activeEntry.nextSibling; while (target === null) { if (next) { if (next.matches('.entry:not(.filtered-out)')) { target = next; } else { next = direction === 'up' ? next.previousSibling : next.nextSibling; } } else { target = false; } } if (target) { target.querySelector('a').click(); target.scrollIntoView(); } } function increaseFontSize() { var sizeEl = document.getElementById('size'); sizeEl.value = Number(sizeEl.value) + 1; sizeEl.onchange(); } function decreaseFontSize() { var sizeEl = document.getElementById('size'); sizeEl.value = Number(sizeEl.value) - 1; sizeEl.onchange(); } function toggleFilter(filter) { // cycle through the possible values for each filter // and set the filters[filter] value, // or at the end of the cycle set it to false var options = { 'style': [false, 'sans', 'serif'], 'rendering': [false, 'vector', 'bitmap'], 'liga': [false, 'yes', 'no'], 'zerostyle': [false, 'slashed', 'dotted', 'empty'], }; var current_index = options[filter].indexOf(filters[filter]); var next = current_index + 1; if (next < options[filter].length) { filters[filter] = options[filter][next]; } else { filters[filter] = options[filter][0]; } applyFilters(); } $(document).ready(function() { var cookieValueSpacing = document.cookie.replace(/(?:(?:^|.*;\s*)spacing\s*=\s*([^;]*).*$)|^.*$/, '$1'); var cookieValueSize = document.cookie.replace(/(?:(?:^|.*;\s*)size\s*=\s*([^;]*).*$)|^.*$/, '$1'); var cookieValueTheme = document.cookie.replace(/(?:(?:^|.*;\s*)theme\s*=\s*([^;]*).*$)|^.*$/, '$1'); var cookieValueLanguage = document.cookie.replace(/(?:(?:^|.*;\s*)language\s*=\s*([^;]*).*$)|^.*$/, '$1'); if (cookieValueSpacing !== '') { $('#spacing').val(cookieValueSpacing); } if (cookieValueSize !== '') { $('#size').val(cookieValueSize); } if (cookieValueTheme !== '') { $('#select-theme').val(cookieValueTheme); } if (cookieValueLanguage !== '') { $('#select-language').val(cookieValueLanguage); } renderSelectList(); selectTheme(); setSize(); setSpacing(); selectLanguage(); $('#theme-next').click(function() { $('#select-theme :selected').next().prop('selected', true); selectTheme(); }); $('#theme-previous').click(function() { $('#select-theme :selected').prev().prop('selected', true); selectTheme(); }); $('#filters button').on('click', function(event) { var button = $(this); event.preventDefault(); event.stopPropagation(); toggleFilter(button.val()); }); $('#authors-list').on('change', function() { filters.author = $(this).val(); applyFilters(); }); $('#name-search').on('keyup', function() { filters.name = $(this).val().toLowerCase(); applyFilters(); }); $('body').on('keydown', function(event) { if ( event.target === document.querySelector('.select-list') && ! event.ctrlKey && ! event.altKey && ! event.metaKey && ! event.shiftKey ) { if (event.key === 'ArrowUp') { event.preventDefault(); event.stopPropagation(); walk('up'); return; } else if (event.key === 'ArrowDown') { event.preventDefault(); event.stopPropagation(); walk('down'); return; } } if (event.ctrlKey || event.metaKey) { if (event.key === '-') { event.preventDefault(); event.stopPropagation(); decreaseFontSize(); } else if (event.key === '=') { event.preventDefault(); event.stopPropagation(); increaseFontSize(); } } }); });