Spaces:
Running
Running
Add postMessage state sync to parent for URL reflection
Browse files- server.R +156 -0
- www/app.js +18 -0
server.R
CHANGED
|
@@ -25,6 +25,9 @@ server <- function(input, output, session) {
|
|
| 25 |
parsed_data_list <- reactiveVal(list())
|
| 26 |
fetch_current_token <- reactiveVal(NULL)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
# Map bounds for Poland
|
| 29 |
map_bounds <- list(
|
| 30 |
lng_min = 14.0,
|
|
@@ -64,6 +67,140 @@ server <- function(input, output, session) {
|
|
| 64 |
}
|
| 65 |
})
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
resolution_key <- reactive({
|
| 68 |
res <- input$temporal_resolution
|
| 69 |
if (is.null(res) || !res %in% c("hourly", "daily", "monthly")) "daily" else res
|
|
@@ -224,6 +361,8 @@ server <- function(input, output, session) {
|
|
| 224 |
slice(1)
|
| 225 |
if (nrow(st) > 0) {
|
| 226 |
highlight_selected_station(st, move_map = TRUE)
|
|
|
|
|
|
|
| 227 |
}
|
| 228 |
})
|
| 229 |
|
|
@@ -684,6 +823,7 @@ server <- function(input, output, session) {
|
|
| 684 |
slice(1)
|
| 685 |
if (nrow(st) > 0) {
|
| 686 |
highlight_selected_station(st, move_map = TRUE)
|
|
|
|
| 687 |
}
|
| 688 |
})
|
| 689 |
|
|
@@ -746,6 +886,7 @@ server <- function(input, output, session) {
|
|
| 746 |
slice(1)
|
| 747 |
if (nrow(st) > 0) {
|
| 748 |
highlight_selected_station(st, move_map = TRUE)
|
|
|
|
| 749 |
}
|
| 750 |
})
|
| 751 |
|
|
@@ -766,6 +907,7 @@ server <- function(input, output, session) {
|
|
| 766 |
# Fetch station data whenever selection/date/resolution changes
|
| 767 |
observeEvent(list(current_station_uid(), input$temporal_resolution, input$date_range),
|
| 768 |
{
|
|
|
|
| 769 |
uid <- current_station_uid()
|
| 770 |
req(uid, input$date_range)
|
| 771 |
|
|
@@ -776,6 +918,9 @@ server <- function(input, output, session) {
|
|
| 776 |
station_fetching(TRUE)
|
| 777 |
loading_status(TRUE)
|
| 778 |
station_fetch_error(NULL)
|
|
|
|
|
|
|
|
|
|
| 779 |
|
| 780 |
start_date <- as.Date(input$date_range[1])
|
| 781 |
end_date <- as.Date(input$date_range[2])
|
|
@@ -955,10 +1100,21 @@ server <- function(input, output, session) {
|
|
| 955 |
|
| 956 |
# Navigate to Dashboard only after data is ready
|
| 957 |
updateNavbarPage(session, "main_nav", selected = "Dashboard")
|
|
|
|
| 958 |
})
|
| 959 |
}, delay = 0.05)
|
| 960 |
})
|
| 961 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 962 |
# Dashboard UI states
|
| 963 |
output$dashboard_station_header <- renderUI({
|
| 964 |
st <- selected_station()
|
|
|
|
| 25 |
parsed_data_list <- reactiveVal(list())
|
| 26 |
fetch_current_token <- reactiveVal(NULL)
|
| 27 |
|
| 28 |
+
# Track if URL params have been applied
|
| 29 |
+
url_initialized <- reactiveVal(FALSE)
|
| 30 |
+
|
| 31 |
# Map bounds for Poland
|
| 32 |
map_bounds <- list(
|
| 33 |
lng_min = 14.0,
|
|
|
|
| 67 |
}
|
| 68 |
})
|
| 69 |
|
| 70 |
+
# Helper: Parse URL query string
|
| 71 |
+
parse_url_params <- function(query_string) {
|
| 72 |
+
if (is.null(query_string) || query_string == "" || query_string == "?") {
|
| 73 |
+
return(list())
|
| 74 |
+
}
|
| 75 |
+
# Remove leading "?"
|
| 76 |
+
qs <- sub("^\\?", "", query_string)
|
| 77 |
+
if (qs == "") {
|
| 78 |
+
return(list())
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
pairs <- strsplit(qs, "&")[[1]]
|
| 82 |
+
params <- list()
|
| 83 |
+
for (pair in pairs) {
|
| 84 |
+
kv <- strsplit(pair, "=")[[1]]
|
| 85 |
+
if (length(kv) == 2) {
|
| 86 |
+
params[[kv[1]]] <- URLdecode(kv[2])
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
params
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
# Helper: Broadcast current state to parent page
|
| 93 |
+
broadcast_state <- function(view_override = NULL) {
|
| 94 |
+
st <- selected_station()
|
| 95 |
+
# Extract scalar values from tibble row
|
| 96 |
+
station_id <- if (!is.null(st) && nrow(st) > 0) as.character(st$id[1]) else NULL
|
| 97 |
+
station_name <- if (!is.null(st) && nrow(st) > 0) as.character(st$name[1]) else NULL
|
| 98 |
+
landname <- if (!is.null(st) && nrow(st) > 0 && !is.na(st$metadata_id[1])) paste0(st$station_type[1], "-", st$data_order[1]) else NULL
|
| 99 |
+
resolution <- input$temporal_resolution
|
| 100 |
+
|
| 101 |
+
# Determine current view
|
| 102 |
+
main_tab <- input$main_nav
|
| 103 |
+
view <- if (!is.null(view_override)) {
|
| 104 |
+
view_override
|
| 105 |
+
} else if (!is.null(main_tab)) {
|
| 106 |
+
if (main_tab == "Map View") {
|
| 107 |
+
"map"
|
| 108 |
+
} else if (main_tab == "Stations Info") {
|
| 109 |
+
"station-info"
|
| 110 |
+
} else if (main_tab == "Dashboard") {
|
| 111 |
+
subtab <- input$dashboard_subtabs
|
| 112 |
+
if (!is.null(subtab) && subtab == "Data") {
|
| 113 |
+
"dashboard-data"
|
| 114 |
+
} else {
|
| 115 |
+
"dashboard-plots"
|
| 116 |
+
}
|
| 117 |
+
} else {
|
| 118 |
+
"map"
|
| 119 |
+
}
|
| 120 |
+
} else {
|
| 121 |
+
"map"
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
start_date <- if (!is.null(input$date_range)) as.character(input$date_range[1]) else NULL
|
| 125 |
+
end_date <- if (!is.null(input$date_range)) as.character(input$date_range[2]) else NULL
|
| 126 |
+
|
| 127 |
+
session$sendCustomMessage("updateParentURL", list(
|
| 128 |
+
station = station_id,
|
| 129 |
+
stationName = station_name,
|
| 130 |
+
landname = landname,
|
| 131 |
+
resolution = resolution,
|
| 132 |
+
view = view,
|
| 133 |
+
start = start_date,
|
| 134 |
+
end = end_date
|
| 135 |
+
))
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
# Observer: Apply URL params on app startup
|
| 139 |
+
observe({
|
| 140 |
+
req(!url_initialized())
|
| 141 |
+
|
| 142 |
+
query <- session$clientData$url_search
|
| 143 |
+
params <- parse_url_params(query)
|
| 144 |
+
|
| 145 |
+
if (length(params) == 0) {
|
| 146 |
+
url_initialized(TRUE)
|
| 147 |
+
return()
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
# Apply resolution
|
| 151 |
+
if (!is.null(params$resolution) && params$resolution %in% c("hourly", "daily", "monthly")) {
|
| 152 |
+
updateRadioButtons(session, "temporal_resolution", selected = params$resolution)
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
# Apply date range
|
| 156 |
+
if (!is.null(params$start) && !is.null(params$end)) {
|
| 157 |
+
start_date <- tryCatch(as.Date(params$start), error = function(e) NULL)
|
| 158 |
+
end_date <- tryCatch(as.Date(params$end), error = function(e) NULL)
|
| 159 |
+
if (!is.null(start_date) && !is.null(end_date)) {
|
| 160 |
+
updateDateRangeInput(session, "date_range", start = start_date, end = end_date)
|
| 161 |
+
prev_dates(c(start_date, end_date))
|
| 162 |
+
}
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
# Apply station selection (deferred to allow inputs to update)
|
| 166 |
+
if (!is.null(params$station)) {
|
| 167 |
+
station <- params$station
|
| 168 |
+
|
| 169 |
+
# Match by name or ID
|
| 170 |
+
all_stations <- stations()
|
| 171 |
+
st <- all_stations %>%
|
| 172 |
+
filter(id == station | name == station) %>%
|
| 173 |
+
slice(1)
|
| 174 |
+
|
| 175 |
+
if (nrow(st) > 0) {
|
| 176 |
+
shinyjs::delay(200, {
|
| 177 |
+
current_station_uid(paste(st$id[1], st$name[1], sep = "|"))
|
| 178 |
+
})
|
| 179 |
+
}
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
# Apply view/tab navigation (deferred)
|
| 183 |
+
if (!is.null(params$view)) {
|
| 184 |
+
view <- params$view
|
| 185 |
+
shinyjs::delay(600, {
|
| 186 |
+
if (view == "map") {
|
| 187 |
+
session$sendCustomMessage("switchTab", list(tabId = "Map View"))
|
| 188 |
+
} else if (view == "station-info") {
|
| 189 |
+
session$sendCustomMessage("switchTab", list(tabId = "Stations Info"))
|
| 190 |
+
} else if (view %in% c("dashboard-plots", "dashboard-data")) {
|
| 191 |
+
session$sendCustomMessage("switchTab", list(tabId = "Dashboard"))
|
| 192 |
+
if (view == "dashboard-data") {
|
| 193 |
+
shinyjs::delay(800, {
|
| 194 |
+
shinyjs::runjs("$('a[data-value=\"Data\"]').tab('show');")
|
| 195 |
+
})
|
| 196 |
+
}
|
| 197 |
+
}
|
| 198 |
+
})
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
url_initialized(TRUE)
|
| 202 |
+
})
|
| 203 |
+
|
| 204 |
resolution_key <- reactive({
|
| 205 |
res <- input$temporal_resolution
|
| 206 |
if (is.null(res) || !res %in% c("hourly", "daily", "monthly")) "daily" else res
|
|
|
|
| 361 |
slice(1)
|
| 362 |
if (nrow(st) > 0) {
|
| 363 |
highlight_selected_station(st, move_map = TRUE)
|
| 364 |
+
# Trigger broadcast
|
| 365 |
+
broadcast_state()
|
| 366 |
}
|
| 367 |
})
|
| 368 |
|
|
|
|
| 823 |
slice(1)
|
| 824 |
if (nrow(st) > 0) {
|
| 825 |
highlight_selected_station(st, move_map = TRUE)
|
| 826 |
+
broadcast_state()
|
| 827 |
}
|
| 828 |
})
|
| 829 |
|
|
|
|
| 886 |
slice(1)
|
| 887 |
if (nrow(st) > 0) {
|
| 888 |
highlight_selected_station(st, move_map = TRUE)
|
| 889 |
+
broadcast_state(view_override = "dashboard-plots")
|
| 890 |
}
|
| 891 |
})
|
| 892 |
|
|
|
|
| 907 |
# Fetch station data whenever selection/date/resolution changes
|
| 908 |
observeEvent(list(current_station_uid(), input$temporal_resolution, input$date_range),
|
| 909 |
{
|
| 910 |
+
req(url_initialized()) # Ensure URL params are applied first
|
| 911 |
uid <- current_station_uid()
|
| 912 |
req(uid, input$date_range)
|
| 913 |
|
|
|
|
| 918 |
station_fetching(TRUE)
|
| 919 |
loading_status(TRUE)
|
| 920 |
station_fetch_error(NULL)
|
| 921 |
+
|
| 922 |
+
# Broadcast state on re-fetch
|
| 923 |
+
broadcast_state()
|
| 924 |
|
| 925 |
start_date <- as.Date(input$date_range[1])
|
| 926 |
end_date <- as.Date(input$date_range[2])
|
|
|
|
| 1100 |
|
| 1101 |
# Navigate to Dashboard only after data is ready
|
| 1102 |
updateNavbarPage(session, "main_nav", selected = "Dashboard")
|
| 1103 |
+
broadcast_state()
|
| 1104 |
})
|
| 1105 |
}, delay = 0.05)
|
| 1106 |
})
|
| 1107 |
|
| 1108 |
+
# Listen for tab changes explicitly to broadcast
|
| 1109 |
+
observeEvent(input$main_nav, {
|
| 1110 |
+
req(url_initialized())
|
| 1111 |
+
broadcast_state()
|
| 1112 |
+
})
|
| 1113 |
+
observeEvent(input$dashboard_subtabs, {
|
| 1114 |
+
req(url_initialized())
|
| 1115 |
+
broadcast_state()
|
| 1116 |
+
})
|
| 1117 |
+
|
| 1118 |
# Dashboard UI states
|
| 1119 |
output$dashboard_station_header <- renderUI({
|
| 1120 |
st <- selected_station()
|
www/app.js
CHANGED
|
@@ -85,3 +85,21 @@ Shiny.addCustomMessageHandler('switchTab', function (message) {
|
|
| 85 |
console.warn("Tab not found: " + message.tabId);
|
| 86 |
}
|
| 87 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
console.warn("Tab not found: " + message.tabId);
|
| 86 |
}
|
| 87 |
});
|
| 88 |
+
|
| 89 |
+
// Update parent page URL (for SEO/AdSense integration)
|
| 90 |
+
Shiny.addCustomMessageHandler('updateParentURL', function (message) {
|
| 91 |
+
console.log("Updating parent URL with state:", message);
|
| 92 |
+
// Send message to parent window (Quarto page)
|
| 93 |
+
if (window.parent !== window) {
|
| 94 |
+
window.parent.postMessage({
|
| 95 |
+
type: 'imgw-state-update',
|
| 96 |
+
station: message.station || null,
|
| 97 |
+
stationName: message.stationName || null,
|
| 98 |
+
landname: message.landname || null,
|
| 99 |
+
resolution: message.resolution || null,
|
| 100 |
+
view: message.view || null,
|
| 101 |
+
start: message.start || null,
|
| 102 |
+
end: message.end || null
|
| 103 |
+
}, '*');
|
| 104 |
+
}
|
| 105 |
+
});
|