/library/platform/DOMIE6.py

http://pyjamas.googlecode.com/ · Python · 213 lines · 189 code · 24 blank · 0 comment · 0 complexity · c24ede16510cf54937f7219149571498 MD5 · raw file

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