summaryrefslogtreecommitdiffstats
path: root/tools/Sandcastle/Presentation/hana/Scripts/DataStore.js
blob: 12e072cf8bdb648acef48521d06972da25170de3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// cookie data store
function DataStore(name) 
{
    this.name = name;
    this.load();
}
			
DataStore.prototype.load = function () 
{
    // create a key/value store
	this.language = new Object();

	// get cookie text
	var text = getCookie(this.name);
	
	if (text == null) return;

	// populate the store using the cookie text
	var data = text.split(';');

	for (var i=0; i<data.length; i++) 
	{
	    var datum = data[i];
		var index = datum.indexOf('=');
		
		if (index > 0) 
		{
		    var key = datum.substring(0,index);
			var value = datum.substring(index+1);
			this.language[key] = value;
		}
	}
	
}
			
function setCookie(name, value, expires, path, domain, secure) 
{
    var text = name + "=" + escape(value);
	
	if (expires) 
	{
	
	    var currentDate = new Date();
		var expireDate = new Date( currentDate.getTime() + expires*24*60*60*1000 );
		text = text + ";expires=" + expireDate.toGMTString();
	}
	if (path) text = text + ";path=" + path;
	if (domain) text = text + ";domain=" + domain;
	if (secure) text = text + ";secure";

	document.cookie = text;
}

function removeCookie(name) 
{
    setCookie(name, "", -1);
}

function getCookie(name) 
{
    var text = document.cookie;
    
	var index = text.indexOf(name + "=");
				
	if (index < 0) return(null);
    
    var start = index + name.length + 1;
    var end = text.indexOf(";", start);
	
	if (end < 0) end = text.length;

	var value = unescape( text.substring(start, end) );
	return(value);
}

DataStore.prototype.set = function(key, value) 
{
    this.language[key] = value;
}

DataStore.prototype.get = function(key) 
{
    return(this.language[key]);
}
			
DataStore.prototype.clear = function () 
{
    this.language = new Object();
}	

DataStore.prototype.save = function () 
{
    // prepare a cookie string
	var text = "";

	// construct the string
	for (var key in this.language) 
	{
	    var datum = key + "=" + this.language[key];
		text = text + datum + ";";
	}
				
	// set it
	setCookie(this.name, text);
}

DataStore.prototype.count = function() 
{
    var i = 0;
	for (var key in this.data) 
	{
	    i++;
	}
	return(i);				
}