9 A javascript module for handling the User Preferences cookie
13 Christopher Carpita <csc32@cornell.edu>
19 JSAN.use("CXGN.Base");
20 JSAN.use("CXGN.Cookie");
21 JSAN.use("CXGN.User");
22 JSAN.use("MochiKit.Logging");
24 UserPrefs = window.UserPrefs || {};
29 UserPrefs.parse(Cookie.get('user_prefs'));
33 =head2 parse( cookie_string )
35 Given a cookie_string, parses out key/value relationships and sets
36 them to the object (assc. array) UserPrefs.data
41 parse: function(cookie_string) {
42 cookie_string = decodeURIComponent(cookie_string);
43 var kvps = cookie_string.split(':');
44 for(var j = 0; j < kvps.length; j++){
45 var kv = kvps[j].split('=');
48 UserPrefs.data[k] = v;
55 Build and returncookie string from UserPrefs.data object (assc. array)
61 var data = UserPrefs.data;
62 var cookie_string = '';
65 if(i>0) cookie_string += ":";
66 cookie_string += encodeURIComponent(key) + "=" + encodeURIComponent(data[key])
69 return encodeURIComponent(cookie_string);
76 Calls build() and sets this to the cookie
82 var thisDate = new Date();
83 var base = new Date(0);
84 var skew = base.getTime();
85 var unix_epoch_time = thisDate.getTime();
86 if (skew > 0) unix_epoch_time -= skew; //Steve Jobs had nothing to do with this, apparently
87 UserPrefs.setValue('timestamp', unix_epoch_time);
88 Cookie.set('user_prefs', UserPrefs.build(), 1000);
94 Gets the current value for the key in the associative array
99 getValue: function(key) {
100 return UserPrefs.data[key];
104 =head2 setValue(key, value)
106 Given a key and value, sets the associative array
111 setValue: function(key,value) {
112 UserPrefs.data[key] = value;
118 DEPRECATED CODE that doesn't use double-encoding
122 user_pref_string: '',
123 _preferences: new Object,
125 var user_pref_string = UserPrefs.user_pref_string;
126 //the string is initially set server-side, so if we don't get
127 //anything, we try the client-side cookie
128 if(user_pref_string.length<1) UserPrefs.loadCookie();
130 if(user_pref_string.length>0 && user_pref_string.indexOf('=') && user_pref_string.indexOf(":")) {
131 var pref_array = user_pref_string.split(":");
132 for(var n = 0; n < pref_array.length; n++) {
133 var key_val = pref_array[n].split("=");
134 if(key_val[0].length>0 && key_val[1].length>0){
135 UserPrefs._preferences[key_val[0]] = key_val[1];
139 UserPrefs.setCookie();
142 setCookie: function() {
143 var thisDate = new Date();
144 var base = new Date(0);
145 var skew = base.getTime();
146 var unix_epoch_time = thisDate.getTime();
147 if (skew > 0) unix_epoch_time -= skew; //Steve Jobs had nothing to do with this, apparently
148 UserPrefs._preferences.timestamp = unix_epoch_time;
149 UserPrefs._preferences.sp_person_id = User.sgn_user_id;
150 UserPrefs._preferences.sgn_session_id = User.sgn_session_id;
151 UserPrefs._buildString();
152 Cookie.set('user_prefs', UserPrefs.user_pref_string, 1000);
154 //send will make an AJAX request to the server, writing the cookie string to the database. neat-o!
155 //ONLY use this when you anticipate that the user will set a preference and not load another page. The page-loading process will update the user_prefs string in the database from the user's cookie.
157 var req = new Request();
159 var parameters = "?sgn_session_id=" + User.sgn_session_id + "&user_prefs=" + encodeURIComponent(UserPrefs.user_pref_string);
160 req.sendRequest("/scraps/set_user_prefs.pl", parameters);
165 _response: function(doc) {
166 var updated = doc.getElementsByTagName("updated")[0].firstChild.nodeValue;
167 var errmsgtag = doc.getElementsByTagName("errmsg")[0];
168 var fatalmsgtag = doc.getElementsByTagName("fatalmsg")[0];
170 MochiKit.Logging.log("UserPref setting successful");
173 MochiKit.Logging.logError("UserPref setting failed!");
175 MochiKit.Logging.logError("\n"+errmsgtag.firstChild.nodeValue);
178 MochiKit.Logging.logFatal("\n"+fatalmsgtag.firstChild.nodeValue);
183 loadCookie: function() {
184 UserPrefs.user_pref_string = Cookie.get('user_prefs');
186 _buildString: function() {
189 for(var name in UserPrefs._preferences){
190 if(i>0) new_string += ":";
191 new_string += name + "=" + UserPrefs._preferences[name];
194 UserPrefs.user_pref_string = new_string;
196 set: function(name, value) {
197 UserPrefs._preferences[name] = value;
199 get: function(name) {
200 return UserPrefs._preferences[name];