use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.
the class WebFormUI method initElementScope.
private ElementScope initElementScope(IFormController controller) {
FormScope formScope = controller.getFormScope();
ElementScope elementsScope = new ElementScope(formScope);
// $NON-NLS-1$
formScope.putWithoutFireChange("elements", elementsScope);
return elementsScope;
}
use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.
the class EventExecutor method executeEvent.
public Object executeEvent(WebComponent component, String eventType, int eventId, Object[] eventArgs) {
Scriptable scope = null;
Function f = null;
Object[] newargs = eventArgs != null ? Arrays.copyOf(eventArgs, eventArgs.length) : null;
if (eventId > 0) {
ScriptMethod scriptMethod = formController.getApplication().getFlattenedSolution().getScriptMethod(eventId);
if (scriptMethod != null) {
if (scriptMethod.getParent() instanceof Form) {
FormScope formScope = formController.getFormScope();
f = formScope.getFunctionByName(scriptMethod.getName());
if (f != null && f != Scriptable.NOT_FOUND) {
scope = formScope;
}
} else // is it a global method
if (scriptMethod.getParent() instanceof Solution) {
scope = formController.getApplication().getScriptEngine().getScopesScope().getGlobalScope(scriptMethod.getScopeName());
if (scope != null) {
f = ((GlobalScope) scope).getFunctionByName(scriptMethod.getName());
}
} else // very like a foundset/entity method
{
Scriptable foundsetScope = null;
if (component instanceof WebFormComponent) {
IRecord rec = ((WebFormComponent) component).getDataAdapterList().getRecord();
if (rec != null) {
foundsetScope = (Scriptable) rec.getParentFoundSet();
}
}
if (foundsetScope == null)
foundsetScope = (Scriptable) formController.getFormModel();
if (foundsetScope != null) {
// TODO ViewFoundSets should be come a scriptable if they have foundset methods..
scope = foundsetScope;
Object scopeMethod = scope.getPrototype().get(scriptMethod.getName(), scope);
if (scopeMethod instanceof Function)
f = (Function) scopeMethod;
}
}
if (f == null) {
Debug.error(// $NON-NLS-1$ //$NON-NLS-2$
"No function found for " + scriptMethod + " when trying to execute the event " + eventType + '(' + eventId + ") of component: " + component, // $NON-NLS-1$
new RuntimeException());
return null;
}
} else {
Debug.warn("Couldn't find the ScriptMethod for event: " + eventType + " with event id: " + eventId + " to execute for component " + component);
}
}
// $NON-NLS-1$
if (formController.isInFindMode() && !Utils.getAsBoolean(f.get("_AllowToRunInFind_", f)))
return null;
if (newargs != null) {
for (int i = 0; i < newargs.length; i++) {
if (newargs[i] instanceof JSONObject && "event".equals(((JSONObject) newargs[i]).optString("type"))) {
JSONObject json = (JSONObject) newargs[i];
JSEvent event = new JSEvent();
JSEventType.fillJSEvent(event, json, component, formController);
event.setType(getEventType(eventType));
event.setName(RepositoryHelper.getDisplayName(eventType, BaseComponent.class));
newargs[i] = event;
} else {
// try to convert the received arguments
WebObjectFunctionDefinition propertyDesc = component.getSpecification().getHandler(eventType);
List<PropertyDescription> parameters = propertyDesc.getParameters();
if (i < parameters.size()) {
PropertyDescription parameterPropertyDescription = parameters.get(i);
ValueReference<Boolean> returnValueAdjustedIncommingValueForIndex = new ValueReference<Boolean>(Boolean.FALSE);
newargs[i] = NGConversions.INSTANCE.convertSabloComponentToRhinoValue(JSONUtils.fromJSON(null, newargs[i], parameterPropertyDescription, new BrowserConverterContext(component, PushToServerEnum.allow), returnValueAdjustedIncommingValueForIndex), parameterPropertyDescription, component, scope);
}
// TODO? if in propertyDesc.getAsPropertyDescription().getConfig() we have "type":"${dataproviderType}" and parameterPropertyDescription.getType() is Object
// then get the type from the dataprovider and try to convert the json to that type instead of simply object
}
}
}
if (component instanceof WebFormComponent) {
IPersist persist = ((WebFormComponent) component).getFormElement().getPersistIfAvailable();
if (persist instanceof AbstractBase) {
List<Object> instanceMethodArguments = ((AbstractBase) persist).getFlattenedMethodArguments(eventType);
if (instanceMethodArguments != null && instanceMethodArguments.size() > 0) {
// create entries for the instanceMethodArguments if they are more then callback arguments
if (instanceMethodArguments.size() > newargs.length) {
newargs = Utils.arrayJoin(newargs, new Object[instanceMethodArguments.size() - newargs.length]);
}
// use instanceMethodArguments if not null, else just use the callback argument
for (int i = 0; i < instanceMethodArguments.size(); i++) {
Object value = instanceMethodArguments.get(i);
if (value != null && value != JSONObject.NULL) {
newargs[i] = Utils.parseJSExpression(value);
}
}
}
}
}
try {
formController.getApplication().updateLastAccessed();
return formController.getApplication().getScriptEngine().executeFunction(f, scope, scope, newargs, false, false);
} catch (Exception ex) {
formController.getApplication().reportJSError(ex.getMessage(), ex);
return null;
}
}
use of com.servoy.j2db.scripting.FormScope in project servoy-client by Servoy.
the class RhinoConversion method defaultFromRhino.
/**
* Default conversion used to convert from Rhino property types that do not explicitly implement component <-> Rhino conversions. <BR/><BR/>
* Values of types that don't implement the sablo <-> rhino conversions are by default accessible directly.
*/
@SuppressWarnings("unchecked")
public static // PropertyDescription / IWebObjectContext ... can be made available here if needed
Object defaultFromRhino(// PropertyDescription / IWebObjectContext ... can be made available here if needed
Object propertyValue, // PropertyDescription / IWebObjectContext ... can be made available here if needed
Object oldValue) {
// convert simple values to json values
if (isUndefinedOrNotFound(propertyValue)) {
return null;
}
if (propertyValue instanceof NativeDate) {
return ((NativeDate) propertyValue).unwrap();
}
if (propertyValue instanceof NativeObject) {
Map<String, Object> map = new HashMap<>();
Map<String, Object> oldMap = (oldValue instanceof Map) ? (Map<String, Object>) oldValue : null;
NativeObject no = (NativeObject) propertyValue;
Object[] ids = no.getIds();
for (Object id2 : ids) {
Object value = null;
if (id2 instanceof String) {
value = no.get((String) id2, no);
} else if (id2 instanceof Symbol) {
value = no.get((Symbol) id2, no);
} else if (id2 instanceof Number) {
value = no.get(((Number) id2).intValue(), no);
}
String id = String.valueOf(id2);
map.put(id, defaultFromRhino(value, oldMap != null ? oldMap.get(id) : null));
}
return map;
}
if (propertyValue instanceof NativeArray) {
List<Object> list = new ArrayList<Object>();
List<Object> oldList = (oldValue instanceof List) ? (List<Object>) oldValue : null;
final NativeArray no = (NativeArray) propertyValue;
final long naLength = no.getLength();
for (int id = 0; id < naLength; id++) {
list.add(defaultFromRhino(no.get(id, no), oldList != null && oldList.size() > id ? oldList.get(id) : null));
}
return list;
}
if (propertyValue instanceof FormScope && ((FormScope) propertyValue).getFormController() != null)
return ((FormScope) propertyValue).getFormController().getName();
if (propertyValue instanceof IFormController)
return ((IFormController) propertyValue).getName();
if (propertyValue instanceof JSDataSet) {
return ((JSDataSet) propertyValue).getDataSet();
}
if (propertyValue instanceof RhinoMapOrArrayWrapper) {
return ((RhinoMapOrArrayWrapper) propertyValue).getWrappedValue();
}
if (propertyValue instanceof CharSequence) {
return propertyValue.toString();
}
return propertyValue;
}
Aggregations