Search in sources :

Example 21 with FormElement

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

the class FormComponentPropertyType method toRhinoValue.

@Override
public Object toRhinoValue(Object webComponentValue, PropertyDescription pd, IWebObjectContext componentOrService, Scriptable startScriptable) {
    Scriptable newObject = DefaultScope.newObject(startScriptable);
    WebFormComponent webFormComponent = (WebFormComponent) componentOrService;
    FlattenedSolution fs = webFormComponent.getDataConverterContext().getSolution();
    FormComponentCache cache = null;
    if (webComponentValue instanceof FormComponentSabloValue) {
        cache = ((FormComponentSabloValue) webComponentValue).getCache();
    } else {
        Form form = getForm(webComponentValue, fs);
        if (form == null)
            return null;
        // TODO return here a NativeScriptable object that understand the full hiearchy?
        cache = FormElementHelper.INSTANCE.getFormComponentCache(webFormComponent.getFormElement(), pd, (JSONObject) webComponentValue, form, fs);
    }
    IWebFormUI formUI = webFormComponent.findParent(IWebFormUI.class);
    String prefix = FormElementHelper.getStartElementName(webFormComponent.getFormElement(), pd);
    for (FormElement fe : cache.getFormComponentElements()) {
        String name = fe.getName();
        if (name != null && !name.startsWith(FormElement.SVY_NAME_PREFIX)) {
            RuntimeWebComponent webComponent = formUI.getRuntimeWebComponent(fe.getRawName());
            if (webComponent != null) {
                newObject.put(name.substring(prefix.length()), newObject, webComponent);
            }
        }
    }
    return newObject;
}
Also used : IWebFormUI(com.servoy.j2db.server.ngclient.IWebFormUI) FormComponentCache(com.servoy.j2db.server.ngclient.FormElementHelper.FormComponentCache) JSONObject(org.json.JSONObject) Form(com.servoy.j2db.persistence.Form) JSForm(com.servoy.j2db.scripting.solutionmodel.JSForm) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Scriptable(org.mozilla.javascript.Scriptable) RuntimeWebComponent(com.servoy.j2db.server.ngclient.component.RuntimeWebComponent) FormElement(com.servoy.j2db.server.ngclient.FormElement) INGFormElement(com.servoy.j2db.server.ngclient.INGFormElement) IFormElement(com.servoy.j2db.persistence.IFormElement)

Example 22 with FormElement

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

the class JSEventType method fillJSEvent.

/**
 * @param event Event that needs to be filled
 * @param jsonObject The json data for that has the event data.
 * @param webObject The webObject element (WEbFormComponent or WebFormUI)
 * @param controller Optional the controller object if the caller knows this already
 * @return
 */
