source: trunk/www.guidonia.net/wp/wp-admin/js/utils.dev.js@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 3.2 KB
Line 
1// utility functions
2function 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
25var wpCookies = {
26// The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL.
27
28 each : function(o, cb, s) {
29 var n, l;
30
31 if (!o)
32 return 0;
33
34 s = s || o;
35
36 if (typeof(o.length) != 'undefined') {
37 for (n=0, l = o.length; n<l; n++) {
38 if (cb.call(s, o[n], n, o) === false)
39 return 0;
40 }
41 } else {
42 for (n in o) {
43 if (o.hasOwnProperty(n)) {
44 if (cb.call(s, o[n], n, o) === false) {
45 return 0;
46 }
47 }
48 }
49 }
50 return 1;
51 },
52
53 getHash : function(n) {
54 var v = this.get(n), h;
55
56 if (v) {
57 this.each(v.split('&'), function(v) {
58 v = v.split('=');
59 h = h || {};
60 h[v[0]] = v[1];
61 });
62 }
63 return h;
64 },
65
66 setHash : function(n, v, e, p, d, s) {
67 var o = '';
68
69 this.each(v, function(v, k) {
70 o += (!o ? '' : '&') + k + '=' + v;
71 });
72
73 this.set(n, o, e, p, d, s);
74 },
75
76 get : function(n) {
77 var c = document.cookie, e, p = n + "=", b;
78
79 if (!c)
80 return;
81
82 b = c.indexOf("; " + p);
83
84 if (b == -1) {
85 b = c.indexOf(p);
86
87 if (b != 0)
88 return null;
89
90 } else {
91 b += 2;
92 }
93
94 e = c.indexOf(";", b);
95
96 if (e == -1)
97 e = c.length;
98
99 return decodeURIComponent(c.substring(b + p.length, e));
100 },
101
102 set : function(n, v, e, p, d, s) {
103 document.cookie = n + "=" + encodeURIComponent(v) +
104 ((e) ? "; expires=" + e.toGMTString() : "") +
105 ((p) ? "; path=" + p : "") +
106 ((d) ? "; domain=" + d : "") +
107 ((s) ? "; secure" : "");
108 },
109
110 remove : function(n, p) {
111 var d = new Date();
112
113 d.setTime(d.getTime() - 1000);
114
115 this.set(n, '', d, p, d);
116 }
117};
118
119// Returns the value as string. Second arg or empty string is returned when value is not set.
120function getUserSetting( name, def ) {
121 var o = getAllUserSettings();
122
123 if ( o.hasOwnProperty(name) )
124 return o[name];
125
126 if ( typeof def != 'undefined' )
127 return def;
128
129 return '';
130}
131
132// 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.
134function setUserSetting( name, value, del ) {
135 if ( 'object' !== typeof userSettings )
136 return false;
137
138 var c = 'wp-settings-' + userSettings.uid, o = wpCookies.getHash(c) || {}, d = new Date(), p,
139 n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, '');
140
141 if ( del ) {
142 delete o[n];
143 } else {
144 o[n] = v;
145 }
146
147 d.setTime( d.getTime() + 31536000000 );
148 p = userSettings.url;
149
150 wpCookies.setHash(c, o, d, p);
151 wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, d, p);
152
153 return name;
154}
155
156function deleteUserSetting( name ) {
157 return setUserSetting( name, '', 1 );
158}
159
160// Returns all settings as js object.
161function getAllUserSettings() {
162 if ( 'object' !== typeof userSettings )
163 return {};
164
165 return wpCookies.getHash('wp-settings-' + userSettings.uid) || {};
166}
Note: See TracBrowser for help on using the repository browser.