ClientState.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: cogadmin
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2010
  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) 2008 Cognos ULC, an IBM Company. All rights reserved.
  11. // Cognos (R) is a trademark of Cognos ULC, (formerly Cognos Incorporated).
  12. //----------------------------------------------------------
  13. com.cognos.admin.ObjectFactory("com.cognos.admin.util.ClientState");
  14. /*
  15. * This is a J2HTML client state management class
  16. *
  17. *
  18. */
  19. com.cognos.admin.util.ClientState = function (fragId) {
  20. this.fragId = fragId;
  21. this.states = {};
  22. this.skipPersistStates = [];
  23. this.states[com.cognos.admin.util.ClientState.J2HTML_UI_STATE] = {};
  24. };
  25. com.cognos.admin.util.ClientState.VERSION = "0.1.0";
  26. com.cognos.admin.util.ClientState.J2HTML_UI_STATE = "j2html-ui-state"
  27. com.cognos.admin.util.ClientState.SELECTED_ROW = "selectedRow";
  28. com.cognos.admin.util.ClientState.GROUP_ACTION_ROWS = "grpActionRows";
  29. com.cognos.admin.util.ClientState.BREADCRUMBS = "breadcrumbs";
  30. com.cognos.admin.util.ClientState.SORT = "sort";
  31. com.cognos.admin.util.ClientState.SCROLL_TOP = "scrollTop";
  32. com.cognos.admin.util.ClientState.SCROLL_LEFT = "scrollLeft";
  33. com.cognos.admin.util.ClientState.COGADMIN_WINDOW_STATE = "cogadminWindowState";
  34. com.cognos.admin.util.ClientState.prototype = {
  35. restoreStates : function () {
  36. if (com.cognos.admin.publicParam.states[this.fragId]){
  37. this.states = com.cognos.admin.publicParam.states[this.fragId];
  38. }
  39. },
  40. saveStates : function () {
  41. var isEmpty = true;
  42. for (var o in this.states) {
  43. if (typeof this.states[o] !== 'function') {
  44. if (this.states[o]){
  45. isEmpty = false;
  46. break;
  47. }
  48. }
  49. }
  50. if (!isEmpty) {
  51. if (!com.cognos.admin.publicParam.states[this.fragId])
  52. com.cognos.admin.publicParam.states[this.fragId] = [];
  53. com.cognos.admin.publicParam.states[this.fragId] = this.states;
  54. }
  55. },
  56. /*
  57. * format the "," as a seperator's string to array
  58. */
  59. formatedString : function (value){
  60. return value.toString().split(",");
  61. },
  62. // add a state param as a array
  63. addState : function (key,value,isUIState) {
  64. if (key) {
  65. var ret = this.formatedString(value);
  66. var state = isUIState?this.states[com.cognos.admin.util.ClientState.J2HTML_UI_STATE]:this.states;
  67. if (!state[key] || state[key].length == 0){
  68. state[key] = ret;
  69. } else if (state[key] instanceof Array) {
  70. state[key] = _F_Array.unique(state[key].concat(ret));
  71. }
  72. this.saveStates();
  73. }
  74. },
  75. // add a state param as a single type
  76. setState : function (key,value,isUIState) {
  77. if (key) {
  78. var state = isUIState?this.states[com.cognos.admin.util.ClientState.J2HTML_UI_STATE]:this.states;
  79. state[key] = value;
  80. this.saveStates();
  81. }
  82. },
  83. getState : function (key,isUIState) {
  84. var state = isUIState?this.states[com.cognos.admin.util.ClientState.J2HTML_UI_STATE]:this.states;
  85. if (state[key] === undefined)
  86. return;
  87. return state[key];
  88. },
  89. /**
  90. * Remove the input value from the array, or set the value to "" for the string
  91. * @param {string} key
  92. * @param {Object} value
  93. * @param {Boolean} isUIState whether is a j2html_ui_state
  94. */
  95. removeState : function (key,value,isUIState) {
  96. var state = isUIState?this.states[com.cognos.admin.util.ClientState.J2HTML_UI_STATE]:this.states;
  97. if (state[key]){
  98. if (state[key] instanceof Array){
  99. _F_Array.remove(state[key],value);
  100. } else {
  101. this.clearState(key);
  102. }
  103. this.saveStates();
  104. }
  105. },
  106. /**
  107. * Get rid of the key from the state set.
  108. * @param {String} key
  109. * @param {Boolean} isUIState
  110. */
  111. clearState : function (key,isUIState) {
  112. var state = isUIState?this.states[com.cognos.admin.util.ClientState.J2HTML_UI_STATE]:this.states;
  113. if (state[key]){
  114. if (state[key] instanceof Array){
  115. state[key] = [];
  116. } else if (typeof state[key] == "object"){
  117. state[key] = null;
  118. } else {
  119. state[key] = "";
  120. }
  121. this.saveStates();
  122. }
  123. },
  124. /**
  125. * remove the client state form the round trip list
  126. * @param {Object} skipPersistStates could be either a string (option: seperated by ",") or an array
  127. */
  128. setSkipPersistStates : function (skipPersistStates){
  129. if (skipPersistStates){
  130. if (typeof skipPersistStates == "string"){
  131. this.skipPersistStates = skipPersistStates.split(",");
  132. } else {
  133. this.skipPersistStates = skipPersistStates;
  134. }
  135. }
  136. },
  137. getSkipPersistStates : function (){
  138. return this.skipPersistStates;
  139. },
  140. clearSkipPersistStates : function () {
  141. var skipPersistStates = this.getSkipPersistStates();
  142. for (var i = 0; i < skipPersistStates.length; i++){
  143. this.clearState(skipPersistStates[i],true);
  144. }
  145. },
  146. getStateURL : function () {
  147. var str = "";
  148. this.clearSkipPersistStates();
  149. for (var o in this.states){
  150. //this is to filter out the expended funcitons for Array object.
  151. if (!(this.states[o] instanceof Function)){
  152. var param;
  153. if (typeof this.states[o] == "object"){
  154. param = com.cognos.admin.util.Tools.JSON.stringify(this.states[o]);
  155. } else {
  156. param = this.states[o];
  157. }
  158. str = com.cognos.admin.util.Tools.generateQueryString(str,o,param,true);
  159. }
  160. }
  161. return str;
  162. }
  163. };