diff options
author | Michal Rezler <rezlemic@fel.cvut.cz> | 2011-03-26 15:42:22 +0100 |
---|---|---|
committer | Michal Rezler <rezlemic@fel.cvut.cz> | 2011-03-26 15:42:22 +0100 |
commit | fdfb9c6a2877dcbadeab697aad779bea76780965 (patch) | |
tree | abcf9001620e93fd471f53c396f905a14142c269 /lib/scripts/cookie.js | |
parent | 3044fa04f4879fe194de48009c13d119ac7782a5 (diff) | |
download | rpg-fdfb9c6a2877dcbadeab697aad779bea76780965.tar.gz rpg-fdfb9c6a2877dcbadeab697aad779bea76780965.tar.bz2 |
cookie.js is jQueryfied, added jQuery-cookie-plugin
Diffstat (limited to 'lib/scripts/cookie.js')
-rw-r--r-- | lib/scripts/cookie.js | 99 |
1 files changed, 42 insertions, 57 deletions
diff --git a/lib/scripts/cookie.js b/lib/scripts/cookie.js index d7e6b3550..1fccf85ba 100644 --- a/lib/scripts/cookie.js +++ b/lib/scripts/cookie.js @@ -4,109 +4,94 @@ * Only a single cookie is written and read. You may only save * sime name-value pairs - no complex types! * - * You should only use the getValue and setValue methods * * @author Andreas Gohr <andi@splitbrain.org> + * @author Michal Rezler <m.rezler@centrum.cz> */ -DokuCookie = { - data: Array(), - name: 'DOKU_PREFS', + +var setDokuCookie, getDokuCookie; + +(function ($) { + var init, setCookie, fixDate; + + var data = Array(); + var name = 'DOKU_PREFS'; /** * Save a value to the cookie * * @author Andreas Gohr <andi@splitbrain.org> */ - setValue: function(key,val){ - DokuCookie.init(); - DokuCookie.data[key] = val; + setDokuCookie = function(key,val){ + init(); + data[key] = val; // prepare expire date var now = new Date(); - DokuCookie.fixDate(now); + fixDate(now); now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000); //expire in a year //save the whole data array var text = ''; - for(var key in DokuCookie.data){ - if (!DokuCookie.data.hasOwnProperty(key)) continue; - text += '#'+escape(key)+'#'+DokuCookie.data[key]; + for(var key in data){ + if (!data.hasOwnProperty(key)) continue; + text += '#'+escape(key)+'#'+data[key]; } - DokuCookie.setCookie(DokuCookie.name,text.substr(1),now,DOKU_BASE); - }, + setCookie(name,text.substr(1),now,DOKU_BASE); + }; /** * Get a Value from the Cookie * * @author Andreas Gohr <andi@splitbrain.org> */ - getValue: function(key){ - DokuCookie.init(); - return DokuCookie.data[key]; - }, + getDokuCookie = function(key){ + init(); + return data[key]; + }; /** * Loads the current set cookie * * @author Andreas Gohr <andi@splitbrain.org> */ - init: function(){ - if(DokuCookie.data.length) return; - var text = DokuCookie.getCookie(DokuCookie.name); + init = function(){ + if(data.length) return; + var text = $.cookie(name); + if(text){ var parts = text.split('#'); for(var i=0; i<parts.length; i+=2){ - DokuCookie.data[unescape(parts[i])] = unescape(parts[i+1]); + data[unescape(parts[i])] = unescape(parts[i+1]); } } - }, + }; /** - * This sets a cookie by JavaScript + * This sets a cookie * - * @link http://www.webreference.com/js/column8/functions.html */ - setCookie: function(name, value, expires, path, domain, secure) { - var curCookie = name + "=" + escape(value) + - ((expires) ? "; expires=" + expires.toGMTString() : "") + - ((path) ? "; path=" + path : "") + - ((domain) ? "; domain=" + domain : "") + - ((secure) ? "; secure" : ""); - document.cookie = curCookie; - }, - - /** - * This reads a cookie by JavaScript - * - * @link http://www.webreference.com/js/column8/functions.html - */ - getCookie: function(name) { - var dc = document.cookie; - var prefix = name + "="; - var begin = dc.indexOf("; " + prefix); - if (begin == -1) { - begin = dc.indexOf(prefix); - if (begin !== 0){ return null; } - } else { - begin += 2; - } - var end = document.cookie.indexOf(";", begin); - if (end == -1){ - end = dc.length; - } - return unescape(dc.substring(begin + prefix.length, end)); - }, + setCookie = function(name, value, expires_, path_, domain_, secure_) { + var params = { + expires: expires_, + path: path_, + domain: domain_, + secure: secure_, + }; + $.cookie(name, value, params); + }; /** * This is needed for the cookie functions * * @link http://www.webreference.com/js/column8/functions.html */ - fixDate: function(date) { + fixDate = function(date) { var base = new Date(0); var skew = base.getTime(); if (skew > 0){ date.setTime(date.getTime() - skew); } - } -}; + }; + +}(jQuery)); |