Search in sources :

Example 41 with Function

use of org.mozilla.javascript.Function in project servoy-client by Servoy.

the class ScriptVariableScope method put.

/*
	 * @see Scriptable#put(String, Scriptable, Object)
	 */
@Override
public void put(String name, Scriptable arg1, Object value) {
    if (value instanceof Function) {
        super.put(name, arg1, value);
    } else {
        try {
            Context currentContext = Context.getCurrentContext();
            if (currentContext != null) {
                Debugger debugger = currentContext.getDebugger();
                if (debugger != null) {
                    if (debugger instanceof IDebuggerWithWatchPoints) {
                        IDebuggerWithWatchPoints wp = (IDebuggerWithWatchPoints) debugger;
                        wp.modification(name, this);
                    }
                }
            }
            put(name, value);
        } catch (RuntimeException re) {
            throw new WrappedException(re);
        }
    }
}
Also used : Context(org.mozilla.javascript.Context) Debugger(org.mozilla.javascript.debug.Debugger) Function(org.mozilla.javascript.Function) WrappedException(org.mozilla.javascript.WrappedException) IDebuggerWithWatchPoints(org.mozilla.javascript.debug.IDebuggerWithWatchPoints)

Example 42 with Function

use of org.mozilla.javascript.Function in project servoy-client by Servoy.

the class FunctionDefinition method exists.

/**
 * Test if the given methodName or formName do exist. Will return one of the {@link Exist} enums.
 * @since 5.2
 */
public Exist exists(IClientPluginAccess access) {
    final Exist[] retVal = new Exist[] { Exist.METHOD_NOT_FOUND };
    if (access instanceof ClientPluginAccessProvider) {
        final IApplication application = ((ClientPluginAccessProvider) access).getApplication();
        application.invokeAndWait(new Runnable() {

            public void run() {
                if (application.getSolution() != null) {
                    if (contextName.startsWith(ScriptVariable.SCOPES_DOT_PREFIX)) {
                        GlobalScope gs = application.getScriptEngine().getScopesScope().getGlobalScope(contextName.substring(ScriptVariable.SCOPES_DOT_PREFIX.length()));
                        if (gs != null && gs.get(methodName) instanceof Function) {
                            retVal[0] = Exist.METHOD_FOUND;
                        }
                    } else {
                        IFormController fp = application.getFormManager().leaseFormPanel(contextName);
                        if (fp == null) {
                            retVal[0] = Exist.FORM_NOT_FOUND;
                        } else if (fp.getFormScope().get(methodName, fp.getFormScope()) instanceof Function) {
                            retVal[0] = Exist.METHOD_FOUND;
                        }
                    }
                } else {
                    retVal[0] = Exist.NO_SOLUTION;
                }
            }
        });
    }
    return retVal[0];
}
Also used : ClientPluginAccessProvider(com.servoy.j2db.plugins.ClientPluginAccessProvider) Function(org.mozilla.javascript.Function) IApplication(com.servoy.j2db.IApplication) IFormController(com.servoy.j2db.IFormController)

Example 43 with Function

use of org.mozilla.javascript.Function in project servoy-client by Servoy.

the class MapSerializer method convertToMap.

public static Map<String, Object> convertToMap(Object jsobj) {
    Map<String, Object> retval = new HashMap<String, Object>();
    if (jsobj == null || jsobj == Undefined.instance || jsobj instanceof IFoundSet || jsobj instanceof IRecord || !(jsobj instanceof NativeObject)) {
        return retval;
    }
    IdScriptableObject no = (IdScriptableObject) jsobj;
    Object[] noIDs = no.getIds();
    String propertyKey;
    Object propertyValue;
    for (Object element : noIDs) {
        // id can be Integer or String
        if (element instanceof Integer) {
            propertyKey = ((Integer) element).toString();
            propertyValue = no.get(((Integer) element).intValue(), no);
        } else if (element instanceof String) {
            propertyKey = (String) element;
            propertyValue = no.get((String) element, no);
        } else {
            // should not happen
            continue;
        }
        if (// allow but ignore functions nested in objects
        propertyValue instanceof Function) {
            continue;
        }
        if (propertyValue instanceof NativeObject) {
            propertyValue = convertToMap(propertyValue);
        }
        if (propertyValue instanceof Wrapper) {
            propertyValue = ((Wrapper) propertyValue).unwrap();
        }
        retval.put(propertyKey, propertyValue);
    }
    return retval;
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) Function(org.mozilla.javascript.Function) Wrapper(org.mozilla.javascript.Wrapper) IFoundSet(com.servoy.j2db.dataprocessing.IFoundSet) HashMap(java.util.HashMap) IRecord(com.servoy.j2db.dataprocessing.IRecord) NativeObject(org.mozilla.javascript.NativeObject) IdScriptableObject(org.mozilla.javascript.IdScriptableObject) IdScriptableObject(org.mozilla.javascript.IdScriptableObject)

