1/** 2 * Handles the cookie used by several JavaScript functions 3 * 4 * Only a single cookie is written and read. You may only save 5 * sime name-value pairs - no complex types! 6 * 7 * 8 * @author Andreas Gohr <andi@splitbrain.org> 9 * @author Michal Rezler <m.rezler@centrum.cz> 10 */ 11 12var setDokuCookie, getDokuCookie; 13 14(function ($) { 15 var init, setCookie, fixDate; 16 17 var data = Array(); 18 var name = 'DOKU_PREFS'; 19 20 /** 21 * Save a value to the cookie 22 * 23 * @author Andreas Gohr <andi@splitbrain.org> 24 */ 25 setDokuCookie = function(key,val){ 26 init(); 27 data[key] = val; 28 29 // prepare expire date 30 var now = new Date(); 31 fixDate(now); 32 now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000); //expire in a year 33 34 //save the whole data array 35 var text = ''; 36 for(var key in data){ 37 if (!data.hasOwnProperty(key)) continue; 38 text += '#'+escape(key)+'#'+data[key]; 39 } 40 setCookie(name,text.substr(1),now,DOKU_BASE); 41 }; 42 43 /** 44 * Get a Value from the Cookie 45 * 46 * @author Andreas Gohr <andi@splitbrain.org> 47 */ 48 getDokuCookie = function(key){ 49 init(); 50 return data[key]; 51 }; 52 53 /** 54 * Loads the current set cookie 55 * 56 * @author Andreas Gohr <andi@splitbrain.org> 57 */ 58 init = function(){ 59 if(data.length) return; 60 var text = $.cookie(name); 61 62 if(text){ 63 var parts = text.split('#'); 64 for(var i=0; i<parts.length; i+=2){ 65 data[unescape(parts[i])] = unescape(parts[i+1]); 66 } 67 } 68 }; 69 70 /** 71 * This sets a cookie 72 * 73 */ 74 setCookie = function(name, value, expires_, path_, domain_, secure_) { 75 var params = { 76 expires: expires_, 77 path: path_, 78 domain: domain_, 79 secure: secure_, 80 }; 81 $.cookie(name, value, params); 82 }; 83 84 /** 85 * This is needed for the cookie functions 86 * 87 * @link http://www.webreference.com/js/column8/functions.html 88 */ 89 fixDate = function(date) { 90 var base = new Date(0); 91 var skew = base.getTime(); 92 if (skew > 0){ 93 date.setTime(date.getTime() - skew); 94 } 95 }; 96 97}(jQuery)); 98