Improve open layers
[gpl/argeo-suite.git] / org.argeo.app.core / src / org / argeo / app / ux / js / JsClient.java
index b7fc724d4d0a3b46a748f836506d9db5da1967c5..2503cf05e482a06eb0bfb14a8feaf0ce21986cbf 100644 (file)
@@ -4,6 +4,7 @@ import java.util.Arrays;
 import java.util.Locale;
 import java.util.Map;
 import java.util.StringJoiner;
+import java.util.concurrent.CompletionStage;
 import java.util.function.Function;
 
 /**
@@ -43,7 +44,13 @@ public interface JsClient {
        String createJsFunction(String name, Function<Object[], Object> toDo);
 
        /** Get a global variable name. */
-       public String getJsVarName(String name);
+       String getJsVarName(String name);
+
+       /**
+        * Completion stage when the client is ready (typically the page has loaded in
+        * the browser).
+        */
+       CompletionStage<Boolean> getReadyStage();
 
        /*
         * DEFAULTS
@@ -57,13 +64,17 @@ public interface JsClient {
                execute(jsObject + '.' + methodCall, args);
        }
 
+       default boolean isInstanceOf(String reference, String jsClass) {
+               return (Boolean) evaluate("return "+getJsVarName(reference) + " instanceof " + jsClass);
+       }
+
        /*
         * UTILITIES
         */
 
        static String toJsValue(Object o) {
                if (o instanceof CharSequence)
-                       return '\"' + o.toString() + '\"';
+                       return '\'' + o.toString() + '\'';
                else if (o instanceof Number)
                        return o.toString();
                else if (o instanceof Boolean)
@@ -83,8 +94,10 @@ public interface JsClient {
                                return jsObject.newJs();
                        else
                                return jsObject.getJsReference();
+               } else if (o instanceof JsReference jsReference) {
+                       return jsReference.get();
                } else
-                       return '\"' + o.toString() + '\"';
+                       return '\'' + o.toString() + '\'';
        }
 
        static String toJsArgs(Object... arr) {
@@ -128,4 +141,8 @@ public interface JsClient {
                return sj.toString();
        }
 
+       static String escapeQuotes(String str) {
+               return str.replace("'", "\\'").replace("\"", "\\\"");
+       }
+
 }