Skip to content

Commit

Permalink
Moar annotations stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
pierotofy committed May 26, 2024
1 parent 6a69f1e commit 3021849
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
10 changes: 7 additions & 3 deletions app/static/app/js/classes/plugins/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ export default {
endpoints: [
["willAddControls", leafletPreCheck],
["didAddControls", layersControlPreCheck],
["addActionButton", leafletPreCheck],
["didAddAnnotation"]
["addActionButton", leafletPreCheck]
],

functions: [
"handleClick"
"handleClick",
"addAnnotation",
"updateAnnotation",
"deleteAnnotation",
"toggleAnnotation",
"annotationDeleted"
]
};

54 changes: 46 additions & 8 deletions app/static/app/js/components/LayersControlAnnotations.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React from 'react';
import PropTypes from 'prop-types';
import '../css/LayersControlAnnotations.scss';
import PluginsAPI from '../classes/plugins/API';
import { Checkbox, ExpandButton } from './Toggle';
import { _ } from '../classes/gettext';

class AnnotationLayer extends React.Component{
static propTypes = {
parent: PropTypes.object,
layer: PropTypes.object
}

Expand All @@ -17,8 +19,32 @@ class AnnotationLayer extends React.Component{
}
}

componentDidUpdate(prevProps, prevState){
if (prevState.visible !== this.state.visible && this.props.parent.state.visible){
PluginsAPI.Map.toggleAnnotation(this.props.layer, this.state.visible);
}
}

componentDidMount(){
PluginsAPI.Map.onUpdateAnnotation(this.handleUpdate);
}

componentWillUnmount(){
PluginsAPI.Map.offUpdateAnnotation(this.handleUpdate);
}

handleUpdate = (layer, name) => {
if (this.props.layer === layer){
const meta = layer[Symbol.for("meta")];
meta.name = name;
this.forceUpdate();
}
}

handleFocus = () => {
const { layer } = this.props;
if (!layer._map) return;

if (layer.options.bounds || layer.getBounds){
const bounds = layer.options.bounds !== undefined ?
layer.options.bounds :
Expand All @@ -32,7 +58,9 @@ class AnnotationLayer extends React.Component{
}

handleDelete = () => {

if (window.confirm(_('Are you sure you want to delete this?'))){
PluginsAPI.Map.deleteAnnotation(this.props.layer);
}
}

render(){
Expand All @@ -51,13 +79,13 @@ export default class LayersControlAnnotations extends React.Component {
static defaultProps = {
expanded: true,
visible: true

};
};
static propTypes = {
expanded: PropTypes.bool,
visible: PropTypes.bool,
layers: PropTypes.array
}
}

constructor(props){
super(props);
Expand All @@ -66,11 +94,21 @@ export default class LayersControlAnnotations extends React.Component {
visible: props.visible,
expanded: props.expanded
};

this.annRefs = new Array(props.layers.length);
}

handleAnnotationsClick = () => {
this.setState({expanded: !this.state.expanded});
}

handleLayerClick = () => {
console.log("TODO")
componentDidUpdate(prevProps, prevState){
if (prevState.visible !== this.state.visible){
this.annRefs.forEach(ann => {
let visible = this.state.visible ? ann.state.visible : false;
PluginsAPI.Map.toggleAnnotation(ann.props.layer, visible);
});
}
}


Expand All @@ -81,12 +119,12 @@ export default class LayersControlAnnotations extends React.Component {
return (<div className="layers-control-layer">
<div className="layer-control-title">
<ExpandButton bind={[this, 'expanded']} /><Checkbox bind={[this, 'visible']}/>
<a title={_("Annotations")} className="layer-label" href="javascript:void(0);" onClick={this.handleLayerClick}><div className="layer-title"><i className="layer-icon fa fa-sticky-note fa-fw"></i> {_("Annotations")}</div></a>
<a title={_("Annotations")} className="layer-label" href="javascript:void(0);" onClick={this.handleAnnotationsClick}><div className="layer-title"><i className="layer-icon fa fa-sticky-note fa-fw"></i> {_("Annotations")}</div></a>
</div>

{this.state.expanded ?
<div className="layer-expanded">
{layers.map((layer, i) => <AnnotationLayer key={i} layer={layer} />)}
{layers.map((layer, i) => <AnnotationLayer parent={this} ref={domNode => this.annRefs[i] = domNode} key={i} layer={layer} />)}
</div> : ""}
</div>);

Expand Down
22 changes: 16 additions & 6 deletions app/static/app/js/components/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ class Map extends React.Component {
// leaflet bug?
$(this.container).addClass("leaflet-touch");

PluginsAPI.Map.onAddAnnotation(this.handleAddAnnotation);
PluginsAPI.Map.onAnnotationDeleted(this.handleDeleteAnnotation);

PluginsAPI.Map.triggerWillAddControls({
map: this.map,
tiles,
Expand Down Expand Up @@ -641,22 +644,25 @@ _('Example:'),
pluginActionButtons: {$push: [button]}
}));
});
}

PluginsAPI.Map.didAddAnnotation([], (opts) => {
const layer = opts.layer;
handleAddAnnotation = (layer, name, task) => {
const meta = {
name: opts.name || "",
icon: opts.icon || "fa fa-sticky-note fa-fw"
name: name || "",
icon: "fa fa-sticky-note fa-fw"
};
if (this.props.tiles.length > 1 && opts.task){
meta.group = {id: opts.task.id, name: opts.task.name};
meta.group = {id: task.id, name: task.name};
}
layer[Symbol.for("meta")] = meta;

this.setState(update(this.state, {
annotations: {$push: [layer]}
}));
});
}

handleDeleteAnnotation = (layer) => {
this.setState({annotations: this.state.annotations.filter(l => l !== layer)});
}

componentDidUpdate(prevProps, prevState) {
Expand All @@ -683,6 +689,10 @@ _('Example:'),
this.tileJsonRequests.forEach(tileJsonRequest => tileJsonRequest.abort());
this.tileJsonRequests = [];
}

PluginsAPI.Map.offAddAnnotation(this.handleAddAnnotation);
PluginsAPI.Map.offAnnotationDeleted(this.handleAddAnnotation);

}

handleMapMouseDown(e){
Expand Down

0 comments on commit 3021849

Please sign in to comment.