This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Long click in Custom JS
#2
Hi
Erwin did this long time ago, scene button is what you describing here
Code:
$(function() {
 
 // For scene buttons use 1 byte signe integer (5) object and following additional classes:
 // Scene 0 : -> scenebutton normalpressed_0 longpressed_128
 //  or
 // Scene 1 : -> scenebutton normalpressed_1 longpressed_129
 // ........ >>>>
 // Scene 16: -> scenebutton normalpressed_15 longpressed_143
 
 // Long pushtime is set by 'var defaultlongpushtime' or by classname longpresstime_xxx where xxx = (xxx * 0.1) seconds
 // Scene 0 as 1 sec long press : -> scenebutton normalpressed_0 longpressed_128 longpresstime_10
 
 // For doorbel button with bit values use 1 bit unsigned (1) object and following additional classes:
 // Pressed 1 : -> doorbellbutton_bit released_0 pressed_1
 //    or
 // Pressed 2 : -> doorbellbutton_bit released_1 pressed_0
 
 // For doorbel button with byte values use 1 byte unsigned integer (5) object and following additional classes:
 // Sampel 1 : -> doorbellbutton_byte released_0 pressed_255
 // Sampel 2 : -> doorbellbutton_byte released_255 pressed_0
 // Sampel 3 : -> doorbellbutton_byte released_100 pressed_50
 // Etc ...
 
 // Declare variables for timers
 var longpushtime, defaultlongpushtime = 30, intervaltime = 100, timer = 0, timerInterval;

 function sendvalue(addr, value, typemajor, typeminor) {
   var id = Scada.encodeGroupAddress(addr), obj = objectStore.objects[ id ];
   if (obj && obj.datatype.major == typemajor && obj.datatype.minor == typeminor) {
     setObjectValue({ address: obj.address , rawdatatype: obj.rawdatatype }, value, "text");
   }
 }

 // Function to get values from bit object with additional classnames
 function getconfig(el, prefix, minval, maxval) {
   var reg = new RegExp(prefix + "_(\\d+)"), matches = el.className.match(reg), res = 0;
   if (matches) {
     res = parseInt(matches[ 1 ], 10) || 0;
   }
   res = Math.max(minval, res);
   res = Math.min(maxval, res);
   return res;
 }
 
 // Function to get pushtime values from additional classnames
 function getpushtime(el, prefix) {
   var reg = new RegExp(prefix + "_(\\d+)"), matches = el.className.match(reg), res = defaultlongpushtime;
   if (matches) {
     res = parseInt(matches[ 1 ], 10) || 0;
   }
   res = Math.max(0, res);
   res = Math.min(1000, res);
   return res;
 }

 // Remove original events and add new press events
 $(".scenebutton")
   .off("vclick")
   .on("vmousedown", function() {
     var btnthis = $(this), objthis = btnthis.data("object"),
     longpressvalue = getconfig(this, "longpressed", 0, 255);
       longpushtime = getpushtime(this, "longpresstime");
     timerInterval = setInterval(function() {
       timer += 1;
       if (timer == longpushtime) {
         btnthis.css("opacity", 0.5);
         sendvalue(objthis, longpressvalue, 5, 0);
         clearInterval(timerInterval);
       }
     }, intervaltime);
   })
   .on("vclick", function() {
     var btnthis = $(this), objthis = btnthis.data("object"),
     normalpressvalue = getconfig(this, "normalpressed", 0, 255);
       longpushtime = getpushtime(this, "longpresstime");
     clearInterval(timerInterval);
     if (timer < longpushtime) {
       sendvalue(objthis, normalpressvalue, 5, 0);
     }
     btnthis.css("opacity", 1);
     timer = 0;
      })
     .on("vmouseout", function() {
     var btnthis = $(this), objthis = btnthis.data("object"),
     normalpressvalue = getconfig(this, "normalpressed", 0, 255);
       longpushtime = getpushtime(this, "longpresstime");
     clearInterval(timerInterval);
     if (timer > 0 && timer < longpushtime) {
       sendvalue(objthis, normalpressvalue, 5, 0);
     }
     btnthis.css("opacity", 1);
     timer = 0;
 });
 
 // Remove original events and add new press events
 $(".doorbellbutton_bit")
   .off("vclick")
   .on("vmousedown", function() {
     var btnthis = $(this), objthis = btnthis.data("object"),
     pressedvalue = getconfig(this, "pressed", 0, 1);
     sendvalue(objthis, pressedvalue, 1, 0);
   })
   .on("vclick vmouseout", function() {
     var btnthis = $(this), objthis = btnthis.data("object"),
     releasevalue = getconfig(this, "released", 0, 1);
     sendvalue(objthis, releasevalue, 1, 0);
  });
 
 // Remove original events and add new press events
 $(".doorbellbutton_byte")
   .off("vclick")
   .on("vmousedown", function() {
     var btnthis = $(this), objthis = btnthis.data("object"),
     pressedvalue = getconfig(this, "pressed", 0, 255);
     sendvalue(objthis, pressedvalue, 5, 0);
   })
   .on("vclick vmouseout", function() {
     var btnthis = $(this), objthis = btnthis.data("object"),
     releasevalue = getconfig(this, "released", 0, 255);
     sendvalue(objthis, releasevalue, 5, 0);
  });
});
------------------------------
Ctrl+F5
Reply


Messages In This Thread
Long click in Custom JS - by buuuudzik - 06.02.2018, 14:13
RE: Long click in Custom JS - by Daniel - 06.02.2018, 15:59
RE: Long click in Custom JS - by buuuudzik - 06.02.2018, 16:11
RE: Long click in Custom JS - by buuuudzik - 06.02.2018, 20:26
RE: Long click in Custom JS - by admin - 08.02.2018, 09:32

Forum Jump: