]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/utils.dev.js
Wordpress 3.2
[autoinstalls/wordpress.git] / wp-admin / js / utils.dev.js
1 // utility functions
2
3 var wpCookies = {
4 // The following functions are from Cookie.js class in TinyMCE, Moxiecode, used under LGPL.
5
6         each : function(o, cb, s) {
7                 var n, l;
8
9                 if (!o)
10                         return 0;
11
12                 s = s || o;
13
14                 if (typeof(o.length) != 'undefined') {
15                         for (n=0, l = o.length; n<l; n++) {
16                                 if (cb.call(s, o[n], n, o) === false)
17                                         return 0;
18                         }
19                 } else {
20                         for (n in o) {
21                                 if (o.hasOwnProperty(n)) {
22                                         if (cb.call(s, o[n], n, o) === false) {
23                                                 return 0;
24                                         }
25                                 }
26                         }
27                 }
28                 return 1;
29         },
30
31         getHash : function(n) {
32                 var v = this.get(n), h;
33
34                 if (v) {
35                         this.each(v.split('&'), function(v) {
36                                 v = v.split('=');
37                                 h = h || {};
38                                 h[v[0]] = v[1];
39                         });
40                 }
41                 return h;
42         },
43
44         setHash : function(n, v, e, p, d, s) {
45                 var o = '';
46
47                 this.each(v, function(v, k) {
48                         o += (!o ? '' : '&') + k + '=' + v;
49                 });
50
51                 this.set(n, o, e, p, d, s);
52         },
53
54         get : function(n) {
55                 var c = document.cookie, e, p = n + "=", b;
56
57                 if (!c)
58                         return;
59
60                 b = c.indexOf("; " + p);
61
62                 if (b == -1) {
63                         b = c.indexOf(p);
64
65                         if (b != 0)
66                                 return null;
67
68                 } else {
69                         b += 2;
70                 }
71
72                 e = c.indexOf(";", b);
73
74                 if (e == -1)
75                         e = c.length;
76
77                 return decodeURIComponent(c.substring(b + p.length, e));
78         },
79
80         set : function(n, v, e, p, d, s) {
81                 document.cookie = n + "=" + encodeURIComponent(v) +
82                         ((e) ? "; expires=" + e.toGMTString() : "") +
83                         ((p) ? "; path=" + p : "") +
84                         ((d) ? "; domain=" + d : "") +
85                         ((s) ? "; secure" : "");
86         },
87
88         remove : function(n, p) {
89                 var d = new Date();
90
91                 d.setTime(d.getTime() - 1000);
92
93                 this.set(n, '', d, p, d);
94         }
95 };
96
97 // Returns the value as string. Second arg or empty string is returned when value is not set.
98 function getUserSetting( name, def ) {
99         var o = getAllUserSettings();
100
101         if ( o.hasOwnProperty(name) )
102                 return o[name];
103
104         if ( typeof def != 'undefined' )
105                 return def;
106
107         return '';
108 }
109
110 // Both name and value must be only ASCII letters, numbers or underscore
111 // and the shorter, the better (cookies can store maximum 4KB). Not suitable to store text.
112 function setUserSetting( name, value, del ) {
113         if ( 'object' !== typeof userSettings )
114                 return false;
115
116         var c = 'wp-settings-' + userSettings.uid, o = wpCookies.getHash(c) || {}, d = new Date(), p,
117         n = name.toString().replace(/[^A-Za-z0-9_]/, ''), v = value.toString().replace(/[^A-Za-z0-9_]/, '');
118
119         if ( del ) {
120                 delete o[n];
121         } else {
122                 o[n] = v;
123         }
124
125         d.setTime( d.getTime() + 31536000000 );
126         p = userSettings.url;
127
128         wpCookies.setHash(c, o, d, p);
129         wpCookies.set('wp-settings-time-'+userSettings.uid, userSettings.time, d, p);
130
131         return name;
132 }
133
134 function deleteUserSetting( name ) {
135         return setUserSetting( name, '', 1 );
136 }
137
138 // Returns all settings as js object.
139 function getAllUserSettings() {
140         if ( 'object' !== typeof userSettings )
141                 return {};
142
143         return wpCookies.getHash('wp-settings-' + userSettings.uid) || {};
144 }