xref: /dokuwiki/lib/scripts/cookie.js (revision 9a3288d64bca5fe18d3c72e8a6a73d064ac15e43)
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 * You should only use the getValue and setValue methods
8 *
9 * @author Andreas Gohr <andi@splitbrain.org>
10 */
11DokuCookie = {
12    data: Array(),
13    name: 'DOKU_PREFS',
14
15    /**
16     * Save a value to the cookie
17     *
18     * @author Andreas Gohr <andi@splitbrain.org>
19     */
20    setValue: function(key,val){
21        DokuCookie.init();
22        DokuCookie.data[key] = val;
23
24        // prepare expire date
25        var now = new Date();
26        DokuCookie.fixDate(now);
27        now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000); //expire in a year
28
29        //save the whole data array
30        var text = '';
31        for(var key in DokuCookie.data){
32            text += '#'+escape(key)+'#'+DokuCookie.data[key];
33        }
34        DokuCookie.setCookie(DokuCookie.name,text.substr(1),now,DOKU_BASE);
35    },
36
37    /**
38     * Get a Value from the Cookie
39     *
40     * @author Andreas Gohr <andi@splitbrain.org>
41     */
42    getValue: function(key){
43        DokuCookie.init();
44        return DokuCookie.data[key];
45    },
46
47    /**
48     * Loads the current set cookie
49     *
50     * @author Andreas Gohr <andi@splitbrain.org>
51     */
52    init: function(){
53        if(DokuCookie.data.length) return;
54        var text  = DokuCookie.getCookie(DokuCookie.name);
55        if(text){
56            var parts = text.split('#');
57            for(var i=0; i<parts.length; i+=2){
58                DokuCookie.data[unescape(parts[i])] = unescape(parts[i+1]);
59            }
60        }
61    },
62
63    /**
64     * This sets a cookie by JavaScript
65     *
66     * @link http://www.webreference.com/js/column8/functions.html
67     */
68    setCookie: function(name, value, expires, path, domain, secure) {
69        var curCookie = name + "=" + escape(value) +
70            ((expires) ? "; expires=" + expires.toGMTString() : "") +
71            ((path) ? "; path=" + path : "") +
72            ((domain) ? "; domain=" + domain : "") +
73            ((secure) ? "; secure" : "");
74        document.cookie = curCookie;
75    },
76
77    /**
78     * This reads a cookie by JavaScript
79     *
80     * @link http://www.webreference.com/js/column8/functions.html
81     */
82    getCookie: function(name) {
83        var dc = document.cookie;
84        var prefix = name + "=";
85        var begin = dc.indexOf("; " + prefix);
86        if (begin == -1) {
87            begin = dc.indexOf(prefix);
88            if (begin !== 0){ return null; }
89        } else {
90            begin += 2;
91        }
92        var end = document.cookie.indexOf(";", begin);
93        if (end == -1){
94            end = dc.length;
95        }
96        return unescape(dc.substring(begin + prefix.length, end));
97    },
98
99    /**
100     * This is needed for the cookie functions
101     *
102     * @link http://www.webreference.com/js/column8/functions.html
103     */
104    fixDate: function(date) {
105        var base = new Date(0);
106        var skew = base.getTime();
107        if (skew > 0){
108            date.setTime(date.getTime() - skew);
109        }
110    }
111};
112