use of org.apache.tapestry5.annotations.PublishEvent in project tapestry-5 by apache.
the class PublishServerSideEvents method addEventsAttribute.
private void addEventsAttribute(final Element element) {
if (element == null) {
throw new IllegalStateException("@PublishEvent used inside a page which didn't generate a <body> element");
}
final ComponentResources containerResources = resources.getContainerResources();
final ComponentModel componentModel = containerResources.getComponentModel();
final String metaValue = componentModel.getMeta(InternalConstants.PUBLISH_COMPONENT_EVENTS_META);
final JSONArray componentEvents = new JSONArray(metaValue);
final JSONObject events = new JSONObject();
final String existingValue = element.getAttribute(COMPONENT_EVENTS_ATTRIBUTE_NAME);
if (existingValue != null) {
final JSONObject existing = new JSONObject(existingValue);
for (String key : existing.keys()) {
events.put(key, existing.get(key));
}
}
for (int i = 0; i < componentEvents.length(); i++) {
final String eventName = componentEvents.getString(i);
JSONObject event = new JSONObject();
event.put(PUBLISH_COMPONENT_EVENTS_URL_PROPERTY, containerResources.createEventLink(eventName).toString());
events.put(eventName, event);
}
element.forceAttributes(TapestryConstants.COMPONENT_EVENTS_ATTRIBUTE_NAME, events.toString());
}
Aggregations