room.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. if (!dojo._hasResource["dijit.demos.chat.room"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dijit.demos.chat.room"] = true;
  6. dojo.provide("dijit.demos.chat.room");
  7. dojo.require("dojox.cometd");
  8. dojo.require("dijit._Widget");
  9. dojo.require("dijit._Templated");
  10. dojo.declare("dijit.demos.chat.Room", [dijit._Widget, dijit._Templated], {
  11. _last : "",
  12. _username : null,
  13. roomId : "public",
  14. isPrivate : false,
  15. prompt : "Name:",
  16. templateString : '<div id="${id}" class="chatroom">'
  17. + '<div dojoAttachPoint="chatNode" class="chat"></div>'
  18. + '<div dojoAttachPoint="input" class="input">'
  19. + '<div dojoAttachPoint="joining">'
  20. + '<span>${prompt}</span><input class="username" dojoAttachPoint="username" type="text" dojoAttachEvent="onkeyup: _join"> <input dojoAttachPoint="joinB" class="button" type="submit" name="join" value="Contact" dojoAttachEvent="onclick: _join"/>'
  21. + '</div>'
  22. + '<div dojoAttachPoint="joined" class="hidden">'
  23. + '<input type="text" class="phrase" dojoAttachPoint="phrase" dojoAttachEvent="onkeyup: _cleanInput" />'
  24. + '<input type="submit" class="button" value="Send" dojoAttachPoint="sendB" dojoAttachEvent="onclick: _sendPhrase"/>'
  25. + '</div>' + '</div>' + '</div>',
  26. join : function(name) {
  27. if (name == null || name.length == 0) {
  28. alert('Please enter a username!');
  29. } else {
  30. if (this.isPrivate) {
  31. this.roomId = name;
  32. }
  33. this._username = name;
  34. this.joining.className = 'hidden';
  35. this.joined.className = '';
  36. this.phrase.focus();
  37. console.log(this.roomId);
  38. dojox.cometd.subscribe("/chat/demo/" + this.roomId, this,
  39. "_chat");
  40. dojox.cometd.publish("/chat/demo/" + this.roomId, {
  41. user : this._username,
  42. join : true,
  43. chat : this._username + " has joined the room."
  44. });
  45. dojox.cometd.publish("/chat/demo", {
  46. user : this._username,
  47. joined : this.roomId
  48. });
  49. }
  50. },
  51. _join : function(/* Event */e) {
  52. var key = (e.charCode == dojo.keys.SPACE
  53. ? dojo.keys.SPACE
  54. : e.keyCode);
  55. if (key == dojo.keys.ENTER || e.type == "click") {
  56. this.join(this.username.value);
  57. }
  58. },
  59. leave : function() {
  60. dojox.cometd
  61. .unsubscribe("/chat/demo/" + this.roomId, this, "_chat");
  62. dojox.cometd.publish("/chat/demo/" + this.roomId, {
  63. user : this._username,
  64. leave : true,
  65. chat : this._username + " has left the chat."
  66. });
  67. // switch the input form back to login mode
  68. this.joining.className = '';
  69. this.joined.className = 'hidden';
  70. this.username.focus();
  71. this._username = null;
  72. },
  73. chat : function(text) {
  74. // summary: publish a text message to the room
  75. if (text != null && text.length > 0) {
  76. // lame attempt to prevent markup
  77. text = text.replace(/</g, '&lt;');
  78. text = text.replace(/>/g, '&gt;');
  79. dojox.cometd.publish("/chat/demo/" + this.roomId, {
  80. user : this._username,
  81. chat : text
  82. });
  83. }
  84. },
  85. _chat : function(message) {
  86. // summary: process an incoming message
  87. if (!message.data) {
  88. console.warn("bad message format " + message);
  89. return;
  90. }
  91. var from = message.data.user;
  92. var special = message.data.join || message.data.leave;
  93. var text = message.data.chat;
  94. if (text != null) {
  95. if (!special && from == this._last) {
  96. from = "...";
  97. } else {
  98. this._last = from;
  99. from += ":";
  100. }
  101. if (special) {
  102. this.chatNode.innerHTML += "<span class=\"alert\"><span class=\"from\">"
  103. + from
  104. + "&nbsp;</span><span class=\"text\">"
  105. + text + "</span></span><br/>";
  106. this._last = "";
  107. } else {
  108. this.chatNode.innerHTML += "<span class=\"from\">" + from
  109. + "&nbsp;</span><span class=\"text\">" + text
  110. + "</span><br/>";
  111. this.chatNode.scrollTop = this.chatNode.scrollHeight
  112. - this.chatNode.clientHeight;
  113. }
  114. }
  115. },
  116. startup : function() {
  117. this.joining.className = '';
  118. this.joined.className = 'hidden';
  119. // this.username.focus();
  120. this.username.setAttribute("autocomplete", "OFF");
  121. if (this.registeredAs) {
  122. this.join(this.registeredAs);
  123. }
  124. this.inherited("startup", arguments);
  125. },
  126. _cleanInput : function(/* Event */e) {
  127. var key = (e.charCode == dojo.keys.SPACE
  128. ? dojo.keys.SPACE
  129. : e.keyCode);
  130. if (key == dojo.keys.ENTER || key == 13) {
  131. this.chat(this.phrase.value);
  132. this.phrase.value = '';
  133. }
  134. },
  135. _sendPhrase : function(/* Event */e) {
  136. if (this.phrase.value) {
  137. this.chat(this.phrase.value);
  138. this.phrase.value = '';
  139. }
  140. }
  141. });
  142. }