001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.impl;
017:
018: import com.google.gwt.user.client.Element;
019: import com.google.gwt.user.client.Event;
020:
021: /**
022: * Base implementation of {@link com.google.gwt.user.client.impl.DOMImpl} shared
023: * by those browsers that come a bit closer to supporting a common standard (ie,
024: * not IE).
025: */
026: abstract class DOMImplStandard extends DOMImpl {
027:
028: @Override
029: public native boolean compare(Element elem1, Element elem2) /*-{
030: return (elem1 == elem2);
031: }-*/;
032:
033: @Override
034: public native Element createInputRadioElement(String name) /*-{
035: var elem = $doc.createElement("INPUT");
036: elem.type = 'radio';
037: elem.name = name;
038: return elem;
039: }-*/;
040:
041: @Override
042: public native Element eventGetFromElement(Event evt) /*-{
043: // Standard browsers use relatedTarget rather than fromElement.
044: return evt.relatedTarget ? evt.relatedTarget : null;
045: }-*/;
046:
047: @Override
048: public native Element eventGetTarget(Event evt) /*-{
049: return evt.target || null;
050: }-*/;
051:
052: @Override
053: public native Element eventGetToElement(Event evt) /*-{
054: // Standard browsers use relatedTarget rather than toElement.
055: return evt.relatedTarget || null;
056: }-*/;
057:
058: @Override
059: public native void eventPreventDefault(Event evt) /*-{
060: evt.preventDefault();
061: }-*/;
062:
063: @Override
064: public native String eventToString(Event evt) /*-{
065: return evt.toString();
066: }-*/;
067:
068: @Override
069: public native Element getChild(Element elem, int index) /*-{
070: var count = 0, child = elem.firstChild;
071: while (child) {
072: var next = child.nextSibling;
073: if (child.nodeType == 1) {
074: if (index == count)
075: return child;
076: ++count;
077: }
078: child = next;
079: }
080:
081: return null;
082: }-*/;
083:
084: @Override
085: public native int getChildCount(Element elem) /*-{
086: var count = 0, child = elem.firstChild;
087: while (child) {
088: if (child.nodeType == 1)
089: ++count;
090: child = child.nextSibling;
091: }
092: return count;
093: }-*/;
094:
095: @Override
096: public native int getChildIndex(Element parent, Element toFind) /*-{
097: var count = 0, child = parent.firstChild;
098: while (child) {
099: if (child == toFind)
100: return count;
101: if (child.nodeType == 1)
102: ++count;
103: child = child.nextSibling;
104: }
105:
106: return -1;
107: }-*/;
108:
109: @Override
110: public native Element getFirstChild(Element elem) /*-{
111: var child = elem.firstChild;
112: while (child && child.nodeType != 1)
113: child = child.nextSibling;
114: return child || null;
115: }-*/;
116:
117: @Override
118: public native Element getNextSibling(Element elem) /*-{
119: var sib = elem.nextSibling;
120: while (sib && sib.nodeType != 1)
121: sib = sib.nextSibling;
122: return sib || null;
123: }-*/;
124:
125: @Override
126: public native Element getParent(Element elem) /*-{
127: var parent = elem.parentNode;
128: if(parent == null) {
129: return null;
130: }
131: if (parent.nodeType != 1)
132: parent = null;
133: return parent || null;
134: }-*/;
135:
136: public native String iframeGetSrc(Element elem) /*-{
137: return elem.src;
138: }-*/;
139:
140: @Override
141: public native void insertChild(Element parent, Element toAdd,
142: int index) /*-{
143: var count = 0, child = parent.firstChild, before = null;
144: while (child) {
145: if (child.nodeType == 1) {
146: if (count == index) {
147: before = child;
148: break;
149: }
150: ++count;
151: }
152: child = child.nextSibling;
153: }
154:
155: parent.insertBefore(toAdd, before);
156: }-*/;
157:
158: @Override
159: public native boolean isOrHasChild(Element parent, Element child) /*-{
160: while (child) {
161: if (parent == child) {
162: return true;
163: }
164: child = child.parentNode;
165: if (child && (child.nodeType != 1)) {
166: child = null;
167: }
168: }
169: return false;
170: }-*/;
171:
172: @Override
173: public void releaseCapture(Element elem) {
174: maybeInitializeEventSystem();
175: releaseCaptureImpl(elem);
176: }
177:
178: @Override
179: public void setCapture(Element elem) {
180: maybeInitializeEventSystem();
181: setCaptureImpl(elem);
182: }
183:
184: @Override
185: public void sinkEvents(Element elem, int bits) {
186: maybeInitializeEventSystem();
187: sinkEventsImpl(elem, bits);
188: }
189:
190: @Override
191: protected native void initEventSystem() /*-{
192: // Set up capture event dispatchers.
193: $wnd.__dispatchCapturedMouseEvent = function(evt) {
194: if ($wnd.__dispatchCapturedEvent(evt)) {
195: var cap = $wnd.__captureElem;
196: if (cap && cap.__listener) {
197: @com.google.gwt.user.client.DOM::dispatchEvent(Lcom/google/gwt/user/client/Event;Lcom/google/gwt/user/client/Element;Lcom/google/gwt/user/client/EventListener;)(evt, cap, cap.__listener);
198: evt.stopPropagation();
199: }
200: }
201: };
202:
203: $wnd.__dispatchCapturedEvent = function(evt) {
204: if (!@com.google.gwt.user.client.DOM::previewEvent(Lcom/google/gwt/user/client/Event;)(evt)) {
205: evt.stopPropagation();
206: evt.preventDefault();
207: return false;
208: }
209:
210: return true;
211: };
212:
213: $wnd.addEventListener('click', $wnd.__dispatchCapturedMouseEvent, true);
214: $wnd.addEventListener('dblclick', $wnd.__dispatchCapturedMouseEvent, true);
215: $wnd.addEventListener('mousedown', $wnd.__dispatchCapturedMouseEvent, true);
216: $wnd.addEventListener('mouseup', $wnd.__dispatchCapturedMouseEvent, true);
217: $wnd.addEventListener('mousemove', $wnd.__dispatchCapturedMouseEvent, true);
218: $wnd.addEventListener('mousewheel', $wnd.__dispatchCapturedMouseEvent, true);
219: $wnd.addEventListener('keydown', $wnd.__dispatchCapturedEvent, true);
220: $wnd.addEventListener('keyup', $wnd.__dispatchCapturedEvent, true);
221: $wnd.addEventListener('keypress', $wnd.__dispatchCapturedEvent, true);
222:
223: // Set up normal event dispatcher.
224: $wnd.__dispatchEvent = function(evt) {
225: var listener, curElem = this;
226: while (curElem && !(listener = curElem.__listener))
227: curElem = curElem.parentNode;
228: if (curElem && curElem.nodeType != 1)
229: curElem = null;
230:
231: if (listener)
232: @com.google.gwt.user.client.DOM::dispatchEvent(Lcom/google/gwt/user/client/Event;Lcom/google/gwt/user/client/Element;Lcom/google/gwt/user/client/EventListener;)(evt, curElem, listener);
233: };
234:
235: $wnd.__captureElem = null;
236: }-*/;
237:
238: private native void releaseCaptureImpl(Element elem) /*-{
239: if (elem == $wnd.__captureElem)
240: $wnd.__captureElem = null;
241: }-*/;
242:
243: private native void setCaptureImpl(Element elem) /*-{
244: $wnd.__captureElem = elem;
245: }-*/;
246:
247: private native void sinkEventsImpl(Element elem, int bits) /*-{
248: elem.__eventBits = bits;
249:
250: elem.onclick = (bits & 0x00001) ? $wnd.__dispatchEvent : null;
251: elem.ondblclick = (bits & 0x00002) ? $wnd.__dispatchEvent : null;
252: elem.onmousedown = (bits & 0x00004) ? $wnd.__dispatchEvent : null;
253: elem.onmouseup = (bits & 0x00008) ? $wnd.__dispatchEvent : null;
254: elem.onmouseover = (bits & 0x00010) ? $wnd.__dispatchEvent : null;
255: elem.onmouseout = (bits & 0x00020) ? $wnd.__dispatchEvent : null;
256: elem.onmousemove = (bits & 0x00040) ? $wnd.__dispatchEvent : null;
257: elem.onkeydown = (bits & 0x00080) ? $wnd.__dispatchEvent : null;
258: elem.onkeypress = (bits & 0x00100) ? $wnd.__dispatchEvent : null;
259: elem.onkeyup = (bits & 0x00200) ? $wnd.__dispatchEvent : null;
260: elem.onchange = (bits & 0x00400) ? $wnd.__dispatchEvent : null;
261: elem.onfocus = (bits & 0x00800) ? $wnd.__dispatchEvent : null;
262: elem.onblur = (bits & 0x01000) ? $wnd.__dispatchEvent : null;
263: elem.onlosecapture = (bits & 0x02000) ? $wnd.__dispatchEvent : null;
264: elem.onscroll = (bits & 0x04000) ? $wnd.__dispatchEvent : null;
265: elem.onload = (bits & 0x08000) ? $wnd.__dispatchEvent : null;
266: elem.onerror = (bits & 0x10000) ? $wnd.__dispatchEvent : null;
267: elem.onmousewheel = (bits & 0x20000) ? $wnd.__dispatchEvent : null;
268: }-*/;
269: }
|