Skip to content

Commit 9c38527

Browse files
committed
Initial checkin of Web API
1 parent b4ccd1e commit 9c38527

File tree

9 files changed

+444
-0
lines changed

9 files changed

+444
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2019 - 2020 Volker Berlin (i-net software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.inetsoftware.jwebassembly.web;
17+
18+
import de.inetsoftware.jwebassembly.api.annotation.Import;
19+
20+
/**
21+
* The base type for all wrapped JavaScript objects.
22+
*
23+
* @author Volker Berlin
24+
*/
25+
public class JSObject {
26+
27+
/** module name for JavaScript helper of the web */
28+
private final static String WEB = "Web";
29+
30+
/**
31+
* Native object from JavaScript.
32+
*/
33+
protected final Object peer;
34+
35+
/**
36+
* Create a Java instance as wrapper of the JavaScript object.
37+
*
38+
* @param peer
39+
* the native JavaScript object
40+
*/
41+
protected JSObject( Object peer ) {
42+
this.peer = peer;
43+
}
44+
45+
/**
46+
* Native get a JavaScript property value by name from global variable window.
47+
*
48+
* @param <T>
49+
* the return type
50+
* @param propName
51+
* the name of the property as DOMString
52+
* @return the value of the property
53+
*/
54+
@Import( module = WEB, js = "(p)=>window[p]" )
55+
protected static native <T> T win_get( String propName );
56+
57+
58+
/**
59+
* Native get a JavaScript property value by name.
60+
*
61+
* @param <T>
62+
* the return type
63+
* @param peer
64+
* the JavaScript object
65+
* @param propName
66+
* the name of the property as DOMString
67+
* @return the value of the property
68+
*/
69+
@Import( module = WEB, js = "(o,p)=>o[p]" )
70+
private static native <T> T get0( Object peer, String propName );
71+
72+
/**
73+
* Get the value of a property of this object.
74+
*
75+
* @param <T>
76+
* the return type
77+
* @param propName
78+
* the name of the property
79+
* @return the value of the property
80+
*/
81+
protected <T> T get( String propName ) {
82+
return get0( peer, propName );
83+
}
84+
85+
/**
86+
* Native invoke a JavaScript method with one parameter.
87+
*
88+
* @param <T>
89+
* the return type
90+
* @param peer
91+
* the JavaScript object
92+
* @param methodName
93+
* the method name
94+
* @param param1
95+
* the parameter
96+
* @return the return value
97+
*/
98+
@Import( module = WEB, js = "(o,m,p1)=>o[m](p1)" )
99+
private static native <T> T invoke1( Object peer, String methodName, Object param1 );
100+
101+
/**
102+
* Invoke a JavaScript method with one parameter of this object.
103+
*
104+
* @param <T>
105+
* the return type
106+
* @param methodName
107+
* the method name
108+
* @param param1
109+
* the parameter
110+
* @return the return value
111+
*/
112+
protected <T> T invoke( String methodName, Object param1 ) {
113+
return invoke1( peer, methodName, param1 );
114+
}
115+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2019 - 2020 Volker Berlin (i-net software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.inetsoftware.jwebassembly.web.dom;
17+
18+
/**
19+
* https://developer.mozilla.org/en-US/docs/Web/API/CharacterData
20+
*
21+
* @author Volker Berlin
22+
*/
23+
public class CharacterData extends Node {
24+
25+
/**
26+
* Create a Java instance as wrapper of the JavaScript object.
27+
*
28+
* @param peer
29+
* the native JavaScript object
30+
*/
31+
CharacterData( Object peer ) {
32+
super( peer );
33+
}
34+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2019 - 2020 Volker Berlin (i-net software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.inetsoftware.jwebassembly.web.dom;
17+
18+
/**
19+
* https://developer.mozilla.org/en-US/docs/Web/API/Document
20+
*
21+
* @author Volker Berlin
22+
*/
23+
public class Document extends Node {
24+
25+
/**
26+
* Create a Java instance as wrapper of the JavaScript object.
27+
*
28+
* @param peer
29+
* the native JavaScript object
30+
*/
31+
Document( Object peer ) {
32+
super( peer );
33+
}
34+
35+
/**
36+
* https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement
37+
*
38+
* @param tagName
39+
* type of element
40+
* @return The new Element
41+
*/
42+
public HTMLElement createElement( String tagName ) {
43+
return new HTMLElement( invoke( "createElement", tagName ) );
44+
}
45+
46+
/**
47+
* https://developer.mozilla.org/en-US/docs/Web/API/Document/createTextNode
48+
*
49+
* @param data
50+
* the text data
51+
* @return the text node
52+
*/
53+
public Text createTextNode( String data ) {
54+
return new Text( invoke( "createTextNode", data ) );
55+
}
56+
57+
/**
58+
* https://developer.mozilla.org/en-US/docs/Web/API/Document/body
59+
*
60+
* @return the body
61+
*/
62+
public Node body() {
63+
Object obj = get( "body" );
64+
if( obj == null ) {
65+
return null;
66+
}
67+
return new Node( obj );
68+
}
69+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2019 - 2020 Volker Berlin (i-net software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.inetsoftware.jwebassembly.web.dom;
17+
18+
/**
19+
* https://developer.mozilla.org/en-US/docs/Web/API/Element
20+
*
21+
* @author Volker Berlin
22+
*/
23+
public class Element extends Node {
24+
25+
/**
26+
* Create a Java instance as wrapper of the JavaScript object.
27+
*
28+
* @param peer
29+
* the native JavaScript object
30+
*/
31+
Element( Object peer ) {
32+
super( peer );
33+
}
34+
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2019 - 2020 Volker Berlin (i-net software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.inetsoftware.jwebassembly.web.dom;
17+
18+
import de.inetsoftware.jwebassembly.web.JSObject;
19+
20+
/**
21+
* https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
22+
*
23+
* @author Volker Berlin
24+
*/
25+
public class EventTarget extends JSObject {
26+
/**
27+
* Create a Java instance as wrapper of the JavaScript object.
28+
* @param peer the native JavaScript object
29+
*/
30+
EventTarget( Object peer ) {
31+
super( peer );
32+
}
33+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2019 - 2020 Volker Berlin (i-net software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.inetsoftware.jwebassembly.web.dom;
17+
18+
/**
19+
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement
20+
*
21+
* @author Volker Berlin
22+
*/
23+
public class HTMLElement extends Element {
24+
25+
/**
26+
* Create a Java instance as wrapper of the JavaScript object.
27+
*
28+
* @param peer
29+
* the native JavaScript object
30+
*/
31+
HTMLElement( Object peer ) {
32+
super( peer );
33+
}
34+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2019 - 2020 Volker Berlin (i-net software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.inetsoftware.jwebassembly.web.dom;
17+
18+
/**
19+
* https://developer.mozilla.org/en-US/docs/Web/API/Node
20+
*
21+
* @author Volker Berlin
22+
*/
23+
public class Node extends EventTarget {
24+
25+
/**
26+
* Create a Java instance as wrapper of the JavaScript object.
27+
*
28+
* @param peer
29+
* the native JavaScript object
30+
*/
31+
Node( Object peer ) {
32+
super( peer );
33+
}
34+
35+
/**
36+
* https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild
37+
*
38+
* @param child
39+
* the child
40+
*/
41+
public void appendChild( Node child ) {
42+
invoke( "appendChild", child.peer );
43+
}
44+
}

0 commit comments

Comments
 (0)