communication from javascript (executed inside Browser widget) to RAP [message #69267] |
Wed, 09 January 2008 02:17 |
Eclipse User |
|
|
|
Originally posted by: karlpfeil.yahoo.com
Dear RAP developers and users,
First let me congratulate for the fascinating RAP project. I appreciate
the idea to deploy any RCP over the web after applying minor changes.
I found that the Browser widget gives me quite some freedom to easily
implement arbitrary views including images, input fields, and -- if
nothing helps -- even applets.
But, what I still need is the possibility to communicate between
javascript executed inside the Browser widget and some class in my RAP
application (possibly an extended Browser widget subclassing the rwt
Browser). For an illustration let's assume that the contained javascript
repeats some function call with the help of setInterval("foo()",500). How
can this javascript function foo(), which is invoked periodically, give
any feedback to the rest of the RAP application?
Karl
|
|
|
Re: communication from javascript (executed inside Browser widget) to RAP [message #69369 is a reply to message #69267] |
Wed, 09 January 2008 14:47 |
Benoit Mercier (mercibe) Messages: 11 Registered: July 2009 |
Junior Member |
|
|
Hi Karl,
We achieved to do what you want. The idea is to add some custom
javascript code to the HTML displayed in the Browser widget in order to
communicate with the RAP application.
In our case, our goal was to trigger a modifyText event on a custom
widget when a link (HTML anchor <a>) was clicked in the Browser widget.
We associated the following custom javascript function "goToArticle"
to the <a> onClick event.
<script type="text/javascript" language="javascript">
function goToArticle(articleName)
{
var widgetId = "widgetIdRef"; //replace with widgetId (from RAP 1.1M1
you can use custom widget id's)
// get the widget manager
var widgetManager = parent.org.eclipse.swt.WidgetManager.getInstance();
// look for the javascript widget object
var widget = widgetManager.findWidgetById(widgetId);
// Change the value (content of our Text widget)
widget.setValue(articleName);
// get the request to be sent to the server
var req = parent.org.eclipse.swt.Request.getInstance();
// add the event we want to trigger to the request
req.addEvent("org.eclipse.swt.events.modifyText", widgetId );
// add parameter for the modifyText event
req.addParameter(widgetId + ".text", widget.getValue());
// send the request to the server (the RAP application!)
req.send();
}
</script>
Before RAP 1.1M1 you had to get the widget ID via:
((IWidgetAdapter) myWidget.getAdapter(IWidgetAdapter.class)).getId();
... and dynamically replace a placeholder string in your Jacascript code
with the id.
Probably not the most elegant way of doing things, but it works
perfectly well...
Hope this will help!
Best regards,
Benoit
Karl Pfeil a écrit :
> Dear RAP developers and users,
> First let me congratulate for the fascinating RAP project. I appreciate
> the idea to deploy any RCP over the web after applying minor changes.
>
> I found that the Browser widget gives me quite some freedom to easily
> implement arbitrary views including images, input fields, and -- if
> nothing helps -- even applets.
>
> But, what I still need is the possibility to communicate between
> javascript executed inside the Browser widget and some class in my RAP
> application (possibly an extended Browser widget subclassing the rwt
> Browser). For an illustration let's assume that the contained javascript
> repeats some function call with the help of setInterval("foo()",500).
> How can this javascript function foo(), which is invoked periodically,
> give any feedback to the rest of the RAP application?
>
> Karl
>
|
|
|
Re: communication from javascript (executed inside Browser widget) to RAP [message #69619 is a reply to message #69369] |
Wed, 09 January 2008 23:42 |
Eclipse User |
|
|
|
Originally posted by: karlpfeil.yahoo.com
Thank you, Benoit.
Your suggestion looks like what I need. I'll try to get it running within
the next days. You don't have some example code available for download by
chance, do you? I am still much of a newbie and struggling with thousands
of small problems if I don't have examples to compare with.
Karl
|
|
|
Re: communication from javascript (executed inside Browser widget) to RAP [message #69815 is a reply to message #69369] |
Fri, 11 January 2008 12:30 |
Eclipse User |
|
|
|
Originally posted by: karlpfeil.yahoo.com
Dear Benoit,
I tried to use your code. You suggest to put it right into an HTML file
that is loaded into the Browser widget, correct? Not to extend the
javascript code which renders the Browser widget?
I run into the first problem at this line:
// get the widget manager
var widgetManager = parent.org.eclipse.swt.WidgetManager.getInstance();
The javascript console shows:
Error: uncaught exception: Permission denied to get property Window.org
I found that this can be caused by cross hosting security restrictions.
But both the RAP application (opened as "http://127.0.0.1:57420/rap") and
the file containing the javascript (http://127.0.0.1/~kpfeil/test.html)
which is opened in the Browser widget, reside on 127.0.0.1. So what is
wrong?
Can you give any more hints?
Regards,
Karl
|
|
|
Re: communication from javascript (executed inside Browser widget) to RAP [message #69874 is a reply to message #69815] |
Fri, 11 January 2008 15:21 |
Benoit Mercier (mercibe) Messages: 11 Registered: July 2009 |
Junior Member |
|
|
Hi Karl,
127.0.0.1 and 127.0.0.1:57420 are not the same "host" and so cross
hosting security restriction applies.
To avoid such problem you could use the setText() method of the Browser
widget instead of the setUrl(). You can read or generate your HTML the
way you like and then inject it via setText() later. You could even get
HTML from anywhere and dynamically add your custom Javascript bloc at
the end, for example:
public void createPartControl(final Composite parent)
{
browser = new Browser(parent, SWT.NONE);
// Get the javascript code to inject
String yourCustomJs= getYourCustomJs();
// Injection/replacement of widget ID's in the javascript code
(the old way of doing things - before rap 1.1 M1)
yourCustomJs= yourCustomJs.replaceFirst("widgetIdRef",
yourTextWidgetId);
// set HTML content in the Browser + custom Javascript
browser.setText(yourHTMLSourceFromSomewhere + yourCustomJs);
}
Regards,
Benoit (mercibe)
Karl Pfeil a écrit :
> Dear Benoit,
> I tried to use your code. You suggest to put it right into an HTML file
> that is loaded into the Browser widget, correct? Not to extend the
> javascript code which renders the Browser widget?
>
> I run into the first problem at this line:
> // get the widget manager
> var widgetManager = parent.org.eclipse.swt.WidgetManager.getInstance();
>
> The javascript console shows:
> Error: uncaught exception: Permission denied to get property Window.org
>
> I found that this can be caused by cross hosting security restrictions.
> But both the RAP application (opened as "http://127.0.0.1:57420/rap")
> and the file containing the javascript
> (http://127.0.0.1/~kpfeil/test.html) which is opened in the Browser
> widget, reside on 127.0.0.1. So what is wrong?
>
> Can you give any more hints?
>
> Regards,
> Karl
>
|
|
|
Re: communication from javascript (executed inside Browser widget) to RAP [message #69933 is a reply to message #69874] |
Sat, 12 January 2008 11:34 |
Eclipse User |
|
|
|
Originally posted by: karlpfeil.yahoo.com
Thank you so much, Benoit! Your hint that accessing 127.0.0.1 on different
ports is interpreted as coming from different hosts and therefore
prohibited helped me to solve that part.
Now I would need further assistance. Firstly I am not sure what to put as
widgetIdRef in the line
>var widgetId = "widgetIdRef"; //replace with widgetId (from RAP 1.1M1 you can
>use custom widget id's)
What I tried is to use the ID of the Browser widget itself. To be more
specific, this widget is registered in plugin.xml with
id="org.eclipse.rap.demo.DemoBrowserViewPart", and this I set as widgetId.
When I fetch the widget for this ID with
>// look for the javascript widget object
>var widget = widgetManager.findWidgetById(widgetId);
and display ("alert(widget)") the widget, it is undefined.
If I set "var widget=this" immediately instead of setting the widgetId and
then findwidgetbyId, to have a defined widget, another problem occurs. I
have replaced your line
>widget.setValue(articleName);
with
>widget.setText('I have been changed');
to have same feedback, I get the error: "widget.setText is not a function."
As you have probably noticed, I am very unexperienced with Javascript and
RAP, although I'd say that I have a good knowledge of Java. I hope that
you guide me through a few more problems, until I get this thing running
and hopefully I also understand what is happening behind the scenes.
Thanks
Karl
|
|
|
|
Powered by
FUDForum. Page generated in 0.04471 seconds