I post my second answer as now I know that you can't change the way how DIVs are added to the parent.
After some searching I found Mutation events. They allow you to listen to DOMNodeInserted
event:
JS:
myDiv.addEventListener("DOMNodeInserted", function (ev) {
alert('added');
}, false);
In GWT you need to use JSNI method:
private native void addListener(Element elem) /*-{
elem.addEventListener("DOMNodeInserted", function (ev) {
$wnd.alert('added');
}, false);
}-*/;
It works, but... it's deprecated. You should use MutationObserver instead.
Unfortunately MutationObserver
don't have NodeInserted event to be observed. I thought that subtree
mutation observation would do the job but it didn't work for me. The solution is to observe childList
mutation on the parent:
JS:
// create an observer instance
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
alert(mutation.type);
});
});
// configuration of the observer:
var config = {
childList: true
};
// pass in the target node, as well as the observer options
observer.observe(elem, config);
And, again, for GWT you need to wrap it in JSNI method (you know how).
The mutation
parameter in observer callback is a MutationRecord object.
So, if you can get the parent, use MutationObserver on it and observe childList
mutation. If not, try to use the deprecated Mutation events.
I know that this is not pure GWT solution, but you can use GWT methods to handle event or mutation. You just need to call GWT method form JSNI like this:
[instance-expr.]@class-name::method-name(param-signature)(arguments)
You'll find all JSNI info in the GWT documentation.