Home » Eclipse Projects » Remote Application Platform (RAP) » keybindings and mouse events with a browser widget
| | | | | | | |
Re: keybindings and mouse events with a browser widget [message #1235107 is a reply to message #1234996] |
Thu, 23 January 2014 15:32 |
Tim Buschtoens Messages: 396 Registered: July 2009 |
Senior Member |
|
|
In modern browser you can do it like this:
var event = document.createEvent( "MouseEvent" );
event.initMouseEvent( type,
true, // bubbles
true, //cancelable
window, //view
0, // detail
coordiantes[ 0 ], //screenX
coordiantes[ 1 ], //screenY
coordiantes[ 0 ], //clientX
coordiantes[ 1 ], //clientY
false, //ctrlKey
false, //altKey
false, //shiftKey
false, //metaKey
button, //0 = left
null );
target.dispatchEvent( event );
Am 23.01.2014 10:58, schrieb Martin Domig:
> Im trying to do this, but don't know how to dispatch a mouse click event
> in javascript to the parent frame. Can you give me a hint as how to
> accomplish this? How can I send a mouse event?
--
Tim Buschtöns
Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/
Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
|
|
|
Re: keybindings and mouse events with a browser widget [message #1235480 is a reply to message #1235107] |
Fri, 24 January 2014 13:26 |
Martin Domig Messages: 12 Registered: August 2010 |
Junior Member |
|
|
I had some progress, but still some problems. dispatchEvent seems to do something to the focus, but does not actually send a mouse event.
I have this in the HTML containing my custom browser based widget:
function dispatchToOuter(event) {
// this code is running inside an iframe of the browser based widget.
// window.parent.document is the container of that iframe, the
// document that contains the browser showing our custom widget.
// we here try to dispatch mouse events to that browser, so that we
// can "see" them server side in java, as normal SWT mouse events.
console.log('dispatching ' + event.type);
// In java, the custom browser based widget sends it's ID
// (WidgetUtil.getId(this)) to us so that we can find our browser
// element within the parent document.
var entry = window.parent.rwt.remote.ObjectRegistry.getEntry(widgetId);
// entry is a wrapper, but we can get to the wrapped object
// and to the div containing our browser:
var target = entry.object._element.children[0];
// target now is the div that contains the iframe of our custom widget.
var mouseEvent = document.createEvent("MouseEvent");
mouseEvent.initMouseEvent(event.type, true, true, target, event.detail,
event.screenX, event.screenY, event.clientX, event.clientY,
event.ctrlKey, event.altKey, event.shiftKey, event.metaKey,
event.which, null);
target.dispatchEvent(mouseEvent);
}
document.addEventListener('mousedown', function(event) {
console.log('inner mouseDown');
dispatchToOuter(event);
}, true);
Now, what happens is this:
1. The click is seen by the custom widget, "inner mouseDown" is printed to the console and the event is dispatched to the target.
2. The next click is seen server side as a normal SWT mouse event, but ONLY by the server and NOT by the inner widget.
3. Goto 1
When i set the target as follows:
var target = entry.object._element; // don't use the first child (no .children[0])
I see the same behaviour, but in addition the SWT browser of my widget gets focus events:
1. First click is seen by the custom widget, "inner mouseDown" is printed to the console and the event is dispatched to the target.
2. At the next click, the SWT browser gains focus and then sees the mouse event, but no "inner mouseDown".
3. At the next click, the browser looses focus, and the "inner mouseDown" is back again.
4. Goto 2
I am thoroughly dazed and confused, and now know more about javascript than i ever cared to know (which probably is in no small part the cause of my confusion). But I seem to be able to do something that makes the SAME mouse click visible to the custom widget AND the server side SWT at the same time. I think it's very close to the solution, but I just can't see it.
Help? Please?
|
|
|
Re: keybindings and mouse events with a browser widget [message #1236481 is a reply to message #1235480] |
Mon, 27 January 2014 10:18 |
Tim Buschtoens Messages: 396 Registered: July 2009 |
Senior Member |
|
|
Oh boy, it's also more complicated than I would have envisioned.
Okay, first of all: I'm not really comfortable with using internals. I
found a solution on stackoverflow to get the parent iframe element that
works on DOM level only:
http://stackoverflow.com/questions/935127/how-to-access-parent-iframe-from-javascript
var arrFrames = parent.document.getElementsByTagName("IFRAME");
for (var i = 0; i < arrFrames.length; i++) {
if (arrFrames[i].contentWindow === window) alert("yay!");
}
That would be the iframe itself (.children[0]), the parent is the div
you want.
It's also advisable (though probably not required) to forward not just
mousedown but also mouseup. Otherwise the framwork might get confused
about the missing event.
To me it sounds like dispatching mousedown on the parent frame takes
focus away from the iframes document, and the next click gives it back,
but is seen as a mouse event on the outer frame, not the inner. If
that's it then the solution could be to take back the focus immediately
after sending the fake mousedown. It's only a question on which element
to set the focus, either the iframe element itself or on the iframes
body (i.e. document.body.focus()) or both.
I hope this works, otherwise the solutions I could come up with would
get even more hacky.
I know it's a lot of stuff, but if we manage to get it working the
solution can be put into a small js file to be used by anyone having the
problem.
Greetings,
Tim
Am 24.01.2014 14:26, schrieb Martin Domig:
> I had some progress, but still some problems. dispatchEvent seems to do
> something to the focus, but does not actually send a mouse event.
>
> I have this in the HTML containing my custom browser based widget:
>
> function dispatchToOuter(event) {
> // this code is running inside an iframe of the browser based
> widget.
> // window.parent.document is the container of that iframe, the
> // document that contains the browser showing our custom widget.
> // we here try to dispatch mouse events to that browser, so
> that we
> // can "see" them server side in java, as normal SWT mouse events.
>
> console.log('dispatching ' + event.type);
>
> // In java, the custom browser based widget sends it's ID
> // (WidgetUtil.getId(this)) to us so that we can find our browser
> // element within the parent document.
>
> var entry =
> window.parent.rwt.remote.ObjectRegistry.getEntry(widgetId);
>
> // entry is a wrapper, but we can get to the wrapped object
> // and to the div containing our browser:
>
> var target = entry.object._element.children[0];
>
> // target now is the div that contains the iframe of our custom
> widget.
>
> var mouseEvent = document.createEvent("MouseEvent");
> mouseEvent.initMouseEvent(event.type, true, true, target,
> event.detail,
> event.screenX, event.screenY, event.clientX,
> event.clientY,
> event.ctrlKey, event.altKey, event.shiftKey,
> event.metaKey,
> event.which, null);
>
> target.dispatchEvent(mouseEvent);
> }
>
> document.addEventListener('mousedown', function(event) {
> console.log('inner mouseDown');
> dispatchToOuter(event);
> }, true);
>
>
> Now, what happens is this:
>
> 1. The click is seen by the custom widget, "inner mouseDown" is printed
> to the console and the event is dispatched to the target.
> 2. The next click is seen server side as a normal SWT mouse event, but
> ONLY by the server and NOT by the inner widget.
> 3. Goto 1
>
> When i set the target as follows:
>
>
> var target = entry.object._element; // don't use the first child (no
> .children[0])
>
>
> I see the same behaviour, but in addition the SWT browser of my widget
> gets focus events:
>
> 1. First click is seen by the custom widget, "inner mouseDown" is
> printed to the console and the event is dispatched to the target.
> 2. At the next click, the SWT browser gains focus and then sees the
> mouse event, but no "inner mouseDown".
> 3. At the next click, the browser looses focus, and the "inner
> mouseDown" is back again.
> 4. Goto 2
>
> I am thoroughly dazed and confused, and now know more about javascript
> than i ever cared to know (which probably is in no small part the cause
> of my confusion). But I seem to be able to do something that makes the
> SAME mouse click visible to the custom widget AND the server side SWT at
> the same time. I think it's very close to the solution, but I just can't
> see it.
>
> Help? Please?
--
Tim Buschtöns
Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/
Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
|
|
| |
Goto Forum:
Current Time: Fri Apr 19 10:13:38 GMT 2024
Powered by FUDForum. Page generated in 0.03150 seconds
|