Logic Machine Forum
Set a value when a widget come up - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Set a value when a widget come up (/showthread.php?tid=2532)



Set a value when a widget come up - gdimaria - 21.03.2020

Hi, as in the Thread subjet I need to set an object's value (unsigned Integer) when a widget pops up.

Can you help me?

Thanks

Peppe


RE: Set a value when a widget come up - admin - 22.03.2020

You can use this example. Set Additional classes to widget-open for the element that opens the widget. #widget-2 is widget element id, change it and group address as needed.
Keep in mind that it won't work when transition animation is enabled.
Code:
$(function() {
  $('.widget-open').on('vclick', function() {
    var el = $('#widget-2');
    if (el.is(':visible')) {
      grp.write('1/1/1', 123);
    }
  });
});

There will be widget show/hide events added in RC2 firmware that will work correctly with animation:
Code:
$(function() {
  $('#widget-2').on('widget-show', function() {
    grp.write('1/1/1', true);
  });
  
  $('#widget-2').on('widget-hide', function() {
    grp.write('1/1/1', false);
  });
});



RE: Set a value when a widget come up - gdimaria - 22.03.2020

(22.03.2020, 14:34)admin Wrote: You can use this example. Set Additional classes to widget-open for the element that opens the widget. #widget-2 is widget element id, change it and group address as needed.
Keep in mind that it won't work when transition animation is enabled.
Code:
$(function() {
  $('.widget-open').on('vclick', function() {
    var el = $('#widget-2');
    if (el.is(':visible')) {
      grp.write('1/1/1', 123);
    }
  });
});

There will be widget show/hide events added in RC2 firmware that will work correctly with animation:
Code:
$(function() {
  $('#widget-2').on('widget-show', function() {
    grp.write('1/1/1', true);
  });
 
  $('#widget-2').on('widget-hide', function() {
    grp.write('1/1/1', false);
  });
});


Ok thanks but... how I can get the actual WIDGET ELEMENT ID?


RE: Set a value when a widget come up - admin - 23.03.2020

Open visualization in your browser, open the widget, right click in any empty space inside your widget and select "inspect element". In HTML inspector you will see something like <div id="widget-2"... widget-2 is your widget ID. Make sure that selector in Custom JS has # in the selector before the ID: $('#widget-2');


RE: Set a value when a widget come up - gdimaria - 23.03.2020

Ok! Everything is working... thanks!

Peppe


RE: Set a value when a widget come up - gdimaria - 23.03.2020

Just to make it perfect: How I can make a widget pop when when some grp address is triggered?

like you made for a plan with the code  below:


$(function(){

  if (typeof objectStore !== 'undefined') {

    var id = Scada.encodeGroupAddress('1/1/2');



    objectStore.addListener(id, function(object, type) {

      if (type == 'value') {

        showPlan(69);

      }

    });

  }

});





Tnx


RE: Set a value when a widget come up - Daniel - 23.03.2020

Have you seen this?
https://forum.logicmachine.net/showthread.php?tid=195&pid=1934#pid1934


RE: Set a value when a widget come up - gdimaria - 23.03.2020

(23.03.2020, 11:50)Daniel. Wrote: Have you seen this?
https://forum.logicmachine.net/showthread.php?tid=195&pid=1934#pid1934
yes... it seems it doesn't work   Sad


RE: Set a value when a widget come up - admin - 23.03.2020

There's an easier way to show a widget by simulating a click on the element that opens it. Make sure that your sending script is not attached to the same element otherwise you will get a loop.

Here, when 1/1/1 value is true view is switched to plan 12 and if #widget-2 is not yet shown a click event is triggered on an element with class show-widget
Code:
$(function() {
  if (typeof grp == 'object') {
    grp.listen('1/1/1', function(obj, state) {
      if (state == 'value' && obj.value) {
        showPlan(12);

        if (!$('#widget-2').is(':visible')) {
          $('.show-widget').click();
        }
      }
    });
  }
});



RE: Set a value when a widget come up - gdimaria - 28.04.2020

(22.03.2020, 14:34)admin Wrote: You can use this example. Set Additional classes to widget-open for the element that opens the widget. #widget-2 is widget element id, change it and group address as needed.
Keep in mind that it won't work when transition animation is enabled.
Code:
$(function() {
  $('.widget-open').on('vclick', function() {
    var el = $('#widget-2');
    if (el.is(':visible')) {
      grp.write('1/1/1', 123);
    }
  });
});

There will be widget show/hide events added in RC2 firmware that will work correctly with animation:
Code:
$(function() {
  $('#widget-2').on('widget-show', function() {
    grp.write('1/1/1', true);
  });
 
  $('#widget-2').on('widget-hide', function() {
    grp.write('1/1/1', false);
  });
});

(22.03.2020, 16:40)gdimaria Wrote:
(22.03.2020, 14:34)admin Wrote: You can use this example. Set Additional classes to widget-open for the element that opens the widget. #widget-2 is widget element id, change it and group address as needed.
Keep in mind that it won't work when transition animation is enabled.
Code:
$(function() {
  $('.widget-open').on('vclick', function() {
    var el = $('#widget-2');
    if (el.is(':visible')) {
      grp.write('1/1/1', 123);
    }
  });
});

There will be widget show/hide events added in RC2 firmware that will work correctly with animation:
Code:
$(function() {
  $('#widget-2').on('widget-show', function() {
    grp.write('1/1/1', true);
  });
 
  $('#widget-2').on('widget-hide', function() {
    grp.write('1/1/1', false);
  });
});


Ok thanks but... how I can get the actual WIDGET ELEMENT ID?


when I set a pincode to the button object with the class "#widget-x'" javascript doesn't work!

Is there a solution?

Thanks




RE: Set a value when a widget come up - admin - 30.04.2020

This will not work with a pin-code. You'll have to wait for RC2 to use widget events.