3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Node-RED dashboard のボタンで押したときと離したときを検出したい

Last updated at Posted at 2020-08-13

経緯

ラジコンのコントローラーのようなものを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ノードがあるので検索性が悪かった。

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?