@SuppressWarnings("nls")
public static void fillJSEvent(JSBaseEvent event, JSONObject jsonObject, BaseWebObject webObject, IWebFormController controller) {
    event.setType(jsonObject.optString("eventType"));
    String formName = controller != null ? controller.getName() : "";
    String elementName = "";
    if (webObject instanceof WebFormComponent) {
        elementName = ((WebFormComponent) webObject).getFormElement().getRawName();
        if (elementName == null)
            elementName = "";
        if (formName.isEmpty()) {
            BaseWebObject parentWebObject = ((WebFormComponent) webObject).getParent();
            while (parentWebObject != null && !(parentWebObject instanceof WebFormUI)) {
                parentWebObject = ((WebFormComponent) parentWebObject).getParent();
            }
            if (parentWebObject instanceof WebFormUI) {
                formName = parentWebObject.getName();
            }
        }
    }
    if (formName.isEmpty()) {
        formName = jsonObject.optString("formName");
    }
    if (formName.isEmpty() && webObject instanceof WebFormUI) {
        // executeInlineScript with an event in params tells a DAL to execute it and it gives a formui as context to the fromJSON conversion
        // for JSEventType problem is that it can give any formName from the client (the component/service can give anything there as an arg); or, if the function
        // (sent to client before through "function" type) that will be used to execute the script is a global/scope function
        // then formName can be null and in that case the WebFormUI will be the main form or the window (not the most nested one)
        // so this is just a fallback and we do give priority to the form name determined on client through $window.createJSEvent(...) an put into
        // "jsonObject" (see if above) - to target the correct form - closest one to event
        formName = webObject.getName();
    }
    if (elementName.isEmpty()) {
        elementName = jsonObject.optString("elementName");
    }
    if (!formName.isEmpty())
        event.setFormName(formName);
    if (!elementName.isEmpty())
        event.setElementName(elementName);
    if (!formName.isEmpty()) {
        INGApplication application = ((IContextProvider) webObject).getDataConverterContext().getApplication();
        IWebFormController formController = controller != null ? controller : application.getFormManager().getForm(formName);
        if (formController != null) {
            FormScope formScope = formController.getFormScope();
            if (formScope != null) {
                ElementScope elementsScope = (ElementScope) formScope.get("elements", null);
                if (elementsScope != null) {
                    Object scriptableElement = !elementName.isEmpty() ? elementsScope.get(elementName, null) : null;
                    if (scriptableElement != null && scriptableElement != Scriptable.NOT_FOUND) {
                        event.setSource(scriptableElement);
                    } else if (webObject instanceof WebFormComponent) {
                        // quickly create a scriptable wrapper around the component so that the source can be set to a value that we expect.
                        FormElement fe = ((WebFormComponent) webObject).getFormElement();
                        RuntimeWebComponent runtimeComponent = new RuntimeWebComponent((WebFormComponent) webObject, webObject.getSpecification());
                        if (fe.isLegacy() || ((fe.getForm().getView() == IForm.LIST_VIEW || fe.getForm().getView() == FormController.LOCKED_LIST_VIEW || fe.getForm().getView() == FormController.TABLE_VIEW || fe.getForm().getView() == FormController.LOCKED_TABLE_VIEW) && fe.getTypeName().startsWith("svy-"))) {
                            // add legacy behavior
                            runtimeComponent.setPrototype(new RuntimeLegacyComponent((WebFormComponent) webObject));
                        }
                        event.setSource(runtimeComponent);
                    }
                }
            }
        }
    }
    try {
        if (jsonObject.has("x"))
            event.setLocation(new Point(jsonObject.optInt("x"), jsonObject.optInt("y")));
        if (jsonObject.has("modifiers"))
            event.setModifiers(jsonObject.optInt("modifiers"));
        if (jsonObject.has("data"))
            event.setData(jsonObject.opt("data"));
        if (jsonObject.has("timestamp"))
            event.setTimestamp(new Timestamp(jsonObject.getLong("timestamp")));
        else
            event.setTimestamp(new Date());
    } catch (Exception e) {
        Debug.error("error setting event properties from " + jsonObject + ", for component: " + elementName + " on form " + formName, e);
    }
}
Also used : WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) ElementScope(com.servoy.j2db.scripting.ElementScope) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) Point(java.awt.Point) RuntimeWebComponent(com.servoy.j2db.server.ngclient.component.RuntimeWebComponent) Timestamp(java.sql.Timestamp) FormElement(com.servoy.j2db.server.ngclient.FormElement) Date(java.util.Date) JSONException(org.json.JSONException) BaseWebObject(org.sablo.BaseWebObject) FormScope(com.servoy.j2db.scripting.FormScope) WebFormUI(com.servoy.j2db.server.ngclient.WebFormUI) INGApplication(com.servoy.j2db.server.ngclient.INGApplication) JSONObject(org.json.JSONObject) BaseWebObject(org.sablo.BaseWebObject) RuntimeLegacyComponent(com.servoy.j2db.server.ngclient.component.RuntimeLegacyComponent)

Aggregations

FormElement (com.servoy.j2db.server.ngclient.FormElement)22 IFormElement (com.servoy.j2db.persistence.IFormElement)12 Form (com.servoy.j2db.persistence.Form)11 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)10 HashMap (java.util.HashMap)10 JSONObject (org.json.JSONObject)9 ServoyDataConverterContext (com.servoy.j2db.server.ngclient.ServoyDataConverterContext)6 IDataAdapterList (com.servoy.j2db.server.ngclient.IDataAdapterList)5 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)5 Dimension (java.awt.Dimension)5 Map (java.util.Map)5 Test (org.junit.Test)5 DataAdapterList (com.servoy.j2db.server.ngclient.DataAdapterList)4 FormComponentCache (com.servoy.j2db.server.ngclient.FormElementHelper.FormComponentCache)4 INGFormElement (com.servoy.j2db.server.ngclient.INGFormElement)4 IWebFormUI (com.servoy.j2db.server.ngclient.IWebFormUI)4 WebFormUI (com.servoy.j2db.server.ngclient.WebFormUI)4 CustomValueList (com.servoy.j2db.dataprocessing.CustomValueList)3 IValueList (com.servoy.j2db.dataprocessing.IValueList)3 IPersist (com.servoy.j2db.persistence.IPersist)3