Search in sources :

Example 26 with Function

use of org.mozilla.javascript.Function in project LoboEvolution by LoboEvolution.

the class HtmlController method onMouseDown.

/**
 * <p>onMouseDown.</p>
 *
 * @return True to propagate further, false if consumed.
 * @param node a {@link org.loboevolution.html.dom.nodeimpl.ModelNode} object.
 * @param event a {@link java.awt.event.MouseEvent} object.
 * @param x a int.
 * @param y a int.
 */
public boolean onMouseDown(ModelNode node, MouseEvent event, int x, int y) {
    boolean pass = true;
    if (node instanceof HTMLElementImpl) {
        final HTMLElementImpl uiElement = (HTMLElementImpl) node;
        final Function f = uiElement.getOnmousedown();
        if (f != null) {
            final MouseEventImpl evt = new MouseEventImpl();
            evt.initMouseEvent("mousedown", false, false, null, 0, 0, 0, x, y, true, true, true, true, (short) 0, null);
            evt.setIe(event);
            pass = Executor.executeFunction(uiElement, f, evt, new Object[0]);
        }
    }
    if (node instanceof HTMLLinkElementImpl) {
        ((HTMLLinkElementImpl) node).getCurrentStyle().setOverlayColor("#9090FF80");
        return false;
    }
    if (!pass) {
        return false;
    }
    final ModelNode parent = node.getParentModelNode();
    if (parent == null) {
        return true;
    }
    return onMouseDown(parent, event, 0, 0);
}
Also used : Function(org.mozilla.javascript.Function) ModelNode(org.loboevolution.html.dom.nodeimpl.ModelNode) MouseEventImpl(org.loboevolution.html.js.events.MouseEventImpl)

Example 27 with Function

use of org.mozilla.javascript.Function in project LoboEvolution by LoboEvolution.

the class HtmlController method onMouseOver.

/**
 * <p>onMouseOver.</p>
 *
 * @param renderable a {@link org.loboevolution.html.renderer.BaseBoundableRenderable} object.
 * @param nodeStart a {@link org.loboevolution.html.dom.nodeimpl.ModelNode} object.
 * @param event a {@link java.awt.event.MouseEvent} object.
 * @param x a int.
 * @param y a int.
 * @param limit a {@link org.loboevolution.html.dom.nodeimpl.ModelNode} object.
 */
public void onMouseOver(final BaseBoundableRenderable renderable, final ModelNode nodeStart, final MouseEvent event, final int x, final int y, final ModelNode limit) {
    ModelNode node = nodeStart;
    while (node != null) {
        if (node == limit) {
            break;
        }
        if (node instanceof HTMLElementImpl) {
            final HTMLElementImpl uiElement = (HTMLElementImpl) node;
            uiElement.setMouseOver(true);
            final Function f = uiElement.getOnmouseover();
            if (f != null) {
                final MouseEventImpl evt = new MouseEventImpl();
                evt.initMouseEvent("mouseover", false, false, null, 0, 0, 0, x, y, true, true, true, true, (short) 0, null);
                evt.setIe(event);
                Executor.executeFunction(uiElement, f, evt, new Object[0]);
            }
            final HtmlRendererContext rcontext = uiElement.getHtmlRendererContext();
            if (rcontext != null) {
                rcontext.onMouseOver(uiElement, event);
            }
        }
        node = node.getParentModelNode();
    }
    setMouseOnMouseOver(renderable, nodeStart, limit);
}
Also used : HtmlRendererContext(org.loboevolution.http.HtmlRendererContext) Function(org.mozilla.javascript.Function) ModelNode(org.loboevolution.html.dom.nodeimpl.ModelNode) MouseEventImpl(org.loboevolution.html.js.events.MouseEventImpl)

Example 28 with Function

use of org.mozilla.javascript.Function in project LoboEvolution by LoboEvolution.

the class JavaObjectWrapper method get.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.mozilla.javascript.ScriptableObject#get(java.lang.String,
	 * org.mozilla.javascript.Scriptable)
	 */
/**
 * {@inheritDoc}
 */
