aboutsummaryrefslogtreecommitdiff
path: root/engine/js/lib/session.js
diff options
context:
space:
mode:
authorewinslow <ewinslow@36083f99-b078-4883-b0ff-0f9b5a30f544>2010-11-01 07:34:24 +0000
committerewinslow <ewinslow@36083f99-b078-4883-b0ff-0f9b5a30f544>2010-11-01 07:34:24 +0000
commitf3fa93acb1063be2a2de88a6d2841b5d3a982d85 (patch)
tree50ab3704976d7416baa929673395867d3b07f39a /engine/js/lib/session.js
parent7e54b4efb86eecf550c23aa21146d7e191a2da0e (diff)
downloadelgg-f3fa93acb1063be2a2de88a6d2841b5d3a982d85.tar.gz
elgg-f3fa93acb1063be2a2de88a6d2841b5d3a982d85.tar.bz2
Refs #2538: Pulled in elgg JS object and unit tests
git-svn-id: http://code.elgg.org/elgg/trunk@7173 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'engine/js/lib/session.js')
-rw-r--r--engine/js/lib/session.js86
1 files changed, 86 insertions, 0 deletions
diff --git a/engine/js/lib/session.js b/engine/js/lib/session.js
new file mode 100644
index 000000000..446dbfac1
--- /dev/null
+++ b/engine/js/lib/session.js
@@ -0,0 +1,86 @@
+/**
+ * @todo comment
+ */
+elgg.provide('elgg.session');
+
+/**
+ * Helper function for setting cookies
+ * @param {string} name
+ * @param {string} value
+ * @param {Object} options
+ * {number|Date} options[expires]
+ * {string} options[path]
+ * {string} options[domain]
+ * {boolean} options[secure]
+ *
+ * @return {string} The value of the cookie, if only name is specified
+ */
+elgg.session.cookie = function(name, value, options) {
+ //elgg.session.cookie()
+ if(typeof name == 'undefined') {
+ return document.cookie;
+ }
+
+ //elgg.session.cookie(name)
+ if (typeof value == 'undefined') {
+ if (document.cookie && document.cookie != '') {
+ var cookies = document.cookie.split(';');
+ for (var i = 0; i < cookies.length; i++) {
+ var cookie = jQuery.trim(cookies[i]).split('=');
+ if (cookie[0] == name) {
+ return decodeURIComponent(cookie[1]);
+ }
+ }
+ }
+ return undefined;
+ }
+
+ // elgg.session.cookie(name, value[, opts])
+ var cookies = [];
+
+ options = options || {};
+
+ if (value === null) {
+ value = '';
+ options.expires = -1;
+ }
+
+ cookies.push(name + '=' + value);
+
+ if (typeof options.expires == 'number') {
+ var date, valid = true;
+
+ if (typeof options.expires == 'number') {
+ date = new Date();
+ date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
+ } else if(options.expires.toUTCString) {
+ date = options.expires;
+ } else {
+ valid = false;
+ }
+
+ valid ? cookies.push('expires=' + date.toUTCString()) : 0;
+ }
+
+ // CAUTION: Needed to parenthesize options.path and options.domain
+ // in the following expressions, otherwise they evaluate to undefined
+ // in the packed version for some reason.
+ if (options.path) {
+ cookies.push('path=' + (options.path));
+ }
+
+ if (options.domain) {
+ cookies.push('domain=' + (options.domain));
+ }
+
+ if (options.secure) {
+ cookies.push('secure');
+ }
+
+ document.cookie = cookies.join('; ');
+};
+
+/**
+ * @deprecated Use elgg.session.cookie instead
+ */
+$.cookie = elgg.session.cookie; \ No newline at end of file