diff --git a/assets/controllers/josm_controller.js b/assets/controllers/josm_controller.js index b2a2842..c3c8d03 100644 --- a/assets/controllers/josm_controller.js +++ b/assets/controllers/josm_controller.js @@ -2,70 +2,27 @@ import { Controller } from '@hotwired/stimulus'; export default class extends Controller { static values = { - imagery: String, - importurl: String, - layername: String, - sendosm: Boolean, - bottom: Number, - top: Number, - left: Number, - right: Number, - comment: String, - source: String, - hashtags: String, + commands: String, } remoteControl() { // cf const baseurl = 'http://localhost:8111'; - const _this = this; - var url = baseurl + '/version'; - fetch(url) - .then(function (response) { - return response.json(); - }) - .then(function (json) { - console.log('JOSM v' + json.version); - url = baseurl + '/imagery?' + (new URLSearchParams({ - 'id': _this.imageryValue, - })); - fetch(url) + const _this = this, commands = JSON.parse(this.commandsValue); + for (var command in commands) { + var url = baseurl + '/' + command, params = new URLSearchParams(); + for (var name in commands[command]) { + params.append(name, commands[command][name]); + } + if (Array.from(params.length > 0)) { + url += '?' + params.toString(); + } + fetch(url) .then(function (response) { return response.text(); }) .then(function (text) { - console.log('JOSM imagery ' + text); }); - if (_this.sendosmValue) { - url = baseurl + '/import?' + (new URLSearchParams({ - 'new_layer': true, - 'layer_name': _this.layernameValue, - 'url': _this.importurlValue, - })); - fetch(url) - .then(function (response) { - return response.text(); - }) - .then(function (text) { - console.log('JOSM import ' + text); - }); - } - url = baseurl + '/zoom?' + (new URLSearchParams({ - 'bottom': _this.bottomValue, - 'top': _this.topValue, - 'left': _this.leftValue, - 'right': _this.rightValue, - 'changeset_comment': _this.commentValue, - 'changeset_source': _this.sourceValue, - 'changeset_hashtags': _this.hashtagsValue, - })); - fetch(url) - .then(function (response) { - return response.text(); - }) - .then(function (text) { - console.log('JOSM zoom ' + text); - }); - }); + } } } diff --git a/src/Controller/MapController.php b/src/Controller/MapController.php index 522fdd4..2881ef7 100644 --- a/src/Controller/MapController.php +++ b/src/Controller/MapController.php @@ -14,8 +14,23 @@ class MapController extends AbstractController public function popup(Request $request): Response { $element = json_decode($request->query->get('element'), true); + + $josmCommands = [ + 'imagery' => [ + 'id' => 'osmfr', + ], + 'load_and_zoom' => [ + 'bottom' => $element['bounds']['minlat'], + 'top' => $element['bounds']['maxlat'], + 'left' => $element['bounds']['minlon'], + 'right' => $element['bounds']['maxlon'], + 'select' => sprintf('%s%d', $element['type'], $element['id']), + ], + ]; + return $this->render('partials/_overpass-element-popup.html.twig', [ 'element' => $element, + 'josmCommands' => json_encode($josmCommands), ]); } } diff --git a/src/Controller/TaskController.php b/src/Controller/TaskController.php index 93668d5..5ceca75 100644 --- a/src/Controller/TaskController.php +++ b/src/Controller/TaskController.php @@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Workflow\WorkflowInterface; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; #[Route('/task')] class TaskController extends AbstractController @@ -91,6 +92,7 @@ class TaskController extends AbstractController return $this->redirect($request->headers->get('Referer')); } + $project = $task->getProject(); $nextTask = $repository->findNextOne($task); $comment = new Comment(); @@ -104,12 +106,36 @@ class TaskController extends AbstractController $geom = \geoPHP::load($task->getGeojson(), 'json'); $bbox = $geom->getBBox(); + $josmCommands = [ + 'imagery' => [ + 'id' => $project->hasImagery() ? $project->getImagery() : 'osmfr', + ], + 'import' => [ + 'new_layer' => true, + 'layer_name' => $task->getName(), + 'url' => $this->generateUrl( + 'app_task_osm', + ['slug' => $task->getSlug()], + UrlGeneratorInterface::ABSOLUTE_URL + ), + ], + 'zoom' => [ + 'bottom' => $bbox['miny'], + 'top' => $bbox['maxy'], + 'left' => $bbox['minx'], + 'right' => $bbox['maxx'], + 'changeset_comment' => sprintf('%s %s', $project->getName(), $task->getName()), + 'changeset_source' => $project->getSource(), + 'changeset_hashtags' => $project->getHashtags(), + ], + ]; + return $this->render('task/show.html.twig', [ 'task' => $task, - 'project' => $task->getProject(), + 'project' => $project, 'commentForm' => $commentForm, 'nextTask' => $nextTask, - 'bbox' => $bbox, + 'josmCommands' => json_encode($josmCommands), ]); } diff --git a/src/Entity/Project.php b/src/Entity/Project.php index d5fcf33..3735f72 100644 --- a/src/Entity/Project.php +++ b/src/Entity/Project.php @@ -204,6 +204,11 @@ class Project return $this; } + public function hasImagery(): bool + { + return !empty($this->imagery); + } + public function getImagery(): ?string { return $this->imagery; diff --git a/templates/partials/_overpass-element-popup.html.twig b/templates/partials/_overpass-element-popup.html.twig index 8533226..a36ff28 100644 --- a/templates/partials/_overpass-element-popup.html.twig +++ b/templates/partials/_overpass-element-popup.html.twig @@ -4,6 +4,12 @@ {% if element.type == 'relation' and element.tags and element.tags.route and element.tags.route == 'hiking' %}
sur WayMarkedTrails {% endif %} +
dans {% for key, value in element.tags %} diff --git a/templates/task/show.html.twig b/templates/task/show.html.twig index 87ebaa8..1bdf48a 100644 --- a/templates/task/show.html.twig +++ b/templates/task/show.html.twig @@ -41,18 +41,7 @@ type="button" data-controller="josm" data-action="click->josm#remoteControl" - data-josm-imagery-value="{{ project.imagery is not empty ? project.imagery : 'osmfr' }}" - data-josm-importurl-value="{{ url('app_task_osm', {'slug': task.slug}) }}" - data-josm-layername-value="{{ task.name }}" - data-josm-sendosm-value="{{ task.osm is not empty }}" - data-josm-bottom-value="{{ bbox.miny }}" {# Minimum latitude 43.923196020314 #} - data-josm-top-value="{{ bbox.maxy }}" {# Maximum latitude 43.94450663651 #} - data-josm-left-value="{{ bbox.minx }}" {# Minimum longitude 4.3220213108728 #} - data-josm-right-value="{{ bbox.maxx }}" {# Maximum longitude 4.37742111766 #} - data-josm-comment-value="{{ project.name }} {{ task.name }}" - data-josm-source-value="{{ project.source }}" - data-josm-hashtags-value="{{ project.hashtags }}" - + data-josm-commands-value="{{ josmCommands|escape('html_attr') }}" >Télécommande JOSM {% endif %} {% endif %}
{{ key }}{{ value }}