CookieProvider.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-state-CookieProvider'>/**
  19. </span> * A Provider implementation which saves and retrieves state via cookies. The CookieProvider supports the usual cookie
  20. * options, such as:
  21. *
  22. * - {@link #path}
  23. * - {@link #expires}
  24. * - {@link #domain}
  25. * - {@link #secure}
  26. *
  27. * Example:
  28. *
  29. * var cp = Ext.create('Ext.state.CookieProvider', {
  30. * path: &quot;/cgi-bin/&quot;,
  31. * expires: new Date(new Date().getTime()+(1000*60*60*24*30)), //30 days
  32. * domain: &quot;sencha.com&quot;
  33. * });
  34. *
  35. * Ext.state.Manager.setProvider(cp);
  36. *
  37. */
  38. Ext.define('Ext.state.CookieProvider', {
  39. extend: 'Ext.state.Provider',
  40. <span id='Ext-state-CookieProvider-cfg-path'> /**
  41. </span> * @cfg {String} path
  42. * The path for which the cookie is active. Defaults to root '/' which makes it active for all pages in the site.
  43. */
  44. <span id='Ext-state-CookieProvider-cfg-expires'> /**
  45. </span> * @cfg {Date} expires
  46. * The cookie expiration date. Defaults to 7 days from now.
  47. */
  48. <span id='Ext-state-CookieProvider-cfg-domain'> /**
  49. </span> * @cfg {String} domain
  50. * The domain to save the cookie for. Note that you cannot specify a different domain than your page is on, but you can
  51. * specify a sub-domain, or simply the domain itself like 'sencha.com' to include all sub-domains if you need to access
  52. * cookies across different sub-domains. Defaults to null which uses the same domain the page is running on including
  53. * the 'www' like 'www.sencha.com'.
  54. */
  55. <span id='Ext-state-CookieProvider-cfg-secure'> /**
  56. </span> * @cfg {Boolean} [secure=false]
  57. * True if the site is using SSL
  58. */
  59. <span id='Ext-state-CookieProvider-method-constructor'> /**
  60. </span> * Creates a new CookieProvider.
  61. * @param {Object} [config] Config object.
  62. */
  63. constructor : function(config){
  64. var me = this;
  65. me.path = &quot;/&quot;;
  66. me.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days
  67. me.domain = null;
  68. me.secure = false;
  69. me.callParent(arguments);
  70. me.state = me.readCookies();
  71. },
  72. // private
  73. set : function(name, value){
  74. var me = this;
  75. if(typeof value == &quot;undefined&quot; || value === null){
  76. me.clear(name);
  77. return;
  78. }
  79. me.setCookie(name, value);
  80. me.callParent(arguments);
  81. },
  82. // private
  83. clear : function(name){
  84. this.clearCookie(name);
  85. this.callParent(arguments);
  86. },
  87. // private
  88. readCookies : function(){
  89. var cookies = {},
  90. c = document.cookie + &quot;;&quot;,
  91. re = /\s?(.*?)=(.*?);/g,
  92. prefix = this.prefix,
  93. len = prefix.length,
  94. matches,
  95. name,
  96. value;
  97. while((matches = re.exec(c)) != null){
  98. name = matches[1];
  99. value = matches[2];
  100. if (name &amp;&amp; name.substring(0, len) == prefix){
  101. cookies[name.substr(len)] = this.decodeValue(value);
  102. }
  103. }
  104. return cookies;
  105. },
  106. // private
  107. setCookie : function(name, value){
  108. var me = this;
  109. document.cookie = me.prefix + name + &quot;=&quot; + me.encodeValue(value) +
  110. ((me.expires == null) ? &quot;&quot; : (&quot;; expires=&quot; + me.expires.toGMTString())) +
  111. ((me.path == null) ? &quot;&quot; : (&quot;; path=&quot; + me.path)) +
  112. ((me.domain == null) ? &quot;&quot; : (&quot;; domain=&quot; + me.domain)) +
  113. ((me.secure == true) ? &quot;; secure&quot; : &quot;&quot;);
  114. },
  115. // private
  116. clearCookie : function(name){
  117. var me = this;
  118. document.cookie = me.prefix + name + &quot;=null; expires=Thu, 01-Jan-70 00:00:01 GMT&quot; +
  119. ((me.path == null) ? &quot;&quot; : (&quot;; path=&quot; + me.path)) +
  120. ((me.domain == null) ? &quot;&quot; : (&quot;; domain=&quot; + me.domain)) +
  121. ((me.secure == true) ? &quot;; secure&quot; : &quot;&quot;);
  122. }
  123. });
  124. </pre>
  125. </body>
  126. </html>