From 100cf72b346ad90d4fbfcd718ea19e622881a154 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Fri, 10 Nov 2017 11:37:04 -0500 Subject: handle saves --- htdocs/js/mathy.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'htdocs/js') diff --git a/htdocs/js/mathy.js b/htdocs/js/mathy.js index 7acd7ab..edbb17a 100644 --- a/htdocs/js/mathy.js +++ b/htdocs/js/mathy.js @@ -211,7 +211,7 @@ jQuery(function($) { return false; // build sortable name - var sort = name + var sortable_name = name .replace(/\s+/g, ' ') .replace(/^\s+|\s+$/g, '') .toLowerCase(); @@ -221,20 +221,25 @@ jQuery(function($) { saves = saves ? JSON.parse(saves) : []; if ($.grep(saves, function(row) { - return row.save == sort; + return row.sort == sortable_name; }).length > 0) { if (!confirm("Replace existing \"" + name + " \"?")) return false; } + // exclude duplicates + saves = $.grep(saves, function(row) { + return row.sort != sortable_name; + }); + // append to results saves.push({ name: name, - sort: sort, + sort: sortable_name, text: editor.getValue(), }); - // write saves + // write sorted saves localStorage.setItem('mathy_saves', JSON.stringify(saves.sort(function(a, b) { return b.sort < a.sort; }))); -- cgit v1.2.3