Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
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.

275 lines
9.1 KiB

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