From c1296a3a6b45dfc0fdbb13aff489516b9d15f276 Mon Sep 17 00:00:00 2001 From: Chris Paul Date: Tue, 22 Jan 2019 10:45:16 -0800 Subject: [PATCH] Add favorites toggle --- index.css | 4 +++ index.js | 91 ++++++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 71 insertions(+), 24 deletions(-) diff --git a/index.css b/index.css index bda9cd7..9974ff2 100644 --- a/index.css +++ b/index.css @@ -103,6 +103,10 @@ section.select-list a:active .name { section.select-list .active .name { font-weight: bold; } +section.select-list a.favoritelink { + margin-right: 2px; + color: red; +} section.select-list a.website { position: absolute; top: 5px; diff --git a/index.js b/index.js index 902372d..2538a93 100644 --- a/index.js +++ b/index.js @@ -76,6 +76,72 @@ function selectLanguage() { document.cookie = "language=" + lang + ";max-age=172800"; } +function renderSelectList() { + $("#select-font").empty(); + + var icon = ''; + + var favoritesMap = {}; + try { + var favorites = JSON.parse(localStorage.getItem('favorites')) || []; + favoritesMap = favorites.reduce(function(acc, alias) { + acc[alias] = true; + return acc; + }, {}); + } catch (err) { + console.error('could not render favorites', err); + } + + $.getJSON("fonts.json", function(data) { + data.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(data, 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( + "
" + + "" + + " " + + (favoritesMap[v.alias] ? "♥︎" : "♡") + "" + + "" + v.name + "" + + "" + v.author + " (" + v.year + ") — " + v.style + render_info + liga_info + "" + + "" + + " Info & Download" + icon + "
" + ); + }); + selectFont(); + }); +} + +function toggleFavorite(alias) { + try { + var 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) { + console.error('could not save favorite', err); + } + renderSelectList(); +} + $(document).ready(function() { var cookieValueSpacing = document.cookie.replace(/(?:(?:^|.*;\s*)spacing\s*=\s*([^;]*).*$)|^.*$/, "$1"); var cookieValueSize = document.cookie.replace(/(?:(?:^|.*;\s*)size\s*=\s*([^;]*).*$)|^.*$/, "$1"); @@ -106,30 +172,7 @@ $(document).ready(function() { setSpacing(); setAntialiasing(); selectLanguage(); - - var icon = ''; - - $.getJSON("fonts.json", function(data) { - $.each(data, 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 + "" + - "" + - " Info & Download" + icon + "
" - ); - }); - selectFont(); - }); + renderSelectList(); $("#theme-next").click(function() { $("#select-theme :selected").next().prop("selected", true);