Commit e952b988588e7a52f52dd86f4b2fec43e711b50d
1 parent
d443496b
Exists in
master
and in
7 other branches
Inclusão de rotina de verificação do suporte ao elemento CANVAS por parte do navegador do usuário
Showing
2 changed files
with
1446 additions
and
8 deletions
Show diff stats
| @@ -0,0 +1,1429 @@ | @@ -0,0 +1,1429 @@ | ||
| 1 | +/** | ||
| 2 | +* CPAINT - Cross-Platform Asynchronous INterface Toolkit | ||
| 3 | +* | ||
| 4 | +* http://sf.net/projects/cpaint | ||
| 5 | +* | ||
| 6 | +* released under the terms of the LGPL | ||
| 7 | +* see http://www.fsf.org/licensing/licenses/lgpl.txt for details | ||
| 8 | +* | ||
| 9 | +* @package CPAINT | ||
| 10 | +* @access public | ||
| 11 | +* @copyright Copyright (c) 2005-2006 Paul Sullivan, Dominique Stender - http://sf.net/projects/cpaint | ||
| 12 | +* @author Paul Sullivan <wiley14@gmail.com> | ||
| 13 | +* @author Dominique Stender <dstender@st-webdevelopment.de> | ||
| 14 | +* @author Stephan Tijink <stijink@googlemail.com> | ||
| 15 | +* @version 2.0.3 | ||
| 16 | +*/ | ||
| 17 | +function cpaint() { | ||
| 18 | + /** | ||
| 19 | + * CPAINT version | ||
| 20 | + * | ||
| 21 | + * @access protected | ||
| 22 | + * @var string version | ||
| 23 | + */ | ||
| 24 | + this.version = '2.0.3'; | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * configuration options both for this class but also for the cpaint_call() objects. | ||
| 28 | + * | ||
| 29 | + * @access protected | ||
| 30 | + * @var array config | ||
| 31 | + */ | ||
| 32 | + var config = new Array(); | ||
| 33 | + config['debugging'] = -1; | ||
| 34 | + config['proxy_url'] = ''; | ||
| 35 | + config['transfer_mode'] = 'GET'; | ||
| 36 | + config['async'] = true; | ||
| 37 | + config['response_type'] = 'OBJECT'; | ||
| 38 | + config['persistent_connection'] = false; | ||
| 39 | + config['use_cpaint_api'] = true; | ||
| 40 | + | ||
| 41 | + /** | ||
| 42 | + * maintains the next free index in the stack | ||
| 43 | + * | ||
| 44 | + * @access protected | ||
| 45 | + * @var integer stack_count | ||
| 46 | + */ | ||
| 47 | + var stack_count = 0; | ||
| 48 | + | ||
| 49 | + /** | ||
| 50 | + * property returns whether or not the browser is AJAX capable | ||
| 51 | + * | ||
| 52 | + * @access public | ||
| 53 | + * @return boolean | ||
| 54 | + */ | ||
| 55 | + this.capable = test_ajax_capability(); | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * switches debug mode on/off. | ||
| 59 | + * | ||
| 60 | + * @access public | ||
| 61 | + * @param boolean debug debug flag | ||
| 62 | + * @return void | ||
| 63 | + */ | ||
| 64 | + this.set_debug = function() { | ||
| 65 | + | ||
| 66 | + if (typeof arguments[0] == 'boolean') { | ||
| 67 | + if (arguments[0] === true) { | ||
| 68 | + config['debugging'] = 1; | ||
| 69 | + | ||
| 70 | + } else { | ||
| 71 | + config['debugging'] = 0; | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | + } else if (typeof arguments[0] == 'number') { | ||
| 75 | + config['debugging'] = Math.round(arguments[0]); | ||
| 76 | + } | ||
| 77 | + } | ||
| 78 | + | ||
| 79 | + /** | ||
| 80 | + * defines the URL of the proxy script. | ||
| 81 | + * | ||
| 82 | + * @access public | ||
| 83 | + * @param string proxy_url URL of the proxyscript to connect | ||
| 84 | + * @return void | ||
| 85 | + */ | ||
| 86 | + this.set_proxy_url = function() { | ||
| 87 | + | ||
| 88 | + if (typeof arguments[0] == 'string') { | ||
| 89 | + | ||
| 90 | + config['proxy_url'] = arguments[0]; | ||
| 91 | + } | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + /** | ||
| 95 | + * sets the transfer_mode (GET|POST). | ||
| 96 | + * | ||
| 97 | + * @access public | ||
| 98 | + * @param string transfer_mode transfer_mode | ||
| 99 | + * @return void | ||
| 100 | + */ | ||
| 101 | + this.set_transfer_mode = function() { | ||
| 102 | + | ||
| 103 | + if (arguments[0].toUpperCase() == 'GET' | ||
| 104 | + || arguments[0].toUpperCase() == 'POST') { | ||
| 105 | + | ||
| 106 | + config['transfer_mode'] = arguments[0].toUpperCase(); | ||
| 107 | + } | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + /** | ||
| 111 | + * sets the flag whether or not to use asynchronous calls. | ||
| 112 | + * | ||
| 113 | + * @access public | ||
| 114 | + * @param boolean async syncronization flag | ||
| 115 | + * @return void | ||
| 116 | + */ | ||
| 117 | + this.set_async = function() { | ||
| 118 | + | ||
| 119 | + if (typeof arguments[0] == 'boolean') { | ||
| 120 | + config['async'] = arguments[0]; | ||
| 121 | + } | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + /** | ||
| 125 | + * defines the response type. | ||
| 126 | + * | ||
| 127 | + * allowed values are: | ||
| 128 | + * TEXT = raw text response | ||
| 129 | + * XML = raw XMLHttpObject | ||
| 130 | + * OBJECT = parsed JavaScript object structure from XMLHttpObject | ||
| 131 | + * | ||
| 132 | + * the default is OBJECT. | ||
| 133 | + * | ||
| 134 | + * @access public | ||
| 135 | + * @param string response_type response type | ||
| 136 | + * @return void | ||
| 137 | + */ | ||
| 138 | + this.set_response_type = function() { | ||
| 139 | + | ||
| 140 | + if (arguments[0].toUpperCase() == 'TEXT' | ||
| 141 | + || arguments[0].toUpperCase() == 'XML' | ||
| 142 | + || arguments[0].toUpperCase() == 'OBJECT' | ||
| 143 | + || arguments[0].toUpperCase() == 'E4X' | ||
| 144 | + || arguments[0].toUpperCase() == 'JSON') { | ||
| 145 | + | ||
| 146 | + config['response_type'] = arguments[0].toUpperCase(); | ||
| 147 | + } | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + /** | ||
| 151 | + * sets the flag whether or not to use a persistent connection. | ||
| 152 | + * | ||
| 153 | + * @access public | ||
| 154 | + * @param boolean persistent_connection persistance flag | ||
| 155 | + * @return void | ||
| 156 | + */ | ||
| 157 | + this.set_persistent_connection = function() { | ||
| 158 | + | ||
| 159 | + if (typeof arguments[0] == 'boolean') { | ||
| 160 | + config['persistent_connection'] = arguments[0]; | ||
| 161 | + } | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + | ||
| 165 | + /** | ||
| 166 | + * sets the flag whether or not to use the cpaint api on the backend. | ||
| 167 | + * | ||
| 168 | + * @access public | ||
| 169 | + * @param boolean cpaint_api api_flag | ||
| 170 | + * @return void | ||
| 171 | + */ | ||
| 172 | + this.set_use_cpaint_api = function() { | ||
| 173 | + if (typeof arguments[0] == 'boolean') { | ||
| 174 | + config['use_cpaint_api'] = arguments[0]; | ||
| 175 | + } | ||
| 176 | + } | ||
| 177 | + | ||
| 178 | + /** | ||
| 179 | + * tests whether one of the necessary implementations | ||
| 180 | + * of the XMLHttpRequest class are available | ||
| 181 | + * | ||
| 182 | + * @access protected | ||
| 183 | + * @return boolean | ||
| 184 | + */ | ||
| 185 | + function test_ajax_capability() { | ||
| 186 | + var cpc = new cpaint_call(0, config, this.version); | ||
| 187 | + return cpc.test_ajax_capability(); | ||
| 188 | + } | ||
| 189 | + | ||
| 190 | + /** | ||
| 191 | + * takes the arguments supplied and triggers a call to the CPAINT backend | ||
| 192 | + * based on the settings. | ||
| 193 | + * | ||
| 194 | + * upon response cpaint_call.callback() will automatically be called | ||
| 195 | + * to perform post-processing operations. | ||
| 196 | + * | ||
| 197 | + * @access public | ||
| 198 | + * @param string url remote URL to call | ||
| 199 | + * @param string remote_method remote method to call | ||
| 200 | + * @param object client_callback client side callback method to deliver the remote response to. do NOT supply a string! | ||
| 201 | + * @param mixed argN remote parameters from now on | ||
| 202 | + * @return void | ||
| 203 | + */ | ||
| 204 | + this.call = function() { | ||
| 205 | + var use_stack = -1; | ||
| 206 | + | ||
| 207 | + if (config['persistent_connection'] == true | ||
| 208 | + && __cpaint_stack[0] != null) { | ||
| 209 | + | ||
| 210 | + switch (__cpaint_stack[0].get_http_state()) { | ||
| 211 | + case -1: | ||
| 212 | + // no XMLHttpObject object has already been instanciated | ||
| 213 | + // create new object and configure it | ||
| 214 | + use_stack = 0; | ||
| 215 | + debug('no XMLHttpObject object to re-use for persistence, creating new one later', 2); | ||
| 216 | + break; | ||
| 217 | + | ||
| 218 | + case 4: | ||
| 219 | + // object is ready for a new request, no need to do anything | ||
| 220 | + use_stack = 0 | ||
| 221 | + debug('re-using the persistent connection', 2); | ||
| 222 | + break; | ||
| 223 | + | ||
| 224 | + default: | ||
| 225 | + // connection is currently in use, don't do anything | ||
| 226 | + debug('the persistent connection is in use - skipping this request', 2); | ||
| 227 | + } | ||
| 228 | + | ||
| 229 | + } else if (config['persistent_connection'] == true) { | ||
| 230 | + // persistent connection is active, but no object has been instanciated | ||
| 231 | + use_stack = 0; | ||
| 232 | + __cpaint_stack[use_stack] = new cpaint_call(use_stack, config, this.version); | ||
| 233 | + debug('no cpaint_call object available for re-use, created new one', 2); | ||
| 234 | + | ||
| 235 | + } else { | ||
| 236 | + // no connection persistance | ||
| 237 | + use_stack = stack_count; | ||
| 238 | + __cpaint_stack[use_stack] = new cpaint_call(use_stack, config, this.version); | ||
| 239 | + debug('no cpaint_call object created new one', 2); | ||
| 240 | + } | ||
| 241 | + | ||
| 242 | + // configure cpaint_call if allowed to | ||
| 243 | + if (use_stack != -1) { | ||
| 244 | + __cpaint_stack[use_stack].set_client_callback(arguments[2]); | ||
| 245 | + | ||
| 246 | + // distribute according to proxy use | ||
| 247 | + if (config['proxy_url'] != '') { | ||
| 248 | + __cpaint_stack[use_stack].call_proxy(arguments); | ||
| 249 | + | ||
| 250 | + } else { | ||
| 251 | + __cpaint_stack[use_stack].call_direct(arguments); | ||
| 252 | + } | ||
| 253 | + | ||
| 254 | + // increase stack counter | ||
| 255 | + stack_count++; | ||
| 256 | + debug('stack size: ' + __cpaint_stack.length, 2); | ||
| 257 | + } | ||
| 258 | + } | ||
| 259 | + | ||
| 260 | + /** | ||
| 261 | + * debug method | ||
| 262 | + * | ||
| 263 | + * @access protected | ||
| 264 | + * @param string message the message to debug | ||
| 265 | + * @param integer debug_level debug level at which the message appears | ||
| 266 | + * @return void | ||
| 267 | + */ | ||
| 268 | + var debug = function(message, debug_level) { | ||
| 269 | + var prefix = '[CPAINT Debug] '; | ||
| 270 | + | ||
| 271 | + if (debug_level < 1) { | ||
| 272 | + prefix = '[CPAINT Error] '; | ||
| 273 | + } | ||
| 274 | + | ||
| 275 | + if (config['debugging'] >= debug_level) { | ||
| 276 | + alert(prefix + message); | ||
| 277 | + } | ||
| 278 | + } | ||
| 279 | +} | ||
| 280 | + | ||
| 281 | +/** | ||
| 282 | +* internal FIFO stack of cpaint_call() objects. | ||
| 283 | +* | ||
| 284 | +* @access protected | ||
| 285 | +* @var array __cpaint_stack | ||
| 286 | +*/ | ||
| 287 | +var __cpaint_stack = new Array(); | ||
| 288 | + | ||
| 289 | +/** | ||
| 290 | +* local instance of cpaint_transformer | ||
| 291 | +* MSIE is unable to handle static classes... sheesh. | ||
| 292 | +* | ||
| 293 | +* @access public | ||
| 294 | +* @var object __cpaint_transformer | ||
| 295 | +*/ | ||
| 296 | +var __cpaint_transformer = new cpaint_transformer(); | ||
| 297 | + | ||
| 298 | +/** | ||
| 299 | +* transport agent class | ||
| 300 | +* | ||
| 301 | +* creates the request object, takes care of the response, handles the | ||
| 302 | +* client callback. Is configured by the cpaint() object. | ||
| 303 | +* | ||
| 304 | +* @package CPAINT | ||
| 305 | +* @access public | ||
| 306 | +* @copyright Copyright (c) 2005-2006 Paul Sullivan, Dominique Stender - http://sf.net/projects/cpaint | ||
| 307 | +* @author Dominique Stender <dstender@st-webdevelopment.de> | ||
| 308 | +* @author Paul Sullivan <wiley14@gmail.com> | ||
| 309 | +* @param integer stack_id stack Id in cpaint | ||
| 310 | +* @param array config configuration array for this call | ||
| 311 | +* @param string version CPAINT API version | ||
| 312 | +*/ | ||
| 313 | +function cpaint_call() { | ||
| 314 | + /** | ||
| 315 | + * CPAINT version | ||
| 316 | + * | ||
| 317 | + * @access protected | ||
| 318 | + * @var string version | ||
| 319 | + */ | ||
| 320 | + var version = arguments[2]; | ||
| 321 | + | ||
| 322 | + /** | ||
| 323 | + * configuration options both for this class objects. | ||
| 324 | + * | ||
| 325 | + * @access protected | ||
| 326 | + * @var array config | ||
| 327 | + */ | ||
| 328 | + var config = new Array(); | ||
| 329 | + config['debugging'] = arguments[1]['debugging']; | ||
| 330 | + config['proxy_url'] = arguments[1]['proxy_url']; | ||
| 331 | + config['transfer_mode'] = arguments[1]['transfer_mode']; | ||
| 332 | + config['async'] = arguments[1]['async']; | ||
| 333 | + config['response_type'] = arguments[1]['response_type']; | ||
| 334 | + config['persistent_connection'] = arguments[1]['persistent_connection']; | ||
| 335 | + config['use_cpaint_api'] = arguments[1]['use_cpaint_api']; | ||
| 336 | + | ||
| 337 | + /** | ||
| 338 | + * XMLHttpObject used for this request. | ||
| 339 | + * | ||
| 340 | + * @access protected | ||
| 341 | + * @var object httpobj | ||
| 342 | + */ | ||
| 343 | + var httpobj = false; | ||
| 344 | + | ||
| 345 | + /** | ||
| 346 | + * client callback function. | ||
| 347 | + * | ||
| 348 | + * @access public | ||
| 349 | + * @var function client_callback | ||
| 350 | + */ | ||
| 351 | + var client_callback; | ||
| 352 | + | ||
| 353 | + /** | ||
| 354 | + * stores the stack Id within the cpaint object | ||
| 355 | + * | ||
| 356 | + * @access protected | ||
| 357 | + * @var stack_id | ||
| 358 | + */ | ||
| 359 | + var stack_id = arguments[0]; | ||
| 360 | + | ||
| 361 | + /** | ||
| 362 | + * sets the client callback function. | ||
| 363 | + * | ||
| 364 | + * @access public | ||
| 365 | + * @param function client_callback the client callback function | ||
| 366 | + * @return void | ||
| 367 | + */ | ||
| 368 | + this.set_client_callback = function() { | ||
| 369 | + | ||
| 370 | + if (typeof arguments[0] == 'function') { | ||
| 371 | + client_callback = arguments[0]; | ||
| 372 | + } | ||
| 373 | + } | ||
| 374 | + | ||
| 375 | + /** | ||
| 376 | + * returns the ready state of the internal XMLHttpObject | ||
| 377 | + * | ||
| 378 | + * if no such object was set up already, -1 is returned | ||
| 379 | + * | ||
| 380 | + * @access public | ||
| 381 | + * @return integer | ||
| 382 | + */ | ||
| 383 | + this.get_http_state = function() { | ||
| 384 | + var return_value = -1; | ||
| 385 | + | ||
| 386 | + if (typeof httpobj == 'object') { | ||
| 387 | + return_value = httpobj.readyState; | ||
| 388 | + } | ||
| 389 | + | ||
| 390 | + return return_value; | ||
| 391 | + } | ||
| 392 | + | ||
| 393 | + /** | ||
| 394 | + * internal method for remote calls to the local server without use of the proxy script. | ||
| 395 | + * | ||
| 396 | + * @access public | ||
| 397 | + * @param array call_arguments array of arguments initially passed to cpaint.call() | ||
| 398 | + * @return void | ||
| 399 | + */ | ||
| 400 | + this.call_direct = function(call_arguments) { | ||
| 401 | + var url = call_arguments[0]; | ||
| 402 | + var remote_method = call_arguments[1]; | ||
| 403 | + var querystring = ''; | ||
| 404 | + var i = 0; | ||
| 405 | + | ||
| 406 | + // correct link to self | ||
| 407 | + if (url == 'SELF') { | ||
| 408 | + url = document.location.href; | ||
| 409 | + } | ||
| 410 | + | ||
| 411 | + if (config['use_cpaint_api'] == true) { | ||
| 412 | + // backend uses cpaint api | ||
| 413 | + // pass parameters to remote method | ||
| 414 | + for (i = 3; i < call_arguments.length; i++) { | ||
| 415 | + | ||
| 416 | + if ((typeof call_arguments[i] == 'string' | ||
| 417 | + && call_arguments[i] != '' | ||
| 418 | + && call_arguments[i].search(/^\s+$/g) == -1) | ||
| 419 | + && !isNaN(call_arguments[i]) | ||
| 420 | + && isFinite(call_arguments[i])) { | ||
| 421 | + // numerical value, convert it first | ||
| 422 | + querystring += '&cpaint_argument[]=' + encodeURIComponent(JSON.stringify(Number(call_arguments[i]))); | ||
| 423 | + | ||
| 424 | + } else { | ||
| 425 | + querystring += '&cpaint_argument[]=' + encodeURIComponent(JSON.stringify(call_arguments[i])); | ||
| 426 | + } | ||
| 427 | + } | ||
| 428 | + | ||
| 429 | + // add response type to querystring | ||
| 430 | + querystring += '&cpaint_response_type=' + config['response_type']; | ||
| 431 | + | ||
| 432 | + // build header | ||
| 433 | + if (config['transfer_mode'] == 'GET') { | ||
| 434 | + | ||
| 435 | + if(url.indexOf('?') != -1) { | ||
| 436 | + url = url + '&cpaint_function=' + remote_method + querystring; | ||
| 437 | + | ||
| 438 | + } else { | ||
| 439 | + url = url + '?cpaint_function=' + remote_method + querystring; | ||
| 440 | + } | ||
| 441 | + | ||
| 442 | + } else { | ||
| 443 | + querystring = 'cpaint_function=' + remote_method + querystring; | ||
| 444 | + } | ||
| 445 | + | ||
| 446 | + } else { | ||
| 447 | + // backend does not use cpaint api | ||
| 448 | + // pass parameters to remote method | ||
| 449 | + for (i = 3; i < call_arguments.length; i++) { | ||
| 450 | + | ||
| 451 | + if (i == 3) { | ||
| 452 | + querystring += encodeURIComponent(call_arguments[i]); | ||
| 453 | + | ||
| 454 | + } else { | ||
| 455 | + querystring += '&' + encodeURIComponent(call_arguments[i]); | ||
| 456 | + } | ||
| 457 | + } | ||
| 458 | + | ||
| 459 | + // build header | ||
| 460 | + if (config['transfer_mode'] == 'GET') { | ||
| 461 | + url = url + querystring; | ||
| 462 | + } | ||
| 463 | + } | ||
| 464 | + | ||
| 465 | + // open connection | ||
| 466 | + get_connection_object(); | ||
| 467 | + | ||
| 468 | + // open connection to remote target | ||
| 469 | + debug('opening connection to "' + url + '"', 1); | ||
| 470 | + httpobj.open(config['transfer_mode'], url, config['async']); | ||
| 471 | + | ||
| 472 | + // send "urlencoded" header if necessary (if POST) | ||
| 473 | + if (config['transfer_mode'] == 'POST') { | ||
| 474 | + | ||
| 475 | + try { | ||
| 476 | + httpobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | ||
| 477 | + | ||
| 478 | + } catch (cp_err) { | ||
| 479 | + debug('POST cannot be completed due to incompatible browser. Use GET as your request method.', 0); | ||
| 480 | + } | ||
| 481 | + } | ||
| 482 | + | ||
| 483 | + // make ourselves known | ||
| 484 | + httpobj.setRequestHeader('X-Powered-By', 'CPAINT v' + version + ' :: http://sf.net/projects/cpaint'); | ||
| 485 | + | ||
| 486 | + // callback handling for asynchronous calls | ||
| 487 | + httpobj.onreadystatechange = callback; | ||
| 488 | + | ||
| 489 | + // send content | ||
| 490 | + if (config['transfer_mode'] == 'GET') { | ||
| 491 | + httpobj.send(null); | ||
| 492 | + | ||
| 493 | + } else { | ||
| 494 | + debug('sending query: ' + querystring, 1); | ||
| 495 | + httpobj.send(querystring); | ||
| 496 | + } | ||
| 497 | + | ||
| 498 | + if (config['async'] == true) { | ||
| 499 | + // manual callback handling for synchronized calls | ||
| 500 | + callback(); | ||
| 501 | + } | ||
| 502 | + } | ||
| 503 | + | ||
| 504 | + /** | ||
| 505 | + * internal method for calls to remote servers through the proxy script. | ||
| 506 | + * | ||
| 507 | + * @access public | ||
| 508 | + * @param array call_arguments array of arguments passed to cpaint.call() | ||
| 509 | + * @return void | ||
| 510 | + */ | ||
| 511 | + this.call_proxy = function(call_arguments) { | ||
| 512 | + var proxyscript = config['proxy_url']; | ||
| 513 | + var url = call_arguments[0]; | ||
| 514 | + var remote_method = call_arguments[1]; | ||
| 515 | + var querystring = ''; | ||
| 516 | + var i = 0; | ||
| 517 | + | ||
| 518 | + var querystring_argument_prefix = 'cpaint_argument[]='; | ||
| 519 | + | ||
| 520 | + // pass parameters to remote method | ||
| 521 | + if (config['use_cpaint_api'] == false) { | ||
| 522 | + // when not talking to a CPAINT backend, don't prefix arguments | ||
| 523 | + querystring_argument_prefix = ''; | ||
| 524 | + } | ||
| 525 | + | ||
| 526 | + for (i = 3; i < call_arguments.length; i++) { | ||
| 527 | + | ||
| 528 | + if (config['use_cpaint_api'] == true) { | ||
| 529 | + | ||
| 530 | + if ((typeof call_arguments[i] == 'string' | ||
| 531 | + && call_arguments[i] != '' | ||
| 532 | + && call_arguments[i].search(/^\s+$/g) == -1) | ||
| 533 | + && !isNaN(call_arguments[i]) | ||
| 534 | + && isFinite(call_arguments[i])) { | ||
| 535 | + // numerical value, convert it first | ||
| 536 | + querystring += encodeURIComponent(querystring_argument_prefix + JSON.stringify(Number(call_arguments[i])) + '&'); | ||
| 537 | + | ||
| 538 | + } else { | ||
| 539 | + querystring += encodeURIComponent(querystring_argument_prefix + JSON.stringify(call_arguments[i]) + '&'); | ||
| 540 | + } | ||
| 541 | + | ||
| 542 | + } else { | ||
| 543 | + // no CPAINT in the backend | ||
| 544 | + querystring += encodeURIComponent(querystring_argument_prefix + call_arguments[i] + '&'); | ||
| 545 | + } | ||
| 546 | + } | ||
| 547 | + | ||
| 548 | + if (config['use_cpaint_api'] == true) { | ||
| 549 | + // add remote function name to querystring | ||
| 550 | + querystring += encodeURIComponent('&cpaint_function=' + remote_method); | ||
| 551 | + | ||
| 552 | + // add response type to querystring | ||
| 553 | + querystring += encodeURIComponent('&cpaint_responsetype=' + config['response_type']); | ||
| 554 | + } | ||
| 555 | + | ||
| 556 | + // build header | ||
| 557 | + if (config['transfer_mode'] == 'GET') { | ||
| 558 | + proxyscript += '?cpaint_remote_url=' + encodeURIComponent(url) | ||
| 559 | + + '&cpaint_remote_query=' + querystring | ||
| 560 | + + '&cpaint_remote_method=' + config['transfer_mode'] | ||
| 561 | + + '&cpaint_response_type=' + config['response_type']; | ||
| 562 | + | ||
| 563 | + } else { | ||
| 564 | + querystring = 'cpaint_remote_url=' + encodeURIComponent(url) | ||
| 565 | + + '&cpaint_remote_query=' + querystring | ||
| 566 | + + '&cpaint_remote_method=' + config['transfer_mode'] | ||
| 567 | + + '&cpaint_response_type=' + config['response_type']; | ||
| 568 | + } | ||
| 569 | + | ||
| 570 | + // open connection | ||
| 571 | + get_connection_object(); | ||
| 572 | + | ||
| 573 | + // open connection to remote target | ||
| 574 | + debug('opening connection to proxy "' + proxyscript + '"', 1); | ||
| 575 | + httpobj.open(config['transfer_mode'], proxyscript, config['async']); | ||
| 576 | + | ||
| 577 | + // send "urlencoded" header if necessary (if POST) | ||
| 578 | + if (config['transfer_mode'] == 'POST') { | ||
| 579 | + | ||
| 580 | + try { | ||
| 581 | + httpobj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | ||
| 582 | + | ||
| 583 | + } catch (cp_err) { | ||
| 584 | + debug('POST cannot be completed due to incompatible browser. Use GET as your request method.', 0); | ||
| 585 | + } | ||
| 586 | + } | ||
| 587 | + | ||
| 588 | + httpobj.setRequestHeader('X-Powered-By', 'CPAINT v' + version); | ||
| 589 | + | ||
| 590 | + // callback handling for asynchronous calls | ||
| 591 | + httpobj.onreadystatechange = callback; | ||
| 592 | + | ||
| 593 | + // send content | ||
| 594 | + if (config['transfer_mode'] == 'GET') { | ||
| 595 | + httpobj.send(null); | ||
| 596 | + | ||
| 597 | + } else { | ||
| 598 | + debug('sending query: ' + querystring, 1); | ||
| 599 | + httpobj.send(querystring); | ||
| 600 | + } | ||
| 601 | + | ||
| 602 | + if (config['async'] == false) { | ||
| 603 | + // manual callback handling for synchronized calls | ||
| 604 | + callback(); | ||
| 605 | + } | ||
| 606 | + } | ||
| 607 | + | ||
| 608 | + this.test_ajax_capability = function() { | ||
| 609 | + return get_connection_object(); | ||
| 610 | + } | ||
| 611 | + | ||
| 612 | + /** | ||
| 613 | + * creates a new connection object. | ||
| 614 | + * | ||
| 615 | + * @access protected | ||
| 616 | + * @return boolean | ||
| 617 | + */ | ||
| 618 | + var get_connection_object = function() { | ||
| 619 | + var return_value = false; | ||
| 620 | + var new_connection = false; | ||
| 621 | + | ||
| 622 | + // open new connection only if necessary | ||
| 623 | + if (config['persistent_connection'] == false) { | ||
| 624 | + // no persistance, create a new object every time | ||
| 625 | + debug('Using new connection object', 1); | ||
| 626 | + new_connection = true; | ||
| 627 | + | ||
| 628 | + } else { | ||
| 629 | + // persistent connection object, only open one if no object exists | ||
| 630 | + debug('Using shared connection object.', 1); | ||
| 631 | + | ||
| 632 | + if (typeof httpobj != 'object') { | ||
| 633 | + debug('Getting new persistent connection object.', 1); | ||
| 634 | + new_connection = true; | ||
| 635 | + } | ||
| 636 | + } | ||
| 637 | + | ||
| 638 | + if (new_connection == true) { | ||
| 639 | + | ||
| 640 | + try { | ||
| 641 | + httpobj = new XMLHttpRequest(); | ||
| 642 | + } catch (e1) { | ||
| 643 | + | ||
| 644 | + try { | ||
| 645 | + httpobj = new ActiveXObject('Msxml2.XMLHTTP'); | ||
| 646 | + | ||
| 647 | + } catch (e) { | ||
| 648 | + | ||
| 649 | + try { | ||
| 650 | + httpobj = new ActiveXObject('Microsoft.XMLHTTP'); | ||
| 651 | + | ||
| 652 | + } catch (oc) { | ||
| 653 | + httpobj = null; | ||
| 654 | + } | ||
| 655 | + } | ||
| 656 | + } | ||
| 657 | + | ||
| 658 | + | ||
| 659 | + if (!httpobj) { | ||
| 660 | + debug('Could not create connection object', 0); | ||
| 661 | + | ||
| 662 | + } else { | ||
| 663 | + return_value = true; | ||
| 664 | + } | ||
| 665 | + } | ||
| 666 | + | ||
| 667 | + if (httpobj.readyState != 4) { | ||
| 668 | + httpobj.abort(); | ||
| 669 | + } | ||
| 670 | + | ||
| 671 | + return return_value; | ||
| 672 | + } | ||
| 673 | + | ||
| 674 | + /** | ||
| 675 | + * internal callback function. | ||
| 676 | + * | ||
| 677 | + * will perform some consistency checks (response code, NULL value testing) | ||
| 678 | + * and if response_type = 'OBJECT' it will automatically call | ||
| 679 | + * cpaint_call.parse_ajax_xml() to have a JavaScript object structure generated. | ||
| 680 | + * | ||
| 681 | + * after all that is done the client side callback function will be called | ||
| 682 | + * with the generated response as single value. | ||
| 683 | + * | ||
| 684 | + * @access protected | ||
| 685 | + * @return void | ||
| 686 | + */ | ||
| 687 | + var callback = function() { | ||
| 688 | + var response = null; | ||
| 689 | + | ||
| 690 | + if (httpobj.readyState == 4 | ||
| 691 | + && httpobj.status == 200) { | ||
| 692 | + | ||
| 693 | + debug(httpobj.responseText, 1); | ||
| 694 | + debug('using response type ' + config['response_type'], 2); | ||
| 695 | + | ||
| 696 | + // fetch correct response | ||
| 697 | + switch (config['response_type']) { | ||
| 698 | + case 'XML': | ||
| 699 | + debug(httpobj.responseXML, 2); | ||
| 700 | + response = __cpaint_transformer.xml_conversion(httpobj.responseXML); | ||
| 701 | + break; | ||
| 702 | + | ||
| 703 | + case 'OBJECT': | ||
| 704 | + response = __cpaint_transformer.object_conversion(httpobj.responseXML); | ||
| 705 | + break; | ||
| 706 | + | ||
| 707 | + case 'TEXT': | ||
| 708 | + response = __cpaint_transformer.text_conversion(httpobj.responseText); | ||
| 709 | + break; | ||
| 710 | + | ||
| 711 | + case 'E4X': | ||
| 712 | + response = __cpaint_transformer.e4x_conversion(httpobj.responseText); | ||
| 713 | + break; | ||
| 714 | + | ||
| 715 | + case 'JSON': | ||
| 716 | + response = __cpaint_transformer.json_conversion(httpobj.responseText); | ||
| 717 | + break; | ||
| 718 | + | ||
| 719 | + default: | ||
| 720 | + debug('invalid response type \'' + response_type + '\'', 0); | ||
| 721 | + } | ||
| 722 | + | ||
| 723 | + // call client side callback | ||
| 724 | + if (response != null | ||
| 725 | + && typeof client_callback == 'function') { | ||
| 726 | + client_callback(response, httpobj.responseText); | ||
| 727 | + } | ||
| 728 | + // remove ourselves from the stack | ||
| 729 | + remove_from_stack(); | ||
| 730 | + | ||
| 731 | + } else if (httpobj.readyState == 4 | ||
| 732 | + && httpobj.status != 200) { | ||
| 733 | + // HTTP error of some kind | ||
| 734 | + debug('invalid HTTP response code \'' + Number(httpobj.status) + '\'', 0); | ||
| 735 | + //client_callback("", "erro"); | ||
| 736 | + } | ||
| 737 | + } | ||
| 738 | + | ||
| 739 | + /** | ||
| 740 | + * removes an entry from the stack | ||
| 741 | + * | ||
| 742 | + * @access protected | ||
| 743 | + * @return void | ||
| 744 | + */ | ||
| 745 | + var remove_from_stack = function() { | ||
| 746 | + // remove only if everything is okay and we're not configured as persistent connection | ||
| 747 | + if (typeof stack_id == 'number' | ||
| 748 | + && __cpaint_stack[stack_id] | ||
| 749 | + && config['persistent_connection'] == false) { | ||
| 750 | + | ||
| 751 | + __cpaint_stack[stack_id] = null; | ||
| 752 | + } | ||
| 753 | + } | ||
| 754 | + | ||
| 755 | + /** | ||
| 756 | + * debug method | ||
| 757 | + * | ||
| 758 | + * @access protected | ||
| 759 | + * @param string message the message to debug | ||
| 760 | + * @param integer debug_level debug level at which the message appears | ||
| 761 | + * @return void | ||
| 762 | + */ | ||
| 763 | + var debug = function(message, debug_level) { | ||
| 764 | + var prefix = '[CPAINT Debug] '; | ||
| 765 | + | ||
| 766 | + if (config['debugging'] < 1) { | ||
| 767 | + prefix = '[CPAINT Error] '; | ||
| 768 | + } | ||
| 769 | + | ||
| 770 | + if (config['debugging'] >= debug_level) { | ||
| 771 | + alert(prefix + message); | ||
| 772 | + } | ||
| 773 | + if (message.search("error") > 1){client_callback("", message);} | ||
| 774 | + } | ||
| 775 | +} | ||
| 776 | + | ||
| 777 | +/** | ||
| 778 | +* CPAINT transformation object | ||
| 779 | +* | ||
| 780 | +* @package CPAINT | ||
| 781 | +* @access public | ||
| 782 | +* @copyright Copyright (c) 2005-2006 Paul Sullivan, Dominique Stender - http://sf.net/projects/cpaint | ||
| 783 | +* @author Paul Sullivan <wiley14@gmail.com> | ||
| 784 | +* @author Dominique Stender <dstender@st-webdevelopment.de> | ||
| 785 | +*/ | ||
| 786 | +function cpaint_transformer() { | ||
| 787 | + | ||
| 788 | + /** | ||
| 789 | + * will take a XMLHttpObject and generate a JavaScript | ||
| 790 | + * object structure from it. | ||
| 791 | + * | ||
| 792 | + * is internally called by cpaint_call.callback() if necessary. | ||
| 793 | + * will call cpaint_call.create_object_structure() to create nested object structures. | ||
| 794 | + * | ||
| 795 | + * @access public | ||
| 796 | + * @param object xml_document a XMLHttpObject | ||
| 797 | + * @return object | ||
| 798 | + */ | ||
| 799 | + this.object_conversion = function(xml_document) { | ||
| 800 | + var return_value = new cpaint_result_object(); | ||
| 801 | + var i = 0; | ||
| 802 | + var firstNodeName = ''; | ||
| 803 | + | ||
| 804 | + if (typeof xml_document == 'object' | ||
| 805 | + && xml_document != null) { | ||
| 806 | + | ||
| 807 | + // find the first element node - for MSIE the <?xml?> node is the very first... | ||
| 808 | + for (i = 0; i < xml_document.childNodes.length; i++) { | ||
| 809 | + | ||
| 810 | + if (xml_document.childNodes[i].nodeType == 1) { | ||
| 811 | + firstNodeName = xml_document.childNodes[i].nodeName; | ||
| 812 | + break; | ||
| 813 | + } | ||
| 814 | + } | ||
| 815 | + | ||
| 816 | + var ajax_response = xml_document.getElementsByTagName(firstNodeName); | ||
| 817 | + | ||
| 818 | + return_value[firstNodeName] = new Array(); | ||
| 819 | + | ||
| 820 | + for (i = 0; i < ajax_response.length; i++) { | ||
| 821 | + var tmp_node = create_object_structure(ajax_response[i]); | ||
| 822 | + tmp_node.id = ajax_response[i].getAttribute('id') | ||
| 823 | + return_value[firstNodeName].push(tmp_node); | ||
| 824 | + } | ||
| 825 | + | ||
| 826 | + } else { | ||
| 827 | + debug('received invalid XML response', 0); | ||
| 828 | + } | ||
| 829 | + | ||
| 830 | + return return_value; | ||
| 831 | + } | ||
| 832 | + | ||
| 833 | + /** | ||
| 834 | + * performs the necessary conversions for the XML response type | ||
| 835 | + * | ||
| 836 | + * @access public | ||
| 837 | + * @param object xml_document a XMLHttpObject | ||
| 838 | + * @return object | ||
| 839 | + */ | ||
| 840 | + this.xml_conversion = function(xml_document) { | ||
| 841 | + return xml_document; | ||
| 842 | + } | ||
| 843 | + | ||
| 844 | + /** | ||
| 845 | + * performs the necessary conversions for the TEXT response type | ||
| 846 | + * | ||
| 847 | + * @access public | ||
| 848 | + * @param string text the response text | ||
| 849 | + * @return string | ||
| 850 | + */ | ||
| 851 | + this.text_conversion = function(text) { | ||
| 852 | + return decode(text); | ||
| 853 | + } | ||
| 854 | + | ||
| 855 | + /** | ||
| 856 | + * performs the necessary conversions for the E4X response type | ||
| 857 | + * | ||
| 858 | + * @access public | ||
| 859 | + * @param string text the response text | ||
| 860 | + * @return string | ||
| 861 | + */ | ||
| 862 | + this.e4x_conversion = function(text) { | ||
| 863 | + // remove <?xml ?>tag | ||
| 864 | + text = text.replace(/^\<\?xml[^>]+\>/, ''); | ||
| 865 | + return new XML(text); | ||
| 866 | + } | ||
| 867 | + | ||
| 868 | + /** | ||
| 869 | + * performs the necessary conversions for the JSON response type | ||
| 870 | + * | ||
| 871 | + * @access public | ||
| 872 | + * @param string text the response text | ||
| 873 | + * @return string | ||
| 874 | + */ | ||
| 875 | + this.json_conversion = function(text) { | ||
| 876 | + return JSON.parse(text); | ||
| 877 | + } | ||
| 878 | + | ||
| 879 | + /** | ||
| 880 | + * this method takes a HTML / XML node object and creates a | ||
| 881 | + * JavaScript object structure from it. | ||
| 882 | + * | ||
| 883 | + * @access public | ||
| 884 | + * @param object stream a node in the XML structure | ||
| 885 | + * @return object | ||
| 886 | + */ | ||
| 887 | + var create_object_structure = function(stream) { | ||
| 888 | + var return_value = new cpaint_result_object(); | ||
| 889 | + var node_name = ''; | ||
| 890 | + var i = 0; | ||
| 891 | + var attrib = 0; | ||
| 892 | + | ||
| 893 | + if (stream.hasChildNodes() == true) { | ||
| 894 | + for (i = 0; i < stream.childNodes.length; i++) { | ||
| 895 | + | ||
| 896 | + node_name = stream.childNodes[i].nodeName; | ||
| 897 | + node_name = node_name.replace(/[^a-zA-Z0-9_]*/g, ''); | ||
| 898 | + | ||
| 899 | + // reset / create subnode | ||
| 900 | + if (typeof return_value[node_name] != 'object') { | ||
| 901 | + return_value[node_name] = new Array(); | ||
| 902 | + } | ||
| 903 | + | ||
| 904 | + if (stream.childNodes[i].nodeType == 1) { | ||
| 905 | + var tmp_node = create_object_structure(stream.childNodes[i]); | ||
| 906 | + | ||
| 907 | + for (attrib = 0; attrib < stream.childNodes[i].attributes.length; attrib++) { | ||
| 908 | + tmp_node.set_attribute(stream.childNodes[i].attributes[attrib].nodeName, stream.childNodes[i].attributes[attrib].nodeValue); | ||
| 909 | + } | ||
| 910 | + | ||
| 911 | + return_value[node_name].push(tmp_node); | ||
| 912 | + | ||
| 913 | + } else if (stream.childNodes[i].nodeType == 3) { | ||
| 914 | + return_value.data = decode(String(stream.firstChild.data)); | ||
| 915 | + } | ||
| 916 | + } | ||
| 917 | + } | ||
| 918 | + | ||
| 919 | + return return_value; | ||
| 920 | + } | ||
| 921 | + | ||
| 922 | + /** | ||
| 923 | + * converts an encoded text back to viewable characters. | ||
| 924 | + * | ||
| 925 | + * @access public | ||
| 926 | + * @param string rawtext raw text as provided by the backend | ||
| 927 | + * @return mixed | ||
| 928 | + */ | ||
| 929 | + var decode = function(rawtext) { | ||
| 930 | + var plaintext = ''; | ||
| 931 | + var i = 0; | ||
| 932 | + var c1 = 0; | ||
| 933 | + var c2 = 0; | ||
| 934 | + var c3 = 0; | ||
| 935 | + var u = 0; | ||
| 936 | + var t = 0; | ||
| 937 | + | ||
| 938 | + // remove special JavaScript encoded non-printable characters | ||
| 939 | + while (i < rawtext.length) { | ||
| 940 | + if (rawtext.charAt(i) == '\\' | ||
| 941 | + && rawtext.charAt(i + 1) == 'u') { | ||
| 942 | + | ||
| 943 | + u = 0; | ||
| 944 | + | ||
| 945 | + for (j = 2; j < 6; j += 1) { | ||
| 946 | + t = parseInt(rawtext.charAt(i + j), 16); | ||
| 947 | + | ||
| 948 | + if (!isFinite(t)) { | ||
| 949 | + break; | ||
| 950 | + } | ||
| 951 | + u = u * 16 + t; | ||
| 952 | + } | ||
| 953 | + | ||
| 954 | + plaintext += String.fromCharCode(u); | ||
| 955 | + i += 6; | ||
| 956 | + | ||
| 957 | + } else { | ||
| 958 | + plaintext += rawtext.charAt(i); | ||
| 959 | + i++; | ||
| 960 | + } | ||
| 961 | + } | ||
| 962 | + | ||
| 963 | + // convert numeric data to number type | ||
| 964 | + if (plaintext != '' | ||
| 965 | + && plaintext.search(/^\s+$/g) == -1 | ||
| 966 | + && !isNaN(plaintext) | ||
| 967 | + && isFinite(plaintext)) { | ||
| 968 | + | ||
| 969 | + plaintext = Number(plaintext); | ||
| 970 | + } | ||
| 971 | + | ||
| 972 | + return plaintext; | ||
| 973 | + } | ||
| 974 | +} | ||
| 975 | + | ||
| 976 | +/** | ||
| 977 | +* this is the basic prototype for a cpaint node object | ||
| 978 | +* as used in cpaint_call.parse_ajax_xml() | ||
| 979 | +* | ||
| 980 | +* @package CPAINT | ||
| 981 | +* @access public | ||
| 982 | +* @copyright Copyright (c) 2005-2006 Paul Sullivan, Dominique Stender - http://sf.net/projects/cpaint | ||
| 983 | +* @author Paul Sullivan <wiley14@gmail.com> | ||
| 984 | +* @author Dominique Stender <dstender@st-webdevelopment.de> | ||
| 985 | +*/ | ||
| 986 | +function cpaint_result_object() { | ||
| 987 | + this.id = 0; | ||
| 988 | + this.data = ''; | ||
| 989 | + var __attributes = new Array(); | ||
| 990 | + | ||
| 991 | + /** | ||
| 992 | + * Returns a subnode with the given type and id. | ||
| 993 | + * | ||
| 994 | + * @access public | ||
| 995 | + * @param string type The type of the subnode. Equivalent to the XML tag name. | ||
| 996 | + * @param string id The id of the subnode. Equivalent to the XML tag names id attribute. | ||
| 997 | + * @return object | ||
| 998 | + */ | ||
| 999 | + this.find_item_by_id = function() { | ||
| 1000 | + var return_value = null; | ||
| 1001 | + var type = arguments[0]; | ||
| 1002 | + var id = arguments[1]; | ||
| 1003 | + var i = 0; | ||
| 1004 | + | ||
| 1005 | + if (this[type]) { | ||
| 1006 | + | ||
| 1007 | + for (i = 0; i < this[type].length; i++) { | ||
| 1008 | + | ||
| 1009 | + if (this[type][i].get_attribute('id') == id) { | ||
| 1010 | + return_value = this[type][i]; | ||
| 1011 | + break; | ||
| 1012 | + } | ||
| 1013 | + } | ||
| 1014 | + } | ||
| 1015 | + | ||
| 1016 | + return return_value; | ||
| 1017 | + } | ||
| 1018 | + | ||
| 1019 | + /** | ||
| 1020 | + * retrieves the value of an attribute. | ||
| 1021 | + * | ||
| 1022 | + * @access public | ||
| 1023 | + * @param string name name of the attribute | ||
| 1024 | + * @return mixed | ||
| 1025 | + */ | ||
| 1026 | + this.get_attribute = function() { | ||
| 1027 | + var return_value = null; | ||
| 1028 | + var id = arguments[0]; | ||
| 1029 | + | ||
| 1030 | + if (typeof __attributes[id] != 'undefined') { | ||
| 1031 | + return_value = __attributes[id]; | ||
| 1032 | + } | ||
| 1033 | + | ||
| 1034 | + return return_value; | ||
| 1035 | + } | ||
| 1036 | + | ||
| 1037 | + /** | ||
| 1038 | + * assigns a value to an attribute. | ||
| 1039 | + * | ||
| 1040 | + * if that attribute does not exist it will be created. | ||
| 1041 | + * | ||
| 1042 | + * @access public | ||
| 1043 | + * @param string name name of the attribute | ||
| 1044 | + * @param string value value of the attribute | ||
| 1045 | + * @return void | ||
| 1046 | + */ | ||
| 1047 | + this.set_attribute = function() { | ||
| 1048 | + __attributes[arguments[0]] = arguments[1]; | ||
| 1049 | + } | ||
| 1050 | +} | ||
| 1051 | + | ||
| 1052 | + | ||
| 1053 | +/* | ||
| 1054 | +Copyright (c) 2005 JSON.org | ||
| 1055 | + | ||
| 1056 | +Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 1057 | +of this software and associated documentation files (the "Software"), to deal | ||
| 1058 | +in the Software without restriction, including without limitation the rights | ||
| 1059 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 1060 | +copies of the Software, and to permit persons to whom the Software is | ||
| 1061 | +furnished to do so, subject to the following conditions: | ||
| 1062 | + | ||
| 1063 | +The Software shall be used for Good, not Evil. | ||
| 1064 | + | ||
| 1065 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 1066 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 1067 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 1068 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 1069 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 1070 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 1071 | +SOFTWARE. | ||
| 1072 | +*/ | ||
| 1073 | + | ||
| 1074 | +//Array.prototype.______array = '______array'; | ||
| 1075 | + | ||
| 1076 | +var JSON = { | ||
| 1077 | + org: 'http://www.JSON.org', | ||
| 1078 | + copyright: '(c)2005 JSON.org', | ||
| 1079 | + license: 'http://www.crockford.com/JSON/license.html', | ||
| 1080 | + | ||
| 1081 | + stringify: function (arg) { | ||
| 1082 | + var c, i, l, s = '', v; | ||
| 1083 | + var numeric = true; | ||
| 1084 | + | ||
| 1085 | + switch (typeof arg) { | ||
| 1086 | + case 'object': | ||
| 1087 | + if (arg) { | ||
| 1088 | + if (arg.______array == '______array') { | ||
| 1089 | + // do a test whether all array keys are numeric | ||
| 1090 | + for (i in arg) { | ||
| 1091 | + if (i != '______array' | ||
| 1092 | + && (isNaN(i) | ||
| 1093 | + || !isFinite(i))) { | ||
| 1094 | + numeric = false; | ||
| 1095 | + break; | ||
| 1096 | + } | ||
| 1097 | + } | ||
| 1098 | + | ||
| 1099 | + if (numeric == true) { | ||
| 1100 | + for (i = 0; i < arg.length; ++i) { | ||
| 1101 | + if (typeof arg[i] != 'undefined') { | ||
| 1102 | + v = this.stringify(arg[i]); | ||
| 1103 | + if (s) { | ||
| 1104 | + s += ','; | ||
| 1105 | + } | ||
| 1106 | + s += v; | ||
| 1107 | + } else { | ||
| 1108 | + s += ',null'; | ||
| 1109 | + } | ||
| 1110 | + } | ||
| 1111 | + return '[' + s + ']'; | ||
| 1112 | + } else { | ||
| 1113 | + for (i in arg) { | ||
| 1114 | + if (i != '______array') { | ||
| 1115 | + v = arg[i]; | ||
| 1116 | + if (typeof v != 'undefined' && typeof v != 'function') { | ||
| 1117 | + v = this.stringify(v); | ||
| 1118 | + if (s) { | ||
| 1119 | + s += ','; | ||
| 1120 | + } | ||
| 1121 | + s += this.stringify(i) + ':' + v; | ||
| 1122 | + } | ||
| 1123 | + } | ||
| 1124 | + } | ||
| 1125 | + // return as object | ||
| 1126 | + return '{' + s + '}'; | ||
| 1127 | + } | ||
| 1128 | + } else if (typeof arg.toString != 'undefined') { | ||
| 1129 | + for (i in arg) { | ||
| 1130 | + v = arg[i]; | ||
| 1131 | + if (typeof v != 'undefined' && typeof v != 'function') { | ||
| 1132 | + v = this.stringify(v); | ||
| 1133 | + if (s) { | ||
| 1134 | + s += ','; | ||
| 1135 | + } | ||
| 1136 | + s += this.stringify(i) + ':' + v; | ||
| 1137 | + } | ||
| 1138 | + } | ||
| 1139 | + return '{' + s + '}'; | ||
| 1140 | + } | ||
| 1141 | + } | ||
| 1142 | + return 'null'; | ||
| 1143 | + case 'number': | ||
| 1144 | + return isFinite(arg) ? String(arg) : 'null'; | ||
| 1145 | + case 'string': | ||
| 1146 | + l = arg.length; | ||
| 1147 | + s = '"'; | ||
| 1148 | + for (i = 0; i < l; i += 1) { | ||
| 1149 | + c = arg.charAt(i); | ||
| 1150 | + if (c >= ' ') { | ||
| 1151 | + if (c == '\\' || c == '"') { | ||
| 1152 | + s += '\\'; | ||
| 1153 | + } | ||
| 1154 | + s += c; | ||
| 1155 | + } else { | ||
| 1156 | + switch (c) { | ||
| 1157 | + case '\b': | ||
| 1158 | + s += '\\b'; | ||
| 1159 | + break; | ||
| 1160 | + case '\f': | ||
| 1161 | + s += '\\f'; | ||
| 1162 | + break; | ||
| 1163 | + case '\n': | ||
| 1164 | + s += '\\n'; | ||
| 1165 | + break; | ||
| 1166 | + case '\r': | ||
| 1167 | + s += '\\r'; | ||
| 1168 | + break; | ||
| 1169 | + case '\t': | ||
| 1170 | + s += '\\t'; | ||
| 1171 | + break; | ||
| 1172 | + default: | ||
| 1173 | + c = c.charCodeAt(); | ||
| 1174 | + s += '\\u00' + Math.floor(c / 16).toString(16) + | ||
| 1175 | + (c % 16).toString(16); | ||
| 1176 | + } | ||
| 1177 | + } | ||
| 1178 | + } | ||
| 1179 | + return s + '"'; | ||
| 1180 | + case 'boolean': | ||
| 1181 | + return String(arg); | ||
| 1182 | + default: | ||
| 1183 | + return 'null'; | ||
| 1184 | + } | ||
| 1185 | + }, | ||
| 1186 | + parse: function (text) { | ||
| 1187 | + var at = 0; | ||
| 1188 | + var ch = ' '; | ||
| 1189 | + | ||
| 1190 | + function error(m) { | ||
| 1191 | + throw { | ||
| 1192 | + name: 'JSONError', | ||
| 1193 | + message: m, | ||
| 1194 | + at: at - 1, | ||
| 1195 | + text: text | ||
| 1196 | + }; | ||
| 1197 | + } | ||
| 1198 | + | ||
| 1199 | + function next() { | ||
| 1200 | + ch = text.charAt(at); | ||
| 1201 | + at += 1; | ||
| 1202 | + return ch; | ||
| 1203 | + } | ||
| 1204 | + | ||
| 1205 | + function white() { | ||
| 1206 | + while (ch != '' && ch <= ' ') { | ||
| 1207 | + next(); | ||
| 1208 | + } | ||
| 1209 | + } | ||
| 1210 | + | ||
| 1211 | + function str() { | ||
| 1212 | + var i, s = '', t, u; | ||
| 1213 | + | ||
| 1214 | + if (ch == '"') { | ||
| 1215 | +outer: while (next()) { | ||
| 1216 | + if (ch == '"') { | ||
| 1217 | + next(); | ||
| 1218 | + return s; | ||
| 1219 | + } else if (ch == '\\') { | ||
| 1220 | + switch (next()) { | ||
| 1221 | + case 'b': | ||
| 1222 | + s += '\b'; | ||
| 1223 | + break; | ||
| 1224 | + case 'f': | ||
| 1225 | + s += '\f'; | ||
| 1226 | + break; | ||
| 1227 | + case 'n': | ||
| 1228 | + s += '\n'; | ||
| 1229 | + break; | ||
| 1230 | + case 'r': | ||
| 1231 | + s += '\r'; | ||
| 1232 | + break; | ||
| 1233 | + case 't': | ||
| 1234 | + s += '\t'; | ||
| 1235 | + break; | ||
| 1236 | + case 'u': | ||
| 1237 | + u = 0; | ||
| 1238 | + for (i = 0; i < 4; i += 1) { | ||
| 1239 | + t = parseInt(next(), 16); | ||
| 1240 | + if (!isFinite(t)) { | ||
| 1241 | + break outer; | ||
| 1242 | + } | ||
| 1243 | + u = u * 16 + t; | ||
| 1244 | + } | ||
| 1245 | + s += String.fromCharCode(u); | ||
| 1246 | + break; | ||
| 1247 | + default: | ||
| 1248 | + s += ch; | ||
| 1249 | + } | ||
| 1250 | + } else { | ||
| 1251 | + s += ch; | ||
| 1252 | + } | ||
| 1253 | + } | ||
| 1254 | + } | ||
| 1255 | + error("Bad string"); | ||
| 1256 | + } | ||
| 1257 | + | ||
| 1258 | + function arr() { | ||
| 1259 | + var a = []; | ||
| 1260 | + | ||
| 1261 | + if (ch == '[') { | ||
| 1262 | + next(); | ||
| 1263 | + white(); | ||
| 1264 | + if (ch == ']') { | ||
| 1265 | + next(); | ||
| 1266 | + return a; | ||
| 1267 | + } | ||
| 1268 | + while (ch) { | ||
| 1269 | + a.push(val()); | ||
| 1270 | + white(); | ||
| 1271 | + if (ch == ']') { | ||
| 1272 | + next(); | ||
| 1273 | + return a; | ||
| 1274 | + } else if (ch != ',') { | ||
| 1275 | + break; | ||
| 1276 | + } | ||
| 1277 | + next(); | ||
| 1278 | + white(); | ||
| 1279 | + } | ||
| 1280 | + } | ||
| 1281 | + error("Bad array"); | ||
| 1282 | + } | ||
| 1283 | + | ||
| 1284 | + function obj() { | ||
| 1285 | + var k, o = {}; | ||
| 1286 | + | ||
| 1287 | + if (ch == '{') { | ||
| 1288 | + next(); | ||
| 1289 | + white(); | ||
| 1290 | + if (ch == '}') { | ||
| 1291 | + next(); | ||
| 1292 | + return o; | ||
| 1293 | + } | ||
| 1294 | + while (ch) { | ||
| 1295 | + k = str(); | ||
| 1296 | + white(); | ||
| 1297 | + if (ch != ':') { | ||
| 1298 | + break; | ||
| 1299 | + } | ||
| 1300 | + next(); | ||
| 1301 | + o[k] = val(); | ||
| 1302 | + white(); | ||
| 1303 | + if (ch == '}') { | ||
| 1304 | + next(); | ||
| 1305 | + return o; | ||
| 1306 | + } else if (ch != ',') { | ||
| 1307 | + break; | ||
| 1308 | + } | ||
| 1309 | + next(); | ||
| 1310 | + white(); | ||
| 1311 | + } | ||
| 1312 | + } | ||
| 1313 | + error("Bad object"); | ||
| 1314 | + } | ||
| 1315 | + | ||
| 1316 | + function assoc() { | ||
| 1317 | + var k, a = []; | ||
| 1318 | + | ||
| 1319 | + if (ch == '<') { | ||
| 1320 | + next(); | ||
| 1321 | + white(); | ||
| 1322 | + if (ch == '>') { | ||
| 1323 | + next(); | ||
| 1324 | + return a; | ||
| 1325 | + } | ||
| 1326 | + while (ch) { | ||
| 1327 | + k = str(); | ||
| 1328 | + white(); | ||
| 1329 | + if (ch != ':') { | ||
| 1330 | + break; | ||
| 1331 | + } | ||
| 1332 | + next(); | ||
| 1333 | + a[k] = val(); | ||
| 1334 | + white(); | ||
| 1335 | + if (ch == '>') { | ||
| 1336 | + next(); | ||
| 1337 | + return a; | ||
| 1338 | + } else if (ch != ',') { | ||
| 1339 | + break; | ||
| 1340 | + } | ||
| 1341 | + next(); | ||
| 1342 | + white(); | ||
| 1343 | + } | ||
| 1344 | + } | ||
| 1345 | + error("Bad associative array"); | ||
| 1346 | + } | ||
| 1347 | + | ||
| 1348 | + function num() { | ||
| 1349 | + var n = '', v; | ||
| 1350 | + if (ch == '-') { | ||
| 1351 | + n = '-'; | ||
| 1352 | + next(); | ||
| 1353 | + } | ||
| 1354 | + while (ch >= '0' && ch <= '9') { | ||
| 1355 | + n += ch; | ||
| 1356 | + next(); | ||
| 1357 | + } | ||
| 1358 | + if (ch == '.') { | ||
| 1359 | + n += '.'; | ||
| 1360 | + while (next() && ch >= '0' && ch <= '9') { | ||
| 1361 | + n += ch; | ||
| 1362 | + } | ||
| 1363 | + } | ||
| 1364 | + if (ch == 'e' || ch == 'E') { | ||
| 1365 | + n += 'e'; | ||
| 1366 | + next(); | ||
| 1367 | + if (ch == '-' || ch == '+') { | ||
| 1368 | + n += ch; | ||
| 1369 | + next(); | ||
| 1370 | + } | ||
| 1371 | + while (ch >= '0' && ch <= '9') { | ||
| 1372 | + n += ch; | ||
| 1373 | + next(); | ||
| 1374 | + } | ||
| 1375 | + } | ||
| 1376 | + v = +n; | ||
| 1377 | + if (!isFinite(v)) { | ||
| 1378 | + error("Bad number"); | ||
| 1379 | + } else { | ||
| 1380 | + return v; | ||
| 1381 | + } | ||
| 1382 | + } | ||
| 1383 | + | ||
| 1384 | + function word() { | ||
| 1385 | + switch (ch) { | ||
| 1386 | + case 't': | ||
| 1387 | + if (next() == 'r' && next() == 'u' && next() == 'e') { | ||
| 1388 | + next(); | ||
| 1389 | + return true; | ||
| 1390 | + } | ||
| 1391 | + break; | ||
| 1392 | + case 'f': | ||
| 1393 | + if (next() == 'a' && next() == 'l' && next() == 's' && | ||
| 1394 | + next() == 'e') { | ||
| 1395 | + next(); | ||
| 1396 | + return false; | ||
| 1397 | + } | ||
| 1398 | + break; | ||
| 1399 | + case 'n': | ||
| 1400 | + if (next() == 'u' && next() == 'l' && next() == 'l') { | ||
| 1401 | + next(); | ||
| 1402 | + return null; | ||
| 1403 | + } | ||
| 1404 | + break; | ||
| 1405 | + } | ||
| 1406 | + error("Syntax error"); | ||
| 1407 | + } | ||
| 1408 | + | ||
| 1409 | + function val() { | ||
| 1410 | + white(); | ||
| 1411 | + switch (ch) { | ||
| 1412 | + case '{': | ||
| 1413 | + return obj(); | ||
| 1414 | + case '[': | ||
| 1415 | + return arr(); | ||
| 1416 | + case '<': | ||
| 1417 | + return assoc(); | ||
| 1418 | + case '"': | ||
| 1419 | + return str(); | ||
| 1420 | + case '-': | ||
| 1421 | + return num(); | ||
| 1422 | + default: | ||
| 1423 | + return ch >= '0' && ch <= '9' ? num() : word(); | ||
| 1424 | + } | ||
| 1425 | + } | ||
| 1426 | + | ||
| 1427 | + return val(); | ||
| 1428 | + } | ||
| 1429 | +}; |
ferramentas/selecao/index.js
| @@ -362,9 +362,9 @@ function concluipoligono() | @@ -362,9 +362,9 @@ function concluipoligono() | ||
| 362 | } | 362 | } |
| 363 | function atualizaGrafico() | 363 | function atualizaGrafico() |
| 364 | { | 364 | { |
| 365 | + $i("lugarGrafico").innerHTML = "" | ||
| 365 | var monta = function(retorno) | 366 | var monta = function(retorno) |
| 366 | - { | ||
| 367 | - $i("lugarGrafico").innerHTML = "<canvas id='canvasTest' width='350' height='180' style='border: 1px solid #eee;'></canvas>" | 367 | + { |
| 368 | var dados = retorno.data.dados; | 368 | var dados = retorno.data.dados; |
| 369 | var values = new Array(); | 369 | var values = new Array(); |
| 370 | var labels = new Array(); | 370 | var labels = new Array(); |
| @@ -396,10 +396,19 @@ function atualizaGrafico() | @@ -396,10 +396,19 @@ function atualizaGrafico() | ||
| 396 | renderer.clear(); | 396 | renderer.clear(); |
| 397 | renderer.render(); | 397 | renderer.render(); |
| 398 | } | 398 | } |
| 399 | - if($i("itemX").value == "" || $i("itemY").value == "") | ||
| 400 | - {alert("Escolha as colunas primeiro");} | ||
| 401 | - var p = g_locaplic+"/classesphp/mapa_controle.php?g_sid="+g_sid+"&funcao=graficoSelecao&tema="+$i("comboTemas").value+"&itemclasses="+$i("itemX").value+"&itemvalores="+$i("itemY").value | ||
| 402 | - var cp = new cpaint(); | ||
| 403 | - cp.set_response_type("JSON"); | ||
| 404 | - cp.call(p,"graficoSelecao",monta); | 399 | + if(!$i("canvasTest")) |
| 400 | + $i("lugarGrafico").innerHTML = "<canvas id='canvasTest' width='350' height='180' style='border: 1px solid #eee;'></canvas>" | ||
| 401 | + if ($i("canvasTest").getContext) | ||
| 402 | + { | ||
| 403 | + if($i("itemX").value == "" || $i("itemY").value == "") | ||
| 404 | + {alert("Escolha as colunas primeiro");} | ||
| 405 | + var p = g_locaplic+"/classesphp/mapa_controle.php?g_sid="+g_sid+"&funcao=graficoSelecao&tema="+$i("comboTemas").value+"&itemclasses="+$i("itemX").value+"&itemvalores="+$i("itemY").value | ||
| 406 | + var cp = new cpaint(); | ||
| 407 | + cp.set_response_type("JSON"); | ||
| 408 | + cp.call(p,"graficoSelecao",monta); | ||
| 409 | + } | ||
| 410 | + else | ||
| 411 | + { | ||
| 412 | + $i("lugarGrafico").innerHTML = "<span style=color:red >Você precisa atuallizar seu navegador para que o gráfico possa ser mostrado</span>"; | ||
| 413 | + } | ||
| 405 | } | 414 | } |