Search in sources :

Example 46 with Function

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

the class RuntimeWebComponent method executeScopeFunction.

public Object executeScopeFunction(WebObjectFunctionDefinition functionSpec, Object[] arrayOfSabloJavaMethodArgs) {
    if (functionSpec != null) {
        Object object = scopeObject.get(functionSpec.getName(), scopeObject);
        if (object instanceof Function) {
            Context context = Context.enter();
            try {
                // find spec for method
                List<PropertyDescription> argumentPDs = (functionSpec != null ? functionSpec.getParameters() : null);
                // convert arguments to Rhino
                Object[] array = new Object[arrayOfSabloJavaMethodArgs.length];
                for (int i = 0; i < arrayOfSabloJavaMethodArgs.length; i++) {
                    array[i] = NGConversions.INSTANCE.convertSabloComponentToRhinoValue(arrayOfSabloJavaMethodArgs[i], (argumentPDs != null && argumentPDs.size() > i) ? argumentPDs.get(i) : null, component, this);
                }
                context.putThreadLocal(SERVER_SIDE_SCRIPT_EXECUTE, Boolean.TRUE);
                try {
                    Object retValue = ((Function) object).call(context, scopeObject, scopeObject, array);
                    PropertyDescription retValuePD = (functionSpec != null ? functionSpec.getReturnType() : null);
                    return NGConversions.INSTANCE.convertRhinoToSabloComponentValue(retValue, null, retValuePD, component);
                } finally {
                    context.removeThreadLocal(SERVER_SIDE_SCRIPT_EXECUTE);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            } finally {
                Context.exit();
            }
        } else {
            throw new RuntimeException("trying to call a function '" + functionSpec.getName() + "' that does not exists on a component '" + component.getName() + " with spec: " + webComponentSpec.getName());
        }
    }
    return null;
}
Also used : Context(org.mozilla.javascript.Context) PropertyDescription(org.sablo.specification.PropertyDescription) WebComponentFunction(com.servoy.j2db.server.ngclient.scripting.WebComponentFunction) BaseFunction(org.mozilla.javascript.BaseFunction) Function(org.mozilla.javascript.Function) JSONException(org.json.JSONException)

Example 47 with Function

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

the class FoundSet method sort.

/**
 * Sorts the foundset based on the given record comparator function.
 * Tries to preserve selection based on primary key. If first record is selected or cannot select old record it will select first record after sort.
 * The comparator function is called to compare
 * two records, that are passed as arguments, and
 * it will return -1/0/1 if the first record is less/equal/greater
 * then the second record.
 *
 * The function based sorting does not work with printing.
 * It is just a temporary in-memory sort.
 *
 * NOTE: starting with 7.2 release this function doesn't save the data anymore
 *
 * @sample
 * %%prefix%%foundset.sort(mySortFunction);
 *
 * function mySortFunction(r1, r2)
 * {
 *	var o = 0;
 *	if(r1.id < r2.id)
 *	{
 *		o = -1;
 *	}
 *	else if(r1.id > r2.id)
 *	{
 *		o = 1;
 *	}
 *	return o;
 * }
 *
 * @param recordComparisonFunction record comparator function
 */
@JSFunction
@JSSignature(arguments = { Function.class })
public void sort(Object recordComparisonFunction) {
    if (recordComparisonFunction instanceof Function) {
        final Function func = (Function) recordComparisonFunction;
        final IExecutingEnviroment scriptEngine = fsm.getApplication().getScriptEngine();
        final Scriptable recordComparatorScope = func.getParentScope();
        sort(new Comparator<Object[]>() {

            public int compare(Object[] o1, Object[] o2) {
                try {
                    Object compareResult = scriptEngine.executeFunction(func, recordComparatorScope, recordComparatorScope, new Object[] { getRecord(o1), getRecord(o2) }, false, true);
                    double cmp = Utils.getAsDouble(compareResult, true);
                    return cmp < 0 ? -1 : cmp > 0 ? 1 : 0;
                } catch (Exception ex) {
                    Debug.error(ex);
                }
                return 0;
            }
        });
    }
}
Also used : BaseFunction(org.mozilla.javascript.BaseFunction) JSFunction(org.mozilla.javascript.annotations.JSFunction) Function(org.mozilla.javascript.Function) IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) Scriptable(org.mozilla.javascript.Scriptable) ServoyException(com.servoy.j2db.util.ServoyException) ApplicationException(com.servoy.j2db.ApplicationException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JSSignature(com.servoy.j2db.scripting.annotations.JSSignature) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 48 with Function

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

the class FoundSetManager method getTriggerFunctions.

private List<TriggerFunction> getTriggerFunctions(ITable table, TypedProperty<Integer> property, Scriptable foundsetScope) {
    FlattenedSolution solutionRoot = getApplication().getFlattenedSolution();
    IExecutingEnviroment scriptEngine = getApplication().getScriptEngine();
    return stream(solutionRoot.getTableNodes(table)).map(tableNode -> {
        Object function = null;
        Scriptable scope = null;
        ScriptMethod scriptMethod = solutionRoot.getScriptMethod(((Integer) tableNode.getProperty(property.getPropertyName())).intValue());
        if (scriptMethod != null) {
            if (scriptMethod.getParent() instanceof Solution) {
                // global method
                GlobalScope gs = scriptEngine.getScopesScope().getGlobalScope(scriptMethod.getScopeName());
                if (gs != null) {
                    scope = gs;
                    function = gs.get(scriptMethod.getName());
                }
            } else if (foundsetScope != null) {
                // foundset method
                scope = foundsetScope;
                function = scope.getPrototype().get(scriptMethod.getName(), scope);
            }
        }
        if (function instanceof Function) {
            return new TriggerFunction((Function) function, scope, tableNode);
        }
        return null;
    }).filter(Objects::nonNull).collect(toList());
}
Also used : GlobalScope(com.servoy.j2db.scripting.GlobalScope) Function(org.mozilla.javascript.Function) IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) FlattenedSolution(com.servoy.j2db.FlattenedSolution) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) Scriptable(org.mozilla.javascript.Scriptable) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod) FlattenedSolution(com.servoy.j2db.FlattenedSolution) Solution(com.servoy.j2db.persistence.Solution)

