UNPKG

@photo-sphere-viewer/resolution-plugin

Version:

Photo Sphere Viewer plugin that adds a setting to choose between multiple resolutions of the panorama.

1 lines 10.3 kB
{"version":3,"sources":["src/index.ts","src/events.ts","src/ResolutionPlugin.ts"],"sourcesContent":["import { DEFAULTS } from '@photo-sphere-viewer/core';\nimport * as events from './events';\nimport { ResolutionPlugin } from './ResolutionPlugin';\n\nDEFAULTS.lang[ResolutionPlugin.id] = 'Quality';\n\nexport { ResolutionPlugin };\nexport * from './model';\nexport { events };\n","import { TypedEvent } from '@photo-sphere-viewer/core';\nimport type { ResolutionPlugin } from './ResolutionPlugin';\n\n/**\n * @event Triggered when the resolution is changed\n */\nexport class ResolutionChangedEvent extends TypedEvent<ResolutionPlugin> {\n static override readonly type = 'resolution-changed';\n override type: 'resolution-changed';\n\n /** @internal */\n constructor(public readonly resolutionId: string) {\n super(ResolutionChangedEvent.type);\n }\n}\n\nexport type ResolutionPluginEvents = ResolutionChangedEvent;\n","import type { PluginConstructor, Viewer } from '@photo-sphere-viewer/core';\nimport { AbstractPlugin, events, PSVError, utils } from '@photo-sphere-viewer/core';\nimport type { OptionsSetting, SettingsPlugin } from '@photo-sphere-viewer/settings-plugin';\nimport { ResolutionChangedEvent, ResolutionPluginEvents } from './events';\nimport { Resolution, ResolutionPluginConfig } from './model';\n\nconst getConfig = utils.getConfigParser<ResolutionPluginConfig>({\n resolutions: null,\n defaultResolution: null,\n showBadge: true,\n});\n\n/**\n * Adds a setting to choose between multiple resolutions of the panorama.\n */\nexport class ResolutionPlugin extends AbstractPlugin<ResolutionPluginEvents> {\n static override readonly id = 'resolution';\n static override readonly VERSION = PKG_VERSION;\n\n readonly config: ResolutionPluginConfig;\n\n private resolutions: Resolution[] = [];\n private resolutionsById: Record<string, Resolution> = {};\n\n private readonly state = {\n resolution: null as string,\n };\n\n private settings: SettingsPlugin;\n\n static withConfig(config: ResolutionPluginConfig): [PluginConstructor, any] {\n return [ResolutionPlugin, config];\n }\n\n constructor(viewer: Viewer, config: ResolutionPluginConfig) {\n super(viewer);\n\n this.config = getConfig(config);\n\n if (this.config.defaultResolution && this.viewer.config.panorama) {\n utils.logWarn(\n 'ResolutionPlugin, a defaultResolution was provided '\n + 'but a panorama is already configured on the viewer, '\n + 'the defaultResolution will be ignored.',\n );\n }\n }\n\n /**\n * @internal\n */\n override init() {\n super.init();\n\n this.settings = this.viewer.getPlugin('settings');\n\n if (!this.settings) {\n throw new PSVError('Resolution plugin requires the Settings plugin');\n }\n\n this.settings.addSetting({\n id: ResolutionPlugin.id,\n type: 'options',\n label: ResolutionPlugin.id,\n current: () => this.state.resolution,\n options: () => this.resolutions,\n apply: resolution => this.__setResolutionIfExists(resolution),\n badge: !this.config.showBadge ? null : () => this.state.resolution,\n } as OptionsSetting);\n\n this.viewer.addEventListener(events.PanoramaLoadedEvent.type, this);\n\n if (this.config.resolutions) {\n this.setResolutions(\n this.config.resolutions,\n this.viewer.config.panorama ? null : this.config.defaultResolution,\n );\n delete this.config.resolutions;\n delete this.config.defaultResolution;\n }\n }\n\n /**\n * @internal\n */\n override destroy() {\n this.viewer.removeEventListener(events.PanoramaLoadedEvent.type, this);\n\n this.settings.removeSetting(ResolutionPlugin.id);\n\n super.destroy();\n }\n\n /**\n * @internal\n */\n handleEvent(e: Event) {\n if (e instanceof events.PanoramaLoadedEvent) {\n this.__refreshResolution();\n }\n }\n\n /**\n * Changes the available resolutions\n * @param resolutions\n * @param defaultResolution - if not provided, the current panorama is kept\n * @throws {@link PSVError} if the configuration is invalid\n */\n setResolutions(resolutions: Resolution[], defaultResolution?: string) {\n this.resolutions = resolutions;\n this.resolutionsById = {};\n\n resolutions.forEach((resolution) => {\n if (!resolution.id) {\n throw new PSVError('Missing resolution id');\n }\n if (!resolution.label) {\n throw new PSVError('Missing resolution label');\n }\n if (!resolution.panorama) {\n throw new PSVError('Missing resolution panorama');\n }\n this.resolutionsById[resolution.id] = resolution;\n });\n\n // pick first resolution if no default provided and cannot find match with current panorama\n if (!defaultResolution) {\n if (this.viewer.config.panorama) {\n const resolution = this.resolutions.find(r => utils.deepEqual(this.viewer.config.panorama, r.panorama));\n if (!resolution) {\n defaultResolution = resolutions[0].id;\n }\n } else {\n defaultResolution = resolutions[0].id;\n }\n }\n\n if (defaultResolution) {\n this.setResolution(defaultResolution);\n }\n\n this.__refreshResolution();\n }\n\n /**\n * Changes the current resolution\n * @throws {@link PSVError} if the resolution does not exist\n */\n setResolution(id: string): Promise<unknown> {\n if (!this.resolutionsById[id]) {\n throw new PSVError(`Resolution \"${id}\" unknown`);\n }\n\n return this.__setResolutionIfExists(id);\n }\n\n private __setResolutionIfExists(id: string): Promise<unknown> {\n if (this.resolutionsById[id]) {\n return this.viewer.setPanorama(this.resolutionsById[id].panorama, {\n transition: false,\n showLoader: false,\n panoData: this.resolutionsById[id].panoData,\n });\n } else {\n return Promise.resolve();\n }\n }\n\n /**\n * Returns the current resolution\n */\n getResolution(): string {\n return this.state.resolution;\n }\n\n /**\n * Updates current resolution on panorama load\n */\n private __refreshResolution() {\n const resolution = this.resolutions.find(r => utils.deepEqual(this.viewer.config.panorama, r.panorama));\n if (this.state.resolution !== resolution?.id) {\n this.state.resolution = resolution?.id;\n this.settings?.updateButton();\n this.dispatchEvent(new ResolutionChangedEvent(this.state.resolution));\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAAA,eAAyB;;;ACAzB;AAAA;AAAA;AAAA;AAAA,kBAA2B;AAMpB,IAAM,0BAAN,MAAM,gCAA+B,uBAA6B;AAAA;AAAA,EAKrE,YAA4B,cAAsB;AAC9C,UAAM,wBAAuB,IAAI;AADT;AAAA,EAE5B;AACJ;AARa,wBACgB,OAAO;AAD7B,IAAM,yBAAN;;;ACLP,IAAAC,eAAwD;AAKxD,IAAM,YAAY,mBAAM,gBAAwC;AAAA,EAC5D,aAAa;AAAA,EACb,mBAAmB;AAAA,EACnB,WAAW;AACf,CAAC;AAKM,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