source: trunk/instant_messenger/js/im_connector.js @ 20

Revision 20, 2.0 KB checked in by niltonneto, 17 years ago (diff)

Inclusão do módulo Mensageiro Instantâneo no CVS.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1function 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
12cIM.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   url = _this.DEFAULT_URL + url;
28
29   var _date = new Date();
30   var id = url + '&thread_time=' + _date.getHours() + _date.getMinutes() + _date.getSeconds() + _date.getMilliseconds();
31   if ( _this.id[id] )
32      return false;
33   else
34      _this.id[id] = handler;
35
36   if ( params )
37      method = "POST";
38   else
39      method = "GET";
40
41   handler = _this._interface;
42   _this.connector.request(id, url, method, handler, params);
43}
44cIM.prototype._interface = function(data, url)
45{
46   data = data.getElementsByTagName('retorno').item(0);
47   data = data.firstChild.nodeValue;
48   if ( /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(data) )
49   {
50      eval('data = ' + data);
51      if ( cIM.json )
52         cIM.json = false;
53      else
54         data = json2array(data);
55   }
56
57   var handler = cIM.id[url];
58   cIM.id[url] = false;
59   delete cIM.id[url];
60   handler(data);
61}
62var cIM = new cIM;
63
64
65function json2array(pObject)
66{
67   var retorno = [];
68   for( var i in pObject )
69      if( pObject[i].constructor == Object )
70      {
71         retorno[i] = [];
72         retorno[i] = json2array(pObject[i]);
73      }
74      else
75         retorno[i] = pObject[i];
76
77   return retorno;
78}
79function json2str(pObject)
80{
81   var retorno = '{';
82   for( var i in pObject )
83      if( pObject[i].constructor == Object )
84         retorno += '\'' + i + '\':' + json2str(pObject[i]) + ',';
85      else
86         retorno += '\'' + i + '\':\'' + pObject[i] + '\',';
87
88   retorno = retorno.substr(0, (retorno.length - 1));
89   retorno += '}';
90   return retorno;
91}
Note: See TracBrowser for help on using the repository browser.