ChatSession.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.xmpp.widget.ChatSession"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.xmpp.widget.ChatSession"] = true;
  8. dojo.provide("dojox.xmpp.widget.ChatSession");
  9. dojo.declare("dojox.xmpp.widget.ChatSession",
  10. [dijit.layout.LayoutContainer, dijit._Templated],
  11. {
  12. templateString: dojo.cache("dojox.xmpp.widget", "templates/ChatSession.html", "<div>\n<div dojoAttachPoint=\"messages\" dojoType=\"dijit.layout.ContentPane\" layoutAlign=\"client\" style=\"overflow:auto\">\n</div>\n<div dojoType=\"dijit.layout.ContentPane\" layoutAlign=\"bottom\" style=\"border-top: 2px solid #333333; height: 35px;\"><input dojoAttachPoint=\"chatInput\" dojoAttachEvent=\"onkeypress: onKeyPress\" style=\"width: 100%;height: 35px;\" /></div>\n</div>\n"),
  13. enableSubWidgets: true,
  14. widgetsInTemplate: true,
  15. widgetType: "ChatSession",
  16. chatWith: null,
  17. instance: null,
  18. postCreate: function(){
  19. //console.log("Neato!");
  20. },
  21. displayMessage: function(message, type) {
  22. //console.log("displayMessage", this, message);
  23. if(message) {
  24. var name = message.from ? this.chatWith : "me";
  25. this.messages.domNode.innerHTML += "<b>" + name + ":</b> " + message.body + "<br/>";
  26. this.goToLastMessage();
  27. }
  28. },
  29. goToLastMessage: function() {
  30. this.messages.domNode.scrollTop = this.messages.domNode.scrollHeight;
  31. },
  32. onKeyPress: function(e){
  33. var key = e.keyCode || e.charCode;
  34. if ((key == dojo.keys.ENTER) && (this.chatInput.value != "")){
  35. this.instance.sendMessage({body: this.chatInput.value});
  36. this.displayMessage( {body: this.chatInput.value}, "out");
  37. this.chatInput.value = "";
  38. }
  39. }
  40. });
  41. }