1 | function cIM() |
---|
2 | { |
---|
3 | this.DEFAULT_URL = im_path + 'controller.php?action='; |
---|
4 | this.connector = XMLTools; |
---|
5 | this._onmouseup = document.onmouseup; |
---|
6 | this.isExecuteForm = false; |
---|
7 | this.id = []; |
---|
8 | this._handler = false; |
---|
9 | this.json = false; |
---|
10 | } |
---|
11 | |
---|
12 | cIM.prototype.cExecute = function (url, handler, params, form) |
---|
13 | { |
---|
14 | var _this = this; |
---|
15 | if ( this.isExecuteForm ) |
---|
16 | { |
---|
17 | _this.isExecuteForm = false; |
---|
18 | document.onmouseup = this._onmouseup; |
---|
19 | } |
---|
20 | |
---|
21 | if ( form ) |
---|
22 | { |
---|
23 | //this.cExecuteForm(url, form); |
---|
24 | return; |
---|
25 | } |
---|
26 | |
---|
27 | var id = url; |
---|
28 | if ( _this.id[id] ) |
---|
29 | return false; |
---|
30 | else |
---|
31 | _this.id[id] = handler; |
---|
32 | |
---|
33 | if ( params ) |
---|
34 | method = "POST"; |
---|
35 | else |
---|
36 | method = "GET"; |
---|
37 | |
---|
38 | handler = _this._interface; |
---|
39 | _this.connector.request(url, method, handler, params); |
---|
40 | } |
---|
41 | cIM.prototype._interface = function(data, url) |
---|
42 | { |
---|
43 | data = data.getElementsByTagName('retorno').item(0); |
---|
44 | data = data.firstChild.nodeValue; |
---|
45 | if ( /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(data) ) |
---|
46 | { |
---|
47 | data = eval('(' + data + ')'); |
---|
48 | if ( cIM.json ) |
---|
49 | cIM.json = false; |
---|
50 | else |
---|
51 | if ( data.constructor == Object ) |
---|
52 | data = json2array(data); |
---|
53 | } |
---|
54 | |
---|
55 | var handler = cIM.id[url]; |
---|
56 | cIM.id[url] = false; |
---|
57 | delete cIM.id[url]; |
---|
58 | handler(data); |
---|
59 | } |
---|
60 | var cIM = new cIM; |
---|
61 | |
---|
62 | |
---|
63 | function json2array(pObject) |
---|
64 | { |
---|
65 | var retorno = []; |
---|
66 | for( var i in pObject ) |
---|
67 | if( pObject[i].constructor == Object ) |
---|
68 | { |
---|
69 | retorno[i] = []; |
---|
70 | retorno[i] = json2array(pObject[i]); |
---|
71 | } |
---|
72 | else |
---|
73 | retorno[i] = pObject[i]; |
---|
74 | |
---|
75 | return retorno; |
---|
76 | } |
---|
77 | function json2str(pObject) |
---|
78 | { |
---|
79 | var retorno = '{'; |
---|
80 | for( var i in pObject ) |
---|
81 | if( pObject[i].constructor == Object ) |
---|
82 | retorno += '\'' + i + '\':' + json2str(pObject[i]) + ','; |
---|
83 | else |
---|
84 | retorno += '\'' + i + '\':\'' + pObject[i] + '\','; |
---|
85 | |
---|
86 | retorno = retorno.substr(0, (retorno.length - 1)); |
---|
87 | retorno += '}'; |
---|
88 | return retorno; |
---|
89 | } |
---|