Skip to content

Commit

Permalink
Fix #1471 virtual-tour: fix updateNode in GPS mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Oct 27, 2024
1 parent 06140c2 commit 22c38db
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 48 deletions.
10 changes: 1 addition & 9 deletions packages/virtual-tour-plugin/src/VirtualTourPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,16 +445,8 @@ export class VirtualTourPlugin extends AbstractConfigurablePlugin<
if (this.isServerSide) {
throw new PSVError('Cannot update node in server side mode');
}
if (!newNode.id) {
throw new PSVError('No id given for node');
}

const node = this.datasource.nodes[newNode.id];
if (!node) {
throw new PSVError(`Node ${newNode.id} does not exist`);
}

Object.assign(node, newNode);
const node = (this.datasource as ClientSideDatasource).updateNode(newNode);

if (newNode.name || newNode.thumbnail || newNode.panorama) {
this.__setGalleryItems();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ export abstract class AbstractDatasource {
destroy() {}

/**
* @summary Loads a node
* @param {string} nodeId
* @return {Promise<PSV.plugins.VirtualTourPlugin.Node>}
* Loads a node
*/
abstract loadNode(nodeId: string): Promise<VirtualTourNode>;

Expand All @@ -42,6 +40,10 @@ export abstract class AbstractDatasource {
if (!this.plugin.isGps && node.markers?.some((marker) => marker.gps && !marker.position)) {
throw new PSVError(`Cannot use GPS positioning for markers in manual mode`);
}
if (!node.links) {
utils.logWarn(`Node ${node.id} has no links`);
node.links = [];
}
}

/**
Expand All @@ -51,6 +53,9 @@ export abstract class AbstractDatasource {
if (!link.nodeId) {
throw new PSVError(`Link of node ${node.id} has no target id`);
}
if (link.nodeId === node.id) {
throw new PSVError(`Node ${node.id} links to itself`);
}
if (Array.isArray(link.position)) {
utils.logWarn('Use the "gps" property to configure the GPS position of a virtual link');
link.gps = link.position as any;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { VirtualTourNode } from '../model';
import { AbstractDatasource } from './AbstractDataSource';

export class ClientSideDatasource extends AbstractDatasource {
loadNode(nodeId: string) {
async loadNode(nodeId: string) {
if (this.nodes[nodeId]) {
return Promise.resolve(this.nodes[nodeId]);
return this.nodes[nodeId];
} else {
return Promise.reject(new PSVError(`Node ${nodeId} not found`));
throw new PSVError(`Node ${nodeId} not found`);
}
}

Expand All @@ -25,24 +25,14 @@ export class ClientSideDatasource extends AbstractDatasource {
if (nodes[node.id]) {
throw new PSVError(`Duplicate node ${node.id}`);
}
if (!node.links) {
utils.logWarn(`Node ${node.id} has no links`);
node.links = [];
}

nodes[node.id] = node;
});

rawNodes.forEach((node) => {
node.links.forEach((link) => {
if (!nodes[link.nodeId]) {
throw new PSVError(`Target node ${link.nodeId} of node ${node.id} does not exists`);
}

link.gps = link.gps || nodes[link.nodeId].gps;

this.checkLink(node, link);
this.__checkLinks(node, nodes);

node.links.forEach((link) => {
linkedNodes[link.nodeId] = true;
});
});
Expand All @@ -55,4 +45,35 @@ export class ClientSideDatasource extends AbstractDatasource {

this.nodes = nodes;
}

updateNode(rawNode: Partial<VirtualTourNode> & { id: VirtualTourNode['id'] }) {
if (!rawNode.id) {
throw new PSVError('No id given for node');
}

const node = this.nodes[rawNode.id];
if (!node) {
throw new PSVError(`Node ${rawNode.id} does not exist`);
}

Object.assign(node, rawNode);

this.checkNode(node);

this.__checkLinks(node, this.nodes);

return node;
}

private __checkLinks(node: VirtualTourNode, nodes: Record<string, VirtualTourNode>) {
node.links.forEach((link) => {
if (!nodes[link.nodeId]) {
throw new PSVError(`Target node ${link.nodeId} of node ${node.id} does not exists`);
}

link.gps = link.gps || nodes[link.nodeId].gps;

this.checkLink(node, link);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Viewer } from '@photo-sphere-viewer/core';
import { PSVError, utils } from '@photo-sphere-viewer/core';
import { PSVError } from '@photo-sphere-viewer/core';
import { VirtualTourPluginConfig } from '../model';
import { VirtualTourPlugin } from '../VirtualTourPlugin';
import type { VirtualTourPlugin } from '../VirtualTourPlugin';
import { AbstractDatasource } from './AbstractDataSource';

export class ServerSideDatasource extends AbstractDatasource {
Expand All @@ -17,28 +17,21 @@ export class ServerSideDatasource extends AbstractDatasource {
this.nodeResolver = plugin.config.getNode;
}

loadNode(nodeId: string) {
async loadNode(nodeId: string) {
if (this.nodes[nodeId]) {
return Promise.resolve(this.nodes[nodeId]);
return this.nodes[nodeId];
} else {
return Promise.resolve(this.nodeResolver(nodeId)).then((node) => {
this.checkNode(node);
if (!node.links) {
utils.logWarn(`Node ${node.id} has no links`);
node.links = [];
}

node.links.forEach((link) => {
if (this.nodes[link.nodeId]) {
link.gps = link.gps || this.nodes[link.nodeId].gps;
}

this.checkLink(node, link);
});

this.nodes[nodeId] = node;
return node;
const node = await this.nodeResolver(nodeId);

this.checkNode(node);

node.links.forEach((link) => {
this.checkLink(node, link);
});

this.nodes[nodeId] = node;

return node;
}
}

Expand Down

0 comments on commit 22c38db

Please sign in to comment.