[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/modules/Contacts/js/ -> chat.js (source)

   1  /*
   2      Copyright 2005 Rolando Gonzalez (rolosworld@gmail.com)
   3  
   4      This program is free software; you can redistribute it and/or modify
   5      it under the terms of the GNU General Public License as published by
   6      the Free Software Foundation; either version 2 of the License, or
   7      (at your option) any later version.
   8  
   9      This program is distributed in the hope that it will be useful,
  10      but WITHOUT ANY WARRANTY; without even the implied warranty of
  11      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12      GNU General Public License for more details.
  13  
  14      You should have received a copy of the GNU General Public License
  15      along with this program; if not, write to the Free Software
  16      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  17  */
  18  
  19  var chats = new Array(); // private chats opened
  20  var users = new Array(); // users list
  21  var chat_data = null;
  22  var mlid = 0; //msg last id
  23  var DEBUG = false;
  24  var PRIVATES = true;
  25  var PUBLIC = true;
  26  
  27  function getNick(uid)
  28  {
  29    for(var i in users)
  30    {
  31      if(users[i].uid == uid)
  32        return users[i].nick;
  33    }
  34    return null;
  35  };
  36  
  37  function resetChatsZ()
  38  {
  39    for(var i = 0; i < chats.length; i++)
  40      if(chats[i])
  41        chats[i].win.table.style.zIndex = chats[i].win.z;
  42  };
  43  
  44  function debug(dib,msg)
  45  {
  46    if(!DEBUG) return;
  47    if(document.getElementById(dib))
  48      document.getElementById(dib).innerHTML = msg;
  49    else
  50      alert(msg);
  51  };
  52  
  53  // initialize chat connection.
  54  function Chat(conf)
  55  {
  56    var me = this;
  57    this.pcid = conf["pchatid"]?conf["pchatid"]:null;
  58    this.dt = conf["dt"]?conf["dt"]:1000;
  59    this.ulid = conf["ulid"]?conf["ulid"]:null;
  60    this.w = conf["width"]?conf["width"]:"400px";
  61    this.h = conf["height"]?conf["height"]:"300px";
  62    this.ulist = null;
  63  
  64    if(this.pcid)
  65    {
  66      chats[0] = new PubChat(this.dt,this.w,this.h);
  67      document.getElementById(this.pcid).appendChild(chats[0].get());
  68    }
  69  
  70    // evaluate the returned json
  71    this.callback = function(response)
  72    {
  73      me.ajax = null;
  74      try{
  75        me.doRefresh(response.responseText);
  76      }catch(e){
  77        debug("debug","Chat Error: "+response.responseText);
  78        clearInterval(me.interv);
  79        debug("debug",e.toString());
  80        debug("debug",e.filename+":"+e.lineNumber);
  81        return;
  82      }
  83      me.ajax = new Ajax(me.callback);
  84      debug("debug",response.responseText);
  85      window.status=Date();
  86    };
  87  
  88    this.doRefresh = function(response)
  89    {
  90      if(!me.ulist)
  91        me.ulist = new UList(me.dt,me.ulid,me.w,me.h);
  92      chat_data = eval("("+response+")");
  93      me.ulist.refresh();
  94      me.refreshChats();
  95    };
  96  
  97    this.ajax = new Ajax(me.callback);
  98  
  99    // start the ajax request for the chat data
 100    this.refresh = function()
 101    {
 102      if(me.ajax.state() == 0)
 103        me.ajax.process("index.php?mode=chat&module=Contacts&action=chat","submode=get_all&mlid="+mlid);
 104    };
 105    this.interv = setInterval(me.refresh,me.dt);
 106  
 107    this.refreshChats = function()
 108    {
 109      if(chat_data.pvchat)
 110      {
 111        cnum = 0;
 112        for(var i in chat_data.pvchat)
 113        {
 114          cnum = chat_data.pvchat[i].chat;
 115          if(!chats[cnum])
 116            chats[cnum] = new PrivChat(me.dt,cnum,me.w,me.h);
 117  
 118          if(mlid < chat_data.pvchat[i].mlid)
 119            mlid = chat_data.pvchat[i].mlid;
 120  
 121          if(chat_data.pvchat[i].msg.substr(0,5) == "\\sys ")
 122          {
 123            chats[cnum].appendSysMsg(chat_data.pvchat[i].msg.substr(5));
 124          }
 125          else
 126            chats[cnum].appendMsg(chat_data.pvchat[i].from,chat_data.pvchat[i].msg);
 127        }
 128        chat_data.pvchat = null;
 129      }
 130  
 131      if(PUBLIC && chat_data.pchat)
 132      {
 133        for(var i in chat_data.pchat)
 134        {
 135          if(mlid < chat_data.pchat[i].mlid)
 136            mlid = chat_data.pchat[i].mlid;
 137  
 138          if(!chats[0])
 139            continue;
 140  
 141          if(chat_data.pchat[i].msg.substr(0,5) == "\\sys ")
 142          {
 143            chats[0].appendSysMsg(chat_data.pchat[i].msg.substr(5));
 144          }
 145          else
 146            chats[0].appendMsg(chat_data.pchat[i].from,chat_data.pchat[i].msg);
 147        }
 148        chat_data.pchat = null;
 149      }
 150    };
 151  };
 152  
 153  // User list handler
 154  function UList(dt,ulid,w,h)
 155  {
 156    var me = this;
 157    this.w = w;
 158    this.h = h;
 159    this.dt = dt; // delta time to sleep the requests.
 160    this.ulid = ulid; // user list ul tag id.
 161    this.interv = null;
 162  
 163    // updates the users list on the html
 164    this.refreshList = function()
 165    {
 166      if(!document.getElementById(me.ulid))
 167      {
 168        clearInterval(me.interv);
 169        return;
 170      }
 171  
 172      while(document.getElementById(me.ulid).firstChild)
 173        document.getElementById(me.ulid).removeChild(document.getElementById(me.ulid).firstChild);
 174  
 175      if(users)
 176      {
 177        var li;
 178        var a;
 179        var user;
 180        for(var i in users)
 181        {
 182          user = users[i];
 183          li = document.createElement("span");
 184          a = document.createElement("a");
 185          a.appendChild(document.createTextNode(user.nick));
 186          a.setAttribute("href","#");
 187          a.className = "chat";
 188          if(PRIVATES)
 189            a.onclick = me.newPriv(me.dt,user.uid);
 190  
 191          li.appendChild(a);
 192          document.getElementById(me.ulid).appendChild(li);
 193        }
 194        //var date = new Date();
 195        //window.status=date.toString();
 196      }
 197    };
 198  
 199    // higher order stuff
 200    this.newPriv = function(dt,uid)
 201    {
 202      return function()
 203      {
 204        if(!chats[uid])
 205        {
 206          resetChatsZ();
 207          chats[uid] = new PrivChat(dt,uid,me.w,me.h);
 208        }
 209        return false;
 210      };
 211    };
 212  
 213    // check if theres a new user list
 214    this.refresh = function()
 215    {
 216      //users = null;
 217      if(chat_data.ulist)
 218      {
 219        users = chat_data.ulist;
 220        me.refreshList();
 221      }
 222      chat_data.ulist = null;
 223    };
 224  };
 225  
 226  //////////////////////////////////////////////////////////
 227  // Input handler
 228  function chatInput(to)
 229  {
 230    var me = this;
 231    this.to = to?to:null;
 232    this.input = document.createElement("input");
 233    this.input.className = "cinput";
 234    this.input.setAttribute("type","text");
 235    this.input.setAttribute("name","input");
 236  
 237    var table = document.createElement("table");
 238    //table.border = "1";
 239    var tbody = table.appendChild(document.createElement("tbody"));
 240    var tr = tbody.appendChild(document.createElement("tr"));
 241    var td = tr.appendChild(document.createElement("td"));
 242    var td1 = tr.appendChild(document.createElement("td"));
 243    var td2 = tr.appendChild(document.createElement("td"));
 244  
 245    table.className = "cinput";
 246    tbody.className = "cinput";
 247    tr.className = "cinput";
 248    td.className = "ckeyb";
 249    td1.className = "cinput";
 250    td2.className = "csubmit";
 251  
 252    this.input = td1.appendChild(this.input);
 253    td2.onclick = function()
 254    {
 255      var ajax = new Ajax(me.callback);
 256      ajax.process("index.php?mode=chat&module=Contacts&action=chat","submode=submit&msg="+escape(me.input.value,1)+(me.to?"&to="+me.to:""));
 257      me.input.value = "";
 258      me.input.focus();
 259      return false;
 260    };
 261  
 262    this.form = document.createElement("form");
 263    this.form.className = "cinput";
 264  
 265    this.form.appendChild(table);
 266    this.form.onsubmit = function()
 267    {
 268      var ajax = new Ajax(me.callback);
 269      ajax.process("index.php?mode=chat&module=Contacts&action=chat","submode=submit&msg="+escape(me.input.value,1)+(me.to?"&to="+me.to:""));
 270      me.input.value = "";
 271      me.input.focus();
 272      return false;
 273    };
 274  
 275    this.setFocus = function()
 276    {
 277      me.input.focus();
 278    };
 279  
 280    // evaluate the returned json
 281    this.callback = function(response)
 282    {
 283      response = response.responseText;
 284      try{
 285        if(response)
 286          debug("debug",response);
 287      }catch(e){
 288        debug("debug","chatInput Error: "+response);
 289      }
 290    };
 291  
 292    this.get = function()
 293    {
 294      return me.form;
 295    };
 296  };
 297  
 298  //////////////////////////////////////////////////////////
 299  // Private chat handler / abre ventana de usurio + usuario
 300  function PrivChat(dt,to,w,h)
 301  {
 302    var me = this;
 303    this.dt = dt; // delta time to sleep the requests.
 304    this.to = to; // private chat the other user id
 305    this.input = new chatInput(to);
 306    this.toNick = getNick(to);
 307  
 308    var conf = new Array();
 309    conf["topic"] = "Private chat with <span class=\"chatTopicNick\">"+me.toNick+"</span>";
 310    conf["class"] = "chat";
 311    conf["width"] = w;
 312    conf["height"] = h;
 313    conf["drag"] = true;
 314  
 315    this.win = new cssWindow(conf);
 316  
 317    // move z+1..crappy way to send above others
 318    this.win.table.onDragStart = function()
 319    {
 320      resetChatsZ();
 321      chats[me.to].win.table.style.zIndex++;
 322    };
 323  
 324    this.win.cb = function()
 325    {
 326      me.ajax = new Ajax(me.callback);
 327      me.ajax.process("index.php?mode=chat&module=Contacts&action=chat","submode=pvclose&to="+me.to);
 328      chats[me.to]=null;
 329    };
 330  
 331    this.cbox = this.win.setBody(document.createElement("div"));
 332    this.cbox.style.width = "100%";
 333    this.cbox.style.height = "100%";
 334    this.cbox.style.overflow = "auto";
 335    this.cbox.className = "chatbox";
 336  
 337    // Draws the top of the window
 338    this.getHead = function()
 339    {
 340      var t = document.createElement("table");
 341      t.style.width="100%";t.cellSpacing="0";t.cellPadding="0";
 342      var tb = t.appendChild(document.createElement("tbody"));
 343      var tr = tb.appendChild(document.createElement("tr"));
 344      var td = tr.appendChild(document.createElement("td"));
 345      td.className = "chaticon";
 346      td = tr.appendChild(document.createElement("td"));
 347      td.className = "chattopic1";
 348      td.innerHTML = me.win.topic;
 349      var hide = tr.appendChild(document.createElement("td"));
 350      hide.className = "chathide";
 351      hide.onclick = me.win.doHide;
 352      var close = tr.appendChild(document.createElement("td"));
 353      close.className = "chatclose";
 354      close.onclick = me.win.doClose;
 355      return t;
 356    };
 357  
 358    this.win.setHead(this.getHead());
 359    this.win.setFoot(this.input.get());
 360  
 361  
 362    // updates the chat on the html
 363    this.appendMsg = function(from,msg)
 364    {
 365      var div;
 366      var span;
 367  
 368      span = document.createElement("span");
 369      span.appendChild(document.createTextNode(from+": "));
 370      span.className = "cunick";
 371  
 372      div = document.createElement("div");
 373      div.className = "cumsg";
 374          
 375      div.appendChild(span);
 376      div.innerHTML+=msg;
 377      me.cbox.appendChild(div);
 378      me.cbox.scrollTop=me.cbox.scrollHeight;
 379    };
 380  
 381    // updates the chat on the html
 382    this.appendSysMsg = function(msg)
 383    {
 384      var div;
 385      div = document.createElement("div");
 386      div.className = "csmsg";
 387      div.innerHTML+=msg;
 388      me.cbox.appendChild(div);
 389      me.cbox.scrollTop=me.cbox.scrollHeight;
 390    };
 391  
 392    this.win.show();
 393    this.input.setFocus();
 394  };
 395  
 396  //////////////////////////////////////////////////////////
 397  // Public chat handler / abre ventana de usurio + usuario
 398  function PubChat(dt,w,h)
 399  {
 400    var me = this;
 401    this.dt = dt; // delta time to sleep the requests.
 402    this.to = 0; // public chat the other user id
 403    this.input = new chatInput(this.to);
 404  
 405    var conf = new Array();
 406    conf["topic"] = "Public Chat";
 407    conf["class"] = "pchat";
 408    conf["width"] = w;
 409    conf["height"] = h;
 410    conf["drag"] = false;
 411  
 412    this.win = new cssWindow(conf);
 413  
 414    this.cbox = this.win.setBody(document.createElement("div"));
 415    this.cbox.style.width = "100%";
 416    this.cbox.style.height = "100%";
 417    this.cbox.style.overflow = "auto";
 418    this.cbox.className = "chatbox";
 419  
 420    // Draws the top of the window
 421    this.getHead = function()
 422    {
 423      var t = document.createElement("table");
 424      t.style.width="100%";t.cellSpacing="0";t.cellPadding="0";
 425      var tb = t.appendChild(document.createElement("tbody"));
 426      var tr = tb.appendChild(document.createElement("tr"));
 427      var td = tr.appendChild(document.createElement("td"));
 428      td.className = "chattopic";
 429      td.appendChild(document.createTextNode(me.win.topic));
 430      var hide = tr.appendChild(document.createElement("td"));
 431      hide.className = "chathide";
 432      hide.onclick = me.win.doHide;
 433  
 434      return t;
 435    };
 436  
 437    this.win.setHead(this.getHead());
 438    this.win.setFoot(this.input.get());
 439  
 440    // updates the chat on the html
 441    this.appendMsg = function(from,msg)
 442    {
 443      var div;
 444      var span;
 445  
 446      span = document.createElement("span");
 447      span.appendChild(document.createTextNode(from+": "));
 448      span.className = "cunick";
 449  
 450      div = document.createElement("div");
 451      div.className = "cumsg";
 452          
 453      div.appendChild(span);
 454      div.innerHTML+=msg;
 455      me.cbox.appendChild(div);
 456      me.cbox.scrollTop=me.cbox.scrollHeight;
 457    };
 458  
 459    // updates the chat on the html
 460    this.appendSysMsg = function(msg)
 461    {
 462      var div;
 463      div = document.createElement("div");
 464      div.className = "csmsg";
 465      div.innerHTML+=msg;
 466      me.cbox.appendChild(div);
 467      me.cbox.scrollTop=me.cbox.scrollHeight;
 468    };
 469  
 470    this.get = function()
 471    {
 472      me.input.setFocus();
 473      return me.win.get();
 474    };
 475  };


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7