Search in sources :

Example 1 with WebComponentFunction

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

the class RuntimeWebComponent method get.

@Override
public Object get(final String name, final Scriptable start) {
    if (specProperties != null && specProperties.contains(name)) {
        PropertyDescription pd = webComponentSpec.getProperties().get(name);
        if (WebFormComponent.isDesignOnlyProperty(pd))
            return Scriptable.NOT_FOUND;
        return NGConversions.INSTANCE.convertSabloComponentToRhinoValue(component.getProperty(name), pd, component, start);
    }
    if ("getFormName".equals(name)) {
        return new Callable() {

            @Override
            public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                IWebFormUI parent = component.findParent(IWebFormUI.class);
                if (parent != null) {
                    return parent.getController().getName();
                }
                return null;
            }
        };
    }
    if ("getDesignTimeProperty".equals(name) && component.getFormElement().getPersistIfAvailable() instanceof AbstractBase) {
        return new Callable() {

            @Override
            public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                return Utils.parseJSExpression(((AbstractBase) component.getFormElement().getPersistIfAvailable()).getCustomDesignTimeProperty((String) args[0]));
            }
        };
    }
    if ("getDesignProperties".equals(name) && component.getFormElement().getPersistIfAvailable() instanceof AbstractBase) {
        return new Callable() {

            @Override
            public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                Map<String, Object> designProperties = ((AbstractBase) component.getFormElement().getPersistIfAvailable()).getMergedCustomDesignTimeProperties();
                Map<String, Object> parsedMap = new HashMap<String, Object>();
                designProperties.entrySet().forEach(entry -> {
                    parsedMap.put(entry.getKey(), Utils.parseJSExpression(entry.getValue()));
                });
                return parsedMap;
            }
        };
    }
    final Function func = apiFunctions.get(name);
    if (func != null && isApiFunctionEnabled(name)) {
        final List<Pair<String, String>> oldVisibleForms = getVisibleForms();
        return new BaseFunction() {

            @Override
            public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
                Object retValue;
                cx.putThreadLocal(SERVER_SIDE_SCRIPT_EXECUTE, Boolean.TRUE);
                try {
                    retValue = func.call(cx, scope, thisObj, args);
                } finally {
                    cx.removeThreadLocal(SERVER_SIDE_SCRIPT_EXECUTE);
                }
                if (!(func instanceof WebComponentFunction)) {
                    WebObjectFunctionDefinition def = webComponentSpec.getApiFunctions().get(name);
                    retValue = NGConversions.INSTANCE.convertServerSideRhinoToRhinoValue(retValue, def.getReturnType(), component, null);
                }
                updateVisibleContainers(oldVisibleForms);
                return retValue;
            }
        };
    }
    // check if we have a setter/getter for this property
    if (name != null && name.length() > 0) {
        String uName = new StringBuffer(name.substring(0, 1).toUpperCase()).append(name.substring(1)).toString();
        if (apiFunctions.containsKey("set" + uName) && apiFunctions.containsKey("get" + uName)) {
            // call getter
            Function propertyGetter = apiFunctions.get("get" + uName);
            return propertyGetter.call(Context.getCurrentContext(), start, start, new Object[] {});
        }
    }
    if ("svyMarkupId".equals(name)) {
        String formName = null;
        IWebFormUI parent = component.findParent(IWebFormUI.class);
        if (parent != null) {
            formName = parent.getController().getName();
        } else {
            formName = component.getFormElement().getForm().getName();
        }
        return ComponentFactory.getMarkupId(formName, component.getName());
    }
    // is this really needed? will not prototype be looked at automatically by Rhino code?
    if (prototypeScope != null) {
        return prototypeScope.get(name, start);
    }
    return Scriptable.NOT_FOUND;
}
Also used : Context(org.mozilla.javascript.Context) HashMap(java.util.HashMap) AbstractBase(com.servoy.j2db.persistence.AbstractBase) Scriptable(org.mozilla.javascript.Scriptable) WebServiceScriptable(com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable) WebObjectFunctionDefinition(org.sablo.specification.WebObjectFunctionDefinition) Callable(org.mozilla.javascript.Callable) PropertyDescription(org.sablo.specification.PropertyDescription) WebComponentFunction(com.servoy.j2db.server.ngclient.scripting.WebComponentFunction) BaseFunction(org.mozilla.javascript.BaseFunction) Function(org.mozilla.javascript.Function) IWebFormUI(com.servoy.j2db.server.ngclient.IWebFormUI) BaseFunction(org.mozilla.javascript.BaseFunction) WebComponentFunction(com.servoy.j2db.server.ngclient.scripting.WebComponentFunction) Pair(com.servoy.j2db.util.Pair)

Aggregations

AbstractBase (com.servoy.j2db.persistence.AbstractBase)1 IWebFormUI (com.servoy.j2db.server.ngclient.IWebFormUI)1 WebComponentFunction (com.servoy.j2db.server.ngclient.scripting.WebComponentFunction)1 WebServiceScriptable (com.servoy.j2db.server.ngclient.scripting.WebServiceScriptable)1 Pair (com.servoy.j2db.util.Pair)1 HashMap (java.util.HashMap)1 BaseFunction (org.mozilla.javascript.BaseFunction)1 Callable (org.mozilla.javascript.Callable)1 Context (org.mozilla.javascript.Context)1 Function (org.mozilla.javascript.Function)1 Scriptable (org.mozilla.javascript.Scriptable)1 PropertyDescription (org.sablo.specification.PropertyDescription)1 WebObjectFunctionDefinition (org.sablo.specification.WebObjectFunctionDefinition)1