New.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. MochiKit.Base.update(MochiKit.DOM, {
  2. /** @id MochiKit.DOM.makeClipping */
  3. makeClipping: function (element) {
  4. element = MochiKit.DOM.getElement(element);
  5. var oldOverflow = element.style.overflow;
  6. if ((MochiKit.Style.getStyle(element, 'overflow') || 'visible') != 'hidden') {
  7. element.style.overflow = 'hidden';
  8. }
  9. return oldOverflow;
  10. },
  11. /** @id MochiKit.DOM.undoClipping */
  12. undoClipping: function (element, overflow) {
  13. element = MochiKit.DOM.getElement(element);
  14. if (!overflow) {
  15. return;
  16. }
  17. element.style.overflow = overflow;
  18. },
  19. /** @id MochiKit.DOM.makePositioned */
  20. makePositioned: function (element) {
  21. element = MochiKit.DOM.getElement(element);
  22. var pos = MochiKit.Style.getStyle(element, 'position');
  23. if (pos == 'static' || !pos) {
  24. element.style.position = 'relative';
  25. // Opera returns the offset relative to the positioning context,
  26. // when an element is position relative but top and left have
  27. // not been defined
  28. if (/Opera/.test(navigator.userAgent)) {
  29. element.style.top = 0;
  30. element.style.left = 0;
  31. }
  32. }
  33. },
  34. /** @id MochiKit.DOM.undoPositioned */
  35. undoPositioned: function (element) {
  36. element = MochiKit.DOM.getElement(element);
  37. if (element.style.position == 'relative') {
  38. element.style.position = element.style.top = element.style.left = element.style.bottom = element.style.right = '';
  39. }
  40. },
  41. /** @id MochiKit.DOM.getFirstElementByTagAndClassName */
  42. getFirstElementByTagAndClassName: function (tagName, className,
  43. /* optional */parent) {
  44. var self = MochiKit.DOM;
  45. if (typeof(tagName) == 'undefined' || tagName === null) {
  46. tagName = '*';
  47. }
  48. if (typeof(parent) == 'undefined' || parent === null) {
  49. parent = self._document;
  50. }
  51. parent = self.getElement(parent);
  52. var children = (parent.getElementsByTagName(tagName)
  53. || self._document.all);
  54. if (typeof(className) == 'undefined' || className === null) {
  55. return children[0];
  56. }
  57. for (var i = 0; i < children.length; i++) {
  58. var child = children[i];
  59. var classNames = child.className.split(' ');
  60. for (var j = 0; j < classNames.length; j++) {
  61. if (classNames[j] == className) {
  62. return child;
  63. }
  64. }
  65. }
  66. },
  67. /** @id MochiKit.DOM.isParent */
  68. isParent: function (child, element) {
  69. if (!child.parentNode || child == element) {
  70. return false;
  71. }
  72. if (child.parentNode == element) {
  73. return true;
  74. }
  75. return MochiKit.DOM.isParent(child.parentNode, element);
  76. }
  77. });
  78. MochiKit.Position = {
  79. // set to true if needed, warning: firefox performance problems
  80. // NOT neeeded for page scrolling, only if draggable contained in
  81. // scrollable elements
  82. includeScrollOffsets: false,
  83. /** @id MochiKit.Position.prepare */
  84. prepare: function () {
  85. var deltaX = window.pageXOffset
  86. || document.documentElement.scrollLeft
  87. || document.body.scrollLeft
  88. || 0;
  89. var deltaY = window.pageYOffset
  90. || document.documentElement.scrollTop
  91. || document.body.scrollTop
  92. || 0;
  93. this.windowOffset = new MochiKit.Style.Coordinates(deltaX, deltaY);
  94. },
  95. /** @id MochiKit.Position.cumulativeOffset */
  96. cumulativeOffset: function (element) {
  97. var valueT = 0;
  98. var valueL = 0;
  99. do {
  100. valueT += element.offsetTop || 0;
  101. valueL += element.offsetLeft || 0;
  102. element = element.offsetParent;
  103. } while (element);
  104. return new MochiKit.Style.Coordinates(valueL, valueT);
  105. },
  106. /** @id MochiKit.Position.realOffset */
  107. realOffset: function (element) {
  108. var valueT = 0;
  109. var valueL = 0;
  110. do {
  111. valueT += element.scrollTop || 0;
  112. valueL += element.scrollLeft || 0;
  113. element = element.parentNode;
  114. } while (element);
  115. return new MochiKit.Style.Coordinates(valueL, valueT);
  116. },
  117. /** @id MochiKit.Position.within */
  118. within: function (element, x, y) {
  119. if (this.includeScrollOffsets) {
  120. return this.withinIncludingScrolloffsets(element, x, y);
  121. }
  122. this.xcomp = x;
  123. this.ycomp = y;
  124. this.offset = this.cumulativeOffset(element);
  125. if (element.style.position == "fixed") {
  126. this.offset.x += this.windowOffset.x;
  127. this.offset.y += this.windowOffset.y;
  128. }
  129. return (y >= this.offset.y &&
  130. y < this.offset.y + element.offsetHeight &&
  131. x >= this.offset.x &&
  132. x < this.offset.x + element.offsetWidth);
  133. },
  134. /** @id MochiKit.Position.withinIncludingScrolloffsets */
  135. withinIncludingScrolloffsets: function (element, x, y) {
  136. var offsetcache = this.realOffset(element);
  137. this.xcomp = x + offsetcache.x - this.windowOffset.x;
  138. this.ycomp = y + offsetcache.y - this.windowOffset.y;
  139. this.offset = this.cumulativeOffset(element);
  140. return (this.ycomp >= this.offset.y &&
  141. this.ycomp < this.offset.y + element.offsetHeight &&
  142. this.xcomp >= this.offset.x &&
  143. this.xcomp < this.offset.x + element.offsetWidth);
  144. },
  145. // within must be called directly before
  146. /** @id MochiKit.Position.overlap */
  147. overlap: function (mode, element) {
  148. if (!mode) {
  149. return 0;
  150. }
  151. if (mode == 'vertical') {
  152. return ((this.offset.y + element.offsetHeight) - this.ycomp) /
  153. element.offsetHeight;
  154. }
  155. if (mode == 'horizontal') {
  156. return ((this.offset.x + element.offsetWidth) - this.xcomp) /
  157. element.offsetWidth;
  158. }
  159. },
  160. /** @id MochiKit.Position.absolutize */
  161. absolutize: function (element) {
  162. element = MochiKit.DOM.getElement(element);
  163. if (element.style.position == 'absolute') {
  164. return;
  165. }
  166. MochiKit.Position.prepare();
  167. var offsets = MochiKit.Position.positionedOffset(element);
  168. var width = element.clientWidth;
  169. var height = element.clientHeight;
  170. var oldStyle = {
  171. 'position': element.style.position,
  172. 'left': offsets.x - parseFloat(element.style.left || 0),
  173. 'top': offsets.y - parseFloat(element.style.top || 0),
  174. 'width': element.style.width,
  175. 'height': element.style.height
  176. };
  177. element.style.position = 'absolute';
  178. element.style.top = offsets.y + 'px';
  179. element.style.left = offsets.x + 'px';
  180. element.style.width = width + 'px';
  181. element.style.height = height + 'px';
  182. return oldStyle;
  183. },
  184. /** @id MochiKit.Position.positionedOffset */
  185. positionedOffset: function (element) {
  186. var valueT = 0, valueL = 0;
  187. do {
  188. valueT += element.offsetTop || 0;
  189. valueL += element.offsetLeft || 0;
  190. element = element.offsetParent;
  191. if (element) {
  192. p = MochiKit.Style.getStyle(element, 'position');
  193. if (p == 'relative' || p == 'absolute') {
  194. break;
  195. }
  196. }
  197. } while (element);
  198. return new MochiKit.Style.Coordinates(valueL, valueT);
  199. },
  200. /** @id MochiKit.Position.relativize */
  201. relativize: function (element, oldPos) {
  202. element = MochiKit.DOM.getElement(element);
  203. if (element.style.position == 'relative') {
  204. return;
  205. }
  206. MochiKit.Position.prepare();
  207. var top = parseFloat(element.style.top || 0) -
  208. (oldPos['top'] || 0);
  209. var left = parseFloat(element.style.left || 0) -
  210. (oldPos['left'] || 0);
  211. element.style.position = oldPos['position'];
  212. element.style.top = top + 'px';
  213. element.style.left = left + 'px';
  214. element.style.width = oldPos['width'];
  215. element.style.height = oldPos['height'];
  216. },
  217. /** @id MochiKit.Position.clone */
  218. clone: function (source, target) {
  219. source = MochiKit.DOM.getElement(source);
  220. target = MochiKit.DOM.getElement(target);
  221. target.style.position = 'absolute';
  222. var offsets = this.cumulativeOffset(source);
  223. target.style.top = offsets.y + 'px';
  224. target.style.left = offsets.x + 'px';
  225. target.style.width = source.offsetWidth + 'px';
  226. target.style.height = source.offsetHeight + 'px';
  227. },
  228. /** @id MochiKit.Position.page */
  229. page: function (forElement) {
  230. var valueT = 0;
  231. var valueL = 0;
  232. var element = forElement;
  233. do {
  234. valueT += element.offsetTop || 0;
  235. valueL += element.offsetLeft || 0;
  236. // Safari fix
  237. if (element.offsetParent == document.body && MochiKit.Style.getStyle(element, 'position') == 'absolute') {
  238. break;
  239. }
  240. } while (element = element.offsetParent);
  241. element = forElement;
  242. do {
  243. valueT -= element.scrollTop || 0;
  244. valueL -= element.scrollLeft || 0;
  245. } while (element = element.parentNode);
  246. return new MochiKit.Style.Coordinates(valueL, valueT);
  247. }
  248. };