]> git.proxmox.com Git - pve-manager.git/blob - www/mobile/Cookies.js
ui: restore: display "Hostname" for container
[pve-manager.git] / www / mobile / Cookies.js
1 /**
2 * Utility class for setting/reading values from browser cookies.
3 * Values can be written using the {@link #set} method.
4 * Values can be read using the {@link #get} method.
5 * A cookie can be invalidated on the client machine using the {@link #clear} method.
6 */
7 Ext.define('Ext.util.Cookies', {
8 singleton: true,
9
10 /**
11 * Creates a cookie with the specified name and value. Additional settings for the cookie may be optionally specified
12 * (for example: expiration, access restriction, SSL).
13 * @param {String} name The name of the cookie to set.
14 * @param {Object} value The value to set for the cookie.
15 * @param {Object} [expires] Specify an expiration date the cookie is to persist until. Note that the specified Date
16 * object will be converted to Greenwich Mean Time (GMT).
17 * @param {String} [path] Setting a path on the cookie restricts access to pages that match that path. Defaults to all
18 * pages ('/').
19 * @param {String} [domain] Setting a domain restricts access to pages on a given domain (typically used to allow
20 * cookie access across subdomains). For example, "sencha.com" will create a cookie that can be accessed from any
21 * subdomain of sencha.com, including www.sencha.com, support.sencha.com, etc.
22 * @param {Boolean} [secure] Specify true to indicate that the cookie should only be accessible via SSL on a page
23 * using the HTTPS protocol. Defaults to false. Note that this will only work if the page calling this code uses the
24 * HTTPS protocol, otherwise the cookie will be created with default options.
25 */
26 set : function(name, value){
27 var argv = arguments,
28 argc = arguments.length,
29 expires = (argc > 2) ? argv[2] : null,
30 path = (argc > 3) ? argv[3] : '/',
31 domain = (argc > 4) ? argv[4] : null,
32 secure = (argc > 5) ? argv[5] : false;
33
34 document.cookie = name + "=" +
35 escape(value) +
36 ((expires === null) ? "" : ("; expires=" + expires.toUTCString())) +
37 ((path === null) ? "" : ("; path=" + path)) +
38 ((domain === null) ? "" : ("; domain=" + domain)) +
39 ((secure === true) ? "; secure" : "");
40 },
41
42 /**
43 * Retrieves cookies that are accessible by the current page. If a cookie does not exist, `get()` returns null. The
44 * following example retrieves the cookie called "valid" and stores the String value in the variable validStatus.
45 *
46 * var validStatus = Ext.util.Cookies.get("valid");
47 *
48 * @param {String} name The name of the cookie to get
49 * @return {Object} Returns the cookie value for the specified name;
50 * null if the cookie name does not exist.
51 */
52 get : function(name) {
53 var parts = document.cookie.split('; '),
54 len = parts.length,
55 item, i, ret;
56
57 // In modern browsers, a cookie with an empty string will be stored:
58 // MyName=
59 // In older versions of IE, it will be stored as:
60 // MyName
61 // So here we iterate over all the parts in an attempt to match the key.
62 for (i = 0; i < len; ++i) {
63 item = parts[i].split('=');
64 if (item[0] === name) {
65 ret = item[1];
66 return ret ? unescape(ret) : '';
67 }
68 }
69 return null;
70 },
71
72 /**
73 * Removes a cookie with the provided name from the browser
74 * if found by setting its expiration date to sometime in the past.
75 * @param {String} name The name of the cookie to remove
76 * @param {String} [path] The path for the cookie.
77 * This must be included if you included a path while setting the cookie.
78 */
79 clear : function(name, path){
80 if (this.get(name)) {
81 path = path || '/';
82 document.cookie = name + '=' + '; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=' + path;
83 }
84 }
85 });