dom.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. if (typeof document == "undefined")
  33. return;
  34. var XHTML_NS = "http://www.w3.org/1999/xhtml";
  35. exports.getDocumentHead = function(doc) {
  36. if (!doc)
  37. doc = document;
  38. return doc.head || doc.getElementsByTagName("head")[0] || doc.documentElement;
  39. }
  40. exports.createElement = function(tag, ns) {
  41. return document.createElementNS ?
  42. document.createElementNS(ns || XHTML_NS, tag) :
  43. document.createElement(tag);
  44. };
  45. exports.hasCssClass = function(el, name) {
  46. var classes = el.className.split(/\s+/g);
  47. return classes.indexOf(name) !== -1;
  48. };
  49. /*
  50. * Add a CSS class to the list of classes on the given node
  51. */
  52. exports.addCssClass = function(el, name) {
  53. if (!exports.hasCssClass(el, name)) {
  54. el.className += " " + name;
  55. }
  56. };
  57. /*
  58. * Remove a CSS class from the list of classes on the given node
  59. */
  60. exports.removeCssClass = function(el, name) {
  61. var classes = el.className.split(/\s+/g);
  62. while (true) {
  63. var index = classes.indexOf(name);
  64. if (index == -1) {
  65. break;
  66. }
  67. classes.splice(index, 1);
  68. }
  69. el.className = classes.join(" ");
  70. };
  71. exports.toggleCssClass = function(el, name) {
  72. var classes = el.className.split(/\s+/g), add = true;
  73. while (true) {
  74. var index = classes.indexOf(name);
  75. if (index == -1) {
  76. break;
  77. }
  78. add = false;
  79. classes.splice(index, 1);
  80. }
  81. if(add)
  82. classes.push(name);
  83. el.className = classes.join(" ");
  84. return add;
  85. };
  86. /*
  87. * Add or remove a CSS class from the list of classes on the given node
  88. * depending on the value of <tt>include</tt>
  89. */
  90. exports.setCssClass = function(node, className, include) {
  91. if (include) {
  92. exports.addCssClass(node, className);
  93. } else {
  94. exports.removeCssClass(node, className);
  95. }
  96. };
  97. exports.hasCssString = function(id, doc) {
  98. var index = 0, sheets;
  99. doc = doc || document;
  100. if (doc.createStyleSheet && (sheets = doc.styleSheets)) {
  101. while (index < sheets.length)
  102. if (sheets[index++].owningElement.id === id) return true;
  103. } else if ((sheets = doc.getElementsByTagName("style"))) {
  104. while (index < sheets.length)
  105. if (sheets[index++].id === id) return true;
  106. }
  107. return false;
  108. };
  109. exports.importCssString = function importCssString(cssText, id, doc) {
  110. doc = doc || document;
  111. // If style is already imported return immediately.
  112. if (id && exports.hasCssString(id, doc))
  113. return null;
  114. var style;
  115. if (doc.createStyleSheet) {
  116. style = doc.createStyleSheet();
  117. style.cssText = cssText;
  118. if (id)
  119. style.owningElement.id = id;
  120. } else {
  121. style = doc.createElementNS
  122. ? doc.createElementNS(XHTML_NS, "style")
  123. : doc.createElement("style");
  124. style.appendChild(doc.createTextNode(cssText));
  125. if (id)
  126. style.id = id;
  127. exports.getDocumentHead(doc).appendChild(style);
  128. }
  129. };
  130. exports.importCssStylsheet = function(uri, doc) {
  131. if (doc.createStyleSheet) {
  132. doc.createStyleSheet(uri);
  133. } else {
  134. var link = exports.createElement('link');
  135. link.rel = 'stylesheet';
  136. link.href = uri;
  137. exports.getDocumentHead(doc).appendChild(link);
  138. }
  139. };
  140. exports.getInnerWidth = function(element) {
  141. return (
  142. parseInt(exports.computedStyle(element, "paddingLeft"), 10) +
  143. parseInt(exports.computedStyle(element, "paddingRight"), 10) +
  144. element.clientWidth
  145. );
  146. };
  147. exports.getInnerHeight = function(element) {
  148. return (
  149. parseInt(exports.computedStyle(element, "paddingTop"), 10) +
  150. parseInt(exports.computedStyle(element, "paddingBottom"), 10) +
  151. element.clientHeight
  152. );
  153. };
  154. if (window.pageYOffset !== undefined) {
  155. exports.getPageScrollTop = function() {
  156. return window.pageYOffset;
  157. };
  158. exports.getPageScrollLeft = function() {
  159. return window.pageXOffset;
  160. };
  161. }
  162. else {
  163. exports.getPageScrollTop = function() {
  164. return document.body.scrollTop;
  165. };
  166. exports.getPageScrollLeft = function() {
  167. return document.body.scrollLeft;
  168. };
  169. }
  170. if (window.getComputedStyle)
  171. exports.computedStyle = function(element, style) {
  172. if (style)
  173. return (window.getComputedStyle(element, "") || {})[style] || "";
  174. return window.getComputedStyle(element, "") || {};
  175. };
  176. else
  177. exports.computedStyle = function(element, style) {
  178. if (style)
  179. return element.currentStyle[style];
  180. return element.currentStyle;
  181. };
  182. exports.scrollbarWidth = function(document) {
  183. var inner = exports.createElement("ace_inner");
  184. inner.style.width = "100%";
  185. inner.style.minWidth = "0px";
  186. inner.style.height = "200px";
  187. inner.style.display = "block";
  188. var outer = exports.createElement("ace_outer");
  189. var style = outer.style;
  190. style.position = "absolute";
  191. style.left = "-10000px";
  192. style.overflow = "hidden";
  193. style.width = "200px";
  194. style.minWidth = "0px";
  195. style.height = "150px";
  196. style.display = "block";
  197. outer.appendChild(inner);
  198. var body = document.documentElement;
  199. body.appendChild(outer);
  200. var noScrollbar = inner.offsetWidth;
  201. style.overflow = "scroll";
  202. var withScrollbar = inner.offsetWidth;
  203. if (noScrollbar == withScrollbar) {
  204. withScrollbar = outer.clientWidth;
  205. }
  206. body.removeChild(outer);
  207. return noScrollbar-withScrollbar;
  208. };
  209. /*
  210. * Optimized set innerHTML. This is faster than plain innerHTML if the element
  211. * already contains a lot of child elements.
  212. *
  213. * See http://blog.stevenlevithan.com/archives/faster-than-innerhtml for details
  214. */
  215. exports.setInnerHtml = function(el, innerHtml) {
  216. var element = el.cloneNode(false);//document.createElement("div");
  217. element.innerHTML = innerHtml;
  218. el.parentNode.replaceChild(element, el);
  219. return element;
  220. };
  221. if ("textContent" in document.documentElement) {
  222. exports.setInnerText = function(el, innerText) {
  223. el.textContent = innerText;
  224. };
  225. exports.getInnerText = function(el) {
  226. return el.textContent;
  227. };
  228. }
  229. else {
  230. exports.setInnerText = function(el, innerText) {
  231. el.innerText = innerText;
  232. };
  233. exports.getInnerText = function(el) {
  234. return el.innerText;
  235. };
  236. }
  237. exports.getParentWindow = function(document) {
  238. return document.defaultView || document.parentWindow;
  239. };
  240. });