]> git.openstreetmap.org Git - nominatim-ui.git/blobdiff - src/lib/stores.js
new config option -Reverse_Only- (#166)
[nominatim-ui.git] / src / lib / stores.js
index d445ade78ef0ac4be74560cbd1dded145319426e..35344ce7cf97c2473886743c4b3b90dcc60ede69 100644 (file)
@@ -2,19 +2,59 @@ import { writable } from 'svelte/store';
 
 export const map_store = writable();
 export const results_store = writable();
-export const current_result_store = writable();
-export const current_request_latlon = writable();
-export const last_updated_store = writable();
-export const page = writable({ count: 0 });
+export const last_api_request_url_store = writable();
+export const error_store = writable();
+export const page = writable();
 
-export function refresh_page() {
-  let pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, '');
+/**
+ * Update the global page state.
+ *
+ * When called without a parameter, then the current window.location is
+ * parsed and the page state is set accordingly. Otherwise the page state
+ * is set from the parameters. 'pagename' is the overall subpage (without
+ * .html extension). 'params' must be an URLSearchParams object and contain
+ * the requested query parameters. It may also be omitted completely for a
+ * link without query parameters.
+ */
+const default_pagename = Nominatim_Config.Reverse_Only ? 'reverse' : 'search';
+const pagenames = [
+  default_pagename,
+  'reverse',
+  'details',
+  'deletable',
+  'polygons',
+  'status',
+  'about'
+];
 
-  if (['search', 'reverse', 'details', 'deletable', 'polygons'].indexOf(pagename) === -1) {
-    pagename = 'search';
+export function refresh_page(pagename, params) {
+  if (typeof pagename === 'undefined') {
+    pagename = window.location.pathname.replace('.html', '').replace(/^.*\//, '');
+
+    if (!pagenames.includes(pagename)) pagename = default_pagename;
+
+    params = new URLSearchParams(window.location.search);
+  } else {
+    if (!pagenames.includes(pagename)) pagename = default_pagename;
+
+    if (typeof params === 'undefined') {
+      params = new URLSearchParams();
+    }
+
+    let param_str = params.toString();
+    if (param_str) {
+      param_str = '?' + param_str;
+    }
+    let new_url = pagename + '.html' + param_str;
+
+    if (window.location.protocol.match(/^http/)) {
+      window.history.pushState([], '', new_url);
+    } else {
+      window.location.href = new_url;
+    }
   }
 
-  // Add a counter here to make sure the store change is triggered
-  // everytime we refresh, not just when the page changes.
-  page.update(function (v) { return { tab: pagename, count: v.count + 1 }; });
+  page.set({ tab: pagename, params: params });
+  last_api_request_url_store.set(null);
+  error_store.set(null);
 }