Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.io.cookie");
14
dojo.io.cookie.setCookie = function (name, value, days, path, domain, secure) {
15
	var expires = -1;
16
	if ((typeof days == "number") && (days >= 0)) {
17
		var d = new Date();
18
		d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
19
		expires = d.toGMTString();
20
	}
21
	value = escape(value);
22
	document.cookie = name + "=" + value + ";" + (expires != -1 ? " expires=" + expires + ";" : "") + (path ? "path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "; secure" : "");
23
};
24
dojo.io.cookie.set = dojo.io.cookie.setCookie;
25
dojo.io.cookie.getCookie = function (name) {
26
	var idx = document.cookie.lastIndexOf(name + "=");
27
	if (idx == -1) {
28
		return null;
29
	}
30
	var value = document.cookie.substring(idx + name.length + 1);
31
	var end = value.indexOf(";");
32
	if (end == -1) {
33
		end = value.length;
34
	}
35
	value = value.substring(0, end);
36
	value = unescape(value);
37
	return value;
38
};
39
dojo.io.cookie.get = dojo.io.cookie.getCookie;
40
dojo.io.cookie.deleteCookie = function (name) {
41
	dojo.io.cookie.setCookie(name, "-", 0);
42
};
43
dojo.io.cookie.setObjectCookie = function (name, obj, days, path, domain, secure, clearCurrent) {
44
	if (arguments.length == 5) {
45
		clearCurrent = domain;
46
		domain = null;
47
		secure = null;
48
	}
49
	var pairs = [], cookie, value = "";
50
	if (!clearCurrent) {
51
		cookie = dojo.io.cookie.getObjectCookie(name);
52
	}
53
	if (days >= 0) {
54
		if (!cookie) {
55
			cookie = {};
56
		}
57
		for (var prop in obj) {
58
			if (obj[prop] == null) {
59
				delete cookie[prop];
60
			} else {
61
				if ((typeof obj[prop] == "string") || (typeof obj[prop] == "number")) {
62
					cookie[prop] = obj[prop];
63
				}
64
			}
65
		}
66
		prop = null;
67
		for (var prop in cookie) {
68
			pairs.push(escape(prop) + "=" + escape(cookie[prop]));
69
		}
70
		value = pairs.join("&");
71
	}
72
	dojo.io.cookie.setCookie(name, value, days, path, domain, secure);
73
};
74
dojo.io.cookie.getObjectCookie = function (name) {
75
	var values = null, cookie = dojo.io.cookie.getCookie(name);
76
	if (cookie) {
77
		values = {};
78
		var pairs = cookie.split("&");
79
		for (var i = 0; i < pairs.length; i++) {
80
			var pair = pairs[i].split("=");
81
			var value = pair[1];
82
			if (isNaN(value)) {
83
				value = unescape(pair[1]);
84
			}
85
			values[unescape(pair[0])] = value;
86
		}
87
	}
88
	return values;
89
};
90
dojo.io.cookie.isSupported = function () {
91
	if (typeof navigator.cookieEnabled != "boolean") {
92
		dojo.io.cookie.setCookie("__TestingYourBrowserForCookieSupport__", "CookiesAllowed", 90, null);
93
		var cookieVal = dojo.io.cookie.getCookie("__TestingYourBrowserForCookieSupport__");
94
		navigator.cookieEnabled = (cookieVal == "CookiesAllowed");
95
		if (navigator.cookieEnabled) {
96
			this.deleteCookie("__TestingYourBrowserForCookieSupport__");
97
		}
98
	}
99
	return navigator.cookieEnabled;
100
};
101
if (!dojo.io.cookies) {
102
	dojo.io.cookies = dojo.io.cookie;
103
}
104