PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 0ms

/Documentation/Help/scripts/CookieDataStore.js

#
JavaScript | 136 lines | 88 code | 40 blank | 8 comment | 19 complexity | 4883587e079cf93494e8de965bb45304 MD5 | raw file
  1
  2
  3			function setCookie(name, value, expires, path, domain, secure) {
  4				
  5				var text = name + "=" + escape(value);
  6
  7				if (expires) {
  8					var currentDate = new Date();
  9					var expireDate = new Date( currentDate.getTime() + expires*24*60*60*1000 );
 10					text = text + ";expires=" + expireDate.toGMTString();
 11				}
 12				if (path) text = text + ";path=" + path;
 13				if (domain) text = text + ";domain=" + domain;
 14				if (secure) text = text + ";secure";
 15
 16				document.cookie = text;
 17			}
 18
 19			function getCookie(name) {
 20
 21				var text = document.cookie;
 22
 23				var index = text.indexOf(name + "=");
 24				if (index < 0) return(null);
 25
 26				var start = index + name.length + 1;
 27
 28				var end = text.indexOf(";", start);
 29				if (end < 0) end = text.length;
 30
 31				var value = unescape( text.substring(start, end) );
 32				return(value);
 33				
 34			}
 35
 36			function removeCookie(name) {
 37				setCookie(name, "", -1);
 38			}
 39
 40
 41			// cookie data store
 42
 43			function CookieDataStore(name) {
 44				this.name = name;
 45				this.load();
 46			}
 47
 48			CookieDataStore.prototype.load = function () {
 49
 50				// create a key/value store
 51				this.data = new Object();
 52
 53				// get cookie text
 54				var text = getCookie(this.name);
 55				if (text == null) return;
 56
 57				// populate the store using the cookie text
 58				var data = text.split(';');
 59
 60				for (var i=0; i<data.length; i++) {
 61					var datum = data[i];
 62					var index = datum.indexOf('=');
 63					if (index > 0) {
 64						var key = datum.substring(0,index);
 65						var value = datum.substring(index+1);
 66						this.data[key] = value;
 67					}
 68				}
 69
 70			}
 71
 72			CookieDataStore.prototype.save = function () {
 73
 74				// prepare a cookie string
 75				var text = "";
 76
 77				// construct the string
 78				for (var key in this.data) {
 79					var datum = key + "=" + this.data[key];
 80					text = text + datum + ";";
 81				}
 82
 83				// set it
 84				setCookie(this.name, text);
 85
 86			}
 87
 88			CookieDataStore.prototype.clear = function () {
 89				this.data = new Object();
 90			}
 91
 92			CookieDataStore.prototype.set = function(key, value) {
 93				this.data[key] = value;
 94			}
 95
 96			CookieDataStore.prototype.get = function(key) {
 97				return(this.data[key]);
 98			}
 99
100			CookieDataStore.prototype.remove = function(key) {
101				delete(this.data[key]);
102			}
103
104			CookieDataStore.prototype.count = function() {
105				var i = 0;
106				for (var key in this.data) {
107					i++;
108				}
109				return(i);				
110			}
111
112		// The following logic needs to be re-factored out of this file
113
114		function selectLanguage(value) {
115
116			if (value == null) return;
117
118			var selector = document.getElementById('languageSelector');
119			if (selector == null) return;
120
121			var options = selector.options;
122			for(var i=0; i<options.length; i++) {
123				if (options[i].value == value) {
124					selector.selectedIndex = i;
125					setLanguage(value);
126				}
127			}						
128
129		}
130
131
132		function setLanguage(value) {
133			var names = value.split(' ');
134			toggleVisibleLanguage(names[1]);
135			lfc.switchLanguage(names[0]);
136		}