ClientScripting restricted DOM navigation [message #1159307] |
Mon, 28 October 2013 12:42 |
|
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 #1163935 is a reply to message #1162364] |
Thu, 31 October 2013 10:12 |
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.04155 seconds