経緯
ラジコンのコントローラーのようなものをNode-REDのdashboardで作ろうとしたのだが、dashboardのボタンはclickイベントしか検出できないため、ボタンを押している間動くようなことが実現できない。
ui-templateを使えばできそうなのだが、Angular.jsとjQueryがわからず途方に暮れていた。
解決策を発見
いろいろ検索していたら以下のgistを発見した。
This ui-template creates a momentary button that outputs true on mousedown/touchstart and false on mouseup/touchend
とあるので、まさにやりたいことであった。しかも、スマホ操作touchdownとtouchendにも対応。
ui-templateノードの中身だけ抜き出させてもらった。
<style>
.nr-dashboard-template {
padding: 0px;
}
</style>
<div class="momentary">
<md-button style="width:100%; height:48px; margin: 0px"> Momentary Button</md-button>
</div>
<script>
(function($scope) {
$('.momentary').on('touchstart mousedown', function(e) {
e.preventDefault(); //prevent default behavior
$scope.send({"payload": true});
});
$('.momentary').on('touchend mouseup', function(e) {
e.preventDefault(); //prevent default behavior
$scope.send({"payload": false});
});
})(scope);
</script>
これをui-templateノードの中に入れてデプロイすれば、ボタンを押したときはpayloadにtrueが、離したときにはpayloadにfalseが出てくるようになる。
おわりに
node-red-dashboardのtemplateノードを調べたかったのだが、Node-REDにもtemplateノードがあるので検索性が悪かった。