Example 49 with Function

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

the class JSForm method js_setOnShowAllRecordsCmdMethod.

/**
 * @deprecated As of release 4.1, replaced by setOnShowAllRecordsCmd(JSMethod).
 */
@Deprecated
public void js_setOnShowAllRecordsCmdMethod(Object functionOrInteger) {
    checkModification();
    if (functionOrInteger instanceof Function) {
        Function function = (Function) functionOrInteger;
        ScriptMethod scriptMethod = getScriptMethod(function, application.getFlattenedSolution());
        if (scriptMethod != null) {
            getForm().setOnShowAllRecordsCmdMethodID(scriptMethod.getID());
        } else {
            getForm().setOnShowAllRecordsCmdMethodID(0);
        }
    } else if (functionOrInteger instanceof Number) {
        getForm().setOnShowAllRecordsCmdMethodID(((Number) functionOrInteger).intValue());
    }
}
Also used : JSFunction(org.mozilla.javascript.annotations.JSFunction) Function(org.mozilla.javascript.Function) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Example 50 with Function

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

the class JSForm method js_setOnOmitRecordCmdMethod.

/**
 * @deprecated As of release 4.1, replaced by setOnOmitRecordCmd(JSMethod).
 */
@Deprecated
public void js_setOnOmitRecordCmdMethod(Object functionOrInteger) {
    checkModification();
    if (functionOrInteger instanceof Function) {
        Function function = (Function) functionOrInteger;
        ScriptMethod scriptMethod = getScriptMethod(function, application.getFlattenedSolution());
        if (scriptMethod != null) {
            getForm().setOnOmitRecordCmdMethodID(scriptMethod.getID());
        } else {
            getForm().setOnOmitRecordCmdMethodID(0);
        }
    } else if (functionOrInteger instanceof Number) {
        getForm().setOnOmitRecordCmdMethodID(((Number) functionOrInteger).intValue());
    }
}
Also used : JSFunction(org.mozilla.javascript.annotations.JSFunction) Function(org.mozilla.javascript.Function) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

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