Search in sources :

Example 81 with Function

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

the class GlobalEventHandlersImpl method getEventFunction.

/**
 * <p>
 * getEventFunction.
 * </p>
 *
 * @param varValue      a {@link org.mozilla.javascript.Function} object.
 * @param normalAttributeName a {@link java.lang.String} object.
 * @return a {@link org.mozilla.javascript.Function} object.
 */
protected Function getEventFunction(Function varValue, String normalAttributeName) {
    if (varValue != null) {
        return varValue;
    }
    synchronized (this) {
        Map<String, Function> fba = this.functionByAttribute;
        Function f = fba == null ? null : fba.get(normalAttributeName);
        if (f != null) {
            return f;
        }
        final UserAgentContext uac = getUserAgentContext();
        if (uac == null) {
            throw new IllegalStateException("No user agent context.");
        }
        if (uac.isScriptingEnabled() && this instanceof ElementImpl) {
            ElementImpl elem = (ElementImpl) this;
            final String attributeValue = elem.getAttribute(normalAttributeName);
            if (Strings.isCssBlank(attributeValue)) {
                f = null;
            } else {
                final String functionCode = "function " + normalAttributeName + "_" + System.identityHashCode(this) + "() { " + attributeValue + " }";
                final Document doc = this.document;
                if (doc == null) {
                    throw new IllegalStateException("Element does not belong to a document.");
                }
                final Context ctx = Executor.createContext(getDocumentURL(), uac);
                try {
                    final Scriptable scope = (Scriptable) doc.getUserData(Executor.SCOPE_KEY);
                    if (scope == null) {
                        throw new IllegalStateException("Scriptable (scope) instance was expected to be keyed as UserData to document using " + Executor.SCOPE_KEY);
                    }
                    final Scriptable thisScope = (Scriptable) JavaScript.getInstance().getJavascriptObject(this, scope);
                    try {
                        ctx.setLanguageVersion(Context.VERSION_1_8);
                        f = ctx.compileFunction(thisScope, functionCode, elem.getTagName() + "[" + elem.getId() + "]." + normalAttributeName, 1, null);
                    } catch (final RhinoException ecmaError) {
                        logger.log(Level.WARNING, "Javascript error at " + ecmaError.sourceName() + ":" + ecmaError.lineNumber() + ": " + ecmaError.getMessage(), ecmaError.getMessage());
                        f = null;
                    } catch (final Throwable err) {
                        logger.log(Level.WARNING, "Unable to evaluate Javascript code", err);
                        f = null;
                    }
                } finally {
                    Context.exit();
                }
            }
            if (fba == null) {
                fba = new HashMap<>(1);
                this.functionByAttribute = fba;
            }
            fba.put(normalAttributeName, f);
        }
        return f;
    }
}
Also used : Context(org.mozilla.javascript.Context) UserAgentContext(org.loboevolution.http.UserAgentContext) Function(org.mozilla.javascript.Function) UserAgentContext(org.loboevolution.http.UserAgentContext) RhinoException(org.mozilla.javascript.RhinoException) Document(org.loboevolution.html.node.Document) Scriptable(org.mozilla.javascript.Scriptable)

Example 82 with Function

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

the class EventTargetImpl method dispatchEvent.

/**
 * {@inheritDoc}
 */
@Override
public boolean dispatchEvent(Node htmlElementImpl, Event evt) {
    Map<String, List<Function>> map = this.onEventHandlers.get(htmlElementImpl);
    if (map != null) {
        List<Function> handlers = map.get(evt.getType());
        if (handlers != null) {
            for (final Function h : handlers) {
                if (!clicked.contains(htmlElementImpl)) {
                    Executor.executeFunction(this, h, evt, new Object[0]);
                    clicked.add(htmlElementImpl);
                } else {
                    clicked.clear();
                }
            }
        }
    }
    return false;
}
Also used : Function(org.mozilla.javascript.Function)

Example 83 with Function

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

the class HtmlController method onMouseOver.

/**
 * <p>onMouseOver.</p>
 *
 * @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.
 * @param limit a {@link org.loboevolution.html.dom.nodeimpl.ModelNode} object.
 */
public void onMouseOver(ModelNode node, MouseEvent event, int x, int y, ModelNode limit) {
    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();
    }
}
Also used : HtmlRendererContext(org.loboevolution.http.HtmlRendererContext) Function(org.mozilla.javascript.Function) MouseEventImpl(org.loboevolution.html.js.events.MouseEventImpl)

Example 84 with Function

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

the class HtmlController method onMouseScroll.

/**
 * <p>onMouseScroll.</p>
 * @param node a {@link org.loboevolution.html.dom.nodeimpl.ModelNode} object.
 */
public void onMouseScroll(ModelNode node) {
    if (node instanceof HTMLElementImpl) {
        final HTMLElementImpl uiElement = (HTMLElementImpl) node;
        final Function f = uiElement.getOnscroll();
        final MouseEventImpl evt = new MouseEventImpl();
        evt.initMouseEvent("scroll", false, false, null, 0, 0, 0, 0, 0, true, true, true, true, (short) 0, null);
        uiElement.dispatchEvent(uiElement, evt);
        if (f != null) {
            Executor.executeFunction(uiElement, f, evt, new Object[0]);
        }
    }
}
Also used : Function(org.mozilla.javascript.Function) MouseEventImpl(org.loboevolution.html.js.events.MouseEventImpl)

Example 85 with Function

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

the class HtmlController method onMouseOut.

/**
 * <p>onMouseOut.</p>
 *
 * @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.
 * @param limit a {@link org.loboevolution.html.dom.nodeimpl.ModelNode} object.
 */
public void onMouseOut(ModelNode node, MouseEvent event, int x, int y, ModelNode limit) {
    while (node != null) {
        if (node == limit) {
            break;
        }
        if (node instanceof HTMLElementImpl) {
            final HTMLElementImpl uiElement = (HTMLElementImpl) node;
            uiElement.setMouseOver(false);
            final Function f = uiElement.getOnmouseout();
            if (f != null) {
                final MouseEventImpl evt = new MouseEventImpl();
                evt.initMouseEvent("mouseout", 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.onMouseOut(uiElement, event);
            }
        }
        node = node.getParentModelNode();
    }
}
Also used : HtmlRendererContext(org.loboevolution.http.HtmlRendererContext) Function(org.mozilla.javascript.Function) MouseEventImpl(org.loboevolution.html.js.events.MouseEventImpl)

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