1 | /** |
---|
2 | * t29v6: user specific preferences (persistent storage via cookieS) |
---|
3 | * |
---|
4 | * t29.prefs is the domain for user settings, whereas |
---|
5 | * t29.conf is a global page specific storage for server side |
---|
6 | * information. |
---|
7 | * |
---|
8 | * Examples for typical content: |
---|
9 | * |
---|
10 | * where name typical content desc |
---|
11 | * ----- ---- --------------- ---- |
---|
12 | * t29.conf lang "de", "en" Page language |
---|
13 | * t29.conf seiten_id "suche" current page name |
---|
14 | * t29.prefs scroll... some menu prefs |
---|
15 | * |
---|
16 | * |
---|
17 | * 2012 Sven Koeppel, Public Domain |
---|
18 | * |
---|
19 | **/ |
---|
20 | |
---|
21 | if(!t29) window.t29 = {}; // the t29 namespace |
---|
22 | t29.prefs = {}; |
---|
23 | |
---|
24 | // configuration for prefs system |
---|
25 | t29.prefs.conf = { |
---|
26 | cookie_name: "t29prefs", |
---|
27 | cookie_options: { path: '/', expires: 7 }, |
---|
28 | }; |
---|
29 | |
---|
30 | // internal members (private) |
---|
31 | t29.prefs._store = {}; |
---|
32 | |
---|
33 | t29.prefs.setup = function() { |
---|
34 | c = t29.prefs.conf; |
---|
35 | // read initial data |
---|
36 | json_str = $.cookie(c.cookie_name); |
---|
37 | t29.prefs._store = json_str ? JSON.parse(json_str) : {}; |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * Get a setting value. The value will be read out from the local |
---|
42 | * storage which is synced by cookie on setup. |
---|
43 | * @param key Name of the setting |
---|
44 | * @param fallback_value default value if setting is not given yet. |
---|
45 | **/ |
---|
46 | t29.prefs.get = function(key, fallback_value) { |
---|
47 | if(fallback_value === undefined) fallback_value = null; |
---|
48 | return (t29.prefs._store[key] !== undefined) ? t29.prefs._store[key] : fallback_value; |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * Store a setting value. The cookie storage will be updated, too. |
---|
53 | * @param key Name of the setting. Shall be string by convention. |
---|
54 | * @param value Value to store |
---|
55 | **/ |
---|
56 | t29.prefs.set = function(key, value) { |
---|
57 | t29.prefs._store[key] = value; |
---|
58 | $.cookie(t29.prefs.conf.cookie_name, JSON.stringify(t29.prefs._store), t29.prefs.cookie_options); |
---|
59 | //log("new prefs: "+$.cookie(t29.prefs.conf.cookie_name)); |
---|
60 | }; |
---|
61 | |
---|
62 | t29.prefs.del = function() { |
---|
63 | // be nice and kill the cookie |
---|
64 | log("Delete called but not created yet"); |
---|
65 | //$.cookie(t29.prefs.conf.cookie_name, null, t29.prefs.cookie_options); |
---|
66 | }; |
---|
67 | |
---|
68 | /*! |
---|
69 | * jQuery Cookie Plugin |
---|
70 | * https://github.com/carhartl/jquery-cookie |
---|
71 | * |
---|
72 | * Copyright 2011, Klaus Hartl |
---|
73 | * Dual licensed under the MIT or GPL Version 2 licenses. |
---|
74 | * http://www.opensource.org/licenses/mit-license.php |
---|
75 | * http://www.opensource.org/licenses/GPL-2.0 |
---|
76 | */ |
---|
77 | (function($) { |
---|
78 | $.cookie = function(key, value, options) { |
---|
79 | |
---|
80 | // key and at least value given, set cookie... |
---|
81 | if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) { |
---|
82 | options = $.extend({}, $.cookie.defaults, options); |
---|
83 | |
---|
84 | if (value === null || value === undefined) { |
---|
85 | options.expires = -1; |
---|
86 | } |
---|
87 | |
---|
88 | if (typeof options.expires === 'number') { |
---|
89 | var days = options.expires, t = options.expires = new Date(); |
---|
90 | t.setDate(t.getDate() + days); |
---|
91 | } |
---|
92 | |
---|
93 | value = String(value); |
---|
94 | |
---|
95 | return (document.cookie = [ |
---|
96 | encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value), |
---|
97 | options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE |
---|
98 | options.path ? '; path=' + options.path : '', |
---|
99 | options.domain ? '; domain=' + options.domain : '', |
---|
100 | options.secure ? '; secure' : '' |
---|
101 | ].join('')); |
---|
102 | } |
---|
103 | |
---|
104 | // key and possibly options given, get cookie... |
---|
105 | options = value || $.cookie.defaults || {}; |
---|
106 | var decode = options.raw ? function(s) { return s; } : decodeURIComponent; |
---|
107 | |
---|
108 | var cookies = document.cookie.split('; '); |
---|
109 | for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) { |
---|
110 | if (decode(parts.shift()) === key) { |
---|
111 | return decode(parts.join('=')); |
---|
112 | } |
---|
113 | } |
---|
114 | return null; |
---|
115 | }; |
---|
116 | |
---|
117 | $.cookie.defaults = {}; |
---|
118 | |
---|
119 | })(jQuery); |
---|