GUtil.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2013
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or
  8. *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *|
  10. *+------------------------------------------------------------------------+
  11. */
  12. /*******************************************
  13. Common Utility Functions
  14. *******************************************/
  15. var GUtil = {};
  16. GUtil.createHiddenForm = function(name, method, viewerId, target) {
  17. var form = document.getElementById(name);
  18. if(form) {
  19. document.body.removeChild(form);
  20. }
  21. form = document.createElement("form");
  22. form.id = name;
  23. form.name = name;
  24. form.method = method;
  25. form.style.display = "none";
  26. form.action = document.forms["formWarpRequest" + viewerId].action;
  27. form.target = target + (new Date()).getTime();
  28. document.body.appendChild(form);
  29. return form;
  30. };
  31. GUtil.createFormField = function(el, name, value) {
  32. var input = document.createElement("input");
  33. input.type = "hidden";
  34. input.name = name;
  35. input.value = value;
  36. el.appendChild(input);
  37. };
  38. /*----------------------------------------------------------
  39. GenerateCallback:
  40. func: Function object to use. To call foo.test() pass in foo.test.
  41. aParams: Array of parameters to pass to func.
  42. oContext: Calling context. If "foo" in foo.test is and instance of "Foo" object, then you need
  43. to pass the instance as a context. This is this difference between calling Foo.test() and
  44. foo.test(), where foo = new Foo();
  45. Returns: A function object that can be used as a callback, but allows for the passing of stored parameters.
  46. Example: var foo = new Foo();
  47. var func1 = GUtil.generateCallback(alert, ["Hello World"]);
  48. var func2 = GUtil.generateCallback(window.alert, ["Hi there"], window);
  49. //alert "Hello World" after 5 seconds.
  50. setTimeout(func1, 5000);
  51. //alert "Hi there" after 10 seconds.
  52. setTimeout(func2, 10000);
  53. ------------------------------------------------------------*/
  54. GUtil.generateCallback = function(func, aParams, oContext){
  55. //normally "this" is probably the window object, but
  56. //we can pass in a different context object if we need to.
  57. if (func) {
  58. var funcContext = oContext || this;
  59. aParams = (aParams instanceof Array)? aParams : [];
  60. return (function(response){
  61. if(typeof response != "undefined" && aParams.length == 0) {
  62. aParams.push(response);
  63. }
  64. return func.apply(funcContext,aParams);
  65. });
  66. } else {
  67. //do nothing
  68. return (function() {});
  69. }
  70. };
  71. /*----------------------------------------------------------
  72. destroyProperties:
  73. inParam: an Object or Array
  74. bDestroyCognosViewer: if a property is an instance of CCognsoViewer object,
  75. * delete the properties of CV object
  76. * only if this parameter is true
  77. ------------------------------------------------------------*/
  78. GUtil.destroyProperties = function(inParam, bDestroyCognosViewer){
  79. var property;
  80. if (inParam instanceof Array) {
  81. for(var i=0; i<inParam.length; i++) {
  82. property = inParam[i];
  83. if (property instanceof String) {
  84. property = null;
  85. } else {
  86. if (property && property.destroy && !property._beingDestroyed) {
  87. property.destroy();
  88. }
  89. GUtil.destroyProperties(property);
  90. }
  91. }
  92. } else if (inParam instanceof Object){
  93. if (inParam._beingDestroyed) {
  94. return;
  95. }
  96. var obj = inParam;
  97. obj._beingDestroyed = true;
  98. for (var field in obj) {
  99. property = obj[field];
  100. if (field === "_beingDestroyed" || field === "m_destroyed" || field === "_destroyed" || typeof property == "function") {
  101. // skip - don't want to remove these
  102. continue;
  103. }
  104. if (property instanceof Array) {
  105. GUtil.destroyProperties(property);
  106. } else if (property instanceof Object) {
  107. if (typeof property.destroy == "function" && !property._destroyed && (property!==CCognosViewer || bDestroyCognosViewer)) {
  108. property.destroy();
  109. }
  110. }
  111. delete obj[field];
  112. }
  113. }
  114. };