Style.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /***
  2. MochiKit.Style 1.4
  3. See <http://mochikit.com/> for documentation, downloads, license, etc.
  4. (c) 2005-2006 Bob Ippolito, Beau Hartshorne. All rights Reserved.
  5. ***/
  6. if (typeof(dojo) != 'undefined') {
  7. dojo.provide('MochiKit.Style');
  8. dojo.require('MochiKit.Base');
  9. dojo.require('MochiKit.DOM');
  10. }
  11. if (typeof(JSAN) != 'undefined') {
  12. JSAN.use('MochiKit.Base', []);
  13. }
  14. try {
  15. if (typeof(MochiKit.Base) == 'undefined') {
  16. throw '';
  17. }
  18. } catch (e) {
  19. throw 'MochiKit.Style depends on MochiKit.Base!';
  20. }
  21. try {
  22. if (typeof(MochiKit.DOM) == 'undefined') {
  23. throw '';
  24. }
  25. } catch (e) {
  26. throw 'MochiKit.Style depends on MochiKit.DOM!';
  27. }
  28. if (typeof(MochiKit.Style) == 'undefined') {
  29. MochiKit.Style = {};
  30. }
  31. MochiKit.Style.NAME = 'MochiKit.Style';
  32. MochiKit.Style.VERSION = '1.4';
  33. MochiKit.Style.__repr__ = function () {
  34. return '[' + this.NAME + ' ' + this.VERSION + ']';
  35. };
  36. MochiKit.Style.toString = function () {
  37. return this.__repr__();
  38. };
  39. MochiKit.Style.EXPORT_OK = [];
  40. MochiKit.Style.EXPORT = [
  41. 'setOpacity',
  42. 'computedStyle',
  43. 'getElementDimensions',
  44. 'elementDimensions', // deprecated
  45. 'setElementDimensions',
  46. 'getElementPosition',
  47. 'elementPosition', // deprecated
  48. 'setElementPosition',
  49. 'setDisplayForElement',
  50. 'hideElement',
  51. 'showElement',
  52. 'getViewportDimensions',
  53. 'Dimensions',
  54. 'Coordinates'
  55. ];
  56. /*
  57. Dimensions
  58. */
  59. MochiKit.Style.Dimensions = function (w, h) {
  60. this.w = w;
  61. this.h = h;
  62. };
  63. MochiKit.Style.Dimensions.prototype.__repr__ = function () {
  64. var repr = MochiKit.Base.repr;
  65. return '{w: ' + repr(this.w) + ', h: ' + repr(this.h) + '}';
  66. };
  67. MochiKit.Style.Dimensions.prototype.toString = function () {
  68. return this.__repr__();
  69. };
  70. /*
  71. Coordinates
  72. */
  73. MochiKit.Style.Coordinates = function (x, y) {
  74. this.x = x;
  75. this.y = y;
  76. };
  77. MochiKit.Style.Coordinates.prototype.__repr__ = function () {
  78. var repr = MochiKit.Base.repr;
  79. return '{x: ' + repr(this.x) + ', y: ' + repr(this.y) + '}';
  80. };
  81. MochiKit.Style.Coordinates.prototype.toString = function () {
  82. return this.__repr__();
  83. };
  84. MochiKit.Base.update(MochiKit.Style, {
  85. computedStyle: function (elem, cssProperty) {
  86. var dom = MochiKit.DOM;
  87. var d = dom._document;
  88. elem = dom.getElement(elem);
  89. cssProperty = MochiKit.Base.camelize(cssProperty);
  90. if (!elem || elem == d) {
  91. return undefined;
  92. }
  93. /* from YUI 0.10.0 */
  94. if (cssProperty == 'opacity' && elem.filters) { // IE opacity
  95. try {
  96. return elem.filters.item('DXImageTransform.Microsoft.Alpha'
  97. ).opacity / 100;
  98. } catch(e) {
  99. try {
  100. return elem.filters.item('alpha').opacity / 100;
  101. } catch(e) {}
  102. }
  103. }
  104. if (elem.currentStyle) {
  105. return elem.currentStyle[cssProperty];
  106. }
  107. if (typeof(d.defaultView) == 'undefined') {
  108. return undefined;
  109. }
  110. if (d.defaultView === null) {
  111. return undefined;
  112. }
  113. var style = d.defaultView.getComputedStyle(elem, null);
  114. if (typeof(style) == 'undefined' || style === null) {
  115. return undefined;
  116. }
  117. var selectorCase = cssProperty.replace(/([A-Z])/g, '-$1'
  118. ).toLowerCase(); // from dojo.style.toSelectorCase
  119. return style.getPropertyValue(selectorCase);
  120. },
  121. setOpacity: function(elem, o) {
  122. elem = MochiKit.DOM.getElement(elem);
  123. MochiKit.DOM.updateNodeAttributes(elem, {'style': {
  124. 'opacity': o,
  125. '-moz-opacity': o,
  126. '-khtml-opacity': o,
  127. 'filter':' alpha(opacity=' + (o * 100) + ')'
  128. }});
  129. },
  130. /*
  131. getElementPosition is adapted from YAHOO.util.Dom.getXY v0.9.0.
  132. Copyright: Copyright (c) 2006, Yahoo! Inc. All rights reserved.
  133. License: BSD, http://developer.yahoo.net/yui/license.txt
  134. */
  135. getElementPosition: function (elem, /* optional */relativeTo) {
  136. var self = MochiKit.Style;
  137. var dom = MochiKit.DOM;
  138. elem = dom.getElement(elem);
  139. if (!elem ||
  140. (!(elem.x && elem.y) &&
  141. (!elem.parentNode == null ||
  142. self.computedStyle(elem, 'display') == 'none'))) {
  143. return undefined;
  144. }
  145. var c = new self.Coordinates(0, 0);
  146. var box = null;
  147. var parent = null;
  148. var d = MochiKit.DOM._document;
  149. var de = d.documentElement;
  150. var b = d.body;
  151. if (!elem.parentNode && elem.x && elem.y) {
  152. /* it's just a MochiKit.Style.Coordinates object */
  153. c.x += elem.x || 0;
  154. c.y += elem.y || 0;
  155. } else if (elem.getBoundingClientRect) { // IE shortcut
  156. /*
  157. The IE shortcut can be off by two. We fix it. See:
  158. http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/getboundingclientrect.asp
  159. This is similar to the method used in
  160. MochiKit.Signal.Event.mouse().
  161. */
  162. box = elem.getBoundingClientRect();
  163. c.x += box.left +
  164. (de.scrollLeft || b.scrollLeft) -
  165. (de.clientLeft || 0);
  166. c.y += box.top +
  167. (de.scrollTop || b.scrollTop) -
  168. (de.clientTop || 0);
  169. } else if (elem.offsetParent) {
  170. c.x += elem.offsetLeft;
  171. c.y += elem.offsetTop;
  172. parent = elem.offsetParent;
  173. if (parent != elem) {
  174. while (parent) {
  175. c.x += parent.offsetLeft;
  176. c.y += parent.offsetTop;
  177. parent = parent.offsetParent;
  178. }
  179. }
  180. /*
  181. Opera < 9 and old Safari (absolute) incorrectly account for
  182. body offsetTop and offsetLeft.
  183. */
  184. var ua = navigator.userAgent.toLowerCase();
  185. if ((typeof(opera) != 'undefined' &&
  186. parseFloat(opera.version()) < 9) ||
  187. (ua.indexOf('safari') != -1 &&
  188. self.computedStyle(elem, 'position') == 'absolute')) {
  189. c.x -= b.offsetLeft;
  190. c.y -= b.offsetTop;
  191. }
  192. }
  193. if (typeof(relativeTo) != 'undefined') {
  194. relativeTo = arguments.callee(relativeTo);
  195. if (relativeTo) {
  196. c.x -= (relativeTo.x || 0);
  197. c.y -= (relativeTo.y || 0);
  198. }
  199. }
  200. if (elem.parentNode) {
  201. parent = elem.parentNode;
  202. } else {
  203. parent = null;
  204. }
  205. while (parent && parent.tagName != 'BODY' &&
  206. parent.tagName != 'HTML') {
  207. c.x -= parent.scrollLeft;
  208. c.y -= parent.scrollTop;
  209. if (parent.parentNode) {
  210. parent = parent.parentNode;
  211. } else {
  212. parent = null;
  213. }
  214. }
  215. return c;
  216. },
  217. setElementPosition: function (elem, newPos/* optional */, units) {
  218. elem = MochiKit.DOM.getElement(elem);
  219. if (typeof(units) == 'undefined') {
  220. units = 'px';
  221. }
  222. MochiKit.DOM.updateNodeAttributes(elem, {'style': {
  223. 'left': newPos.x + units,
  224. 'top': newPos.y + units
  225. }});
  226. },
  227. getElementDimensions: function (elem) {
  228. var self = MochiKit.Style;
  229. var dom = MochiKit.DOM;
  230. if (typeof(elem.w) == 'number' || typeof(elem.h) == 'number') {
  231. return new self.Dimensions(elem.w || 0, elem.h || 0);
  232. }
  233. elem = dom.getElement(elem);
  234. if (!elem) {
  235. return undefined;
  236. }
  237. if (self.computedStyle(elem, 'display') != 'none') {
  238. return new self.Dimensions(elem.offsetWidth || 0,
  239. elem.offsetHeight || 0);
  240. }
  241. var s = elem.style;
  242. var originalVisibility = s.visibility;
  243. var originalPosition = s.position;
  244. s.visibility = 'hidden';
  245. s.position = 'absolute';
  246. s.display = '';
  247. var originalWidth = elem.offsetWidth;
  248. var originalHeight = elem.offsetHeight;
  249. s.display = 'none';
  250. s.position = originalPosition;
  251. s.visibility = originalVisibility;
  252. return new self.Dimensions(originalWidth, originalHeight);
  253. },
  254. setElementDimensions: function (elem, newSize/* optional */, units) {
  255. elem = MochiKit.DOM.getElement(elem);
  256. if (typeof(units) == 'undefined') {
  257. units = 'px';
  258. }
  259. MochiKit.DOM.updateNodeAttributes(elem, {'style': {
  260. 'width': newSize.w + units,
  261. 'height': newSize.h + units
  262. }});
  263. },
  264. setDisplayForElement: function (display, element/*, ...*/) {
  265. var elements = MochiKit.Base.extend(null, arguments, 1);
  266. var getElement = MochiKit.DOM.getElement;
  267. for (var i = 0; i < elements.length; i++) {
  268. var element = getElement(elements[i]);
  269. if (element) {
  270. element.style.display = display;
  271. }
  272. }
  273. },
  274. getViewportDimensions: function() {
  275. var d = new MochiKit.Style.Dimensions();
  276. var w = MochiKit.DOM._window;
  277. var b = MochiKit.DOM._document.body;
  278. if (w.innerWidth) {
  279. d.w = w.innerWidth;
  280. d.h = w.innerHeight;
  281. } else if (b.parentElement.clientWidth) {
  282. d.w = b.parentElement.clientWidth;
  283. d.h = b.parentElement.clientHeight;
  284. } else if (b && b.clientWidth) {
  285. d.w = b.clientWidth;
  286. d.h = b.clientHeight;
  287. }
  288. return d;
  289. },
  290. __new__: function () {
  291. var m = MochiKit.Base;
  292. this.elementPosition = this.getElementPosition;
  293. this.elementDimensions = this.getElementDimensions;
  294. this.hideElement = m.partial(this.setDisplayForElement, 'none');
  295. this.showElement = m.partial(this.setDisplayForElement, 'block');
  296. this.EXPORT_TAGS = {
  297. ':common': this.EXPORT,
  298. ':all': m.concat(this.EXPORT, this.EXPORT_OK)
  299. };
  300. m.nameFunctions(this);
  301. }
  302. });
  303. MochiKit.Style.__new__();
  304. MochiKit.Base._exportSymbols(this, MochiKit.Style);