cpscore.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 = cpsdojo.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) return;
  26. var ele = new xDraggableElement(id,false,null,null,null);
  27. xRemoveEventListener(ele, 'mousedown', xDragMouseDown);
  28. xRemoveEventListener(document, 'mousemove', xDragMouseMove);
  29. }
  30. function xEnableDrag(id,dragStart,drag,dragEnd) {
  31. var ele = new xDraggableElement(id,true,dragStart,drag,dragEnd);
  32. xAddEventListener(ele, 'mousedown', xDragMouseDown);
  33. xAddEventListener(document, 'mousemove', xDragMouseMove);
  34. }
  35. function xDragMouseDown(e) {
  36. var evt = cpsdojo.fixEvent(e);
  37. var ele = evt.target;
  38. while(ele && !ele.draggable) {
  39. ele = xParent(ele);
  40. }
  41. if (ele) {
  42. evt.preventDefault();
  43. ele.xDPX = evt.pageX;
  44. ele.xDPY = evt.pageY;
  45. xDragElementSource = ele;
  46. xAddEventListener(document, 'mouseup', xDragMouseUp);
  47. if (ele.dragStart) {
  48. ele.dragStart(ele, evt.pageX, evt.pageY);
  49. }
  50. }
  51. }
  52. function xDragMouseMove(e) {
  53. var evt = cpsdojo.fixEvent(e);
  54. if (xDragElementSource) {
  55. evt.preventDefault();
  56. var dx = evt.pageX - xDragElementSource.xDPX;
  57. var dy = evt.pageY - xDragElementSource.xDPY;
  58. xDragElementSource.xDPX = evt.pageX;
  59. xDragElementSource.xDPY = evt.pageY;
  60. if (xDragElementSource.drag) {
  61. xDragElementSource.drag(xDragElementSource, dx, dy);
  62. }
  63. else {
  64. xMoveTo(xDragElementSource, xLeft(xDragElementSource) + dx, xTop(xDragElementSource) + dy);
  65. }
  66. }
  67. }
  68. function xDragMouseUp(e) {
  69. var evt = cpsdojo.fixEvent(e);
  70. if (xDragElementSource) {
  71. evt.preventDefault();
  72. xRemoveEventListener(document, 'mouseup', xDragMouseUp);
  73. if (xDragElementSource.dragEnd) {
  74. xDragElementSource.dragEnd(xDragElementSource, evt.pageX, evt.pageY);
  75. }
  76. xDragElementSource = null;
  77. }
  78. }
  79. //Functions from xEvent
  80. // hashmap of the handles for all registered events.
  81. var handleMap = new Object();
  82. function xEvent(evt) {
  83. return cpsdojo.fixEvent(evt);
  84. }
  85. function xHandleGenerator(ele, type, listener) {
  86. return ('' + ele.id + type + listener.name).toLowerCase();
  87. }
  88. function xAddEventListener(ele, type, listener, bCapture) {
  89. if (xIE4Up && type=="load") {
  90. var loadHandle = cpsdojo.addOnLoad(listener);
  91. return loadHandle;
  92. }
  93. var handle = cpsdojo.connect(cpsdojo.byId(ele), type, listener);
  94. var handleKey = xHandleGenerator(ele, type, listener);
  95. handleMap[handleKey] = handle;
  96. return handle;
  97. }
  98. function xRemoveEventListener(ele, type, listener, bCapture) {
  99. var handleKey = xHandleGenerator(ele, type, listener);
  100. cpsdojo.disconnect(handleMap[handleKey]);
  101. delete handleMap[handleKey];
  102. }
  103. function xPreventDefault(evt) {
  104. var dxEvt = cpsdojo.fixEvent(evt);
  105. dxEvt.preventDefault();
  106. }
  107. function xStopPropagation(evt) {
  108. var dxEvt = cpsdojo.fixEvent(evt);
  109. dxEvt.stopPropagation();
  110. }
  111. //Functions from xDom
  112. function xGetElementById(id) {
  113. return cpsdojo.byId(id);
  114. }
  115. function xGetElementsByTagName(tag, base) {
  116. return cpsdojo.query(tag, base);
  117. }
  118. function xGetElementsByClassName(tag, base) {
  119. return cpsdojo.query("."+tag, base);
  120. }
  121. function xCreateElement(el) {
  122. if (document.createElement) {
  123. return document.createElement(el);
  124. }
  125. }
  126. function xAppendChild(parentEl, childEl) {
  127. parentEl.appendChild(childEl);
  128. }
  129. function xFirstChild(el,tag) {
  130. if (tag === undefined) {
  131. var nodeList = cpsdojo.query(":first-child", el);
  132. return (nodeList.length)?nodeList[0]:null;
  133. }
  134. else {
  135. var nodeList = cpsdojo.query(tag + ":first-of-type", el);
  136. return (nodeList.length)?nodeList[0]:null;
  137. }
  138. }
  139. function xGetElementsByAttribute(tag, attribute, match) {
  140. var nodeList = null;
  141. if (match === undefined) {
  142. nodeList = cpsdojo.query(tag + "[" + attribute + "]");
  143. } else {
  144. nodeList = cpsdojo.query(tag + "[" + attribute + "=" + match + "]");
  145. }
  146. return (nodeList.length && nodeList.length < 1)? null : nodeList;
  147. }
  148. function xParent(el, returnNode) {
  149. el = cpsdojo.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 = cpsdojo.byId(elem);
  160. while(elem && ancestorNumber--) {
  161. elem = elem.parentNode;
  162. }
  163. return elem;
  164. }
  165. function xNextSib(elem, tag) {
  166. elem = cpsdojo.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 = cpsdojo.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 = cpsdojo.byId(elem);
  210. returnVal = (val) ? cpsdojo.style(elem, "display", val): cpsdojo.style(elem, "display");
  211. return returnVal;
  212. }
  213. function xOpacity(e, o) {
  214. e = cpsdojo.byId(e);
  215. if (o != undefined) {
  216. cpsdojo.style(e,"opacity",(o+''));
  217. }
  218. else {
  219. o = parseFloat(cpsdojo.style(e,"opacity"));
  220. }
  221. return isNaN(o) ? 1 : o;
  222. }
  223. function xVisibility(elem, show) {
  224. elem = cpsdojo.byId(elem);
  225. var returnVal = null;
  226. if (show != undefined) {
  227. if (show) {
  228. cpsdojo.style(elem,"visibility","visible");
  229. }
  230. else {
  231. cpsdojo.style(elem,"visibility","hidden");
  232. }
  233. }
  234. return cpsdojo.style(elem,"visibility");
  235. }
  236. function xHide(elem) {
  237. elem = cpsdojo.byId(elem);
  238. cpsdojo.style(elem,"visibility","hidden");
  239. return cpsdojo.style(elem,"visibility");
  240. }
  241. function xShow(elem) {
  242. elem = cpsdojo.byId(elem);
  243. cpsdojo.style(elem,"visibility","visible");
  244. return cpsdojo.style(elem,"visibility");
  245. }
  246. function xGetComputedStyle(elem, style, isInt) {
  247. elem = cpsdojo.byId(elem);
  248. if (!elem) {
  249. return null;
  250. }
  251. var camStyle = xCamelize(style);
  252. var returnVal = null;
  253. var cssNode = cpsdojo.getComputedStyle(elem);
  254. if (cssNode) {
  255. returnVal = cssNode[camStyle];
  256. } else {
  257. return null;
  258. }
  259. return (isInt) ? (parseInt(returnVal) || 0) : returnVal;
  260. }
  261. function xLeft(el, l) {
  262. return (l===undefined)?cpsdojo.coords(el).l:cpsdojo.style(el,"left",l);
  263. }
  264. function xTop(el, t) {
  265. return (t===undefined)?cpsdojo.coords(el).t:cpsdojo.style(el,"top",t);
  266. }
  267. function xMoveTo(el, x, y) {
  268. cpsdojo.style(el,"left",x);
  269. cpsdojo.style(el,"top",y);
  270. }
  271. function xHeight(el, h) {
  272. if (h===undefined) {
  273. return el.offsetHeight;
  274. }
  275. else {
  276. cpsdojo.style(el,"height",h<0?0:(h+"px"));
  277. return h;
  278. }
  279. }
  280. function xWidth(el, w) {
  281. if (w===undefined) {
  282. return el.offsetWidth;
  283. }
  284. else {
  285. cpsdojo.style(el,"width",w<0?0:(w+"px"));
  286. return w;
  287. }
  288. }
  289. function xResizeTo(el, w, h) {
  290. xHeight(el,h);
  291. xWidth(el,w);
  292. }
  293. function xPageX(el) {
  294. return cpsdojo.coords(el).x;
  295. }
  296. function xPageY(el) {
  297. return cpsdojo.coords(el).y;
  298. }
  299. function xNum() {
  300. for(var i=0,len=arguments.length;i<len;++i){if(typeof arguments[i]!=="number"||isNaN(arguments[i])){return false;}}
  301. return true;
  302. }
  303. function xStr() {
  304. for(var i=0,len=arguments.length;i<len;++i){if(typeof arguments[i]!=="string"){return false;}}
  305. return true;
  306. }
  307. function xDef() {
  308. for(var i=0,len=arguments.length;i<len;++i){if(arguments[i]===undefined){return false;}}
  309. return true;
  310. }
  311. function xOffsetLeft(el) {
  312. el = cpsdojo.byId(el);
  313. if(!el)return 0;
  314. return el.offsetLeft;
  315. }
  316. function xOffsetTop(el) {
  317. el = cpsdojo.byId(el);
  318. if(!el)return 0;
  319. return el.offsetTop;
  320. }
  321. function xClientWidth() {
  322. if (window.innerWidth) {
  323. return window.innerWidth;
  324. } else if(!(document.documentElement.clientWidth == 0)) {
  325. return document.documentElement.clientWidth; //IE standards
  326. } else {
  327. return document.body.clientWidth; //IE quirks
  328. }
  329. }
  330. function xClientHeight() {
  331. if (window.innerHeight) {
  332. return window.innerHeight;
  333. } else if(!(document.documentElement.clientHeight == 0)) {
  334. return document.documentElement.clientHeight; // IE strict mode
  335. } else {
  336. return document.body.clientHeight; //IE quirks
  337. }
  338. }
  339. function xScrollLeft(e, bWin) {
  340. var offset=0;
  341. if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
  342. var w = window;
  343. if (bWin && e) w = e;
  344. if(w.document.documentElement && w.document.documentElement.scrollLeft) offset=w.document.documentElement.scrollLeft;
  345. else if(w.document.body && xDef(w.document.body.scrollLeft)) offset=w.document.body.scrollLeft;
  346. }
  347. else {
  348. e = cpsdojo.byId(e);
  349. if (e && xNum(e.scrollLeft)) offset = e.scrollLeft;
  350. }
  351. return offset;
  352. }
  353. function xScrollTop(e, bWin) {
  354. var offset=0;
  355. if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
  356. var w = window;
  357. if (bWin && e) w = e;
  358. if(w.document.documentElement && w.document.documentElement.scrollTop) offset=w.document.documentElement.scrollTop;
  359. else if(w.document.body && xDef(w.document.body.scrollTop)) offset=w.document.body.scrollTop;
  360. }
  361. else {
  362. e = cpsdojo.byId(e);
  363. if (e && xNum(e.scrollTop)) offset = e.scrollTop;
  364. }
  365. return offset;
  366. }