Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | 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
 
11
dojo.provide("dojo.io.cookie");
12
dojo.io.cookie.setCookie = function (name, value, days, path, domain, secure) {
13
	var expires = -1;
14
	if ((typeof days == "number") && (days >= 0)) {
15
		var d = new Date();
16
		d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
17
		expires = d.toGMTString();
18
	}
19
	value = escape(value);
20
	document.cookie = name + "=" + value + ";" + (expires != -1 ? " expires=" + expires + ";" : "") + (path ? "path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "; secure" : "");
21
};
22
dojo.io.cookie.set = dojo.io.cookie.setCookie;
23
dojo.io.cookie.getCookie = function (name) {
24
	var idx = document.cookie.lastIndexOf(name + "=");
25
	if (idx == -1) {
26
		return null;
27
	}
28
	var value = document.cookie.substring(idx + name.length + 1);
29
	var end = value.indexOf(";");
30
	if (end == -1) {
31
		end = value.length;
32
	}
33
	value = value.substring(0, end);
34
	value = unescape(value);
35
	return value;
36
};
37
dojo.io.cookie.get = dojo.io.cookie.getCookie;
38
dojo.io.cookie.deleteCookie = function (name) {
39
	dojo.io.cookie.setCookie(name, "-", 0);
40
};
41
dojo.io.cookie.setObjectCookie = function (name, obj, days, path, domain, secure, clearCurrent) {
42
	if (arguments.length == 5) {
43
		clearCurrent = domain;
44
		domain = null;
45
		secure = null;
46
	}
47
	var pairs = [], cookie, value = "";
48
	if (!clearCurrent) {
49
		cookie = dojo.io.cookie.getObjectCookie(name);
50
	}
51
	if (days >= 0) {
52
		if (!cookie) {
53
			cookie = {};
54
		}
55
		for (var prop in obj) {
56
			if (obj[prop] == null) {
57
				delete cookie[prop];
58
			} else {
59
				if ((typeof obj[prop] == "string") || (typeof obj[prop] == "number")) {
60
					cookie[prop] = obj[prop];
61
				}
62
			}
63
		}
64
		prop = null;
65
		for (var prop in cookie) {
66
			pairs.push(escape(prop) + "=" + escape(cookie[prop]));
67
		}
68
		value = pairs.join("&");
69
	}
70
	dojo.io.cookie.setCookie(name, value, days, path, domain, secure);
71
};
72
dojo.io.cookie.getObjectCookie = function (name) {
73
	var values = null, cookie = dojo.io.cookie.getCookie(name);
74
	if (cookie) {
75
		values = {};
76
		var pairs = cookie.split("&");
77
		for (var i = 0; i < pairs.length; i++) {
78
			var pair = pairs[i].split("=");
79
			var value = pair[1];
80
			if (isNaN(value)) {
81
				value = unescape(pair[1]);
82
			}
83
			values[unescape(pair[0])] = value;
84
		}
85
	}
86
	return values;
87
};
88
dojo.io.cookie.isSupported = function () {
89
	if (typeof navigator.cookieEnabled != "boolean") {
90
		dojo.io.cookie.setCookie("__TestingYourBrowserForCookieSupport__", "CookiesAllowed", 90, null);
91
		var cookieVal = dojo.io.cookie.getCookie("__TestingYourBrowserForCookieSupport__");
92
		navigator.cookieEnabled = (cookieVal == "CookiesAllowed");
93
		if (navigator.cookieEnabled) {
94
			this.deleteCookie("__TestingYourBrowserForCookieSupport__");
95
		}
96
	}
97
	return navigator.cookieEnabled;
98
};
99
if (!dojo.io.cookies) {
100
	dojo.io.cookies = dojo.io.cookie;
101
}
102