core.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: cpscrn
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2011
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. //
  9. //
  10. // Copyright (C) 2009 Cognos ULC, an IBM Company. All rights reserved.
  11. // Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  12. var xUA = navigator.userAgent.toLowerCase();
  13. var xIE4Up = xUA.indexOf('msie') != -1; // we don't support IE4 anymore so don't check for version
  14. // Functions from xDrag
  15. var xDragElementSource = null;
  16. function xDraggableElement(id, draggable, dragStart, drag, dragEnd) {
  17. var ele = dojo.byId(id);
  18. ele.draggable = draggable;
  19. ele.dragStart = dragStart;
  20. ele.drag = drag;
  21. ele.dragEnd = dragEnd;
  22. return ele;
  23. }
  24. function xDisableDrag(id, last) {
  25. if (!xDragElementSource) {
  26. return;
  27. }
  28. var ele = new xDraggableElement(id, false, null, null, null);
  29. xRemoveEventListener(ele, 'mousedown', xDragMouseDown);
  30. xRemoveEventListener(document, 'mousemove', xDragMouseMove);
  31. }
  32. function xEnableDrag(id, dragStart, drag, dragEnd) {
  33. var ele = new xDraggableElement(id, true, dragStart, drag, dragEnd);
  34. xAddEventListener(ele, 'mousedown', xDragMouseDown);
  35. xAddEventListener(document, 'mousemove', xDragMouseMove);
  36. }
  37. function xDragMouseDown(e) {
  38. var evt = dojo.fixEvent(e);
  39. var ele = evt.target;
  40. while (ele && !ele.draggable) {
  41. ele = xParent(ele);
  42. }
  43. if (ele) {
  44. evt.preventDefault();
  45. ele.xDPX = evt.pageX;
  46. ele.xDPY = evt.pageY;
  47. xDragElementSource = ele;
  48. xAddEventListener(document, 'mouseup', xDragMouseUp);
  49. if (ele.dragStart) {
  50. ele.dragStart(ele, evt.pageX, evt.pageY);
  51. }
  52. }
  53. }
  54. function xDragMouseMove(e) {
  55. var evt = dojo.fixEvent(e);
  56. if (xDragElementSource) {
  57. evt.preventDefault();
  58. var dx = evt.pageX - xDragElementSource.xDPX;
  59. var dy = evt.pageY - xDragElementSource.xDPY;
  60. xDragElementSource.xDPX = evt.pageX;
  61. xDragElementSource.xDPY = evt.pageY;
  62. if (xDragElementSource.drag) {
  63. xDragElementSource.drag(xDragElementSource, dx, dy);
  64. } else {
  65. xMoveTo(xDragElementSource, xLeft(xDragElementSource) + dx, xTop(xDragElementSource) + dy);
  66. }
  67. }
  68. }
  69. function xDragMouseUp(e) {
  70. var evt = dojo.fixEvent(e);
  71. if (xDragElementSource) {
  72. evt.preventDefault();
  73. xRemoveEventListener(document, 'mouseup', xDragMouseUp);
  74. if (xDragElementSource.dragEnd) {
  75. xDragElementSource.dragEnd(xDragElementSource, evt.pageX, evt.pageY);
  76. }
  77. xDragElementSource = null;
  78. }
  79. }
  80. //Functions from xEvent
  81. // hashmap of the handles for all registered events.
  82. var handleMap = {};
  83. function xEvent(evt) {
  84. return dojo.fixEvent(evt);
  85. }
  86. function xHandleGenerator(ele, type, listener) {
  87. return ('' + ele.id + type + listener.name).toLowerCase();
  88. }
  89. function xAddEventListener(ele, type, listener, bCapture) {
  90. if (xIE4Up && type == "load") {
  91. var loadHandle = dojo.addOnLoad(listener);
  92. return loadHandle;
  93. }
  94. var handle = dojo.connect(dojo.byId(ele), type, listener);
  95. var handleKey = xHandleGenerator(ele, type, listener);
  96. handleMap[handleKey] = handle;
  97. return handle;
  98. }
  99. function xRemoveEventListener(ele, type, listener, bCapture) {
  100. var handleKey = xHandleGenerator(ele, type, listener);
  101. dojo.disconnect(handleMap[handleKey]);
  102. delete handleMap[handleKey];
  103. }
  104. function xPreventDefault(evt) {
  105. var dxEvt = dojo.fixEvent(evt);
  106. dxEvt.preventDefault();
  107. }
  108. function xStopPropagation(evt) {
  109. var dxEvt = dojo.fixEvent(evt);
  110. dxEvt.stopPropagation();
  111. }
  112. //Functions from xDom
  113. function xGetElementById(id) {
  114. return dojo.byId(id);
  115. }
  116. function xGetElementsByTagName(tag, base) {
  117. return dojo.query(tag, base);
  118. }
  119. function xGetElementsByClassName(tag, base) {
  120. return dojo.query("." + tag, base);
  121. }
  122. function xCreateElement(el) {
  123. if (document.createElement) {
  124. return document.createElement(el);
  125. }
  126. }
  127. function xAppendChild(parentEl, childEl) {
  128. parentEl.appendChild(childEl);
  129. }
  130. function xFirstChild(el, tag) {
  131. var nodeList;
  132. if (tag === undefined) {
  133. nodeList = dojo.query(":first-child", el);
  134. } else {
  135. nodeList = dojo.query(tag + ":first-of-type", el);
  136. }
  137. return (nodeList.length) ? nodeList[0] : null;
  138. }
  139. function xGetElementsByAttribute(tag, attribute, match) {
  140. var nodeList = null;
  141. if (match === undefined) {
  142. nodeList = dojo.query(tag + "[" + attribute + "]");
  143. } else {
  144. nodeList = dojo.query(tag + "[" + attribute + "=" + match + "]");
  145. }
  146. return (nodeList.length && nodeList.length < 1) ? null : nodeList;
  147. }
  148. function xParent(el, returnNode) {
  149. el = dojo.byId(el);
  150. var parentNode;
  151. if (returnNode) {
  152. parentNode = (el && el.parentNode) ? el.parentNode : null;
  153. } else {
  154. parentNode = (el && el.offsetParent) ? el.offsetParent : null;
  155. }
  156. return parentNode;
  157. }
  158. function xParentNode(elem, ancestorNumber) {
  159. elem = dojo.byId(elem);
  160. while (elem && ancestorNumber--) {
  161. elem = elem.parentNode;
  162. }
  163. return elem;
  164. }
  165. function xNextSib(elem, tag) {
  166. elem = dojo.byId(elem);
  167. var sib = elem.nextSibling;
  168. while (sib) {
  169. if (sib.nodeType == 1 && (!tag || sib.nodeName.toLowerCase() == tag.toLowerCase())) {
  170. break;
  171. } else {
  172. sib = sib.nextSibling;
  173. }
  174. }
  175. return sib;
  176. }
  177. function xPrevSib(elem, tag) {
  178. elem = dojo.byId(elem);
  179. var sib = elem.previousSibling;
  180. while (sib) {
  181. if ((sib.nodeType == 1) && (!tag || sib.nodeName.toLowerCase() == tag.toLowerCase())) {
  182. break;
  183. } else {
  184. sib = sib.previousSibling;
  185. }
  186. }
  187. return sib;
  188. }
  189. //Functions From xCore
  190. function xCamelize(cssProp) {
  191. if (cssProp != '') {
  192. var split = cssProp.split("-");
  193. var returnVal = null;
  194. if (split.length < 1) {
  195. return cssProp;
  196. } else {
  197. returnVal = split[0];
  198. for (var i = 1; i < split.length; i++) {
  199. returnVal += split[i].substr(0, 1).toUpperCase() + split[i].substr(1, split[i].length);
  200. }
  201. }
  202. return returnVal;
  203. } else {
  204. return cssProp;
  205. }
  206. }
  207. function xDisplay(elem, val) {
  208. var returnVal = null;
  209. elem = dojo.byId(elem);
  210. returnVal = (val) ? dojo.style(elem, "display", val): dojo.style(elem, "display");
  211. return returnVal;
  212. }
  213. function xOpacity(e, o) {
  214. e = dojo.byId(e);
  215. if (o != undefined) {
  216. dojo.style(e, "opacity", (o + ''));
  217. } else {
  218. o = parseFloat(dojo.style(e, "opacity"));
  219. }
  220. return isNaN(o) ? 1 : o;
  221. }
  222. function xVisibility(elem, show) {
  223. elem = dojo.byId(elem);
  224. if (show != undefined) {
  225. if (show) {
  226. dojo.style(elem, "visibility", "visible");
  227. } else {
  228. dojo.style(elem, "visibility", "hidden");
  229. }
  230. }
  231. return dojo.style(elem, "visibility");
  232. }
  233. function xHide(elem) {
  234. elem = dojo.byId(elem);
  235. dojo.style(elem, "visibility", "hidden");
  236. return dojo.style(elem, "visibility");
  237. }
  238. function xShow(elem) {
  239. elem = dojo.byId(elem);
  240. dojo.style(elem, "visibility", "visible");
  241. return dojo.style(elem, "visibility");
  242. }
  243. function xGetComputedStyle(elem, style, isInt) {
  244. elem = dojo.byId(elem);
  245. if (!elem) {
  246. return null;
  247. }
  248. var camStyle = xCamelize(style);
  249. var returnVal = null;
  250. var cssNode = dojo.getComputedStyle(elem);
  251. if (cssNode) {
  252. returnVal = cssNode[camStyle];
  253. } else {
  254. return null;
  255. }
  256. return (isInt) ? (parseInt(returnVal, 10) || 0) : returnVal;
  257. }
  258. function xLeft(el, l) {
  259. return (l === undefined) ? dojo.coords(el).l : dojo.style(el, "left", l);
  260. }
  261. function xTop(el, t) {
  262. return (t === undefined) ? dojo.coords(el).t : dojo.style(el, "top", t);
  263. }
  264. function xMoveTo(el, x, y) {
  265. dojo.style(el, "left", x);
  266. dojo.style(el, "top", y);
  267. }
  268. function xHeight(el, h) {
  269. if (h === undefined) {
  270. return el.offsetHeight;
  271. } else {
  272. dojo.style(el, "height", h < 0 ? 0 : (h + "px"));
  273. return h;
  274. }
  275. }
  276. function xWidth(el, w) {
  277. if (w === undefined) {
  278. return el.offsetWidth;
  279. } else {
  280. dojo.style(el, "width", w < 0 ? 0 : (w + "px"));
  281. return w;
  282. }
  283. }
  284. function xResizeTo(el, w, h) {
  285. xHeight(el, h);
  286. xWidth(el, w);
  287. }
  288. function xPageX(el) {
  289. return dojo.coords(el).x;
  290. }
  291. function xPageY(el) {
  292. return dojo.coords(el).y;
  293. }
  294. function xNum() {
  295. for (var i=0,len=arguments.length;i<len;++i) {if (typeof arguments[i]!=="number"||isNaN(arguments[i])) {return false;}}
  296. return true;
  297. }
  298. function xStr() {
  299. for (var i=0,len=arguments.length;i<len;++i) {if (typeof arguments[i]!=="string") {return false;}}
  300. return true;
  301. }
  302. function xDef() {
  303. for (var i=0,len=arguments.length;i<len;++i) {if (arguments[i]===undefined) {return false;}}
  304. return true;
  305. }
  306. function xOffsetLeft(el) {
  307. el = dojo.byId(el);
  308. if (!el) {
  309. return 0;
  310. }
  311. return el.offsetLeft;
  312. }
  313. function xOffsetTop(el) {
  314. el = dojo.byId(el);
  315. if (!el) {
  316. return 0;
  317. }
  318. return el.offsetTop;
  319. }
  320. function xClientWidth() {
  321. if (window.innerWidth) {
  322. return window.innerWidth;
  323. } else if (document.documentElement.clientWidth !== 0) {
  324. return document.documentElement.clientWidth; //IE standards
  325. } else {
  326. return document.body.clientWidth; //IE quirks
  327. }
  328. }
  329. function xClientHeight() {
  330. if (window.innerHeight) {
  331. return window.innerHeight;
  332. } else if (document.documentElement.clientHeight !== 0) {
  333. return document.documentElement.clientHeight; // IE strict mode
  334. } else {
  335. return document.body.clientHeight; //IE quirks
  336. }
  337. }
  338. function xScrollLeft(e, bWin) {
  339. var offset = 0;
  340. if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
  341. var w = window;
  342. if (bWin && e) {
  343. w = e;
  344. }
  345. if (w.document.documentElement && w.document.documentElement.scrollLeft) {
  346. offset = w.document.documentElement.scrollLeft;
  347. } else if (w.document.body && xDef(w.document.body.scrollLeft)) {
  348. offset = w.document.body.scrollLeft;
  349. }
  350. } else {
  351. e = dojo.byId(e);
  352. if (e && xNum(e.scrollLeft)) {
  353. offset = e.scrollLeft;
  354. }
  355. }
  356. return offset;
  357. }
  358. function xScrollTop(e, bWin) {
  359. var offset = 0;
  360. if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
  361. var w = window;
  362. if (bWin && e) {
  363. w = e;
  364. }
  365. if (w.document.documentElement && w.document.documentElement.scrollTop) {
  366. offset = w.document.documentElement.scrollTop;
  367. } else if (w.document.body && xDef(w.document.body.scrollTop)) {
  368. offset = w.document.body.scrollTop;
  369. }
  370. } else {
  371. e = dojo.byId(e);
  372. if (e && xNum(e.scrollTop)) {
  373. offset = e.scrollTop;
  374. }
  375. }
  376. return offset;
  377. }