PersonalDefaultPromptSetSelections.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. define(function () {
  2. "use strict";
  3. var oPage;
  4. function promptValueSaved() {
  5. };
  6. promptValueSaved.prototype.draw = function (oControlHost) {
  7. oPage = oControlHost.page;
  8. var elDate = oControlHost.container;
  9. elDate.innerHTML =
  10. '<style>' +
  11. '.myButtonDate { height:32px; width:180px; cursor:pointer; margin-left:5px; color:#4178BE; font-size:14px; padding:6px 12px 6px 12px; background-color:white; border:1px solid #4178BE; }' +
  12. '.myButtonDate:hover { background-color:#4178BE; color:white; border:1px solid #4178BE; }' +
  13. '</style>' +
  14. '<button class="myButtonDate btnSavePromptValues" type="button">Save Prompt Values</button>' +
  15. '<button class="myButtonDate btnFetchPromptSelections" type="button">Fetch Prompt Selections</button>';
  16. elDate.querySelector(".btnSavePromptValues").onclick = buttonActionSavePromptSelections;
  17. elDate.querySelector(".btnFetchPromptSelections").onclick = buttonActionFetchPromptSelections;
  18. };
  19. /*
  20. *
  21. * This function coordinates the basic operations of this program as so:
  22. * 1. Loop through the prompts on the page
  23. * 2. For each prompt look to see if there is a cookie with the naming convention cog<prompt name> containing prompt values
  24. * 3. Apply the saved values to each prompt
  25. *
  26. * Note:
  27. * The prompt names must not contain spaces or special characters
  28. * The prompt names must match the prompt names in subsequent reports
  29. * Cookie values are JSON encoded and ready for use
  30. */
  31. function buttonActionFetchPromptSelections() {
  32. // Get the current prompt value
  33. // Create the report object
  34. // Create an array of prompt controls
  35. var aPromptControls = oPage.getAllPromptControls();
  36. // Determine
  37. var loadedValues = false;
  38. // Loop through the prompt controls on this page
  39. for (var i = 0; i < aPromptControls.length; i++) {
  40. // For each prompt control ...
  41. var currentPrompt = aPromptControls[i];
  42. // Create the cookie name
  43. var cookieName = "cog" + currentPrompt.name;
  44. // Fetch the cookie values (if they exist) as a JavaScript object
  45. var JSONEncodedPromptSelections = fetchCookie(cookieName);
  46. if (JSONEncodedPromptSelections) {
  47. if (JSONEncodedPromptSelections.length > 0) {
  48. // Apply cookie values to prompt
  49. currentPrompt.setValues(JSONEncodedPromptSelections);
  50. loadedValues = true;
  51. };
  52. };
  53. }; // end for
  54. if (loadedValues) {
  55. alert("Your personal default prompt selections have been loaded. You may now edit them as required and press the Save Prompt Selections button.");
  56. }
  57. // Return false, also in an effort to stop the onClick event from propagating to other controls
  58. return false;
  59. };
  60. /*
  61. *
  62. * This function parses the browser cookie string looking for the cookie
  63. * and passing it to the calling function
  64. * @cookieName is the name of the cookie we wish to find
  65. *
  66. */
  67. function fetchCookie(passedCookieName) {
  68. // Create an array of strings from the document cookie object using the split method to separate the cookies
  69. var aCookies = document.cookie.split(";");
  70. //create a variable to hold the cookie value
  71. var cookie = "";
  72. // Loop through the cookie array to find cognosPrompts
  73. for (var i = 0; i < aCookies.length; i++) {
  74. var currentCookie = aCookies[i];
  75. // The = sign separates the cookie name from the cookie value
  76. var signLocation = currentCookie.indexOf("=");
  77. var currentCookieName = currentCookie.substr(0, signLocation);
  78. var regexPattern = passedCookieName.valueOf();
  79. if (currentCookieName.match(regexPattern)) {
  80. cookie = currentCookie.substr(signLocation + 1, currentCookie.length);
  81. }
  82. }
  83. // Convert the JavaScript object to useable form
  84. cookie = eval(cookie);
  85. return cookie;
  86. };
  87. // Set
  88. function buttonActionSavePromptSelections() {
  89. // Get the current prompt value
  90. // Create an array of prompt controls
  91. var aPromptControls = oPage.getAllPromptControls();
  92. // Loop through the prompt controls on this page
  93. for (var i = 0; i < aPromptControls.length; i++) {
  94. // For each prompt control, call a function to JSON encode the user selections (prompt values)
  95. var currentPrompt = aPromptControls[i];
  96. // Create the cookie name
  97. var cookieName = "cog" + currentPrompt.name;
  98. // Now save the JSON into a cookie
  99. storeCookie(cookieName, JSON.stringify(currentPrompt.getValues()));
  100. };
  101. alert("Your personal default prompt selections have been saved. You may now close this report.");
  102. // Return false, also in an effort to stop the onClick event from propagating to other controls
  103. return false;
  104. };
  105. /*
  106. *
  107. * This function creates a cookie
  108. * @cookieName is the name of the cookie to be created
  109. * @cookieValue is the value to be used to create the cookie
  110. * The date is always set to expire 1 January 2100 to ensure the cookie is effectively permanent
  111. *
  112. */
  113. function storeCookie(cookieName, cookieValue) {
  114. // Set the cookie expiry date to 1 January 2100 to ensure it does not expire
  115. var expDate = new Date();
  116. expDate.setFullYear(2100, 0, 1);
  117. document.cookie = cookieName + "=" + cookieValue + "; expires=" + expDate.toUTCString();
  118. };
  119. return promptValueSaved;
  120. }
  121. );