Search in sources :

Example 1 with FormWrapper

use of com.servoy.j2db.server.ngclient.template.FormWrapper in project servoy-client by Servoy.

the class AngularFormGenerator method generateJS.

@SuppressWarnings("nls")
@Override
public String generateJS() throws IOException {
    IWebFormController cachedFormController = client != null ? client.getFormManager().getCachedFormController(realFormName) : null;
    FormTemplateObjectWrapper formTemplate = new FormTemplateObjectWrapper(getAContext(), true, false);
    FormWrapper formWrapper = formTemplate.getFormWrapper(form);
    // for this form it is really just some json.
    StringWriter stringWriter = new StringWriter();
    final JSONWriter writer = new JSONWriter(stringWriter);
    writer.object();
    writer.key(realFormName);
    writer.object();
    writer.key("responsive");
    writer.value(form.isResponsiveLayout());
    writer.key("size");
    writer.object();
    writer.key("width");
    writer.value(form.getWidth());
    writer.key("height");
    writer.value(form.getSize().getHeight());
    writer.endObject();
    String styleClasses = form.getStyleClass();
    if (styleClasses != null) {
        writer.key("styleclass");
        String[] classes = styleClasses.split(" ");
        writer.array();
        for (String cls : classes) {
            writer.value(cls);
        }
        writer.endArray();
    }
    writer.key("children");
    // write the default form value object.
    writer.array();
    writer.object();
    writer.key("name");
    writer.value("");
    writer.key("model");
    writer.object();
    Map<String, Object> containerProperties = null;
    if (cachedFormController != null && cachedFormController.getFormUI() instanceof Container) {
        Container con = (Container) cachedFormController.getFormUI();
        DataConversion dataConversion = new DataConversion();
        TypedData<Map<String, Object>> typedProperties = con.getProperties();
        con.writeProperties(FullValueToJSONConverter.INSTANCE, null, writer, typedProperties, dataConversion);
        JSONUtils.writeClientConversions(writer, dataConversion);
        containerProperties = typedProperties.content;
    }
    final Map<String, Object> finalContainerProperties = containerProperties;
    if (formWrapper != null) {
        Map<String, Object> properties = formWrapper.getProperties();
        properties.forEach((key, value) -> {
            if (finalContainerProperties == null || !finalContainerProperties.containsKey(key)) {
                writer.key(key);
                if (value instanceof Integer || value instanceof Long) {
                    writer.value(((Number) value).longValue());
                } else if (value instanceof Float || value instanceof Double) {
                    writer.value(((Number) value).doubleValue());
                } else if (value instanceof Boolean) {
                    writer.value(((Boolean) value).booleanValue());
                } else if (value instanceof Dimension) {
                    writer.object();
                    writer.key("width");
                    writer.value(((Dimension) value).getWidth());
                    writer.key("height");
                    writer.value(((Dimension) value).getHeight());
                    writer.endObject();
                } else {
                    writer.value(value);
                }
            }
        });
    }
    writer.endObject();
    writer.endObject();
    if (form.isResponsiveLayout()) {
        if (zoomedInContainer != null) {
            zoomedInContainer.acceptVisitor(new ChildrenJSONGenerator(writer, getAContext(), form, null, null, form, true, isDesigner), PositionComparator.XY_PERSIST_COMPARATOR);
        } else {
            form.acceptVisitor(new ChildrenJSONGenerator(writer, getAContext(), form, null, null, form, true, isDesigner), PositionComparator.XY_PERSIST_COMPARATOR);
        }
    } else {
        Iterator<Part> it = form.getParts();
        while (it.hasNext()) {
            Part part = it.next();
            if (!Part.rendersOnlyInPrint(part.getPartType())) {
                writer.object();
                writer.key("part");
                writer.value(true);
                writer.key("classes");
                writer.array();
                writer.value("svy-" + PartWrapper.getName(part));
                if (part.getStyleClass() != null) {
                    writer.value(part.getStyleClass());
                }
                writer.endArray();
                writer.key("layout");
                writer.object();
                writer.key("position");
                writer.value("absolute");
                writer.key("left");
                writer.value("0px");
                writer.key("right");
                writer.value("0px");
                int top = form.getPartStartYPos(part.getID());
                if (part.getPartType() <= Part.BODY) {
                    writer.key("top");
                    writer.value(top + "px");
                }
                if (part.getPartType() >= Part.BODY) {
                    writer.key("bottom");
                    writer.value(form.getSize().height - part.getHeight() + "px");
                }
                if (part.getPartType() != Part.BODY) {
                    writer.key("height");
                    writer.value((part.getHeight() - top) + "px");
                }
                if (part.getBackground() != null && !form.getTransparent()) {
                    writer.key("background-color");
                    writer.value(PersistHelper.createColorString(part.getBackground()));
                }
                if (part.getPartType() == Part.BODY) {
                    writer.key("overflow-x");
                    writer.value(AbstractFormLayoutProvider.getCSSScrolling(form.getScrollbars(), true));
                    writer.key("overflow-y");
                    writer.value(AbstractFormLayoutProvider.getCSSScrolling(form.getScrollbars(), false));
                } else {
                    writer.key("overflow");
                    // $NON-NLS-1$
                    writer.value("hidden");
                }
                writer.endObject();
                writer.key("children");
                // write the default form value object.
                writer.array();
                form.acceptVisitor(new ChildrenJSONGenerator(writer, getAContext(), form, null, part, form, true, isDesigner), ChildrenJSONGenerator.FORM_INDEX_WITH_HIERARCHY_COMPARATOR);
                writer.endArray();
                writer.endObject();
            }
        }
    }
    writer.endArray();
    writer.endObject();
    writer.endObject();
    return stringWriter.toString();
}
Also used : JSONWriter(org.json.JSONWriter) FormWrapper(com.servoy.j2db.server.ngclient.template.FormWrapper) Dimension(java.awt.Dimension) FormTemplateObjectWrapper(com.servoy.j2db.server.ngclient.template.FormTemplateObjectWrapper) Point(java.awt.Point) DataConversion(org.sablo.websocket.utils.DataConversion) LayoutContainer(com.servoy.j2db.persistence.LayoutContainer) Container(org.sablo.Container) StringWriter(java.io.StringWriter) Part(com.servoy.j2db.persistence.Part) Map(java.util.Map)

Aggregations

LayoutContainer (com.servoy.j2db.persistence.LayoutContainer)1 Part (com.servoy.j2db.persistence.Part)1 FormTemplateObjectWrapper (com.servoy.j2db.server.ngclient.template.FormTemplateObjectWrapper)1 FormWrapper (com.servoy.j2db.server.ngclient.template.FormWrapper)1 Dimension (java.awt.Dimension)1 Point (java.awt.Point)1 StringWriter (java.io.StringWriter)1 Map (java.util.Map)1 JSONWriter (org.json.JSONWriter)1 Container (org.sablo.Container)1 DataConversion (org.sablo.websocket.utils.DataConversion)1