PageRenderTime 133ms CodeModel.GetById 60ms app.highlight 7ms RepoModel.GetById 63ms app.codeStats 1ms

/library/platform/DOMIE6.py

http://pyjamas.googlecode.com/
Python | 213 lines | 213 code | 0 blank | 0 comment | 0 complexity | c24ede16510cf54937f7219149571498 MD5 | raw file
  1def init():
  2    JS("""
  3    // Set up event dispatchers.
  4    $wnd.__dispatchEvent = function() {
  5        if ($wnd.event.returnValue == null) {
  6            $wnd.event.returnValue = true;
  7            if (!DOM_previewEvent($wnd.event))
  8                return;
  9        }
 10
 11        var listener, curElem = this;
 12        while (curElem && !(listener = curElem.__listener))
 13            curElem = curElem.parentElement;
 14    
 15        if (listener)
 16            DOM_dispatchEvent($wnd.event, curElem, listener);
 17    };
 18
 19    $wnd.__dispatchDblClickEvent = function() {
 20        var newEvent = $doc.createEventObject();
 21        this.fireEvent('onclick', newEvent);
 22        if (this.__eventBits & 2)
 23            $wnd.__dispatchEvent.call(this);
 24    };
 25
 26    $doc.body.onclick       =
 27    $doc.body.onmousedown   =
 28    $doc.body.onmouseup     =
 29    $doc.body.onmousemove   =
 30    $doc.body.onkeydown     =
 31    $doc.body.onkeypress    =
 32    $doc.body.onkeyup       =
 33    $doc.body.onfocus       =
 34    $doc.body.onblur        =
 35    $doc.body.ondblclick    = $wnd.__dispatchEvent;
 36    """)
 37
 38def compare(elem1, elem2):
 39    JS("""
 40    if (!elem1 && !elem2)
 41        return true;
 42    else if (!elem1 || !elem2)
 43        return false;
 44    return (elem1.uniqueID == elem2.uniqueID);
 45    """)
 46
 47def createInputRadio(group):
 48    JS("""
 49    return $doc.createElement("<INPUT type='RADIO' name='" + group + "'>");
 50    """)
 51
 52def eventGetTarget(evt):
 53    JS("""
 54    var elem = evt.srcElement;
 55    return elem ? elem : null;
 56    """)
 57
 58def eventGetToElement(evt):
 59    JS("""
 60    return evt.toElement ? evt.toElement : null;
 61    """)
 62
 63def eventPreventDefault(evt):
 64    JS("""
 65    evt.returnValue = false;
 66    """)
 67
 68def eventToString(evt):
 69    JS("""
 70    if (evt.toString) return evt.toString();
 71    return "[object Event]";
 72    """)
 73
 74def getAbsoluteLeft(elem):
 75    JS("""
 76    var scrollLeft = $doc.documentElement.scrollLeft;
 77    if(scrollLeft == 0){
 78        scrollLeft = $doc.body.scrollLeft
 79    }
 80    
 81    return (elem.getBoundingClientRect().left + scrollLeft) - 2;
 82    """)
 83
 84def getAbsoluteTop(elem):
 85    JS("""
 86    var scrollTop = $doc.documentElement.scrollTop;
 87    if(scrollTop == 0){
 88        scrollTop = $doc.body.scrollTop
 89    }
 90    
 91    return (elem.getBoundingClientRect().top +  scrollTop) - 2;
 92    """)
 93
 94
 95def getChild(elem, index):
 96    JS("""
 97    var child = elem.children[index];
 98    return child ? child : null;
 99    """)
100
101def getChildCount(elem):
102    JS("""
103    return elem.children.length;
104    """)
105
106def getChildIndex(parent, child):
107    JS("""
108    var count = parent.children.length;
109    for (var i = 0; i < count; ++i) {
110        if (child.uniqueID == parent.children[i].uniqueID)
111            return i;
112    }
113    return -1;
114    """)
115
116def getFirstChild(elem):
117    JS("""
118    var child = elem.firstChild;
119    return child ? child : null;
120    """)
121
122def getInnerText(elem):
123    JS("""
124    var ret = elem.innerText;
125    return (ret == null) ? null : ret;
126    """)
127
128def getNextSibling(elem):
129    JS("""
130    var sib = elem.nextSibling;
131    return sib ? sib : null;
132    """)
133
134def getParent(elem):
135    JS("""
136    var parent = elem.parentElement;
137    return parent ? parent : null;
138    """)
139
140def insertChild(parent, child, index):
141    JS("""
142    if (index == parent.children.length)
143        parent.appendChild(child);
144    else
145        parent.insertBefore(child, parent.children[index]);
146    """)
147
148def insertListItem(select, text, value, index):
149    JS("""
150    var newOption = document.createElement("Option");
151    if(index==-1) {
152        select.add(newOption);
153    } else {
154        select.add(newOption,index);
155    }
156    newOption.text=text;
157    newOption.value=value;
158    """)
159
160def isOrHasChild(parent, child):
161    JS("""
162    while (child) {
163        if (parent.uniqueID == child.uniqueID)
164            return true;
165        child = child.parentElement;
166    }
167    return false;
168    """)
169
170def releaseCapture(elem):
171    JS("""
172    elem.releaseCapture();
173    """)
174
175def setCapture(elem):
176    JS("""
177    elem.setCapture();
178    """)
179
180def setInnerText(elem, text):
181    JS("""
182    if (!text)
183        text = '';
184    elem.innerText = text;
185    """)
186
187def sinkEvents(elem, bits):
188    JS("""
189    elem.__eventBits = bits;
190
191    elem.onclick       = (bits & 0x00001) ? $wnd.__dispatchEvent : null;
192    elem.ondblclick    = (bits & 0x00002) ? $wnd.__dispatchDblClickEvent : null;
193    elem.onmousedown   = (bits & 0x00004) ? $wnd.__dispatchEvent : null;
194    elem.onmouseup     = (bits & 0x00008) ? $wnd.__dispatchEvent : null;
195    elem.onmouseover   = (bits & 0x00010) ? $wnd.__dispatchEvent : null;
196    elem.onmouseout    = (bits & 0x00020) ? $wnd.__dispatchEvent : null;
197    elem.onmousemove   = (bits & 0x00040) ? $wnd.__dispatchEvent : null;
198    elem.onkeydown     = (bits & 0x00080) ? $wnd.__dispatchEvent : null;
199    elem.onkeypress    = (bits & 0x00100) ? $wnd.__dispatchEvent : null;
200    elem.onkeyup       = (bits & 0x00200) ? $wnd.__dispatchEvent : null;
201    elem.onchange      = (bits & 0x00400) ? $wnd.__dispatchEvent : null;
202    elem.onfocus       = (bits & 0x00800) ? $wnd.__dispatchEvent : null;
203    elem.onblur        = (bits & 0x01000) ? $wnd.__dispatchEvent : null;
204    elem.onlosecapture = (bits & 0x02000) ? $wnd.__dispatchEvent : null;
205    elem.onscroll      = (bits & 0x04000) ? $wnd.__dispatchEvent : null;
206    elem.onload        = (bits & 0x08000) ? $wnd.__dispatchEvent : null;
207    elem.onerror       = (bits & 0x10000) ? $wnd.__dispatchEvent : null;
208    """)
209
210def toString(elem):
211    JS("""
212    return elem.outerHTML;
213    """)