You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

113 lines
2.9 KiB

/**
Implémente la télécommande JOSM
Le HTML géré pourrait ressembler à :
```html
<button
type="button"
data-controller="josm"
data-action="click->josm#remoteControl"
data-josm-commands-value="…"
>JOSM</button>
```
Où la valeur `commands` prend la forme d’un objet JSON dont chacune des entrées
représente une commande à enchaîner successivement et où la clef est le chemin
de la commande et la valeur un objet dont chacune des entrées représente les
paramètres de la commande.
Par exemple :
```json
{
"version": {},
"imagery": {
"id": "osmfr",
}
}
```
Avec cette valeur de `commands`, les appels successifs aux endpoints `/version`
puis `imagery?id=osmfr` seront effectués.
Cf <https://josm.openstreetmap.de/wiki/Help/RemoteControlCommands>
TODO Gérer le cas où il n’y a pas de JOSM en face pourrait être plus user-friendly.
**/
import { Controller } from '@hotwired/stimulus';
function until(conditionFunction, maximum, message) {
var current = maximum;
const poll = function (resolve) {
if (conditionFunction()) {
resolve();
} else {
current -= 1;
if (current > 0) {
setTimeout(function() {
return poll(resolve);
}, 400);
} else {
alert(message);
}
}
};
return new Promise(poll);
}
export default class extends Controller {
static values = {
commands: String,
ready: Boolean
}
remoteControl() {
const baseurl = 'http://localhost:8111';
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) {
});
}
}
isReady() {
const url = 'http://localhost:8111/version';
const _this = this;
this.readyValue = false;
fetch(url)
.catch(function (error) {
_this.readyValue = false;
})
.then(function (response) {
if (typeof response === 'undefined') {
return;
}
return response.text();
})
.then(function (text) {
if (typeof response === 'undefined') {
_this.readyValue = false;
} else {
_this.readyValue = true;
}
});
}
remoteControlIfReady() {
const _this = this;
until(function () { return _this.isReady(); }, 10, 'JOSM n´est pas disponible pour le moment').then(function () { _this.remoteControl(); });
}
}