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.

109 lines
4.4 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.util.Cookies
  17. * Utility class for managing and interacting with cookies.
  18. * @singleton
  19. */
  20. Ext.util.Cookies = {
  21. /**
  22. * Create a cookie with the specified name and value. Additional settings
  23. * for the cookie may be optionally specified (for example: expiration,
  24. * access restriction, SSL).
  25. * @param {String} name The name of the cookie to set.
  26. * @param {Mixed} value The value to set for the cookie.
  27. * @param {Object} expires (Optional) Specify an expiration date the
  28. * cookie is to persist until. Note that the specified Date object will
  29. * be converted to Greenwich Mean Time (GMT).
  30. * @param {String} path (Optional) Setting a path on the cookie restricts
  31. * access to pages that match that path. Defaults to all pages (<tt>'/'</tt>).
  32. * @param {String} domain (Optional) Setting a domain restricts access to
  33. * pages on a given domain (typically used to allow cookie access across
  34. * subdomains). For example, "extjs.com" will create a cookie that can be
  35. * accessed from any subdomain of extjs.com, including www.extjs.com,
  36. * support.extjs.com, etc.
  37. * @param {Boolean} secure (Optional) Specify true to indicate that the cookie
  38. * should only be accessible via SSL on a page using the HTTPS protocol.
  39. * Defaults to <tt>false</tt>. Note that this will only work if the page
  40. * calling this code uses the HTTPS protocol, otherwise the cookie will be
  41. * created with default options.
  42. */
  43. set : function(name, value){
  44. var argv = arguments;
  45. var argc = arguments.length;
  46. var expires = (argc > 2) ? argv[2] : null;
  47. var path = (argc > 3) ? argv[3] : '/';
  48. var domain = (argc > 4) ? argv[4] : null;
  49. var secure = (argc > 5) ? argv[5] : false;
  50. document.cookie = name + "=" + escape(value) + ((expires === null) ? "" : ("; expires=" + expires.toGMTString())) + ((path === null) ? "" : ("; path=" + path)) + ((domain === null) ? "" : ("; domain=" + domain)) + ((secure === true) ? "; secure" : "");
  51. },
  52. /**
  53. * Retrieves cookies that are accessible by the current page. If a cookie
  54. * does not exist, <code>get()</code> returns <tt>null</tt>. The following
  55. * example retrieves the cookie called "valid" and stores the String value
  56. * in the variable <tt>validStatus</tt>.
  57. * <pre><code>
  58. * var validStatus = Ext.util.Cookies.get("valid");
  59. * </code></pre>
  60. * @param {String} name The name of the cookie to get
  61. * @return {Mixed} Returns the cookie value for the specified name;
  62. * null if the cookie name does not exist.
  63. */
  64. get : function(name){
  65. var arg = name + "=";
  66. var alen = arg.length;
  67. var clen = document.cookie.length;
  68. var i = 0;
  69. var j = 0;
  70. while(i < clen){
  71. j = i + alen;
  72. if(document.cookie.substring(i, j) == arg){
  73. return Ext.util.Cookies.getCookieVal(j);
  74. }
  75. i = document.cookie.indexOf(" ", i) + 1;
  76. if(i === 0){
  77. break;
  78. }
  79. }
  80. return null;
  81. },
  82. /**
  83. * Removes a cookie with the provided name from the browser
  84. * if found by setting its expiration date to sometime in the past.
  85. * @param {String} name The name of the cookie to remove
  86. */
  87. clear : function(name){
  88. if(Ext.util.Cookies.get(name)){
  89. document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  90. }
  91. },
  92. /**
  93. * @private
  94. */
  95. getCookieVal : function(offset){
  96. var endstr = document.cookie.indexOf(";", offset);
  97. if(endstr == -1){
  98. endstr = document.cookie.length;
  99. }
  100. return unescape(document.cookie.substring(offset, endstr));
  101. }
  102. };