use of com.servoy.j2db.server.ngclient.property.types.NGConversions.IDesignerDefaultWriter in project servoy-client by Servoy.
the class ChildrenJSONGenerator method writeFormElement.
public static void writeFormElement(JSONWriter writer, IPersist o, Form form, FormElement fe, WebFormUI formUI, ServoyDataConverterContext context, boolean designer) {
writer.key("name");
String name = designer ? fe.getDesignId() : fe.getName();
writer.value(name);
writer.key("type");
if (o instanceof TabPanel) {
// special support for TabPanel so that we have a specific tabpanel,tablesspanel,accordion and splitpane
String type = "servoydefault-tabpanel";
int orient = ((TabPanel) o).getTabOrientation();
if (orient == TabPanel.SPLIT_HORIZONTAL || orient == TabPanel.SPLIT_VERTICAL)
type = "servoydefault-splitpane";
else if (orient == TabPanel.ACCORDION_PANEL)
type = "servoydefault-accordion";
else if (orient == TabPanel.HIDE || (orient == TabPanel.DEFAULT_ORIENTATION && ((TabPanel) o).hasOneTab()))
type = "servoydefault-tablesspanel";
writer.value(ClientService.convertToJSName(type));
} else {
// hack for now to map it to the types that we know are there, so that we can test responsive without really already having to have bootstrap components.
writer.value(ClientService.convertToJSName(FormTemplateGenerator.getComponentTypeName((IFormElement) o)));
}
WebFormComponent webComponent = (formUI != null) ? formUI.getWebComponent(fe.getName()) : null;
AngularFormGenerator.writePosition(writer, o, form, webComponent, designer);
writer.key("model");
writer.object();
if (formUI != null) {
// there is a existing form, take the current properties from that.
if (webComponent != null) {
TypedData<Map<String, Object>> properties = webComponent.getProperties();
TypedData<Map<String, Object>> templateProperties = fe.propertiesForTemplateJSON();
// remove from the templates properties all the properties that are current "live" in the component
templateProperties.content.keySet().removeAll(properties.content.keySet());
DataConversion dataConversion = new DataConversion();
// write the template properties that are left
JSONUtils.writeData(FormElementToJSON.INSTANCE, writer, templateProperties.content, templateProperties.contentType, dataConversion, new FormElementContext(fe));
// write the actual values
webComponent.writeProperties(FullValueToJSONConverter.INSTANCE, null, writer, properties, dataConversion);
JSONUtils.writeClientConversions(writer, dataConversion);
} else {
System.err.println("null");
}
} else {
fe.propertiesAsTemplateJSON(writer, new FormElementContext(fe, context, null), false);
if (designer && Utils.isInheritedFormElement(o, form)) {
writer.key("svyInheritedElement");
writer.value(true);
}
}
if (designer) {
DataConversion dataConversion = new DataConversion();
fe.getWebComponentSpec().getProperties().values().forEach(pd -> {
if (pd.getType() instanceof IDesignerDefaultWriter)
((IDesignerDefaultWriter) pd.getType()).toDesignerDefaultJSONValue(writer, pd.getName(), dataConversion);
});
if (!dataConversion.getConversions().isEmpty()) {
JSONUtils.writeClientConversions(writer, dataConversion);
}
}
if (o instanceof BaseComponent) {
writer.key("servoyAttributes");
writer.object();
Map<String, String> attributes = new HashMap<String, String>(((BaseComponent) fe.getPersistIfAvailable()).getMergedAttributes());
if (designer) {
attributes.put("svy-id", fe.getDesignId());
attributes.put("svy-formelement-type", fe.getTypeName());
attributes.put("svy-name", fe.getName());
attributes.put("svy-anchors", Integer.toString(((BaseComponent) o).getAnchors()));
List<String>[] typeAndPropertyNames = fe.getSvyTypesAndPropertiesNames();
if (typeAndPropertyNames[0].size() > 0) {
attributes.put("svy-types", String.join(",", typeAndPropertyNames[0]));
attributes.put("svy-types-properties", String.join(",", typeAndPropertyNames[1]));
}
attributes.put("svy-priority", form.isResponsiveLayout() ? String.valueOf(((ISupportBounds) o).getLocation().x) : String.valueOf(((BaseComponent) o).getFormIndex()));
}
if (Utils.getAsBoolean(Settings.getInstance().getProperty("servoy.ngclient.testingMode", "false"))) {
String elementName = name;
if (elementName.startsWith("svy_") && o.getUUID() != null) {
elementName = "svy_" + o.getUUID().toString();
}
attributes.put("data-cy", form.getName() + "." + elementName);
}
String directEditPropertyName = getDirectEditProperty(fe);
if (directEditPropertyName != null) {
attributes.put("directEditPropertyName", directEditPropertyName);
}
attributes.forEach((key, value) -> {
writer.key(StringEscapeUtils.escapeEcmaScript(key));
writer.value(value);
});
writer.endObject();
}
writer.endObject();
WebObjectSpecification spec = fe.getWebComponentSpec();
if (spec != null) {
Collection<PropertyDescription> properties = spec.getProperties(FormComponentPropertyType.INSTANCE);
if (properties.size() > 0) {
boolean isResponsive = false;
for (PropertyDescription pd : properties) {
Object propertyValue = fe.getPropertyValue(pd.getName());
Form frm = FormComponentPropertyType.INSTANCE.getForm(propertyValue, context.getSolution());
if (frm == null)
continue;
isResponsive = frm.isResponsiveLayout();
}
// responsive state can change, so send it with updates
writer.key("responsive");
writer.value(isResponsive);
}
}
Collection<String> handlers = fe.getHandlers();
if (handlers.size() > 0) {
writer.key("handlers");
writer.array();
for (String handler : handlers) {
writer.value(handler);
}
writer.endArray();
}
}
Aggregations