Provider.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Ext JS Library 2.0 Copyright(c) 2006-2007, Ext JS, LLC. licensing@extjs.com
  3. *
  4. * http://extjs.com/license
  5. */
  6. /**
  7. * @class Ext.state.Provider Abstract base class for state provider
  8. * implementations. This class provides methods for encoding and decoding
  9. * <b>typed</b> variables including dates and defines the Provider
  10. * interface.
  11. */
  12. Ext.state.Provider = function() {
  13. /**
  14. * @event statechange Fires when a state change occurs.
  15. * @param {Provider}
  16. * this This state provider
  17. * @param {String}
  18. * key The state key which was changed
  19. * @param {String}
  20. * value The encoded value for the state
  21. */
  22. this.addEvents("statechange");
  23. this.state = {};
  24. Ext.state.Provider.superclass.constructor.call(this);
  25. };
  26. Ext.extend(Ext.state.Provider, Ext.util.Observable, {
  27. /**
  28. * Returns the current value for a key
  29. *
  30. * @param {String}
  31. * name The key name
  32. * @param {Mixed}
  33. * defaultValue A default value to return if the key's
  34. * value is not found
  35. * @return {Mixed} The state data
  36. */
  37. get : function(name, defaultValue) {
  38. return typeof this.state[name] == "undefined"
  39. ? defaultValue
  40. : this.state[name];
  41. },
  42. /**
  43. * Clears a value from the state
  44. *
  45. * @param {String}
  46. * name The key name
  47. */
  48. clear : function(name) {
  49. delete this.state[name];
  50. this.fireEvent("statechange", this, name, null);
  51. },
  52. /**
  53. * Sets the value for a key
  54. *
  55. * @param {String}
  56. * name The key name
  57. * @param {Mixed}
  58. * value The value to set
  59. */
  60. set : function(name, value) {
  61. this.state[name] = value;
  62. // console.log(value);
  63. this.fireEvent("statechange", this, name, value);
  64. },
  65. /**
  66. * Decodes a string previously encoded with {@link #encodeValue}.
  67. *
  68. * @param {String}
  69. * value The value to decode
  70. * @return {Mixed} The decoded value
  71. */
  72. decodeValue : function(cookie) {
  73. var re = /^(a|n|d|b|s|o)\:(.*)$/;
  74. var matches = re.exec(unescape(cookie));
  75. if (!matches || !matches[1])
  76. return; // non state cookie
  77. var type = matches[1];
  78. var v = matches[2];
  79. switch (type) {
  80. case "n" :
  81. return parseFloat(v);
  82. case "d" :
  83. return new Date(Date.parse(v));
  84. case "b" :
  85. return (v == "1");
  86. case "a" :
  87. var all = [];
  88. var values = v.split("^");
  89. for (var i = 0, len = values.length; i < len; i++) {
  90. all.push(this.decodeValue(values[i]));
  91. }
  92. return all;
  93. case "o" :
  94. var all = {};
  95. var values = v.split("^");
  96. for (var i = 0, len = values.length; i < len; i++) {
  97. var kv = values[i].split("=");
  98. all[kv[0]] = this.decodeValue(kv[1]);
  99. }
  100. return all;
  101. default :
  102. return v;
  103. }
  104. },
  105. /**
  106. * Encodes a value including type information. Decode with
  107. * {@link #decodeValue}.
  108. *
  109. * @param {Mixed}
  110. * value The value to encode
  111. * @return {String} The encoded value
  112. */
  113. encodeValue : function(v) {
  114. var enc;
  115. if (typeof v == "number") {
  116. enc = "n:" + v;
  117. } else if (typeof v == "boolean") {
  118. enc = "b:" + (v ? "1" : "0");
  119. } else if (v instanceof Date) {
  120. enc = "d:" + v.toGMTString();
  121. } else if (v instanceof Array) {
  122. var flat = "";
  123. for (var i = 0, len = v.length; i < len; i++) {
  124. flat += this.encodeValue(v[i]);
  125. if (i != len - 1)
  126. flat += "^";
  127. }
  128. enc = "a:" + flat;
  129. } else if (typeof v == "object") {
  130. var flat = "";
  131. for (var key in v) {
  132. if (typeof v[key] != "function" && v[key] !== undefined) {
  133. flat += key + "=" + this.encodeValue(v[key]) + "^";
  134. }
  135. }
  136. enc = "o:" + flat.substring(0, flat.length - 1);
  137. } else {
  138. enc = "s:" + v;
  139. }
  140. return escape(enc);
  141. }
  142. });