Skip to content

Commit

Permalink
Fix #626 make plugins init independant of order
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Jan 22, 2022
1 parent ffdda28 commit 05b1e46
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 80 deletions.
11 changes: 9 additions & 2 deletions docs/plugins/writing-a-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The recommended way to create your own plugin is as an ES6 class extending `Abst
**Requirements:**
- The plugin class **must** take a `PSV.Viewer` object as first parameter and pass it to the `super` constructor.
- It **must** have a `static id` property.
- It **must** implement the `init` method to perform initialization, like subscribing to events.
- It **must** implement the `destroy` method which is used to cleanup the plugin when the viewer is unloaded.
- The constructor **can** take an `options` object as second parameter.

Expand All @@ -25,7 +26,9 @@ export class PhotoSphereViewerCustomPlugin extends AbstractPlugin {

constructor(psv, options) {
super(psv);

}

init() {
// do your initialisation logic here
}

Expand Down Expand Up @@ -203,7 +206,11 @@ export default class PhotoSphereViewerCustomPlugin extends AbstractPlugin {
/**
* @type {PSV.plugins.SettingsPlugin}
*/
this.settings = psv.getPlugin('settings');
this.settings = null;
}

init() {
this.settings = this.psv.getPlugin('settings');

// the user may choose to not import the Settings plugin
// you may choose to make it a requirement by throwing an error...
Expand Down
1 change: 1 addition & 0 deletions src/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export class Viewer extends EventEmitter {
this.config.plugins.forEach(([plugin, opts]) => {
this.plugins[plugin.id] = new plugin(this, opts); // eslint-disable-line new-cap
});
each(this.plugins, plugin => plugin.init());

// init buttons
this.navbar.setButtons(this.config.navbar);
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/AbstractPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ export class AbstractPlugin extends EventEmitter {
this.psv = psv;
}

/**
* @summary Initializes the plugin
* @package
*/
init() {
}

/**
* @summary Destroys the plugin
* @package
Expand Down
16 changes: 12 additions & 4 deletions src/plugins/autorotate-keypoints/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export class AutorotateKeypointsPlugin extends AbstractPlugin {
this.config = {
startFromClosest: true,
...options,
keypoints : null,
};

/**
Expand All @@ -74,10 +73,20 @@ export class AutorotateKeypointsPlugin extends AbstractPlugin {
* @type {PSV.plugins.MarkersPlugin}
* @private
*/
this.markers = null;
}

/**
* @package
*/
init() {
super.init();

this.markers = this.psv.getPlugin('markers');

if (options?.keypoints) {
this.setKeypoints(options.keypoints);
if (this.config.keypoints) {
this.setKeypoints(this.config.keypoints);
delete this.config.keypoints;
}

this.psv.on(CONSTANTS.EVENTS.AUTOROTATE, this);
Expand All @@ -93,7 +102,6 @@ export class AutorotateKeypointsPlugin extends AbstractPlugin {

delete this.markers;
delete this.keypoints;
delete this.state;

super.destroy();
}
Expand Down
28 changes: 19 additions & 9 deletions src/plugins/compass/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class CompassPlugin extends AbstractPlugin {
* @type {PSV.plugins.MarkersPlugin}
* @private
*/
this.markers = this.psv.getPlugin('markers');
this.markers = null;

/**
* @member {HTMLElement}
Expand All @@ -90,27 +90,37 @@ export class CompassPlugin extends AbstractPlugin {
this.container.style.marginLeft = `calc(-${this.config.size} / 2)`;
}

/**
* @member {HTMLCanvasElement}
* @readonly
* @private
*/
this.canvas = document.createElement('canvas');

this.container.appendChild(this.canvas);

if (this.config.navigation) {
this.container.addEventListener('mouseenter', this);
this.container.addEventListener('mouseleave', this);
this.container.addEventListener('mousemove', this);
this.container.addEventListener('mousedown', this);
this.container.addEventListener('mouseup', this);
}
}

/**
* @package
*/
init() {
super.init();

this.markers = this.psv.getPlugin('markers');

this.psv.container.appendChild(this.container);

/**
* @member {HTMLCanvasElement}
* @readonly
* @private
*/
this.canvas = document.createElement('canvas');
this.canvas.width = this.container.clientWidth * SYSTEM.pixelRatio;
this.canvas.height = this.container.clientWidth * SYSTEM.pixelRatio;

this.container.appendChild(this.canvas);

this.psv.on(CONSTANTS.EVENTS.RENDER, this);

if (this.markers) {
Expand Down
8 changes: 7 additions & 1 deletion src/plugins/gyroscope/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ export class GyroscopePlugin extends AbstractPlugin {
* @private
*/
this.controls = null;
}

/**
* @package
*/
init() {
super.init();

this.psv.on(CONSTANTS.EVENTS.STOP_ALL, this);
this.psv.on(CONSTANTS.EVENTS.BEFORE_ROTATE, this);
Expand All @@ -91,7 +98,6 @@ export class GyroscopePlugin extends AbstractPlugin {
this.stop();

delete this.controls;
delete this.prop;

super.destroy();
}
Expand Down
32 changes: 20 additions & 12 deletions src/plugins/markers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,6 @@ export class MarkersPlugin extends AbstractPlugin {
constructor(psv, options) {
super(psv);

/**
* @member {HTMLElement}
* @readonly
*/
this.container = document.createElement('div');
this.container.className = 'psv-markers';
this.container.style.cursor = this.psv.config.mousemove ? 'move' : 'default';
this.psv.container.appendChild(this.container);

/**
* @summary All registered markers
* @member {Object<string, PSV.plugins.MarkersPlugin.Marker>}
Expand Down Expand Up @@ -99,6 +90,14 @@ export class MarkersPlugin extends AbstractPlugin {
...options,
};

/**
* @member {HTMLElement}
* @readonly
*/
this.container = document.createElement('div');
this.container.className = 'psv-markers';
this.container.style.cursor = this.psv.config.mousemove ? 'move' : 'default';

/**
* @member {SVGElement}
* @readonly
Expand All @@ -112,16 +111,26 @@ export class MarkersPlugin extends AbstractPlugin {
this.container.addEventListener('mouseleave', this, true);
this.container.addEventListener('mousemove', this, true);
this.container.addEventListener('contextmenu', this);
}

/**
* @package
*/
init() {
super.init();

this.psv.container.appendChild(this.container);

// Viewer events
this.psv.on(CONSTANTS.EVENTS.CLICK, this);
this.psv.on(CONSTANTS.EVENTS.DOUBLE_CLICK, this);
this.psv.on(CONSTANTS.EVENTS.RENDER, this);
this.psv.on(CONSTANTS.EVENTS.CONFIG_CHANGED, this);

if (options?.markers) {
if (this.config.markers) {
this.psv.once(CONSTANTS.EVENTS.READY, () => {
this.setMarkers(options.markers);
this.setMarkers(this.config.markers);
delete this.config.markers;
});
}
}
Expand All @@ -147,7 +156,6 @@ export class MarkersPlugin extends AbstractPlugin {
delete this.svgContainer;
delete this.markers;
delete this.container;
delete this.prop;

super.destroy();
}
Expand Down
49 changes: 33 additions & 16 deletions src/plugins/resolution/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,7 @@ export class ResolutionPlugin extends AbstractPlugin {
* @readonly
* @private
*/
this.settings = psv.getPlugin('settings');

if (!this.settings) {
throw new PSVError('Resolution plugin requires the Settings plugin');
}

this.settings.addSetting({
id : ResolutionPlugin.id,
type : 'options',
label : this.psv.config.lang.resolution,
current: () => this.prop.resolution,
options: () => this.__getSettingsOptions(),
apply : resolution => this.setResolution(resolution),
});
this.settings = null;

/**
* @summary Available resolutions
Expand All @@ -85,10 +72,40 @@ export class ResolutionPlugin extends AbstractPlugin {
resolution: null,
};

/**
* @type {PSV.plugins.ResolutionPlugin.Options}
*/
this.config = {
...options,
};
}

/**
* @package
*/
init() {
super.init();

this.settings = this.psv.getPlugin('settings');

if (!this.settings) {
throw new PSVError('Resolution plugin requires the Settings plugin');
}

this.settings.addSetting({
id : ResolutionPlugin.id,
type : 'options',
label : this.psv.config.lang.resolution,
current: () => this.prop.resolution,
options: () => this.__getSettingsOptions(),
apply : resolution => this.setResolution(resolution),
});

this.psv.on(CONSTANTS.EVENTS.PANORAMA_LOADED, this);

if (options?.resolutions) {
this.setResolutions(options.resolutions);
if (this.config.resolutions) {
this.setResolutions(this.config.resolutions);
delete this.config.resolutions;
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/plugins/settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,19 @@ export class SettingsPlugin extends AbstractPlugin {
this.settings = [];
}

/**
* @package
*/
init() {
super.init();
}

/**
* @package
*/
destroy() {
delete this.settings;

super.destroy();
}

Expand Down
Loading

0 comments on commit 05b1e46

Please sign in to comment.