Widgets.asElement does not wire up events
This is probably going to be tricky to get right, so it might be worth making this into a documentation bug, rather than a fault in the library.
A GWT Widget does not attach its event listeners to the dom until the widget it attached. Being attached means that its root node has a parent node, and so on until a RootPanel instance - so the root element of the widget is actually attached to the page's body.
This solves a memory leak that old browsers had - ancient versions of firefox had this issue, as did most every version of IE until Edge came around (I am certain that IE9 has the bug, IE10 and 11 haven't been tested by me).
This also provides a set of lifecycle methods to Widget which normal Elements do not have (though WebComponents do, in the form of the custom element's connected/disconnected callbacks - this could be one way to implement this in general).
However, when Widgets.asElement is called on a widget, it returns the root element of that widget, and the widget will never be marked as attached, so none of its event listeners will ever be attached to the DOM. If Widgets.asElement were to trigger the attach lifecycle when called, this could break the widget, since its own handlers would not be able to find a parent element, and would not be able to check the element's sizes, etc. That said, this is what gwtquery does in cases like this, where there is no higher widget in the page's structure, or the element this is being attached to is not yet itself connected to the document: https://github.com/gwtquery/gwtquery/blob/master/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/widgets/WidgetsUtils.java#L176-L217
--
Other cases to consider:
- Elements (1)
- Widget (2)
- Elements (3)
- Widget (4)
- Elements (3)
- Widget (2)
In this case, the widget isn't directly attached to another widget, and if the elements at (3) were not connected to the widget at (2) when (4) is added to them, you'll get different behavior than if they were. Likewise, removing (2) from the dom should cause (4) to be told it has detached.
--
Not all widgets are going to care if you get this exactly right, so I think this library needs to describe what level of correctness is offered, and possibly offer some utility methods to enable projects to attain their preferred level of correctness from there.
Thanks @niloc132 for bringing this up. As you pointed out there's no easy fix for this.
I'd like to make it clear in the documentation that Widgets.asElement() does not take care of attaching the widget. I'd like to leave it in the callers responsibility to take care of proper widget initialization. Elemento is mostly about Elemental2 and I added the Widgets class only for backward compatibility reasons.
But first I like to hear what others think about this issue.
I'm closing this since there's no support for Widgets anymore.