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;
}
}
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;
}
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();
}
}
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]);
}
}
}
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();
}
}
Aggregations