adding a semicolon... yay!
[cxgn-jslib.git] / CXGN / UserPrefs.js
blob7f4755ac8c6f911c26243db0d86480c1091c5a8f
1 /**
3 =head1 NAME
5 CXGN.UserPrefs
7 =head1 SYNOPSIS
9 A javascript module for handling the User Preferences cookie
11 =head1 AUTHOR
13 Christopher Carpita <csc32@cornell.edu>
15 =cut
19 JSAN.use("CXGN.Base");
20 JSAN.use("CXGN.Cookie");
21 JSAN.use("CXGN.User");
22 JSAN.use("MochiKit.Logging");
24 UserPrefs = window.UserPrefs || {};
26 var UserPrefs = {
27 data: new Object,
28 _init: function() {
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
38 =cut
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('=');
46 var k = kv[0];
47 var v = kv[1];
48 UserPrefs.data[k] = v;
53 =head2 build()
55 Build and returncookie string from UserPrefs.data object (assc. array)
57 =cut
60 build: function() {
61 var data = UserPrefs.data;
62 var cookie_string = '';
63 var i = 0;
64 for(key in data){
65 if(i>0) cookie_string += ":";
66 cookie_string += encodeURIComponent(key) + "=" + encodeURIComponent(data[key])
67 i++;
69 return encodeURIComponent(cookie_string);
74 =head2 save()
76 Calls build() and sets this to the cookie
78 =cut
81 save: function() {
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);
90 /*
92 =head2 getValue(key)
94 Gets the current value for the key in the associative array
96 =cut
99 getValue: function(key) {
100 return UserPrefs.data[key];
104 =head2 setValue(key, value)
106 Given a key and value, sets the associative array
108 =cut
111 setValue: function(key,value) {
112 UserPrefs.data[key] = value;
118 DEPRECATED CODE that doesn't use double-encoding
121 var UserPrefs = {
122 user_pref_string: '',
123 _preferences: new Object,
124 _init: function() {
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.
156 send: function() {
157 var req = new Request();
158 if(req.isValid()){
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);
161 return true;
163 return false;
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];
169 if(updated) {
170 MochiKit.Logging.log("UserPref setting successful");
172 else {
173 MochiKit.Logging.logError("UserPref setting failed!");
174 if(errmsgtag){
175 MochiKit.Logging.logError("\n"+errmsgtag.firstChild.nodeValue);
177 if(fatalmsgtag){
178 MochiKit.Logging.logFatal("\n"+fatalmsgtag.firstChild.nodeValue);
183 loadCookie: function() {
184 UserPrefs.user_pref_string = Cookie.get('user_prefs');
186 _buildString: function() {
187 var new_string = '';
188 var i = 0;
189 for(var name in UserPrefs._preferences){
190 if(i>0) new_string += ":";
191 new_string += name + "=" + UserPrefs._preferences[name];
192 i++;
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];
203 UserPrefs._init();