Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » ClientScripting restricted DOM navigation(How to access related widgets in ClientScripting)
ClientScripting restricted DOM navigation [message #1159307] Mon, 28 October 2013 12:42 Go to next message
John Gymer is currently offline John GymerFriend
Messages: 279
Registered: November 2012
Location: UK
Senior Member
Currently I use the following JavaScript code in my ClientScripting to highlight an entry field in pale yellow when the validation picks up a problem:

event.widget.setBackground([255, 255, 128]);


This is great... but... I often place the Text control inside a Composite (to do with sizing and vertical alignment, but that's another story!), so what I'd really like to do is to set the background colour of the parent Composite AND the Text within the JavaScript.

I see lots of warnings in the RAP ClientScripting docs to avoid accessing the DOM directly, but is this type of access 'safe'? If so, how can I obtain the Parent Composite for the Text control within JavaScript?

Something like this would be good:

event.widget.setBackground([255, 255, 128]);
event.widget.getParent().setBackground([255, 255, 128]);


Is something like this possible... advisable... sensible...?! If so, what would the JS methods be?

Thanks, John


---
Just because you can doesn't mean you should
Re: ClientScripting restricted DOM navigation [message #1161494 is a reply to message #1159307] Tue, 29 October 2013 21:08 Go to previous messageGo to next message
Milko Krastev is currently offline Milko KrastevFriend
Messages: 17
Registered: July 2013
Junior Member
What you ask is how to manipulate the properties of another widget within client scripting, a.k.a. Cross-Widget Scripting. The way to achieve that is very well described here:



http://eclipsesource.com/blogs/2013/06/07/rap-client-scripting-phase-ii-33/

Quote:

As demonstrated the last time, we can add simple objects like strings to widgets and use them in a ClientListener. While it is not (at least not yet) possible to directly pass other widgets to the listener with this method, you can add their RAP protocol id in the following way:

widget.setData( "myOtherWidget", WidgetUtil.getId( otherWidget ) );

In the ClientListener, you can convert the id to a ClientScripting widget object:

var handleEvent = function( event ) {
var id = event.widget.getData( "myOtherWidget" );
var otherWidget = rap.getObject( id );
//...
}


You can then use otherWidget.setBackground(255, 255, 255).
Re: ClientScripting restricted DOM navigation [message #1162364 is a reply to message #1161494] Wed, 30 October 2013 10:25 Go to previous messageGo to next message
John Gymer is currently offline John GymerFriend
Messages: 279
Registered: November 2012
Location: UK
Senior Member
Thanks Milko - I hadn't spotted that blog - very useful and exactly what I needed!


---
Just because you can doesn't mean you should
Re: ClientScripting restricted DOM navigation [message #1163935 is a reply to message #1162364] Thu, 31 October 2013 10:12 Go to previous message
John Gymer is currently offline John GymerFriend
Messages: 279
Registered: November 2012
Location: UK
Senior Member
I believe I understand how it is supposed to work, but I don't seem to be able to retrieve any of these values in the JavaScript... here's what I'm doing...

In the Java, I output the Ids for both a Comp and a Text widget (the Text sits inside the Composite, but I don't think this is relevant), then use setData so that I can reference "containingCompId" and "myOwnId" attributes from the Text's perspective:
System.out.println("myTextWidgetId: '" + WidgetUtil.getId(myTextWidget) + "' " + "myCompWidgetId: " + WidgetUtil.getId(myCompWidget) + "'");
myTextWidget.setData("containingCompId", WidgetUtil.getId(myCompWidget));
myTextWidget.setData("myOwnId", WidgetUtil.getId(myTextWidget));


The println gives me good 'w99999' style values, so I know I have good Ids within the Java.

Then, I register a ClientScripting listener:
String scriptCode = ResourceLoaderUtil.readTextContent(RESOURCES_PREFIX + "IntegerOnly.js");
myTextWidget.addListener(SWT.Verify, new ClientListener(scriptCode));


The JS for the ClientScripting is simple:
var handleEvent = function( event ) {
  ...do some validation
  if (validateBad) {
  // highlight the field in yellow when bad characters are typed
    event.doit = false;
    var myField = event.widget;
    myField.setBackground([255, 255, 128]);
    var compId = myField.getData("containingCompId");
    var compWidget = rap.getObject(compId);
    if (compWidget != null) {
      compWidget.setBackground([255, 255, 128]);
    }
    var myId = myField.getData("myOwnId");
    alert("compId: '" + compId + "' myId: '" + myId + "'");
  }
}


Neither of the getData calls in the JS ever return anything - always null.
Do I have to register these in some special way in order for them to be accessible in the JS?

Thanks, John


---
Just because you can doesn't mean you should
Previous Topic:Client push
Next Topic:NullPointerException from DecorationScheduler
Goto Forum:
  


Current Time: Fri Mar 29 09:18:05 GMT 2024

Powered by FUDForum. Page generated in 0.04155 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top