diff --git a/content/js/game.js b/content/js/game.js
index 370fbe4..caf2f56 100644
--- a/content/js/game.js
+++ b/content/js/game.js
@@ -1,14 +1,18 @@
-// Globally available data and helper functions
-
-// "Parameterize" a string similar to Ruby
-// https://www.w3resource.com/javascript-exercises/javascript-string-exercise-7.php
-const string_parameterize = function (str1) {
- return str1.trim().toLowerCase().replace(/[^a-zA-Z0-9 -]/, '').replace(/\s/g, '-');
-};
-
// Gets the name of the specific game
const game_name = string_parameterize(document.getElementById('title').innerText);
+// Add to the total drink count
+function incrementTotal() {
+ // Reference the counter
+ const total_elem = document.getElementById('total');
+
+ // Increment the counter
+ total_elem.innerText++;
+
+ // Add it to localStorage
+ localStorage.setItem(`drinking-game-${game_name}-total-count`, total_elem.innerText);
+}
+
// The individual 'drink!' function
function drink(game) {
// Increments the counter
@@ -19,6 +23,9 @@ function drink(game) {
// Save to localStorage
localStorage.setItem(`drinking-game-${game_name}-${drink_name}`, game.parentElement.dataset.counter);
+
+ // Add to grand total
+ incrementTotal();
}
// Load saved drinks from localStorage
@@ -39,6 +46,13 @@ function fetchDrinks() {
current_drink.parentElement.dataset.counter = current_drink_count;
}
});
+
+ // Fetch total counter value if set
+ const total_drink_count = localStorage.getItem(`drinking-game-${game_name}-total-count`);
+
+ if (total_drink_count > 0) {
+ document.getElementById('total').innerText = total_drink_count;
+ }
}
// Run fetchDrinks after DOM is loaded
diff --git a/content/js/global.js b/content/js/global.js
new file mode 100644
index 0000000..eefd275
--- /dev/null
+++ b/content/js/global.js
@@ -0,0 +1,7 @@
+// Globally available data and helper functions
+
+// "Parameterize" a string similar to Ruby
+// https://www.w3resource.com/javascript-exercises/javascript-string-exercise-7.php
+const string_parameterize = function (str1) {
+ return str1.trim().toLowerCase().replace(/[^a-zA-Z0-9 -]/, '').replace(/\s/g, '-');
+};
diff --git a/content/js/index.js b/content/js/index.js
new file mode 100644
index 0000000..2e88aa6
--- /dev/null
+++ b/content/js/index.js
@@ -0,0 +1,21 @@
+function fetchTotalDrinks() {
+ const games = Array.from(document.getElementsByTagName('a'));
+
+ games.forEach(function(game) {
+ // Get current drink name from text
+ const game_name = string_parameterize(game.innerText);
+
+ // Get current drink value from localStorage
+ const game_count = localStorage.getItem(`drinking-game-${game_name}-total-count`);
+
+ // Set counter to localStorage value if set
+ if (game_count > 0) {
+ game.dataset.counter = game_count;
+ }
+ })
+}
+
+// Run fetchTotalDrinks after DOM is loaded
+document.addEventListener('DOMContentLoaded', function(event) {
+ fetchTotalDrinks();
+});
diff --git a/layouts/default.html b/layouts/default.html
index f8ec9de..0a99830 100644
--- a/layouts/default.html
+++ b/layouts/default.html
@@ -11,8 +11,11 @@
<%= yield %>
+
<% if @item[:title] %>
+ <% else %>
+
<% end %>