PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/draft/examples/widget-demo/api.widget.wwwroot/static/lib/o2.js/o2.dom.dimension.js

https://github.com/v0lkan/o2.js
JavaScript | 477 lines | 168 code | 61 blank | 248 comment | 24 complexity | d389b8880d204c7e298941c6d59613d0 MD5 | raw file
  1. /**
  2. * @module dom.dimension
  3. * @requires core
  4. * @requires dom.style
  5. * @requires string.core
  6. *
  7. * <!--
  8. * This program is distributed under
  9. * the terms of the MIT license.
  10. * Please see the LICENSE file for details.
  11. *
  12. * lastModified: 2012-06-02 22:47:21.699341
  13. * -->
  14. *
  15. * <p>Includes dimension (<strong>i.e. width-height related</strong>) helper
  16. * methods.</p>
  17. */
  18. (function(framework, window, document, UNDEFINED) {
  19. 'use strict';
  20. var _ = framework.protecteds;
  21. var attr = _.getAttr;
  22. var create = attr(_, 'create');
  23. var def = attr(_, 'define');
  24. var require = attr(_, 'require');
  25. var exports = {};
  26. /*
  27. * Module Name
  28. */
  29. var kModuleName = 'Dom';
  30. /*
  31. * Dom (dimension)
  32. */
  33. var me = create(kModuleName);
  34. /*
  35. * Aliases
  36. */
  37. var $ = require('$');
  38. var concat = require('String', 'concat');
  39. var setStyle = require(kModuleName, 'setStyle');
  40. var self = attr(window, 'self');
  41. /*
  42. * Common Constants
  43. */
  44. var kHeight = 'height';
  45. var kModernCss = 'CSS1Compat';
  46. var kPixel = 'px';
  47. var kWidth = 'width';
  48. /*
  49. *
  50. */
  51. var getDocumentElement = function() {
  52. // document.body can be null when refreshing.
  53. if (!document || !document.body) {
  54. return null;
  55. }
  56. var result = (document.documentElement &&
  57. document.compatMode === kModernCss
  58. ) ? document.documentElement : document.body;
  59. getDocumentElement = function() {
  60. return result;
  61. };
  62. return result;
  63. };
  64. /**
  65. * @function {static} o2.Dom.getDimension
  66. *
  67. * <p>Gets the dimension of the given element in the form
  68. * <code>{width: w, height: h}</code>, where <strong>w</strong> and
  69. * <strong>h</strong> are in pixels.
  70. *
  71. * <p><strong>Usage example:</strong></p>
  72. *
  73. * <pre>
  74. * var dimensions = o2.Dom.getDimension('container');
  75. * </pre>
  76. *
  77. * @param {Object} obj - the <strong>DOMNode</strong> to get the dimension
  78. * of, or the <code>String</code> <strong>id</strong> of it.
  79. *
  80. * @return the dimension of the <strong>DOMNode</strong> in the form
  81. * <code>{width: w, height: h}</code>.
  82. */
  83. exports.getDimension = def(me, 'getDimension', function(obj) {
  84. obj = $(obj);
  85. if (!obj || obj.offsetWidth === UNDEFINED) {
  86. return {width : 0, height : 0};
  87. }
  88. return {
  89. width : obj.offsetWidth,
  90. height : obj.offsetHeight
  91. };
  92. });
  93. /*
  94. *
  95. */
  96. var getDimension = require(kModuleName, 'getDimension');
  97. /**
  98. * @function {static} o2.Dom.getDocumentDimension
  99. *
  100. * <p>Gets the dimension of the document in the form <code>{width: w,
  101. * height: h}</code>. If the visible (i.e. <code>clientHeight</code>) is
  102. * greater than the document's height returns the height of the visible
  103. * area as the height portion.
  104. *
  105. * <p><strong>Usage example:</strong></p>
  106. *
  107. * <pre>
  108. * var viewportInfo = o2.Dom.getDocumentDimension();
  109. * </pre>
  110. *
  111. * @return the dimension of the document in the form <code>{width: w,
  112. * height: h}</code>.
  113. */
  114. exports.getDocumentDimension = def(me, 'getDocumentDimension', function() {
  115. var doc = getDocumentElement();
  116. if(!doc) {
  117. return {width : 0, height : 0};
  118. }
  119. return {
  120. width : Math.max(
  121. doc.scrollHeight,
  122. doc.offsetHeight,
  123. doc.clientHeight
  124. ),
  125. height : Math.max(
  126. doc.scrollWidth,
  127. doc.offsetWidth,
  128. doc.clientWidth
  129. )
  130. };
  131. });
  132. /*
  133. *
  134. */
  135. var getDocumentDimension = require(kModuleName, 'getDocumentDimension');
  136. /**
  137. * @function {static} o2.Dom.getDocumentHeight
  138. *
  139. * <p>Gets the total height of the document in pixels.</p>
  140. *
  141. * <p><strong>Usage example:</strong></p>
  142. *
  143. * <pre>
  144. * var viewportHeight = o2.Dom.getDocumentHeight();
  145. * </pre>
  146. *
  147. * @return the document's height.
  148. */
  149. exports.getDocumentHeight = def(me, 'getDocumentHeight', function() {
  150. return getDocumentDimension().height;
  151. });
  152. /**
  153. * @function {static} o2.Dom.getDocumentWidth
  154. *
  155. * <p>Gets the total width of the document in pixels.</p>
  156. *
  157. * <p><strong>Usage example:</strong></p>
  158. *
  159. * <pre>
  160. * var viewportWidth = o2.Dom.getDocumentWidth();
  161. * </pre>
  162. *
  163. * @return the document's width.
  164. */
  165. exports.getDocumentWidth = def(me, 'getDocumentWidth', function() {
  166. return getDocumentDimension().width;
  167. });
  168. /**
  169. * @function {static} o2.Dom.getHeight
  170. *
  171. * <p>Gets the <strong>height</strong> of the given element, in pixels.</p>
  172. *
  173. * <p><strong>Usage example:</strong></p>
  174. *
  175. * <pre>
  176. * var containerHeight = o2.Dom.getHeight('container');
  177. * </pre>
  178. *
  179. * @param {Object} obj - the <strong>DOMNode</strong> to get the dimension
  180. * of, or the <code>String</code> <strong>id</strong> of it.
  181. *
  182. * @return the height of the element, in pixels.
  183. */
  184. exports.getHeight = def(me, 'getHeight', function(obj) {
  185. return getDimension(obj).height;
  186. });
  187. /**
  188. * @function {static} o2.Dom.getViewportInfo
  189. *
  190. * <p>Gets the viewport information in the form
  191. * <code>{scrollTop : #, scrollLeft: #, width: #, height: #}</code>.</p>
  192. *
  193. * <p><strong>Usage example:</strong></p>
  194. *
  195. * <pre>
  196. * var details = o2.Dom.getViewportInfo();
  197. * </pre>
  198. *
  199. * @return the viewport information.
  200. */
  201. exports.getViewportInfo = def(me, 'getViewportInfo', function() {
  202. var d = getDocumentElement();
  203. if (!d) {
  204. return {
  205. scrollTop : 0,
  206. scrollLeft : 0,
  207. width : 0,
  208. height : 0
  209. };
  210. }
  211. return {
  212. scrollTop : d.scrollTop,
  213. scrollLeft : d.scrollLeft,
  214. width : self.innerWidth || d.clientWidth,
  215. height : self.innerHeight || d.clientHeight
  216. };
  217. });
  218. /**
  219. * @function {static} o2.Dom.getWidth
  220. *
  221. * <p>Gets the <strong>width</strong> of the given element, in pixels.</p>
  222. *
  223. * @param {Object} obj - the <strong>DOMNode</strong> to get the dimension
  224. * of, or the <code>String</code> <strong>id</strong> of it.
  225. *
  226. * <p><strong>Usage example:</strong></p>
  227. *
  228. * <pre>
  229. * var elementWidth = o2.Dom.getWidth('container');
  230. * </pre>
  231. *
  232. * @return the width of the element, in pixels.
  233. */
  234. exports.getWidth = def(me, 'getWidth', function(obj) {
  235. return getDimension(obj).width;
  236. });
  237. if (window.innerWidth !== UNDEFINED) {
  238. /**
  239. * @function {static} o2.Dom.getWindowInnerDimension
  240. *
  241. * <p>Gets the dimension of the visible area of the browser in the form
  242. * <code>{width: w, height: h}</code>.
  243. *
  244. * <p><strong>Usage example:</strong></p>
  245. *
  246. * <pre>
  247. * var windowDimensions = o2.Dom.getWindowInnerDimension();
  248. * </pre>
  249. *
  250. * @return the dimension of the visible area of the browser in the form
  251. * <code>{width: w, height: h}</code>.
  252. */
  253. exports.getWindowInnerDimension = def(me, 'getWindowInnerDimension',
  254. function() {
  255. return {
  256. width : window.innerWidth || 0,
  257. height : window.innerHeight || 0
  258. };
  259. });
  260. } else {
  261. exports.getWindowInnerDimension = def(me, 'getWindowInnerDimension',
  262. function() {
  263. var doc = getDocumentElement();
  264. if (!doc) {
  265. return {width : 0, height : 0};
  266. }
  267. return {
  268. width : doc.clientWidth || 0,
  269. height : doc.clientHeight || 0
  270. };
  271. });
  272. }
  273. /*
  274. *
  275. */
  276. var getWindowInnerDimension = require(kModuleName, 'getWindowInnerDimension');
  277. /**
  278. * @function {static} o2.Dom.getWindowInnerHeight
  279. *
  280. * <p>Gets the inner height of the visible area.</p>
  281. *
  282. * <p><strong>Usage example:</strong></p>
  283. *
  284. * <pre>
  285. * var innerHeight = o2.Dom.getWindow.innerHeight();
  286. * </pre>
  287. *
  288. * @return the inner height of the window in pixels.
  289. */
  290. exports.getWindowInnerHeight = def(me, 'getWindowInnerHeight', function() {
  291. return getWindowInnerDimension().height;
  292. });
  293. /**
  294. * @function {static} o2.Dom.getWindowInnerWidth
  295. *
  296. * <p>Gets the inner width of the visible area.</p>
  297. *
  298. * <p><strong>Usage example:</strong></p>
  299. *
  300. * <pre>
  301. * var innerWidth = o2.Dom.getWindowInnerWidth();
  302. * </pre>
  303. *
  304. * @return the inner width of the window in pixels.
  305. */
  306. exports.getWindowInnerWidth = def(me, 'getWindowInnerWidth', function() {
  307. return getWindowInnerDimension().width;
  308. });
  309. /**
  310. * @function {static} o2.Dom.setWidth
  311. *
  312. * <p>Sets the <strong>width</strong> of the given element.</p>
  313. *
  314. * <p><strong>Usage example:</strong></p>
  315. *
  316. * <pre>
  317. * o2.Dom.setWidth('container', 500);
  318. * </pre>
  319. *
  320. * @param {Object} obj - the <strong>DOMNode</strong> to get the dimension
  321. * of, or the <code>String</code> <strong>id</strong> of it.
  322. * @param {Integer} width - the new width in pixels.
  323. */
  324. exports.setWidth = def(me, 'setWidth', function(obj, width) {
  325. obj = $(obj);
  326. if (!obj) {
  327. return;
  328. }
  329. var difference = 0;
  330. var cssWidth = 0;
  331. // IE (as always) doesn't play nice with the box model.
  332. // The calculation below takes care of that.
  333. // Also note that since offsetWidth is a read-only property
  334. // we can only change the element's width through it's style
  335. // collection.
  336. if (obj.offsetWidth !== UNDEFINED) {
  337. setStyle(obj, kWidth, concat(width, kPixel));
  338. difference = obj.offsetWidth - width;
  339. }
  340. if (isNaN(difference)) {
  341. difference = 0;
  342. }
  343. cssWidth = width - difference;
  344. if (cssWidth <= 0) {
  345. return;
  346. }
  347. setStyle(obj, kWidth, concat(width, kPixel));
  348. });
  349. /*
  350. *
  351. */
  352. var setWidth = require(kModuleName, 'setWidth');
  353. /**
  354. * @function {static} o2.Dom.setHeight
  355. *
  356. * <p>Sets the <strong>height</strong> of the given element.</p>
  357. *
  358. * <p><strong>Usage example:</strong></p>
  359. *
  360. * <pre>
  361. * o2.Dom.setHeight('container', 300);
  362. * </pre>
  363. *
  364. * @param {Object} obj - the <strong>DOMNode</strong> to get the dimension
  365. * of, or the <code>String</code> <strong>id</strong> of it.
  366. * @param {Integer} height - the new height in pixels.
  367. */
  368. exports.setHeight = def(me, 'setHeight', function(obj, height) {
  369. obj = $(obj);
  370. if (!obj) {
  371. return;
  372. }
  373. var difference = 0;
  374. var cssHeight = 0;
  375. if (obj.offsetWidth !== UNDEFINED) {
  376. setStyle(obj, kHeight, concat(height, kPixel));
  377. difference = obj.offsetHeight - height;
  378. }
  379. if (isNaN(difference)) {
  380. difference = 0;
  381. }
  382. cssHeight = height - difference;
  383. if (cssHeight <= 0) {
  384. return;
  385. }
  386. setStyle(obj, kHeight, concat(height, kPixel));
  387. });
  388. /*
  389. *
  390. */
  391. var setHeight = require(kModuleName, 'setHeight');
  392. /**
  393. * @function {static} o2.Dom.setDimension
  394. *
  395. * <p>Sets the dimension of the given element.</p>
  396. *
  397. * <p><strong>Usage example:</strong></p>
  398. *
  399. * <pre>
  400. * o2.Dom.setDimension('container', {width: 400, height: 200});
  401. * </pre>
  402. *
  403. * @param {Object} obj - the <strong>DOMNode</strong> to get the dimension
  404. * of, or the <code>String</code> <strong>id</strong> of it.
  405. * @param {Object} dimension - the new dimension in the form
  406. * <code>{width: w, height: h}</code>.
  407. */
  408. exports.setDimension = def(me, 'setDimension', function(obj, dimension) {
  409. obj = $(obj);
  410. if (!obj) {
  411. return;
  412. }
  413. setWidth(obj, dimension.width);
  414. setHeight(obj, dimension.height);
  415. });
  416. }(this.o2, this, this.document));