1 // utility functions |
1 // utility functions |
2 function convertEntities(o) { |
|
3 var c, v; |
|
4 c = function(s) { |
|
5 if (/&[^;]+;/.test(s)) { |
|
6 var e = document.createElement("div"); |
|
7 e.innerHTML = s; |
|
8 return !e.firstChild ? s : e.firstChild.nodeValue; |
|
9 } |
|
10 return s; |
|
11 } |
|
12 |
|
13 if ( typeof o === 'string' ) { |
|
14 return c(o); |
|
15 } else if ( typeof o === 'object' ) { |
|
16 for (v in o) { |
|
17 if ( typeof o[v] === 'string' ) { |
|
18 o[v] = c(o[v]); |
|
19 } |
|
20 } |
|
21 } |
|
22 return o; |
|
23 } |
|
24 |
2 |
25 var wpCookies = { |
3 var wpCookies = { |
26 // The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL. |
4 // The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL. |
27 |
5 |
28 each : function(o, cb, s) { |
6 each : function(obj, cb, scope) { |
29 var n, l; |
7 var n, l; |
30 |
8 |
31 if (!o) |
9 if ( !obj ) |
32 return 0; |
10 return 0; |
33 |
11 |
34 s = s || o; |
12 scope = scope || obj; |
35 |
13 |
36 if (typeof(o.length) != 'undefined') { |
14 if ( typeof(obj.length) != 'undefined' ) { |
37 for (n=0, l = o.length; n<l; n++) { |
15 for ( n = 0, l = obj.length; n < l; n++ ) { |
38 if (cb.call(s, o[n], n, o) === false) |
16 if ( cb.call(scope, obj[n], n, obj) === false ) |
39 return 0; |
17 return 0; |
40 } |
18 } |
41 } else { |
19 } else { |
42 for (n in o) { |
20 for ( n in obj ) { |
43 if (o.hasOwnProperty(n)) { |
21 if ( obj.hasOwnProperty(n) ) { |
44 if (cb.call(s, o[n], n, o) === false) { |
22 if ( cb.call(scope, obj[n], n, obj) === false ) { |
45 return 0; |
23 return 0; |
46 } |
24 } |
47 } |
25 } |
48 } |
26 } |
49 } |
27 } |
50 return 1; |
28 return 1; |
51 }, |
29 }, |
52 |
30 |
53 getHash : function(n) { |
31 /** |
54 var v = this.get(n), h; |
32 * Get a multi-values cookie. |
|
33 * Returns a JS object with the name: 'value' pairs. |
|
34 */ |
|
35 getHash : function(name) { |
|
36 var all = this.get(name), ret; |
55 |
37 |
56 if (v) { |
38 if ( all ) { |
57 this.each(v.split('&'), function(v) { |
39 this.each( all.split('&'), function(pair) { |
58 v = v.split('='); |
40 pair = pair.split('='); |
59 h = h || {}; |
41 ret = ret || {}; |
60 h[v[0]] = v[1]; |
42 ret[pair[0]] = pair[1]; |
61 }); |
43 }); |
62 } |
44 } |
63 return h; |
45 return ret; |
64 }, |
46 }, |
65 |
47 |
66 setHash : function(n, v, e, p, d, s) { |
48 /** |
67 var o = ''; |
49 * Set a multi-values cookie. |
|
50 * |
|
51 * 'values_obj' is the JS object that is stored. It is encoded as URI in wpCookies.set(). |
|
52 */ |
|
53 setHash : function(name, values_obj, expires, path, domain, secure) { |
|
54 var str = ''; |
68 |
55 |
69 this.each(v, function(v, k) { |
56 this.each(values_obj, function(val, key) { |
70 o += (!o ? '' : '&') + k + '=' + v; |
57 str += (!str ? '' : '&') + key + '=' + val; |
71 }); |
58 }); |
72 |
59 |
73 this.set(n, o, e, p, d, s); |
60 this.set(name, str, expires, path, domain, secure); |
74 }, |
61 }, |
75 |
62 |
76 get : function(n) { |
63 /** |
77 var c = document.cookie, e, p = n + "=", b; |
64 * Get a cookie. |
|
65 */ |
|
66 get : function(name) { |
|
67 var cookie = document.cookie, e, p = name + "=", b; |
78 |
68 |
79 if (!c) |
69 if ( !cookie ) |
80 return; |
70 return; |
81 |
71 |
82 b = c.indexOf("; " + p); |
72 b = cookie.indexOf("; " + p); |
83 |
73 |
84 if (b == -1) { |
74 if ( b == -1 ) { |
85 b = c.indexOf(p); |
75 b = cookie.indexOf(p); |
86 |
76 |
87 if (b != 0) |
77 if ( b != 0 ) |
88 return null; |
78 return null; |
89 |
79 |
90 } else { |
80 } else { |
91 b += 2; |
81 b += 2; |
92 } |
82 } |
93 |
83 |
94 e = c.indexOf(";", b); |
84 e = cookie.indexOf(";", b); |
95 |
85 |
96 if (e == -1) |
86 if ( e == -1 ) |
97 e = c.length; |
87 e = cookie.length; |
98 |
88 |
99 return decodeURIComponent(c.substring(b + p.length, e)); |
89 return decodeURIComponent( cookie.substring(b + p.length, e) ); |
100 }, |
90 }, |
101 |
91 |
102 set : function(n, v, e, p, d, s) { |
92 /** |
103 document.cookie = n + "=" + encodeURIComponent(v) + |
93 * Set a cookie. |
104 ((e) ? "; expires=" + e.toGMTString() : "") + |
94 * |
105 ((p) ? "; path=" + p : "") + |
95 * The 'expires' arg can be either a JS Date() object set to the expiration date (back-compat) |
106 ((d) ? "; domain=" + d : "") + |
96 * or the number of seconds until expiration |
107 ((s) ? "; secure" : ""); |
97 */ |
|
98 set : function(name, value, expires, path, domain, secure) { |
|
99 var d = new Date(); |
|
100 |
|
101 if ( typeof(expires) == 'object' && expires.toGMTString ) { |
|
102 expires = expires.toGMTString(); |
|
103 } else if ( parseInt(expires, 10) ) { |
|
104 d.setTime( d.getTime() + ( parseInt(expires, 10) * 1000 ) ); // time must be in miliseconds |
|
105 expires = d.toGMTString(); |
|
106 } else { |
|
107 expires = ''; |
|
108 } |
|
109 |
|
110 document.cookie = name + "=" + encodeURIComponent(value) + |
|
111 ((expires) ? "; expires=" + expires : "") + |
|
112 ((path) ? "; path=" + path : "") + |
|
113 ((domain) ? "; domain=" + domain : "") + |
|
114 ((secure) ? "; secure" : ""); |
108 }, |
115 }, |
109 |
116 |
110 remove : function(n, p) { |
117 /** |
111 var d = new Date(); |
118 * Remove a cookie. |
112 |
119 * |
113 d.setTime(d.getTime() - 1000); |
120 * This is done by setting it to an empty value and setting the expiration time in the past. |
114 |
121 */ |
115 this.set(n, '', d, p, d); |
122 remove : function(name, path) { |
|
123 this.set(name, '', -1000, path); |
116 } |
124 } |
117 }; |
125 }; |
118 |
126 |
119 // Returns the value as string. Second arg or empty string is returned when value is not set. |
127 // Returns the value as string. Second arg or empty string is returned when value is not set. |
120 function getUserSetting( name, def ) { |
128 function getUserSetting( name, def ) { |
121 var o = getAllUserSettings(); |
129 var obj = getAllUserSettings(); |
122 |
130 |
123 if ( o.hasOwnProperty(name) ) |
131 if ( obj.hasOwnProperty(name) ) |
124 return o[name]; |
132 return obj[name]; |
125 |
133 |
126 if ( typeof def != 'undefined' ) |
134 if ( typeof def != 'undefined' ) |
127 return def; |
135 return def; |
128 |
136 |
129 return ''; |
137 return ''; |
130 } |
138 } |
131 |
139 |
132 // Both name and value must be only ASCII letters, numbers or underscore |
140 // Both name and value must be only ASCII letters, numbers or underscore |
133 // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text. |
141 // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text. |
134 function setUserSetting( name, value, del ) { |
142 function setUserSetting( name, value, _del ) { |
135 if ( 'object' !== typeof userSettings ) |
143 if ( 'object' !== typeof userSettings ) |
136 return false; |
144 return false; |
137 |
145 |
138 var c = 'wp-settings-' + userSettings.uid, o = wpCookies.getHash(c) || {}, d = new Date(), p, |
146 var cookie = 'wp-settings-' + userSettings.uid, all = wpCookies.getHash(cookie) || {}, path = userSettings.url, |
139 n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, ''); |
147 n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, ''); |
140 |
148 |
141 if ( del ) { |
149 if ( _del ) { |
142 delete o[n]; |
150 delete all[n]; |
143 } else { |
151 } else { |
144 o[n] = v; |
152 all[n] = v; |
145 } |
153 } |
146 |
154 |
147 d.setTime( d.getTime() + 31536000000 ); |
155 wpCookies.setHash(cookie, all, 31536000, path); |
148 p = userSettings.url; |
156 wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, 31536000, path); |
149 |
|
150 wpCookies.setHash(c, o, d, p); |
|
151 wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, d, p); |
|
152 |
157 |
153 return name; |
158 return name; |
154 } |
159 } |
155 |
160 |
156 function deleteUserSetting( name ) { |
161 function deleteUserSetting( name ) { |