r20517: re-add cleaned-up webapps
[kai/samba.git] / webapps / qooxdoo-0.6.3-sdk / frontend / framework / source / class / qx / lang / MshtmlEmu.js
1 /* ************************************************************************
2
3    qooxdoo - the new era of web development
4
5    http://qooxdoo.org
6
7    Copyright:
8      2004-2006 by 1&1 Internet AG, Germany, http://www.1and1.org
9
10    License:
11      LGPL 2.1: http://www.gnu.org/licenses/lgpl.html
12
13    Authors:
14      * Sebastian Werner (wpbasti)
15      * Andreas Ecker (ecker)
16
17 ************************************************************************ */
18
19 /* ************************************************************************
20
21 #require(qx.sys.Client)
22
23 ************************************************************************ */
24
25 qx.OO.defineClass("qx.lang.MshtmlEmu");
26
27 /*
28   Parts are based on:
29   moz-behaviors.xml - version 1.1.1 (2005-08-19)
30   Copyright 2004-2005, Dean Edwards
31   License: http://creativecommons.org/licenses/LGPL/2.1/
32   Thanks to Erik Arvidsson (http://webfx.eae.net/dhtml/ieemu/)
33 */
34
35 /*
36   We're going to mess about with some of mozilla's interfaces to
37   make them more explorer-like
38 */
39
40 /*
41   Note:
42   In my comments where i say support/mimic a property:
43
44   * support = exactly the same as explorer
45   * mimic = close enough
46 */
47
48 if (qx.sys.Client.getInstance().isGecko())
49 {
50   /*
51   --------------------------------------------------------------------------------
52     CSS STYLES: FLOAT
53   --------------------------------------------------------------------------------
54   */
55
56   /*
57     Support microsoft's styleFloat
58   */
59
60   CSSStyleDeclaration.prototype.__defineGetter__("styleFloat", function() {
61     return this.cssFloat;
62   });
63
64   CSSStyleDeclaration.prototype.__defineSetter__("styleFloat", function(vValue) {
65     this.cssFloat = vValue;
66   });
67
68
69
70
71
72   /*
73   --------------------------------------------------------------------------------
74     CSS STYLES: DIMENSIONS
75   --------------------------------------------------------------------------------
76   */
77
78   /*
79     Mimic microsoft's pixel representations of left/top/width/height
80     the getters only work for values that are already pixels
81   */
82
83   CSSStyleDeclaration.prototype.__defineGetter__("pixelLeft", function() {
84     return parseInt(this.left) || 0;
85   });
86
87   CSSStyleDeclaration.prototype.__defineSetter__("pixelLeft", function(vValue) {
88     this.left = vValue + "px";
89   });
90
91   CSSStyleDeclaration.prototype.__defineGetter__("pixelTop", function() {
92     return parseInt(this.top) || 0;
93   });
94
95   CSSStyleDeclaration.prototype.__defineSetter__("pixelTop", function(vValue) {
96     this.top = vValue + "px";
97   });
98
99   CSSStyleDeclaration.prototype.__defineGetter__("pixelWidth", function() {
100     return parseInt(this.width) || 0;
101   });
102
103   CSSStyleDeclaration.prototype.__defineSetter__("pixelWidth", function(vValue) {
104     this.width = vValue + "px";
105   });
106
107   CSSStyleDeclaration.prototype.__defineGetter__("pixelHeight", function() {
108     return parseInt(this.height) || 0;
109   });
110
111   CSSStyleDeclaration.prototype.__defineSetter__("pixelHeight", function(vValue) {
112     this.height = vValue + "px";
113   });
114
115
116
117
118
119   /*
120   --------------------------------------------------------------------------------
121     HTML DOCUMENT EXTENSIONS
122   --------------------------------------------------------------------------------
123   */
124
125   /*
126     Support Microsoft's "all" property
127   */
128   HTMLDocument.prototype.__defineGetter__("all", function() {
129     return this.getElementsByTagName("*");
130   });
131
132   /*
133     Mimic the "createEventObject" method for the document object
134   */
135   HTMLDocument.prototype.createEventObject = function() {
136     return document.createEvent("Events");
137   }
138
139
140
141
142
143
144   /*
145   --------------------------------------------------------------------------------
146     HTML ELEMENT EXTENSIONS
147   --------------------------------------------------------------------------------
148   */
149
150   /*
151     Mimic Microsoft's "all" property
152   */
153   HTMLElement.prototype.__defineGetter__("all", function() {
154     return this.getElementsByTagName("*");
155   });
156
157   /*
158     Support "parentElement"
159   */
160   HTMLElement.prototype.__defineGetter__("parentElement", function() {
161     return (this.parentNode == this.ownerDocument) ? null : this.parentNode;
162   });
163
164   /*
165     Support "uniqueID"
166   */
167   HTMLElement.prototype.__defineGetter__("uniqueID", function()
168   {
169     // a global counter is stored privately as a property of this getter function.
170     // initialise the counter
171     if (!arguments.callee.count) {
172       arguments.callee.count = 0;
173     }
174
175     // create the id and increment the counter
176     var vUniqueID = "moz_id" + arguments.callee.count++;
177
178     // creating a unique id, creates a global reference
179     window[vUniqueID] = this;
180
181     // we don't want to increment next time, so redefine the getter
182     this.__defineGetter__("uniqueID", function(){return vUniqueID;});
183
184     return vUniqueID;
185   });
186
187   /*
188     Mimic Microsoft's "currentStyle"
189   */
190   HTMLElement.prototype.__defineGetter__("currentStyle", function() {
191     return getComputedStyle(this, null);
192   });
193
194   /*
195     Mimic Microsoft's "runtimeStyle"
196   */
197   HTMLElement.prototype.__defineGetter__("runtimeStyle", function()
198   {
199     /*
200       this doesn't work yet (https://bugzilla.mozilla.org/show_bug.cgi?id=45424)
201       return this.ownerDocument.defaultView.getOverrideStyle(this, null);
202     */
203
204     return this.style;
205   });
206
207   /*
208     Support "innerText"
209   */
210   HTMLElement.prototype.__defineGetter__("innerText", function() {
211     return this.textContent;
212   });
213
214   HTMLElement.prototype.__defineSetter__("innerText", function(vValue) {
215     this.textContent = vValue;
216   });
217
218   /*
219     Mimic the "attachEvent" method
220   */
221   HTMLElement.prototype.attachEvent = function(vName, vHandler) {
222     this.addEventListener(vName.slice(2), vHandler, false);
223   }
224
225   /*
226     Mimic the "removeEvent" method
227   */
228   HTMLElement.prototype.removeEvent = function(vName, vHandler) {
229     this.removeEventListener(vName.slice(2), vHandler, false);
230   }
231
232   /*
233     Mimic the "createEventObject" method
234   */
235   HTMLElement.prototype.createEventObject = function() {
236     return this.ownerDocument.createEventObject();
237   }
238
239   /*
240     Mimic the "fireEvent" method
241   */
242   HTMLElement.prototype.fireEvent = function(vName, vEvent)
243   {
244     if (!vEvent) {
245       vEvent = this.ownerDocument.createEventObject();
246     }
247
248     vEvent.initEvent(vName.slice(2), false, false);
249
250     this.dispatchEvent(vEvent);
251
252     // not sure that this should be here??
253     if (typeof this[vName] === "function")
254     {
255       this[vName]();
256     }
257     else if (this.getAttribute(vName))
258     {
259       eval(this.getAttribute(vName));
260     }
261   }
262
263   /*
264     Support the "contains" method
265   */
266   HTMLElement.prototype.contains = function(vElement) {
267     return Boolean(vElement == this || (vElement && this.contains(vElement.parentElement)));
268   }
269
270
271
272
273
274   /*
275   --------------------------------------------------------------------------------
276     EVENT EXTENSIONS
277   --------------------------------------------------------------------------------
278   */
279
280   /*
281     Support Microsoft's proprietary event properties
282   */
283   Event.prototype.__defineGetter__("srcElement", function() {
284     return (this.target.nodeType == Node.ELEMENT_NODE) ? this.target : this.target.parentNode;
285   });
286
287   Event.prototype.__defineGetter__("fromElement",function() {
288     return (this.type == "mouseover") ? this.relatedTarget : (this.type == "mouseout") ? this.srcElement : null;
289   });
290
291   Event.prototype.__defineGetter__("toElement", function() {
292     return (this.type == "mouseout") ? this.relatedTarget : (this.type == "mouseover") ? this.srcElement : null;
293   });
294
295   /*
296     Convert w3c button id's to Microsoft's
297     Breaks with qooxdoo's internal event handling!!!
298   */
299   /*
300   Event.prototype.__defineGetter__("button", function() {
301     return (this.which == 1) ? 1 : (this.which == 2) ? 4 : 2;
302   });
303   */
304
305
306   /*
307     Mimic "returnValue" (default is "true")
308     Breaks with qooxdoo's internal event handling!!!
309   */
310   /*
311   Event.prototype.__defineGetter__("returnValue", function() {
312     return true;
313   });
314
315   Event.prototype.__defineSetter__("returnValue", function(vValue)
316   {
317     if (this.cancelable && !vValue)
318     {
319       // this can't be undone!
320       this.preventDefault();
321
322       this.__defineGetter__("returnValue", function() {
323         return false;
324       });
325     }
326   });
327   */
328
329   /*
330     Mozilla already supports the read-only "cancelBubble"
331     so we only need to define the setter
332   */
333   Event.prototype.__defineSetter__("cancelBubble", function(vValue)
334   {
335     // this can't be undone!
336     if (vValue) {
337       this.stopPropagation();
338     }
339   });
340
341   Event.prototype.__defineGetter__("offsetX", function() {
342     return this.layerX;
343   });
344
345   Event.prototype.__defineGetter__("offsetY", function() {
346     return this.layerY;
347   });
348 }