Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable zoom by box selection #319

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions components/MapSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class MapSelection extends React.Component {
}
componentWillUnmount() {
this.map.removeLayer(this.selectionLayer);
this.removeDrawInteraction();
}
addDrawInteraction = () => {
// cleanup old interaction
Expand All @@ -111,8 +112,7 @@ class MapSelection extends React.Component {
}
if (this.props.geomType === "DragBox") {
this.drawInteraction = new ol.interaction.DragBox({
className: 'selection-drag-box',
condition: ol.events.condition.shiftKeyOnly
className: 'selection-drag-box'
});

this.drawInteraction.on('boxend', () => {
Expand Down
109 changes: 94 additions & 15 deletions plugins/ZoomButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import React from 'react';
import {connect} from 'react-redux';

import classnames from 'classnames';
import PropTypes from 'prop-types';

import {changeZoomLevel} from '../actions/map';
import {changeZoomLevel, zoomToExtent, zoomToPoint} from '../actions/map';
import {setCurrentTask} from '../actions/task';
import Icon from '../components/Icon';
import MapSelection from '../components/MapSelection';
import LocaleUtils from '../utils/LocaleUtils';
import ThemeUtils from '../utils/ThemeUtils';

Expand All @@ -26,18 +29,53 @@ import './style/Buttons.css';
class ZoomButton extends React.Component {
static propTypes = {
changeZoomLevel: PropTypes.func,
click: PropTypes.object,
currentTask: PropTypes.string,
currentZoom: PropTypes.number,
direction: PropTypes.number,
/** Enable zoom in or out by box selection. */
enableZoomByBoxSelection: PropTypes.bool,
mapCrs: PropTypes.string,
mapMargins: PropTypes.object,
maxZoom: PropTypes.number,
/** The position slot index of the map button, from the bottom (0: bottom slot). */
position: PropTypes.number,
setCurrentTask: PropTypes.func,
theme: PropTypes.object,
/** Omit the button in themes matching one of these flags. */
themeFlagBlacklist: PropTypes.arrayOf(PropTypes.string),
/** Only show the button in themes matching one of these flags. */
themeFlagWhitelist: PropTypes.arrayOf(PropTypes.string)
themeFlagWhitelist: PropTypes.arrayOf(PropTypes.string),
zoomToExtent: PropTypes.func,
zoomToPoint: PropTypes.func
};
static defaultProps = {
enableZoomByBoxSelection: false
};
state = {
disabled: false,
task: null,
zoomBox: null
};
componentDidUpdate(prevProps) {
if (prevProps !== this.props) {
if (this.props.direction > 0) {
this.setState({disabled: this.props.currentZoom >= this.props.maxZoom, task: "ZoomIn"});
} else if (this.props.direction < 0) {
this.setState({disabled: this.props.currentZoom <= 0, task: "ZoomOut"});
}
}
if (this.props.currentTask !== null && this.props.currentTask === this.state.task && this.state.disabled) {
this.props.setCurrentTask(null);
}
if (this.props.currentTask === this.state.task && this.props.click !== prevProps.click) {
const point = this.props.click.coordinate;
if (point) {
const zoom = Math.max(0, this.props.currentZoom + this.props.direction);
this.props.zoomToPoint(point, zoom, this.mapCrs);
}
}
}
render() {
if (!ThemeUtils.themFlagsAllowed(this.props.theme, this.props.themeFlagWhitelist, this.props.themeFlagBlacklist)) {
return null;
Expand All @@ -50,42 +88,83 @@ class ZoomButton extends React.Component {
right: 'calc(1.5em + ' + right + 'px)',
bottom: 'calc(' + bottom + 'px + ' + (5 + 4 * position) + 'em)'
};
let disabled = false;
if (this.props.direction > 0) {
disabled = this.props.currentZoom >= this.props.maxZoom;
} else if (this.props.direction < 0) {
disabled = this.props.currentZoom <= 0;
}
const tooltip = this.props.direction > 0 ? LocaleUtils.tr("tooltip.zoomin") : LocaleUtils.tr("tooltip.zoomout");
return (
<button className="map-button"
disabled={disabled}
onClick={() => this.props.changeZoomLevel(this.props.currentZoom + this.props.direction)}
const classes = classnames({
"map-button": true,
"map-button-active": this.props.enableZoomByBoxSelection && this.props.currentTask === this.state.task,
"map-button-disabled": this.state.disabled
});
return [(
<button className={classes}
disabled={this.state.disabled}
key={this.state.task + "Button"}
onClick={this.buttonClicked}
style={style}
title={tooltip}
>
<Icon icon={this.props.direction > 0 ? "plus" : "minus"} title={tooltip}/>
</button>
);
), (
this.props.currentTask === this.state.task ? (
<MapSelection
active cursor={this.props.direction > 0 ? "zoom-in" : "zoom-out"}
geomType="DragBox"
geometryChanged={(geom) => this.updateZoom(geom)}
key="MapSelection"
/>
) : null
)];
}
buttonClicked = () => {
if (this.props.enableZoomByBoxSelection) {
const task = this.props.direction > 0 ? "ZoomIn" : "ZoomOut";
this.props.setCurrentTask(this.props.currentTask === task ? null : task);
} else {
this.props.changeZoomLevel(this.props.currentZoom + this.props.direction);
}
};
updateZoom = (geom) => {
this.setState(() => ({zoomBox: geom.coordinates[0]}), () => {
if (this.props.direction > 0) {
this.props.zoomToExtent(this.state.zoomBox, this.props.mapCrs);
} else {
const bounds = this.state.zoomBox;
const center = [0.5 * (bounds[0] + bounds[2]), 0.5 * (bounds[1] + bounds[3])];
const zoom = Math.max(0, this.props.currentZoom + this.props.direction);
this.props.zoomToPoint(center, zoom, this.props.mapCrs);
}
});
};
}

export const ZoomInPlugin = connect((state) => ({
click: state.map.click,
currentTask: state.task.id,
currentZoom: state.map.zoom,
maxZoom: state.map.resolutions.length - 1,
direction: +1,
mapCrs: state.map.projection,
mapMargins: state.windows.mapMargins,
theme: state.theme.current
}), {
changeZoomLevel: changeZoomLevel
changeZoomLevel: changeZoomLevel,
setCurrentTask: setCurrentTask,
zoomToExtent: zoomToExtent,
zoomToPoint: zoomToPoint
})(ZoomButton);

export const ZoomOutPlugin = connect((state) => ({
click: state.map.click || {},
currentTask: state.task.id,
currentZoom: state.map.zoom,
maxZoom: state.map.resolutions.length - 1,
direction: -1,
mapCrs: state.map.projection,
mapMargins: state.windows.mapMargins,
theme: state.theme.current
}), {
changeZoomLevel: changeZoomLevel
changeZoomLevel: changeZoomLevel,
setCurrentTask: setCurrentTask,
zoomToExtent: zoomToExtent,
zoomToPoint: zoomToPoint
})(ZoomButton);
10 changes: 10 additions & 0 deletions plugins/style/Buttons.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ button.map-button-active {
color: var(--map-button-active-text-color);
}

button.map-button-disabled {
opacity: 0.7;
cursor: default;
}

button.map-button-disabled:hover {
background-color: var(--map-button-bg-color);
color: var(--map-button-text-color);
}

button.map-button.locate-button-PERMISSION_DENIED {
opacity: 0.7;
cursor: default;
Expand Down
Loading