Reputation: 3
I have implemented a wicket behavior as shown below:
@Priority(100)
public class MouseOverTextBehavior extends Behavior {
private final String description;
public MouseOverTextBehavior(String description) {
this.description = description;
}
@Override
public void onComponentTag(Component component, ComponentTag tag) {
super.onComponentTag(component, tag);
// Set the title attribute
tag.put("title", description);
// Create a JavaScript function that will be executed when the mouse
// pointer enters the component
String jsFunction = String.format(
"$('<div class=\"description\">%s</div>').appendTo('body');"
+ "var labelRect = $(this)[0].getBoundingClientRect();"
+ "var popupRect = $('.description:last')[0].getBoundingClientRect();"
+ "var popupTop = labelRect.top - popupRect.height - 10;"
+ "var popupLeft = labelRect.left + (labelRect.width - popupRect.width) / 2;"
+ "$('.description:last').css({ top: popupTop, left: popupLeft });",
description != null? description.replace("'", "\\'"): null
);
tag.put("onmouseover", jsFunction);
jsFunction = "$(this).prev('.description').remove();";
tag.put("onmouseout", jsFunction);
// Set the position: relative
tag.put("style", "position: relative;");
}
}
Then i tested it for being added to some Component like: Label I.E:
Button button = new Button("myButton");
button.add(new MouseOverTextBehavior("This is a button"));
add(button);
And it is working fine.
The problem is that behavior is working fine on some cases like the example above, and not working in other cases like the following case:
add(newReferenceCodeLabel("id").add(new MouseOverTextBehavior("Show this message!!")));
Knowing that:
private Label newReferenceCodeLabel(final String id) {
return new Label(id, new AbstractReadOnlyModel<String>() {
private static final long serialVersionUID = 5340631294817017953L;
@Override
public String getObject() {
final AfElement afElement = getAfElement();
String referenceAeCode = afElement.getReferencedAeCode();
if (afElement instanceof AeProductPromotion
&& afElement.getProductCode().equals(referenceAeCode)) {
referenceAeCode = null;
}
return referenceAeCode;
}
});
}
I want to know the reason why this behaves like that, and how to fix it?
Upvotes: 0
Views: 58