com.belteshazzar.jquery.JQuery Maven / Gradle / Ivy
The newest version!
package com.belteshazzar.jquery;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.w3c.dom.Element;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.html.HTMLDocument;
import com.belteshazzar.jquery.functions.IntElementBooleanFunction;
import com.belteshazzar.jquery.functions.IntElementFunction;
import com.belteshazzar.jquery.functions.IntElementObjectFunction;
import com.belteshazzar.jquery.functions.IntElementVoidFunction;
import com.belteshazzar.jquery.functions.IntIntIntFunction;
import com.belteshazzar.jquery.functions.IntIntStringFunction;
import com.belteshazzar.jquery.functions.IntJQueryFunction;
import com.belteshazzar.jquery.functions.IntObjectObjectFunction;
import com.belteshazzar.jquery.functions.IntOffsetOffsetFunction;
import com.belteshazzar.jquery.functions.IntStringBooleanStringFunction;
import com.belteshazzar.jquery.functions.IntStringElementFunction;
import com.belteshazzar.jquery.functions.IntStringFunction;
import com.belteshazzar.jquery.functions.IntStringIntFunction;
import com.belteshazzar.jquery.functions.IntStringJQueryFunction;
import com.belteshazzar.jquery.functions.IntStringStringFunction;
import com.belteshazzar.jquery.functions.IntTweenVoidFunction;
import com.belteshazzar.jquery.functions.JQueryFunction;
import com.belteshazzar.jquery.functions.PromiseBooleanVoidFunction;
import com.belteshazzar.jquery.functions.PromiseIntIntVoidFunction;
import com.belteshazzar.jquery.functions.PromiseVoidFunction;
import com.belteshazzar.jquery.functions.VoidFunction;
import com.belteshazzar.jquery.functions.VoidFunctionFunction;
import com.belteshazzar.jquery.functions.callers.EventHandlerCaller;
import com.belteshazzar.jquery.functions.callers.IntElementBooleanFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntElementFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntElementObjectFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntIntIntFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntIntStringFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntJQueryFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntObjectObjectFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntOffsetOffsetFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntStringBooleanStringFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntStringElementFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntStringFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntStringIntFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntStringJQueryFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntStringStringFunctionCaller;
import com.belteshazzar.jquery.functions.callers.IntTweenVoidFunctionCaller;
import com.belteshazzar.jquery.functions.callers.PromiseBooleanVoidFunctionCaller;
import com.belteshazzar.jquery.functions.callers.PromiseIntIntVoidFunctionCaller;
import com.belteshazzar.jquery.functions.callers.PromiseVoidFunctionCaller;
import com.belteshazzar.jquery.functions.callers.VoidFunctionCaller;
import com.belteshazzar.jquery.functions.callers.VoidFunctionFunctionCaller;
import com.sun.webkit.dom.HTMLScriptElementImpl;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.scene.web.WebEngine;
import netscape.javascript.JSException;
import netscape.javascript.JSObject;
public class JQuery {
public static final String DEFAULT_JQUERY_LOCAL = JQuery.class.getResource("jquery-2.2.1.min.js").toExternalForm();
public static final String DEFAULT_JQUERY_REMOTE = "https://code.jquery.com/jquery-2.2.2.min.js";
public static final boolean DEFAULT_CLEAR_READY_FUNCTIONS = true;
public static class Config {
String src;
boolean clearReadyFunctions;
Config() {
this.src = DEFAULT_JQUERY_LOCAL;
this.clearReadyFunctions = DEFAULT_CLEAR_READY_FUNCTIONS;
}
}
private static final List readyFunctions = new ArrayList();
private static final Map eventHandlerMap = new HashMap();
private static WebEngine webEngine;
private static JSObject window = null;
public static final Config config = new Config();
public static void setEngine(WebEngine webEngine) {
JQuery.webEngine = webEngine;
webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener() {
@Override
public void changed(ObservableValue ov,
State oldState, State newState) {
if (newState == State.SUCCEEDED) {
documentLoaded();
} else {
window = null;
}
}
}
);
if (webEngine.getLoadWorker().getState()== State.SUCCEEDED) {
documentLoaded();
}
}
private static void documentLoaded() {
try {
@SuppressWarnings("unused")
Object jQuery = webEngine.executeScript("jQuery");
window = (JSObject)webEngine.executeScript("window");
documentReady();
} catch (JSException jsex) {
HTMLDocument doc = (HTMLDocument)webEngine.getDocument();
HTMLScriptElementImpl script = (HTMLScriptElementImpl)doc.createElement("script");
((EventTarget)script).addEventListener("load", new EventListener() {
@Override
public void handleEvent(org.w3c.dom.events.Event evt) {
Object jQuery = webEngine.executeScript("jQuery");
if (!(jQuery instanceof JSObject)) {
throw new IllegalStateException("JQuery hasn't been loaded into web page");
}
window = (JSObject)webEngine.executeScript("window");
documentReady();
}
}, false);
((EventTarget)script).addEventListener("error", new EventListener() {
@Override
public void handleEvent(org.w3c.dom.events.Event evt) {
throw new IllegalStateException("failed to load jquery from: " + script.getSrc());
}
}, false);
script.setSrc(JQuery.config.src);
doc.getBody().appendChild(script);
}
}
private static void documentReady() {
for (VoidFunction f : readyFunctions) {
f.apply();
}
if (JQuery.config.clearReadyFunctions) readyFunctions.clear();
}
static JSObject createFunction(IntStringStringFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntStringStringFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,s) { return obj.java.call(i,s); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(EventHandler handler) {
JSObject obj = createObject();
obj.setMember("java", new EventHandlerCaller(handler));
obj.eval("(function(obj) { obj.fn = function(ev) { obj.java.call(ev); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntElementObjectFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntElementObjectFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,el) { return obj.java.call(i,el); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntStringFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntStringFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i) { return obj.java.call(i); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntStringBooleanStringFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntStringBooleanStringFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,s,b) { return obj.java.call(i,s,b); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntElementFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntElementFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,s,b) { return obj.java.call(i,s,b); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntJQueryFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntJQueryFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,s,b) { return obj.java.call(i,s,b); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntStringJQueryFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntStringJQueryFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,s,b) { return obj.java.call(i,s,b); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntStringElementFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntStringElementFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,s,b) { return obj.java.call(i,s,b); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntIntStringFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntIntStringFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,j) { return obj.java.call(i,j); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntIntIntFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntIntIntFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,j) { return obj.java.call(i,j); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(VoidFunction function) {
JSObject obj = createObject();
obj.setMember("java", new VoidFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function() { obj.java.call(); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntObjectObjectFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntObjectObjectFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,o) { return obj.java.call(i,o); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntTweenVoidFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntTweenVoidFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,t) { return obj.java.call(i,t); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntElementBooleanFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntElementBooleanFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,el) { return obj.java.call(i,el); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntOffsetOffsetFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntOffsetOffsetFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,os) { return obj.java.call(i,os); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(PromiseVoidFunction function) {
JSObject obj = createObject();
obj.setMember("java", new PromiseVoidFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(promise) { obj.java.call(promise); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(PromiseBooleanVoidFunction function) {
JSObject obj = createObject();
obj.setMember("java", new PromiseBooleanVoidFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(promise,finished) { obj.java.call(promise,finished); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(PromiseIntIntVoidFunction function) {
JSObject obj = createObject();
obj.setMember("java", new PromiseIntIntVoidFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(promise,i,j) { obj.java.call(promise,i,j); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(IntStringIntFunction function) {
JSObject obj = createObject();
obj.setMember("java", new IntStringIntFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function(i,s) { return obj.java.call(i,s); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createFunction(VoidFunctionFunction function) {
JSObject obj = createObject();
obj.setMember("java", new VoidFunctionFunctionCaller(function));
obj.eval("(function(obj) { obj.fn = function() { return obj.java.call(); } })(this)");
return (JSObject)obj.getMember("fn");
}
static JSObject createArray(Object[] values) {
JSObject array = createArray();
for (int i=0 ; i