@panoramax/web-viewer 3.0.2-develop-a8ea8e60-develop-10c06f44 → 3.0.2-develop-a8ea8e60-develop-e3ac47c8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@panoramax/web-viewer",
3
- "version": "3.0.2-develop-a8ea8e60-develop-10c06f44",
3
+ "version": "3.0.2-develop-a8ea8e60-develop-e3ac47c8",
4
4
  "description": "Panoramax web viewer for geolocated pictures",
5
5
  "main": "build/index.js",
6
6
  "author": "Panoramax team",
package/src/Viewer.js CHANGED
@@ -989,6 +989,10 @@ class Viewer extends CoreView {
989
989
  setFocus(focus, skipEvent = false) {
990
990
  if(focus === "map" && !this.map) { throw new Error("Map is not enabled"); }
991
991
  if(!["map", "pic"].includes(focus)) { throw new Error("Invalid focus value (should be pic or map)"); }
992
+ if(
993
+ (focus === "map" && this.map && this.isMapWide())
994
+ || (focus === "pic" && (!this.map || !this.isMapWide()))
995
+ ) { return; }
992
996
 
993
997
  this.mapContainer.parentElement?.removeChild(this.mapContainer);
994
998
  this.psvContainer.parentElement?.removeChild(this.psvContainer);
@@ -313,15 +313,28 @@ export default class URLHash extends EventTarget {
313
313
  }
314
314
 
315
315
  this._delay = setTimeout(() => {
316
- // Replace if already present, else append the updated hash string
317
- const location = window.location.href.replace(
318
- /(#.+)?$/,
319
- this._viewer ? this.getHashString() : ""
320
- );
316
+ const prevUrl = new URL(window.location.href);
317
+ const nextUrl = new URL(window.location.href);
318
+ nextUrl.hash = this._viewer ? this.getHashString() : "";
319
+
320
+ // Skip hash update if no changes
321
+ if(prevUrl.hash == nextUrl.hash) { return; }
322
+
323
+ const prevPic = this._getCurrentHash().pic || "";
324
+ const nextPic = this._viewer?.psv?.getPictureMetadata()?.id || "";
325
+
321
326
  try {
322
- window.history.replaceState(window.history.state, null, location);
327
+ // If different pic, add entry in browser history
328
+ if(prevPic != nextPic) {
329
+ window.history.pushState(window.history.state, null, nextUrl.href);
330
+ }
331
+ // If same pic, just update viewer params
332
+ else {
333
+ window.history.replaceState(window.history.state, null, nextUrl.href);
334
+ }
335
+
323
336
  if(this._viewer) {
324
- const event = new CustomEvent("url-changed", { detail: {url: location}});
337
+ const event = new CustomEvent("url-changed", { detail: {url: nextUrl.href}});
325
338
  this.dispatchEvent(event);
326
339
  }
327
340
  } catch (SecurityError) {
@@ -501,7 +501,7 @@ describe("_updateHash", () => {
501
501
  delete window.location;
502
502
 
503
503
  window.history = { replaceState: jest.fn(), state: {} };
504
- window.location = { href: "http://localhost:5000/#a=b&b=c" };
504
+ window.location = { href: "http://localhost:5000/#a=b&b=c", hash: "#a=b&b=c" };
505
505
 
506
506
  const v = { addEventListener: jest.fn() };
507
507
  const uh = new URLHash(v);
@@ -515,12 +515,31 @@ describe("_updateHash", () => {
515
515
  expect(uh.dispatchEvent.mock.calls).toMatchSnapshot();
516
516
  });
517
517
 
518
+ it("works with pic change", async () => {
519
+ delete window.history;
520
+ delete window.location;
521
+
522
+ window.history = { pushState: jest.fn(), state: {} };
523
+ window.location = { href: "http://localhost:5000/#a=b&b=c", hash: "#a=b&pic=bla" };
524
+
525
+ const v = { addEventListener: jest.fn() };
526
+ const uh = new URLHash(v);
527
+ uh.getHashString = () => "#c=d";
528
+ uh.dispatchEvent = jest.fn();
529
+
530
+ uh._updateHash();
531
+ await new Promise((r) => setTimeout(r, 1000));
532
+
533
+ expect(window.history.pushState.mock.calls.pop()).toEqual([{}, null, "http://localhost:5000/#c=d"]);
534
+ expect(uh.dispatchEvent.mock.calls).toMatchSnapshot();
535
+ });
536
+
518
537
  it("deduplicates calls", async () => {
519
538
  delete window.history;
520
539
  delete window.location;
521
540
 
522
541
  window.history = { replaceState: jest.fn(), state: {} };
523
- window.location = { href: "http://localhost:5000/#a=b&b=c" };
542
+ window.location = { href: "http://localhost:5000/#a=b&b=c", hash: "#a=b&b=c" };
524
543
 
525
544
  const v = { addEventListener: jest.fn() };
526
545
  const uh = new URLHash(v);
@@ -10,6 +10,16 @@ Array [
10
10
  ]
11
11
  `;
12
12
 
13
+ exports[`_updateHash works with pic change 1`] = `
14
+ Array [
15
+ Array [
16
+ CustomEvent {
17
+ "isTrusted": false,
18
+ },
19
+ ],
20
+ ]
21
+ `;
22
+
13
23
  exports[`bindMapEvents works 1`] = `
14
24
  Array [
15
25
  Array [