1 package swingjs.api.js;
3 import java.awt.Dimension;
4 import java.awt.Rectangle;
7 * A mix of direct DOM calls on DOM nodes and convenience methods to do that.
9 * NOTE: DO NOT OVERLOAD THESE METHODS, as this package will not be qualified.
14 public interface DOMNode {
16 public static JQuery jQuery = /** @j2sNative jQuery.$ || (jQuery.$ = jQuery) || */null;
18 // "abstract" in the sense that these are the exact calls to JavaScript
21 public void addEventListener(String event, Object listener);
22 public void removeEventListener(String event);
23 public void removeEventListener(String event, Object listener);
27 public String[] getAttributeNames();
29 public String getAttribute(String name);
31 public void setAttribute(String attr, String val);
33 public void appendChild(DOMNode node);
35 public void prepend(DOMNode node);
37 public void insertBefore(DOMNode node, DOMNode refNode);
39 public DOMNode removeChild(DOMNode node);
42 public boolean hasFocus();
45 public DOMNode removeAttribute(String attr);
47 public void setSelectionRange(int start, int end, String direction);
49 public Rectangle getBoundingClientRect();
51 // static convenience methods
53 public static DOMNode createElement(String key, String id) {
57 * node = document.createElement(key);
58 * id && (node.id = id);
63 public static DOMNode getElement(String id) {
64 return (/** @j2sNative document.getElementById(id) ||*/ null);
67 public static DOMNode createTextNode(String text) {
68 return (/** @j2sNative document.createTextNode(text) || */ null);
71 public static DOMNode getParent(DOMNode node) {
72 return (/** @j2sNative node.parentNode ||*/ null);
75 public static DOMNode getPreviousSibling(DOMNode node) {
76 return (/** @j2sNative node.previousSibling ||*/ null);
79 public static DOMNode firstChild(DOMNode node) {
80 return (/** @j2sNative node.firstChild ||*/ null);
83 public static DOMNode lastChild(DOMNode node) {
84 return (/** @j2sNative node.lastChild ||*/ null);
87 public static DOMNode setZ(DOMNode node, int z) {
88 return setStyles(node, "z-index", "" + z);
91 public static Object getAttr(Object node, String attr) {
98 * return (typeof a == "undefined" ? null : a);
105 public static int getAttrInt(DOMNode node, String attr) {
106 return (/** @j2sNative node && node[attr] ||*/ 0);
109 public static String getStyle(DOMNode node, String style) {
110 return (/** @j2sNative node && node.style[style] ||*/ null);
113 public static void getCSSRectangle(DOMNode node, Rectangle r) {
117 * r.x = parseInt(node.style.left.split("p")[0]);
118 * r.y = parseInt(node.style.top.split("p")[0]);
119 * r.width = parseInt(node.style.width.split("p")[0]);
120 * r.height = parseInt(node.style.height.split("p")[0]);
125 public static DOMNode setAttr(DOMNode node, String attr, Object val) {
129 * attr && (node[attr] = (val == "秘TRUE" ? true : val == "秘FALSE" ? false : val));
136 public static void setAttrInt(DOMNode node, String attr, int val) {
147 * allows for null key to be skipped (used in audio)
153 public static DOMNode setAttrs(DOMNode node, Object... attr) {
157 * for (var i = 0; i < attr.length;) {
158 * C$.setAttr(node, attr[i++],attr[i++]);
164 public static DOMNode setStyles(DOMNode node, String... attr) {
168 * if (node) for (var i = 0; i < attr.length;) {
169 * node.style[attr[i++]] = attr[i++];
176 public static DOMNode setSize(DOMNode node, int width, int height) {
177 return setStyles(node, "width", width + "px", "height", height + "px");
180 public static DOMNode setPositionAbsolute(DOMNode node) {
181 return DOMNode.setStyles(node, "position", "absolute");
184 public static void setVisible(DOMNode node, boolean visible) {
185 setStyles(node, "display", visible ? "block" : "none");
188 public static DOMNode setTopLeftAbsolute(DOMNode node, int top, int left) {
189 DOMNode.setStyles(node, "top", top + "px");
190 DOMNode.setStyles(node, "left", left + "px");
191 return DOMNode.setStyles(node, "position", "absolute");
194 public static void addHorizontalGap(DOMNode domNode, int gap) {
195 DOMNode label = DOMNode.setStyles(DOMNode.createElement("label", null),
196 "letter-spacing", gap + "px", "font-size", "0pt");
197 label.appendChild(DOMNode.createTextNode("."));
198 domNode.appendChild(label);
201 public static void appendChildSafely(DOMNode parent, DOMNode node) {
204 * if (!parent || node.parentElement == parent)
207 parent.appendChild(node);
210 // static jQuery calls
218 public static int getHeight(DOMNode node) {
219 return jQuery.$(node).height();
228 public static int getWidth(DOMNode node) {
229 return jQuery.$(node).width();
235 * Remove this node and return its parent. Automatically removing all events
239 * @return parent or null
241 public static void dispose(DOMNode node) {
243 jQuery.$(node).remove();
247 * Just remove the node, keeping its events and data
250 public static void remove(DOMNode node) {
252 // NOTE: IE does not have node.remove()
254 DOMNode p = getParent(node);
260 * just detaches all the nodes; doesn't remove their listeners
263 public static void detachAll(DOMNode node) {
267 * while(node.lastChild)
268 * node.removeChild(node.lastChild);
273 * jQuery detach() + append()
277 * @return parent if container is null, or container if it is not null
279 public static DOMNode transferTo(DOMNode node, DOMNode container) {
282 DOMNode p = getParent(node);
285 jQuery.$(node).detach();
286 } catch (Throwable e) {
289 if (container == null)
291 jQuery.$(container).append(node);
295 public static Object getEmbedded(String name, String type) {
296 DOMNode node = DOMNode.getElement(name + "-div");
303 return new Dimension(DOMNode.getWidth(node), DOMNode.getHeight(node));
305 return DOMNode.getAttr(node, type);