Skip to content

Commit

Permalink
Informative Window is now opened from Breadcrumbs/LayerSwitcher - closes
Browse files Browse the repository at this point in the history
 #279:

- Added a mechanism that allows programatic opening of any given Plugin Window (using globalObserver)
- This mechanism is used in Informative, so we can change chapter and make sure that the Window is visible too.
- Also, moved the 'open' method on to the 'custom' object (was previously a direct prop on BaseWindowPlugin).
  • Loading branch information
jacobwod committed Oct 10, 2019
1 parent be2c060 commit 557f016
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
34 changes: 31 additions & 3 deletions new-client/src/plugins/BaseWindowPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ class BaseWindowPlugin extends React.PureComponent {
// decide whether the Window will render on left or right side of the screen.
this.position = props.options.target === "left" ? "right" : "left";

// Register Window in our global register
props.app.registerWindowPlugin(this);

// Subscribe to a global event that makes it possible to show/hide Windows.
// First we prepare a unique event name for each plugin so it looks like 'showSomeplugin'.
const eventName = `show${this.type.charAt(0).toUpperCase() +
this.type.slice(1)}`;
// Next, subscribe to that event, expect 'opts' array.
// To find all places where this event is publish, search for 'globalObserver.publish("show'
props.app.globalObserver.subscribe(eventName, opts => {
this.showWindow(opts);
});
}

componentDidMount() {
Expand All @@ -57,18 +68,35 @@ class BaseWindowPlugin extends React.PureComponent {
}

handleButtonClick = e => {
this.props.app.onWindowOpen(this);
this.showWindow({
hideOtherPluginWindows: true,
runCallback: true
});
this.props.app.globalObserver.publish("hideDrawer");
};

showWindow = opts => {
const hideOtherPluginWindows = opts.hideOtherPluginWindows || true,
runCallback = opts.runCallback || true;

// Don't continue if visibility hasn't changed
if (this.state.windowVisible === true) {
return null;
}

hideOtherPluginWindows === true && this.props.app.onWindowOpen(this);

this.setState(
{
windowVisible: true
},
() => {
// If there's a callback defined in custom, run it
typeof this.props.custom.onWindowShow === "function" &&
runCallback === true &&
typeof this.props.custom.onWindowShow === "function" &&
this.props.custom.onWindowShow();
}
);
this.props.app.globalObserver.publish("hideDrawer");
};

closeWindow = () => {
Expand Down
18 changes: 14 additions & 4 deletions new-client/src/plugins/informative/informative.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,34 @@ class Informative extends React.PureComponent {
});
}

/**
* Shows the Informative Window and opens a specified chapter.
*
* Opening the Window is achieved using the globalObserver. Each
* Plugin has a unique event (named as: "showPluginName"). See
* BaseWindowPlugin for subscription.
*
* @memberof Informative
*/
open = chapter => {
this.localObserver.publish("changeChapter", chapter);
this.app.globalObserver.publish("showInformative", {
hideOtherPlugins: false
});
};

render() {
return (
<BaseWindowPlugin
{...this.props}
open={this.open} // Expose open() so it can be used from other plugins (BreadCrumbs uses this)
type={this.constructor.name}
custom={{
open: this.open, // Expose open() so it can be used from other plugins (LayerSwitcher/BreadCrumbs uses this)
icon: <SatelliteIcon />,
title: "Översiktsplan",
description: "Läs mer om vad som planeras i kommunen",
height: "auto",
width: 400,
top: undefined,
left: undefined
width: 400
}}
>
<InformativeView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class BreadCrumb extends Component {
const informativeWindow = this.props.app.windows.find(
window => window.type === "informative"
);
informativeWindow.props.open(chapter);
informativeWindow.props.custom.open(chapter);
};

handleClose = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ class LayerGroup extends React.PureComponent {
const informativeWindow = this.props.app.windows.find(
window => window.type === "informative"
);
informativeWindow.props.open(chapter);
informativeWindow.props.custom.open(chapter);
}}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class LayerItem extends React.PureComponent {
const informativeWindow = app.windows.find(
window => window.type === "informative"
);
informativeWindow.props.open(chapter);
informativeWindow.props.custom.open(chapter);
}}
/>
);
Expand Down

0 comments on commit 557f016

Please sign in to comment.