xref: /dokuwiki/lib/scripts/cookie.js (revision 0fbb95872b10d1318aa1ac7be20470a2d07dd7a1)
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* simple 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* @author Michal Rezler <m.rezler@centrum.cz>
11*/
12DokuCookie = {
13    data: {},
14    name: 'DOKU_PREFS',
15
16    /**
17     * Save a value to the cookie
18     *
19     * @author Andreas Gohr <andi@splitbrain.org>
20     */
21    setValue: function(key,val){
22        var text = '';
23        this.init();
24        this.data[key] = val;
25
26        //save the whole data array
27        jQuery.each(this.data, function (key, val) {
28            if (DokuCookie.data.hasOwnProperty(key)) {
29                text += '#'+encodeURIComponent(key)+'#'+encodeURIComponent(val);
30        }
31        });
32        jQuery.cookie(this.name,text.substr(1), {expires: 365, path: DOKU_BASE});
33    },
34
35    /**
36     * Get a Value from the Cookie
37     *
38     * @author Andreas Gohr <andi@splitbrain.org>
39     */
40    getValue: function(key){
41        this.init();
42        return this.data[key];
43    },
44
45    /**
46     * Loads the current set cookie
47     *
48     * @author Andreas Gohr <andi@splitbrain.org>
49     */
50    init: function(){
51        var text, parts, i;
52        if(!jQuery.isEmptyObject(this.data)) {
53            return;
54        }
55        text = jQuery.cookie(this.name);
56        if(text){
57            parts = text.split('#');
58            for(i = 0; i < parts.length; i += 2){
59                this.data[decodeURIComponent(parts[i])] = decodeURIComponent(parts[i+1]);
60            }
61        }
62        }
63};
64