Skip to content

Commit 0e7c83f

Browse files
authored
Persist hiding challenges (#357)
1 parent 398cdea commit 0e7c83f

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

browser-extensions/common/js/content-scripts/content-script-parkrunner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ function create_page() {
531531
set_progress_message("Parsed Results")
532532

533533
set_progress_message("Loading saved data")
534-
browser.storage.local.get(["home_parkrun_info", "athlete_number"]).then((items) => {
534+
browser.storage.local.get(["home_parkrun_info", "athlete_number", "challengeMetadata"]).then((items) => {
535535
set_progress_message("Loaded saved data")
536536
loaded_user_data = items
537537
// console.log("Here is the stored items, fetched with a promise:")

browser-extensions/common/js/lib/challenges_ui.js

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,13 @@ function get_tbody_header(challenge) {
9797
return $('<tbody></tbody>').attr('id', get_tbody_header_id(challenge))
9898
}
9999

100-
function get_tbody_content(challenge) {
101-
return $('<tbody></tbody>').attr('id', get_tbody_content_id(challenge))
100+
function get_tbody_content(challenge, userData) {
101+
var content = $('<tbody></tbody>').attr('id', get_tbody_content_id(challenge))
102+
// Find out whether this should be hidden by default when we initially draw the page
103+
if (isChallengeHidden(challenge.shortname, userData)) {
104+
content.hide()
105+
}
106+
return content
102107
}
103108

104109
function get_tbody_header_id(challenge) {
@@ -137,11 +142,12 @@ function get_challenge_icon(challenge, height, width) {
137142
function get_challenge_header_row(challenge, data) {
138143

139144
var main_row = $('<tr></tr>')
145+
var challengeShortname = challenge.shortname
140146

141147
var badge_img = get_challenge_icon(challenge, 24, 24)
142148
if (badge_img !== undefined) {
143149
badge_img.click(function(){
144-
$("tbody[id=challenge_tbody_content_"+challenge['shortname']+"]").toggle();
150+
toggleVisibilityOfChallenge(challengeShortname)
145151
});
146152
} else {
147153
badge_img = ''
@@ -200,7 +206,7 @@ function generateRegionnaireTableEntry(table, data) {
200206
}
201207

202208
var challenge_tbody_header = get_tbody_header(challenge)
203-
var challenge_tbody_detail = get_tbody_content(challenge)
209+
var challenge_tbody_detail = get_tbody_content(challenge, data.user_data)
204210

205211
// Create the header row and add it to the tbody that exists to hold
206212
// the title row
@@ -1353,7 +1359,7 @@ function drawRegionnaireDataTable(table, data) {
13531359
function generate_standard_table_entry(challenge, table, data) {
13541360

13551361
var challenge_tbody_header = get_tbody_header(challenge)
1356-
var challenge_tbody_detail = get_tbody_content(challenge)
1362+
var challenge_tbody_detail = get_tbody_content(challenge, data.user_data)
13571363

13581364
// Create the header row and add it to the tbody that exists to hold
13591365
// the title row
@@ -1441,3 +1447,57 @@ function add_stats_table(div, data) {
14411447
div.append('<br/>Hover over the stats for a more detailed description')
14421448

14431449
}
1450+
1451+
function isChallengeHidden(challengeShortname, userData) {
1452+
var isHidden = false
1453+
console.log("userData: "+JSON.stringify(userData))
1454+
if (userData !== undefined) {
1455+
if (userData["challengeMetadata"] !== undefined && challengeShortname in userData["challengeMetadata"]) {
1456+
if ("hidden" in userData.challengeMetadata[challengeShortname]) {
1457+
isHidden = userData.challengeMetadata[challengeShortname].hidden
1458+
console.log("isChallengeHidden found existing saved state: "+isHidden)
1459+
}
1460+
}
1461+
}
1462+
return isHidden
1463+
}
1464+
1465+
function saveHiddenPreference(challengeName, isHidden) {
1466+
console.log("Challenge: "+challengeName+", Is Hidden: "+isHidden)
1467+
1468+
browser.storage.local.get({
1469+
challengeMetadata: {}
1470+
}).then((items) => {
1471+
// Items contains the whole object, of which the key we asked for is a sub-item
1472+
console.log(items)
1473+
1474+
// If the challenge already exists in the object, then set the hidden property
1475+
if (challengeName in items.challengeMetadata) {
1476+
items.challengeMetadata[challengeName]["hidden"] = isHidden
1477+
// Else, add a metadata object for this challenge and initialise it
1478+
} else {
1479+
var challengeMetadata = {
1480+
"hidden": isHidden
1481+
}
1482+
items.challengeMetadata[challengeName] = challengeMetadata
1483+
}
1484+
console.log(items.challengeMetadata)
1485+
1486+
// Save the information back into the local storage
1487+
browser.storage.local.set(items)
1488+
})
1489+
1490+
}
1491+
1492+
function toggleVisibilityOfChallenge(challengeShortname) {
1493+
var challengeBodyId = "challenge_tbody_content_"+challengeShortname
1494+
// console.log(challengeBodyId)
1495+
var challengeBodyElement = $("tbody[id="+challengeBodyId+"]")
1496+
// console.log(challengeBodyElement)
1497+
var isCurrentlyHidden = challengeBodyElement.is(":hidden")
1498+
// console.log("isCurrentlyHidden: "+isCurrentlyHidden)
1499+
// Save the preference to retain the fact it is hidden next time
1500+
saveHiddenPreference(challengeShortname, !isCurrentlyHidden)
1501+
// Toggle the visibility of the challenge now
1502+
challengeBodyElement.toggle()
1503+
}

0 commit comments

Comments
 (0)