Recent

Author Topic: Problem with global events. Pas2JS_Widget  (Read 2941 times)

yus

  • New Member
  • *
  • Posts: 47
Problem with global events. Pas2JS_Widget
« on: May 02, 2021, 08:36:34 pm »
Hello.
I use web component library https://github.com/pascaldragon/Pas2JS_Widget
And I try create global event for my application.

Next way.
Project -> New project -> Web GUI Application (Pas2JS)
For form unit next code.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Js, Classes, SysUtils, Graphics, Controls, Forms, Dialogs, WebCtrls, Web;
  9.  
  10. type
  11.  
  12.   { TWForm1 }
  13.  
  14.   TWForm1 = class(TWForm)
  15.     procedure FormCreate(Sender: TObject);
  16.     procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: NativeInt);
  17.   private
  18.     function MouseDownHandler(aEvent: TJSMouseEvent): boolean;
  19.   public
  20.  
  21.   end;
  22.  
  23. var
  24.   WForm1: TWForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TWForm1 }
  31.  
  32. procedure TWForm1.FormCreate(Sender: TObject);
  33. begin
  34.   Writeln('Form Create');
  35.   document.addEventListener('mousedown', @MouseDownHandler);
  36. end;
  37.  
  38. procedure TWForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: NativeInt);
  39. begin
  40.   writeln('Form MouseDown');
  41. end;
  42.  
  43. function TWForm1.MouseDownHandler(aEvent: TJSMouseEvent): boolean;
  44. begin
  45.   Writeln('Global Mouse Down');
  46. end;
  47.  
  48. end.
  49.  

But the global event doesn't fire.
May be you know why?

Thanks.

PS:
OS: Windows 10
Lazarus: 2.1.0
Date: 2021-04-17
FPC: 3.3.1
SVN Revision: 65006
x86_64-win64-win32/win64

Pas2JS: Release 2.0
« Last Edit: May 02, 2021, 08:55:09 pm by yus »

yus

  • New Member
  • *
  • Posts: 47
Re: Problem with global events. Pas2JS_Widget
« Reply #1 on: May 02, 2021, 10:14:18 pm »
I found reason.
At base class TControl for events set StopPropagation for Events.

Code: Pascal  [Select][+][-]
  1. function TControl.HandleMouseDown(AEvent: TJSMouseEvent): boolean;
  2. ...
  3. begin
  4. ...
  5.   AEvent.StopPropagation;
  6. ...
  7. end;
  8.  

But I still can't figure out how to set the global handler.
« Last Edit: May 02, 2021, 10:17:28 pm by yus »

egsuh

  • Hero Member
  • *****
  • Posts: 864
Re: Problem with global events. Pas2JS_Widget
« Reply #2 on: May 03, 2021, 03:27:50 am »
Is it possible to add eventhandler to document object itself?  I think it should be done to document.body, etc.

yus

  • New Member
  • *
  • Posts: 47
Re: Problem with global events. Pas2JS_Widget
« Reply #3 on: May 03, 2021, 04:08:59 pm »
Is it possible to add eventhandler to document object itself?  I think it should be done to document.body, etc.
Yes, it is possible, but as I wrote above, in the event handler of the components on the form, there is a call to AEvent.StopPropagation that stops the transmission of messages.
The global document handler does not receive these messages.

 

TinyPortal © 2005-2018