PersonalDefaultPromptUseSelections.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. define(function () {
  2. "use strict";
  3. function PageModule() {
  4. };
  5. /*
  6. *
  7. * This function coordinates the basic operations of this program as so:
  8. * 1. Loop through the prompts on the page
  9. * 2. For each prompt look to see if there is a cookie with the naming convention cog<prompt name> containing prompt values
  10. * 3. Apply the saved values to each prompt
  11. *
  12. * Note:
  13. * The prompt names must not contain spaces or special characters
  14. * The prompt names must match the prompt names in subsequent reports
  15. * Cookie values are JSON encoded and ready for use
  16. */
  17. function buttonActionFetchPromptSelections(oPage) {
  18. // Get the current prompt value
  19. // Create the report object
  20. // Create an array of prompt controls
  21. var aPromptControls = oPage.getAllPromptControls();
  22. // Determine
  23. var loadedValues = false;
  24. // Loop through the prompt controls on this page
  25. for (var i = 0; i < aPromptControls.length; i++) {
  26. // For each prompt control ...
  27. var currentPrompt = aPromptControls[i];
  28. // Create the cookie name
  29. var cookieName = "cog" + currentPrompt.name;
  30. // Fetch the cookie values (if they exist) as a JavaScript object
  31. var JSONEncodedPromptSelections = fetchCookie(cookieName);
  32. if (JSONEncodedPromptSelections) {
  33. if (JSONEncodedPromptSelections.length > 0) {
  34. // Apply cookie values to prompt
  35. currentPrompt.setValues(JSONEncodedPromptSelections);
  36. loadedValues = true;
  37. };
  38. };
  39. }; // end for
  40. if (loadedValues) {
  41. alert("Your personal default prompt selections have been loaded. You may now edit them as required and press the Save Prompt Selections button.");
  42. }
  43. // Return false, also in an effort to stop the onClick event from propagating to other controls
  44. return false;
  45. };
  46. /*
  47. *
  48. * This function parses the browser cookie string looking for the cookie
  49. * and passing it to the calling function
  50. * @cookieName is the name of the cookie we wish to find
  51. *
  52. */
  53. function fetchCookie(passedCookieName) {
  54. // Create an array of strings from the document cookie object using the split method to separate the cookies
  55. var aCookies = document.cookie.split(";");
  56. //create a variable to hold the cookie value
  57. var cookie = "";
  58. // Loop through the cookie array to find cognosPrompts
  59. for (var i = 0; i < aCookies.length; i++) {
  60. var currentCookie = aCookies[i];
  61. // The = sign separates the cookie name from the cookie value
  62. var signLocation = currentCookie.indexOf("=");
  63. var currentCookieName = currentCookie.substr(0, signLocation);
  64. var regexPattern = passedCookieName.valueOf();
  65. if (currentCookieName.match(regexPattern)) {
  66. cookie = currentCookie.substr(signLocation + 1, currentCookie.length);
  67. }
  68. }
  69. // Convert the JavaScript object to useable form
  70. cookie = eval(cookie);
  71. return cookie;
  72. };
  73. PageModule.prototype.load = function (oPage) {
  74. buttonActionFetchPromptSelections(oPage);
  75. };
  76. return PageModule;
  77. });