| 3 | | /* |
|---|
| 4 | | Following init script is a rip of Scriptaculous init, |
|---|
| 5 | | this will be changed later... |
|---|
| 6 | | */ |
|---|
| 7 | | |
|---|
| 8 | | var protoToolkit = { |
|---|
| 9 | | Version: '1.0.3', |
|---|
| 10 | | req: function(libraryName) { |
|---|
| 11 | | // inserting via DOM fails in Safari 2.0, so brute force approach |
|---|
| 12 | | document.write('<script type="text/javascript" src="'+libraryName+'"></script>'); |
|---|
| 13 | | /* var scripthtml = document.createElement("script"); |
|---|
| 14 | | scripthtml.src = libraryName; |
|---|
| 15 | | document.getElementsByTagName("head")[0].appendChild(scripthtml); */ |
|---|
| 16 | | }, |
|---|
| 17 | | start: function() { |
|---|
| 18 | | if((typeof Prototype=='undefined') || |
|---|
| | 3 | function protoToolkit() { |
|---|
| | 4 | this.version = '1.0.3'; |
|---|
| | 5 | } |
|---|
| | 6 | |
|---|
| | 7 | protoToolkit.prototype.req = function(library_name) { |
|---|
| | 8 | document.write('<script type="text/javascript" src="'+library_name+'"></script>'); |
|---|
| | 9 | } |
|---|
| | 10 | protoToolkit.prototype.start = function() { |
|---|
| | 11 | if((typeof Prototype=='undefined') || |
|---|
| 31 | | /* $A(document.getElementsByTagName("script")).findAll( function(i) { |
|---|
| 32 | | return ( i.src && i.src.match(/protoToolkit\.js(\?.*)?$/)) |
|---|
| 33 | | }).each( function(i) { |
|---|
| 34 | | var path = i.src.replace(/protoToolkit\.js(\?.*)?$/,''); |
|---|
| 35 | | var incs = i.src.match(/\?.*load=([a-zA-Z,]*)/); |
|---|
| 36 | | (incs ? incs[1] : '').split(',').each( |
|---|
| 37 | | function(include) { protoToolkit.req(path+include+'.js') }); |
|---|
| 38 | | }); */ |
|---|
| 39 | | } |
|---|
| 40 | | } |
|---|
| 41 | | |
|---|
| | 24 | } |
|---|
| | 25 | |
|---|
| | 26 | |
|---|
| | 27 | protoToolkit.prototype.parseJSON = function (json_str) { |
|---|
| | 28 | try { |
|---|
| | 29 | return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test( |
|---|
| | 30 | json_str.replace(/"(\\.|[^"\\])*"/g, ''))) && |
|---|
| | 31 | eval('(' + json_str + ')'); |
|---|
| | 32 | } catch (e) { |
|---|
| | 33 | return false; |
|---|
| | 34 | } |
|---|
| | 35 | } |
|---|
| | 36 | protoToolkit.prototype.toJSON = function (item,item_type) { |
|---|
| | 37 | var m = { |
|---|
| | 38 | '\b': '\\b', |
|---|
| | 39 | '\t': '\\t', |
|---|
| | 40 | '\n': '\\n', |
|---|
| | 41 | '\f': '\\f', |
|---|
| | 42 | '\r': '\\r', |
|---|
| | 43 | '"' : '\\"', |
|---|
| | 44 | '\\': '\\\\' |
|---|
| | 45 | }, |
|---|
| | 46 | s = { |
|---|
| | 47 | array: function (x) { |
|---|
| | 48 | var a = ['['], b, f, i, l = x.length, v; |
|---|
| | 49 | for (i = 0; i < l; i += 1) { |
|---|
| | 50 | v = x[i]; |
|---|
| | 51 | f = s[typeof v]; |
|---|
| | 52 | if (f) { |
|---|
| | 53 | v = f(v); |
|---|
| | 54 | if (typeof v == 'string') { |
|---|
| | 55 | if (b) { |
|---|
| | 56 | a[a.length] = ','; |
|---|
| | 57 | } |
|---|
| | 58 | a[a.length] = v; |
|---|
| | 59 | b = true; |
|---|
| | 60 | } |
|---|
| | 61 | } |
|---|
| | 62 | } |
|---|
| | 63 | a[a.length] = ']'; |
|---|
| | 64 | return a.join(''); |
|---|
| | 65 | }, |
|---|
| | 66 | 'boolean': function (x) { |
|---|
| | 67 | return String(x); |
|---|
| | 68 | }, |
|---|
| | 69 | 'null': function (x) { |
|---|
| | 70 | return "null"; |
|---|
| | 71 | }, |
|---|
| | 72 | number: function (x) { |
|---|
| | 73 | return isFinite(x) ? String(x) : 'null'; |
|---|
| | 74 | }, |
|---|
| | 75 | object: function (x) { |
|---|
| | 76 | if (x) { |
|---|
| | 77 | if (x instanceof Array) { |
|---|
| | 78 | return s.array(x); |
|---|
| | 79 | } |
|---|
| | 80 | var a = ['{'], b, f, i, v; |
|---|
| | 81 | for (i in x) { |
|---|
| | 82 | v = x[i]; |
|---|
| | 83 | f = s[typeof v]; |
|---|
| | 84 | if (f) { |
|---|
| | 85 | v = f(v); |
|---|
| | 86 | if (typeof v == 'string') { |
|---|
| | 87 | if (b) { |
|---|
| | 88 | a[a.length] = ','; |
|---|
| | 89 | } |
|---|
| | 90 | a.push(s.string(i), ':', v); |
|---|
| | 91 | b = true; |
|---|
| | 92 | } |
|---|
| | 93 | } |
|---|
| | 94 | } |
|---|
| | 95 | a[a.length] = '}'; |
|---|
| | 96 | return a.join(''); |
|---|
| | 97 | } |
|---|
| | 98 | return 'null'; |
|---|
| | 99 | }, |
|---|
| | 100 | string: function (x) { |
|---|
| | 101 | if (/["\\\x00-\x1f]/.test(x)) { |
|---|
| | 102 | x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) { |
|---|
| | 103 | var c = m[b]; |
|---|
| | 104 | if (c) { |
|---|
| | 105 | return c; |
|---|
| | 106 | } |
|---|
| | 107 | c = b.charCodeAt(); |
|---|
| | 108 | return '\\u00' + |
|---|
| | 109 | Math.floor(c / 16).toString(16) + |
|---|
| | 110 | (c % 16).toString(16); |
|---|
| | 111 | }); |
|---|
| | 112 | } |
|---|
| | 113 | return '"' + x + '"'; |
|---|
| | 114 | } |
|---|
| | 115 | }; |
|---|
| | 116 | |
|---|
| | 117 | Array.prototype.toJSON = function () { |
|---|
| | 118 | return s.array(this); |
|---|
| | 119 | }; |
|---|
| | 120 | |
|---|
| | 121 | var itemtype = item_type || typeof item; |
|---|
| | 122 | switch(itemtype) { |
|---|
| | 123 | case "array": |
|---|
| | 124 | return item.toJSON; |
|---|
| | 125 | break; |
|---|
| | 126 | case "object": |
|---|
| | 127 | return s.object(item); |
|---|
| | 128 | break; |
|---|
| | 129 | case "string": |
|---|
| | 130 | return s.string(item); |
|---|
| | 131 | break; |
|---|
| | 132 | case "number": |
|---|
| | 133 | return s.number(item); |
|---|
| | 134 | break; |
|---|
| | 135 | case "null": |
|---|
| | 136 | return s.null(item); |
|---|
| | 137 | break; |
|---|
| | 138 | case "boolean": |
|---|
| | 139 | return s.boolean(item); |
|---|
| | 140 | break; |
|---|
| | 141 | default: |
|---|
| | 142 | throw("Unknown type for protoToolkit.toJSON"); |
|---|
| | 143 | } |
|---|
| | 144 | |
|---|
| | 145 | } |
|---|
| | 146 | |
|---|
| | 147 | var protoToolkit = new protoToolkit(); |
|---|
| 187 | | |
|---|
| 188 | | |
|---|
| 189 | | |
|---|
| 190 | | /* |
|---|
| 191 | | JSON Functions |
|---|
| 192 | | |
|---|
| 193 | | object.toJSONString() |
|---|
| 194 | | |
|---|
| 195 | | This method produces a JSON text from an object. The |
|---|
| 196 | | object must not contain any cyclical references. |
|---|
| 197 | | |
|---|
| 198 | | array.toJSONString() |
|---|
| 199 | | |
|---|
| 200 | | This method produces a JSON text from an array. The |
|---|
| 201 | | array must not contain any cyclical references. |
|---|
| 202 | | |
|---|
| 203 | | string.parseJSON() |
|---|
| 204 | | |
|---|
| 205 | | This method parses a JSON text to produce an object or |
|---|
| 206 | | array. It will return false if there is an error. |
|---|
| 207 | | */ |
|---|
| 208 | | (function () { |
|---|
| 209 | | var m = { |
|---|
| 210 | | '\b': '\\b', |
|---|
| 211 | | '\t': '\\t', |
|---|
| 212 | | '\n': '\\n', |
|---|
| 213 | | '\f': '\\f', |
|---|
| 214 | | '\r': '\\r', |
|---|
| 215 | | '"' : '\\"', |
|---|
| 216 | | '\\': '\\\\' |
|---|
| 217 | | }, |
|---|
| 218 | | s = { |
|---|
| 219 | | array: function (x) { |
|---|
| 220 | | var a = ['['], b, f, i, l = x.length, v; |
|---|
| 221 | | for (i = 0; i < l; i += 1) { |
|---|
| 222 | | v = x[i]; |
|---|
| 223 | | f = s[typeof v]; |
|---|
| 224 | | if (f) { |
|---|
| 225 | | v = f(v); |
|---|
| 226 | | if (typeof v == 'string') { |
|---|
| 227 | | if (b) { |
|---|
| 228 | | a[a.length] = ','; |
|---|
| 229 | | } |
|---|
| 230 | | a[a.length] = v; |
|---|
| 231 | | b = true; |
|---|
| 232 | | } |
|---|
| 233 | | } |
|---|
| 234 | | } |
|---|
| 235 | | a[a.length] = ']'; |
|---|
| 236 | | return a.join(''); |
|---|
| 237 | | }, |
|---|
| 238 | | 'boolean': function (x) { |
|---|
| 239 | | return String(x); |
|---|
| 240 | | }, |
|---|
| 241 | | 'null': function (x) { |
|---|
| 242 | | return "null"; |
|---|
| 243 | | }, |
|---|
| 244 | | number: function (x) { |
|---|
| 245 | | return isFinite(x) ? String(x) : 'null'; |
|---|
| 246 | | }, |
|---|
| 247 | | object: function (x) { |
|---|
| 248 | | if (x) { |
|---|
| 249 | | if (x instanceof Array) { |
|---|
| 250 | | return s.array(x); |
|---|
| 251 | | } |
|---|
| 252 | | var a = ['{'], b, f, i, v; |
|---|
| 253 | | for (i in x) { |
|---|
| 254 | | v = x[i]; |
|---|
| 255 | | f = s[typeof v]; |
|---|
| 256 | | if (f) { |
|---|
| 257 | | v = f(v); |
|---|
| 258 | | if (typeof v == 'string') { |
|---|
| 259 | | if (b) { |
|---|
| 260 | | a[a.length] = ','; |
|---|
| 261 | | } |
|---|
| 262 | | a.push(s.string(i), ':', v); |
|---|
| 263 | | b = true; |
|---|
| 264 | | } |
|---|
| 265 | | } |
|---|
| 266 | | } |
|---|
| 267 | | a[a.length] = '}'; |
|---|
| 268 | | return a.join(''); |
|---|
| 269 | | } |
|---|
| 270 | | return 'null'; |
|---|
| 271 | | }, |
|---|
| 272 | | string: function (x) { |
|---|
| 273 | | if (/["\\\x00-\x1f]/.test(x)) { |
|---|
| 274 | | x = x.replace(/([\x00-\x1f\\"])/g, function(a, b) { |
|---|
| 275 | | var c = m[b]; |
|---|
| 276 | | if (c) { |
|---|
| 277 | | return c; |
|---|
| 278 | | } |
|---|
| 279 | | c = b.charCodeAt(); |
|---|
| 280 | | return '\\u00' + |
|---|
| 281 | | Math.floor(c / 16).toString(16) + |
|---|
| 282 | | (c % 16).toString(16); |
|---|
| 283 | | }); |
|---|
| 284 | | } |
|---|
| 285 | | return '"' + x + '"'; |
|---|
| 286 | | } |
|---|
| 287 | | }; |
|---|
| 288 | | |
|---|
| 289 | | Object.prototype.toJSONString = function () { |
|---|
| 290 | | return s.object(this); |
|---|
| 291 | | }; |
|---|
| 292 | | |
|---|
| 293 | | Array.prototype.toJSONString = function () { |
|---|
| 294 | | return s.array(this); |
|---|
| 295 | | }; |
|---|
| 296 | | })(); |
|---|
| 297 | | |
|---|
| 298 | | String.prototype.parseJSON = function () { |
|---|
| 299 | | try { |
|---|
| 300 | | return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test( |
|---|
| 301 | | this.replace(/"(\\.|[^"\\])*"/g, ''))) && |
|---|
| 302 | | eval('(' + this + ')'); |
|---|
| 303 | | } catch (e) { |
|---|
| 304 | | return false; |
|---|
| 305 | | } |
|---|
| 306 | | }; |
|---|
| | 291 | |
|---|
| | 292 | |
|---|