Search in sources :

Example 6 with FormScope

use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.

the class JSForm method removeVariableFromScopes.

private void removeVariableFromScopes(ScriptVariable var) {
    List<IFormController> controllers = application.getFormManager().getCachedFormControllers(getForm());
    for (IFormController formController : controllers) {
        FormScope formScope = formController.getFormScope();
        formScope.updateProviderswithCopy(getForm(), getForm());
        formScope.remove(var);
    }
}
Also used : IFormController(com.servoy.j2db.IFormController) FormScope(com.servoy.j2db.scripting.FormScope)

Example 7 with FormScope

use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.

the class JSForm method addVariableToScopes.

private void addVariableToScopes(ScriptVariable var) {
    List<IFormController> controllers = application.getFormManager().getCachedFormControllers(getForm());
    for (IFormController formController : controllers) {
        FormScope formScope = formController.getFormScope();
        formScope.put(var, true);
    }
}
Also used : IFormController(com.servoy.j2db.IFormController) FormScope(com.servoy.j2db.scripting.FormScope)

Example 8 with FormScope

use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.

the class RuntimeWebComponentTest method arrayPropDirectAccess.

@Test
public void arrayPropDirectAccess() throws Exception {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("testForm");
    Assert.assertNotNull(form);
    FormScope formScope = form.getFormScope();
    Context cx = Context.enter();
    try {
        // CHECK INITIAL DEFAULT VALUE FROM SPEC
        RhinoMapOrArrayWrapper stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.stringArray", "Evaluation Test Script", 1, null);
        Assert.assertArrayEquals(new String[] { "a", "b", "c" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
        // same check as above but directly inside Rhino
        Boolean testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.stringArray.length === 3) && elements.testComponent.stringArray.every(function(this_i, i) { return this_i == [\"a\", \"b\", \"c\"][i] } ) ", "Evaluation Test Script", 1, null);
        Assert.assertTrue(testResult.booleanValue());
        // ADD ELEMENT TO EXISTING VALUE
        cx.evaluateString(formScope, "elements.testComponent.getStringArray().push(\"d\") ", "Evaluation Test Script", 1, null);
        // CHECK CHANGED VALUE
        stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.stringArray", "Evaluation Test Script", 1, null);
        Assert.assertArrayEquals(new String[] { "a", "b", "c", "d" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
        // same check as above but directly inside Rhino
        testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.stringArray.length === 4) && elements.testComponent.stringArray.every(function(this_i, i) { return this_i == [\"a\", \"b\", \"c\", \"d\"][i] } ) ", "Evaluation Test Script", 1, null);
        Assert.assertTrue(testResult.booleanValue());
        // ASSIGN DIFFERENT ARRAY BY REF TO PROPERTY
        cx.evaluateString(formScope, "elements.testComponent.stringArray = ['1', '2', '3']", "Evaluation Test Script", 1, null);
        // CHECK NEW VALUE
        stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.stringArray", "Evaluation Test Script", 1, null);
        Assert.assertArrayEquals(new String[] { "1", "2", "3" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
        // same check as above but directly inside Rhino
        testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.stringArray.length === 3) && elements.testComponent.stringArray.every(function(this_i, i) { return this_i == [\"1\", \"2\", \"3\"][i] } ) ", "Evaluation Test Script", 1, null);
        Assert.assertTrue(testResult.booleanValue());
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) RhinoMapOrArrayWrapper(com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) FormScope(com.servoy.j2db.scripting.FormScope) Test(org.junit.Test)

Example 9 with FormScope

use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.

the class RuntimeWebComponentTest method arrayPropAccessThroughGetterAndSetter.

@Test
public void arrayPropAccessThroughGetterAndSetter() throws Exception {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("testForm");
    Assert.assertNotNull(form);
    FormScope formScope = form.getFormScope();
    Context cx = Context.enter();
    try {
        // CHECK INITIAL DEFAULT VALUE FROM SPEC
        RhinoMapOrArrayWrapper stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.getStringArray()", "Evaluation Test Script", 1, // this used to fail with an exception when RuntimeLegacyComponent gave null scope in getter code
        null);
        Assert.assertArrayEquals(new String[] { "a", "b", "c" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
        // same check as above but directly inside Rhino
        Boolean testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.getStringArray().length === 3) && elements.testComponent.getStringArray().every(function(this_i, i) { return this_i == [\"a\", \"b\", \"c\"][i] } ) ", "Evaluation Test Script", 1, null);
        Assert.assertTrue(testResult.booleanValue());
        // ADD ELEMENT TO EXISTING VALUE
        cx.evaluateString(formScope, "elements.testComponent.getStringArray().push(\"d\") ", "Evaluation Test Script", 1, null);
        // CHECK CHANGED VALUE
        stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.getStringArray()", "Evaluation Test Script", 1, null);
        Assert.assertArrayEquals(new String[] { "a", "b", "c", "d" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
        // same check as above but directly inside Rhino
        testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.getStringArray().length === 4) && elements.testComponent.getStringArray().every(function(this_i, i) { return this_i == [\"a\", \"b\", \"c\", \"d\"][i] } ) ", "Evaluation Test Script", 1, null);
        Assert.assertTrue(testResult.booleanValue());
        // ASSIGN DIFFERENT ARRAY BY REF TO PROPERTY
        cx.evaluateString(formScope, "elements.testComponent.setStringArray(['1', '2', '3'])", "Evaluation Test Script", 1, null);
        // CHECK NEW VALUE
        stringArrayProp = (RhinoMapOrArrayWrapper) cx.evaluateString(formScope, "elements.testComponent.getStringArray()", "Evaluation Test Script", 1, null);
        Assert.assertArrayEquals(new String[] { "1", "2", "3" }, ((List<String>) stringArrayProp.getWrappedValue()).toArray());
        // same check as above but directly inside Rhino
        testResult = (Boolean) cx.evaluateString(formScope, "(elements.testComponent.getStringArray().length === 3) && elements.testComponent.getStringArray().every(function(this_i, i) { return this_i == [\"1\", \"2\", \"3\"][i] } ) ", "Evaluation Test Script", 1, null);
        Assert.assertTrue(testResult.booleanValue());
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) RhinoMapOrArrayWrapper(com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) FormScope(com.servoy.j2db.scripting.FormScope) Test(org.junit.Test)

Example 10 with FormScope

use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.

the class BasicFormController method initForJSUsage.

/**
 * Initialize this FormController(or related classes/methods) to be used in javascript
 */
public synchronized JSForm initForJSUsage(CreationalPrototype creationalPrototype) {
    if (formScope == null) {
        try {
            // make scope for state delegation via prototype mechanism
            List<Form> forms = application.getFlattenedSolution().getFormHierarchy(form);
            List<ISupportScriptProviders> scriptProviders = new ArrayList<ISupportScriptProviders>();
            for (Form frm : forms) {
                scriptProviders.add(new RuntimeSupportScriptProviders(application, frm));
            }
            formScope = new FormScope(this, scriptProviders.toArray(new ISupportScriptProviders[scriptProviders.size()]));
            if (formModel != null) {
                ITable formTable = application.getFoundSetManager().getTable(form.getDataSource());
                formScope.setPrototype(new SelectedRecordScope(this, formTable == null ? null : application.getScriptEngine().getTableScope(formTable)));
                // $NON-NLS-1$
                formScope.putWithoutFireChange("foundset", formModel);
            }
            // create JS place holder for this object
            scriptableForm = new BasicFormController.JSForm(this);
            // set parent scope
            NativeJavaObject formObject = new NativeJavaObject(formScope, scriptableForm, ScriptObjectRegistry.getJavaMembers(FormController.JSForm.class, formScope));
            // $NON-NLS-1$
            formScope.putWithoutFireChange("controller", formObject);
            // register the place holder 'scriptableForm' in CreationalPrototype scope
            creationalPrototype.setLocked(false);
            // $NON-NLS-1$
            creationalPrototype.put(((Integer) creationalPrototype.get("length", creationalPrototype)).intValue(), creationalPrototype, formScope);
            creationalPrototype.put(getName(), creationalPrototype, formScope);
            creationalPrototype.setLocked(true);
            formScope.createVars();
        } catch (Exception ex) {
            // $NON-NLS-1$
            application.reportError(application.getI18NMessage("servoy.formPanel.error.initFormScope") + ": " + getName(), ex);
            return null;
        }
    }
    return scriptableForm;
}
Also used : Form(com.servoy.j2db.persistence.Form) ArrayList(java.util.ArrayList) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) FormScope(com.servoy.j2db.scripting.FormScope) ISupportScriptProviders(com.servoy.j2db.persistence.ISupportScriptProviders) RuntimeSupportScriptProviders(com.servoy.j2db.FormController.RuntimeSupportScriptProviders) SelectedRecordScope(com.servoy.j2db.scripting.SelectedRecordScope) ITable(com.servoy.j2db.persistence.ITable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject)

Aggregations

FormScope (com.servoy.j2db.scripting.FormScope)23 IFormController (com.servoy.j2db.IFormController)6 Form (com.servoy.j2db.persistence.Form)5 ArrayList (java.util.ArrayList)4 JSONObject (org.json.JSONObject)4 IRecord (com.servoy.j2db.dataprocessing.IRecord)3 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)3 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)3 GlobalScope (com.servoy.j2db.scripting.GlobalScope)3 IWebFormController (com.servoy.j2db.server.ngclient.IWebFormController)3 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)3 ServoyException (com.servoy.j2db.util.ServoyException)3 JSONException (org.json.JSONException)3 Scriptable (org.mozilla.javascript.Scriptable)3 ApplicationException (com.servoy.j2db.ApplicationException)2 IServiceProvider (com.servoy.j2db.IServiceProvider)2 FindState (com.servoy.j2db.dataprocessing.FindState)2 IServoyAwareBean (com.servoy.j2db.dataui.IServoyAwareBean)2 IPersist (com.servoy.j2db.persistence.IPersist)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2