You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

317 lines
10 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. /**
  16. * @class Ext.state.Provider
  17. * Abstract base class for state provider implementations. This class provides methods
  18. * for encoding and decoding <b>typed</b> variables including dates and defines the
  19. * Provider interface.
  20. */
  21. Ext.state.Provider = Ext.extend(Ext.util.Observable, {
  22. constructor : function(){
  23. /**
  24. * @event statechange
  25. * Fires when a state change occurs.
  26. * @param {Provider} this This state provider
  27. * @param {String} key The state key which was changed
  28. * @param {String} value The encoded value for the state
  29. */
  30. this.addEvents("statechange");
  31. this.state = {};
  32. Ext.state.Provider.superclass.constructor.call(this);
  33. },
  34. /**
  35. * Returns the current value for a key
  36. * @param {String} name The key name
  37. * @param {Mixed} defaultValue A default value to return if the key's value is not found
  38. * @return {Mixed} The state data
  39. */
  40. get : function(name, defaultValue){
  41. return typeof this.state[name] == "undefined" ?
  42. defaultValue : this.state[name];
  43. },
  44. /**
  45. * Clears a value from the state
  46. * @param {String} 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. * @param {String} name The key name
  55. * @param {Mixed} value The value to set
  56. */
  57. set : function(name, value){
  58. this.state[name] = value;
  59. this.fireEvent("statechange", this, name, value);
  60. },
  61. /**
  62. * Decodes a string previously encoded with {@link #encodeValue}.
  63. * @param {String} value The value to decode
  64. * @return {Mixed} The decoded value
  65. */
  66. decodeValue : function(cookie){
  67. /**
  68. * a -> Array
  69. * n -> Number
  70. * d -> Date
  71. * b -> Boolean
  72. * s -> String
  73. * o -> Object
  74. * -> Empty (null)
  75. */
  76. var re = /^(a|n|d|b|s|o|e)\:(.*)$/,
  77. matches = re.exec(unescape(cookie)),
  78. all,
  79. type,
  80. v,
  81. kv;
  82. if(!matches || !matches[1]){
  83. return; // non state cookie
  84. }
  85. type = matches[1];
  86. v = matches[2];
  87. switch(type){
  88. case 'e':
  89. return null;
  90. case 'n':
  91. return parseFloat(v);
  92. case 'd':
  93. return new Date(Date.parse(v));
  94. case 'b':
  95. return (v == '1');
  96. case 'a':
  97. all = [];
  98. if(v != ''){
  99. Ext.each(v.split('^'), function(val){
  100. all.push(this.decodeValue(val));
  101. }, this);
  102. }
  103. return all;
  104. case 'o':
  105. all = {};
  106. if(v != ''){
  107. Ext.each(v.split('^'), function(val){
  108. kv = val.split('=');
  109. all[kv[0]] = this.decodeValue(kv[1]);
  110. }, this);
  111. }
  112. return all;
  113. default:
  114. return v;
  115. }
  116. },
  117. /**
  118. * Encodes a value including type information. Decode with {@link #decodeValue}.
  119. * @param {Mixed} value The value to encode
  120. * @return {String} The encoded value
  121. */
  122. encodeValue : function(v){
  123. var enc,
  124. flat = '',
  125. i = 0,
  126. len,
  127. key;
  128. if(v == null){
  129. return 'e:1';
  130. }else if(typeof v == 'number'){
  131. enc = 'n:' + v;
  132. }else if(typeof v == 'boolean'){
  133. enc = 'b:' + (v ? '1' : '0');
  134. }else if(Ext.isDate(v)){
  135. enc = 'd:' + v.toGMTString();
  136. }else if(Ext.isArray(v)){
  137. for(len = v.length; i < len; i++){
  138. flat += this.encodeValue(v[i]);
  139. if(i != len - 1){
  140. flat += '^';
  141. }
  142. }
  143. enc = 'a:' + flat;
  144. }else if(typeof v == 'object'){
  145. for(key in v){
  146. if(typeof v[key] != 'function' && v[key] !== undefined){
  147. flat += key + '=' + this.encodeValue(v[key]) + '^';
  148. }
  149. }
  150. enc = 'o:' + flat.substring(0, flat.length-1);
  151. }else{
  152. enc = 's:' + v;
  153. }
  154. return escape(enc);
  155. }
  156. });
  157. /**
  158. * @class Ext.state.Manager
  159. * This is the global state manager. By default all components that are "state aware" check this class
  160. * for state information if you don't pass them a custom state provider. In order for this class
  161. * to be useful, it must be initialized with a provider when your application initializes. Example usage:
  162. <pre><code>
  163. // in your initialization function
  164. init : function(){
  165. Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
  166. var win = new Window(...);
  167. win.restoreState();
  168. }
  169. </code></pre>
  170. * @singleton
  171. */
  172. Ext.state.Manager = function(){
  173. var provider = new Ext.state.Provider();
  174. return {
  175. /**
  176. * Configures the default state provider for your application
  177. * @param {Provider} stateProvider The state provider to set
  178. */
  179. setProvider : function(stateProvider){
  180. provider = stateProvider;
  181. },
  182. /**
  183. * Returns the current value for a key
  184. * @param {String} name The key name
  185. * @param {Mixed} defaultValue The default value to return if the key lookup does not match
  186. * @return {Mixed} The state data
  187. */
  188. get : function(key, defaultValue){
  189. return provider.get(key, defaultValue);
  190. },
  191. /**
  192. * Sets the value for a key
  193. * @param {String} name The key name
  194. * @param {Mixed} value The state data
  195. */
  196. set : function(key, value){
  197. provider.set(key, value);
  198. },
  199. /**
  200. * Clears a value from the state
  201. * @param {String} name The key name
  202. */
  203. clear : function(key){
  204. provider.clear(key);
  205. },
  206. /**
  207. * Gets the currently configured state provider
  208. * @return {Provider} The state provider
  209. */
  210. getProvider : function(){
  211. return provider;
  212. }
  213. };
  214. }();
  215. /**
  216. * @class Ext.state.CookieProvider
  217. * @extends Ext.state.Provider
  218. * The default Provider implementation which saves state via cookies.
  219. * <br />Usage:
  220. <pre><code>
  221. var cp = new Ext.state.CookieProvider({
  222. path: "/cgi-bin/",
  223. expires: new Date(new Date().getTime()+(1000*60*60*24*30)), //30 days
  224. domain: "extjs.com"
  225. });
  226. Ext.state.Manager.setProvider(cp);
  227. </code></pre>
  228. * @cfg {String} path The path for which the cookie is active (defaults to root '/' which makes it active for all pages in the site)
  229. * @cfg {Date} expires The cookie expiration date (defaults to 7 days from now)
  230. * @cfg {String} domain The domain to save the cookie for. Note that you cannot specify a different domain than
  231. * your page is on, but you can specify a sub-domain, or simply the domain itself like 'extjs.com' to include
  232. * all sub-domains if you need to access cookies across different sub-domains (defaults to null which uses the same
  233. * domain the page is running on including the 'www' like 'www.extjs.com')
  234. * @cfg {Boolean} secure True if the site is using SSL (defaults to false)
  235. * @constructor
  236. * Create a new CookieProvider
  237. * @param {Object} config The configuration object
  238. */
  239. Ext.state.CookieProvider = Ext.extend(Ext.state.Provider, {
  240. constructor : function(config){
  241. Ext.state.CookieProvider.superclass.constructor.call(this);
  242. this.path = "/";
  243. this.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days
  244. this.domain = null;
  245. this.secure = false;
  246. Ext.apply(this, config);
  247. this.state = this.readCookies();
  248. },
  249. // private
  250. set : function(name, value){
  251. if(typeof value == "undefined" || value === null){
  252. this.clear(name);
  253. return;
  254. }
  255. this.setCookie(name, value);
  256. Ext.state.CookieProvider.superclass.set.call(this, name, value);
  257. },
  258. // private
  259. clear : function(name){
  260. this.clearCookie(name);
  261. Ext.state.CookieProvider.superclass.clear.call(this, name);
  262. },
  263. // private
  264. readCookies : function(){
  265. var cookies = {},
  266. c = document.cookie + ";",
  267. re = /\s?(.*?)=(.*?);/g,
  268. matches,
  269. name,
  270. value;
  271. while((matches = re.exec(c)) != null){
  272. name = matches[1];
  273. value = matches[2];
  274. if(name && name.substring(0,3) == "ys-"){
  275. cookies[name.substr(3)] = this.decodeValue(value);
  276. }
  277. }
  278. return cookies;
  279. },
  280. // private
  281. setCookie : function(name, value){
  282. document.cookie = "ys-"+ name + "=" + this.encodeValue(value) +
  283. ((this.expires == null) ? "" : ("; expires=" + this.expires.toGMTString())) +
  284. ((this.path == null) ? "" : ("; path=" + this.path)) +
  285. ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
  286. ((this.secure == true) ? "; secure" : "");
  287. },
  288. // private
  289. clearCookie : function(name){
  290. document.cookie = "ys-" + name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" +
  291. ((this.path == null) ? "" : ("; path=" + this.path)) +
  292. ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
  293. ((this.secure == true) ? "; secure" : "");
  294. }
  295. });