Example 44 with Function

use of org.mozilla.javascript.Function in project servoy-client by Servoy.

the class RuntimeWebComponent method put.

@Override
public void put(String name, Scriptable start, Object value) {
    if (isInvalidValue(name, value))
        return;
    List<Pair<String, String>> oldVisibleForms = getVisibleForms();
    if (specProperties != null && specProperties.contains(name)) {
        Object previousVal = null;
        PropertyDescription pd = webComponentSpec.getProperties().get(name);
        if (pd.getType() instanceof ISabloComponentToRhino && !(pd.getType() instanceof IRhinoToSabloComponent)) {
            // the it has sablo to rhino conversion but not the other way around then we should just use the
            // value from the conversion so call get(String,Scriptable)
            previousVal = get(name, start);
        } else
            previousVal = component.getProperty(name);
        Object val = NGConversions.INSTANCE.convertRhinoToSabloComponentValue(value, previousVal, pd, component);
        if (val != previousVal)
            component.setProperty(name, val);
        if (pd != null && pd.getType() instanceof VisiblePropertyType) {
            // search all labelfor elements
            for (WebComponent siblingComponent : component.getParent().getComponents()) {
                Collection<PropertyDescription> labelFors = siblingComponent.getSpecification().getProperties(LabelForPropertyType.INSTANCE);
                if (labelFors != null) {
                    for (PropertyDescription labelForProperty : labelFors) {
                        if (Utils.equalObjects(component.getName(), siblingComponent.getProperty(labelForProperty.getName()))) {
                            // sibling component is labelfor, so set value to all its visible properties
                            Collection<PropertyDescription> visibleProperties = siblingComponent.getSpecification().getProperties(VisiblePropertyType.INSTANCE);
                            if (visibleProperties != null) {
                                for (PropertyDescription visibleProperty : visibleProperties) {
                                    previousVal = siblingComponent.getProperty(visibleProperty.getName());
                                    val = NGConversions.INSTANCE.convertRhinoToSabloComponentValue(value, previousVal, visibleProperty, siblingComponent);
                                    if (val != previousVal)
                                        siblingComponent.setProperty(name, val);
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }
    } else if (prototypeScope != null) {
        if (!apiFunctions.containsKey(name)) {
            // check if we have a setter 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 setter
                    Function propertySetter = apiFunctions.get("set" + uName);
                    propertySetter.call(Context.getCurrentContext(), start, start, new Object[] { value });
                } else {
                    prototypeScope.put(name, start, value);
                }
            }
        }
    }
    updateVisibleContainers(oldVisibleForms);
}
Also used : PropertyDescription(org.sablo.specification.PropertyDescription) IRhinoToSabloComponent(com.servoy.j2db.server.ngclient.property.types.NGConversions.IRhinoToSabloComponent) WebComponent(org.sablo.WebComponent) WebComponentFunction(com.servoy.j2db.server.ngclient.scripting.WebComponentFunction) BaseFunction(org.mozilla.javascript.BaseFunction) Function(org.mozilla.javascript.Function) ISabloComponentToRhino(com.servoy.j2db.server.ngclient.property.types.NGConversions.ISabloComponentToRhino) VisiblePropertyType(org.sablo.specification.property.types.VisiblePropertyType) Pair(com.servoy.j2db.util.Pair)

Example 45 with Function

use of org.mozilla.javascript.Function 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

Function (org.mozilla.javascript.Function)126 Scriptable (org.mozilla.javascript.Scriptable)35 Context (org.mozilla.javascript.Context)33 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)27 ScriptableObject (org.mozilla.javascript.ScriptableObject)27 JSFunction (org.mozilla.javascript.annotations.JSFunction)25 MouseEventImpl (org.loboevolution.html.js.events.MouseEventImpl)11 BaseFunction (org.mozilla.javascript.BaseFunction)10 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)10 NativeObject (org.mozilla.javascript.NativeObject)10 RepositoryException (com.servoy.j2db.persistence.RepositoryException)8 GlobalScope (com.servoy.j2db.scripting.GlobalScope)7 ServoyException (com.servoy.j2db.util.ServoyException)7 IOException (java.io.IOException)7 HtmlRendererContext (org.loboevolution.http.HtmlRendererContext)7 JSONObject (org.json.JSONObject)6 ModelNode (org.loboevolution.html.dom.nodeimpl.ModelNode)6 RhinoException (org.mozilla.javascript.RhinoException)6 PropertyDescription (org.sablo.specification.PropertyDescription)6 ArrayList (java.util.ArrayList)5