@Override
public Object get(String name, Scriptable start) {
    PropertyInfo pinfo = this.classWrapper.getProperty(name);
    if (pinfo != null) {
        Method getter = pinfo.getGetter();
        if (getter == null) {
            throw new EvaluatorException("Property '" + name + "' is not readable");
        }
        try {
            // Cannot retain delegate with a strong reference.
            Object javaObject = this.getJavaObject();
            if (javaObject == null) {
                throw new IllegalStateException("Java object (class=" + this.classWrapper + ") is null.");
            }
            Object val = getter.invoke(javaObject, (Object[]) null);
            return JavaScript.getInstance().getJavascriptObject(val, start.getParentScope());
        } catch (Exception err) {
            logger.log(Level.SEVERE, err.getMessage(), err);
            return new Object();
        }
    } else {
        Function f = this.classWrapper.getFunction(name);
        if (f != null) {
            return f;
        } else {
            // Should check properties set in context
            // first. Consider element IDs should not
            // override Window variables set by user.
            Object result = super.get(name, start);
            if (result != Scriptable.NOT_FOUND) {
                return result;
            }
            PropertyInfo ni = this.classWrapper.getNameIndexer();
            if (ni != null) {
                Method getter = ni.getGetter();
                if (getter != null) {
                    // Cannot retain delegate with a strong reference.
                    Object javaObject = this.getJavaObject();
                    if (javaObject == null) {
                        throw new IllegalStateException("Java object (class=" + this.classWrapper + ") is null.");
                    }
                    try {
                        Object val = getter.invoke(javaObject, name);
                        if (val == null) {
                            // There might not be an indexer setter.
                            return super.get(name, start);
                        } else {
                            return JavaScript.getInstance().getJavascriptObject(val, start.getParentScope());
                        }
                    } catch (Exception err) {
                        logger.log(Level.SEVERE, err.getMessage(), err);
                    }
                }
            }
            return Scriptable.NOT_FOUND;
        }
    }
}
Also used : Function(org.mozilla.javascript.Function) EvaluatorException(org.mozilla.javascript.EvaluatorException) ScriptableObject(org.mozilla.javascript.ScriptableObject) Method(java.lang.reflect.Method) PropertyInfo(org.loboevolution.info.PropertyInfo) EvaluatorException(org.mozilla.javascript.EvaluatorException)

Example 29 with Function

use of org.mozilla.javascript.Function in project LoboEvolution by LoboEvolution.

the class JavaScript method defineElementClass.

public void defineElementClass(Scriptable scope, final Document document, final String jsClassName, final String elementName, Class<?> javaClass) {
    JavaInstantiator ji = () -> {
        Document d = document;
        if (d == null) {
            throw new IllegalStateException("Document not set in current context.");
        }
        return d.createElement(elementName);
    };
    JavaClassWrapper classWrapper = JavaClassWrapperFactory.getInstance().getClassWrapper(javaClass);
    Function constructorFunction = new JavaConstructorObject(jsClassName, classWrapper, ji);
    ScriptableObject.defineProperty(scope, jsClassName, constructorFunction, ScriptableObject.READONLY);
}
Also used : Function(org.mozilla.javascript.Function) Document(org.loboevolution.html.node.Document)

Example 30 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)

Aggregations

Function (org.mozilla.javascript.Function)120 Context (org.mozilla.javascript.Context)32 Scriptable (org.mozilla.javascript.Scriptable)32 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)9 RepositoryException (com.servoy.j2db.persistence.RepositoryException)8 GlobalScope (com.servoy.j2db.scripting.GlobalScope)7 ServoyException (com.servoy.j2db.util.ServoyException)7 HtmlRendererContext (org.loboevolution.http.HtmlRendererContext)7 NativeObject (org.mozilla.javascript.NativeObject)7 JSONObject (org.json.JSONObject)6 ModelNode (org.loboevolution.html.dom.nodeimpl.ModelNode)6 PropertyDescription (org.sablo.specification.PropertyDescription)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 JSONException (org.json.JSONException)5