1 if(!dojo._hasResource["dijit.demos.chat.room"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dijit.demos.chat.room"] = true;
3 dojo.provide("dijit.demos.chat.room");
5 dojo.require("dojox.cometd");
6 dojo.require("dijit._Widget");
7 dojo.require("dijit._Templated");
9 dojo.declare("dijit.demos.chat.Room",
10 [dijit._Widget,dijit._Templated],
19 templateString: '<div id="${id}" class="chatroom">'
20 +'<div dojoAttachPoint="chatNode" class="chat"></div>'
21 +'<div dojoAttachPoint="input" class="input">'
22 +'<div dojoAttachPoint="joining">'
23 +'<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"/>'
25 +'<div dojoAttachPoint="joined" class="hidden">'
26 +'<input type="text" class="phrase" dojoAttachPoint="phrase" dojoAttachEvent="onkeyup: _cleanInput" />'
27 +'<input type="submit" class="button" value="Send" dojoAttachPoint="sendB" dojoAttachEvent="onclick: _sendPhrase"/>'
33 if(name == null || name.length==0){
34 alert('Please enter a username!');
36 if(this.isPrivate){ this.roomId = name; }
38 this.joining.className='hidden';
39 this.joined.className='';
41 console.log(this.roomId);
42 dojox.cometd.subscribe("/chat/demo/" + this.roomId, this, "_chat");
43 dojox.cometd.publish("/chat/demo/" + this.roomId, { user: this._username, join: true, chat : this._username+" has joined the room."});
44 dojox.cometd.publish("/chat/demo", { user: this._username, joined: this.roomId });
48 _join: function(/* Event */e){
49 var key = (e.charCode == dojo.keys.SPACE ? dojo.keys.SPACE : e.keyCode);
50 if (key == dojo.keys.ENTER || e.type=="click"){
51 this.join(this.username.value);
56 dojox.cometd.unsubscribe("/chat/demo/" + this.roomId, this, "_chat");
57 dojox.cometd.publish("/chat/demo/" + this.roomId, { user: this._username, leave: true, chat : this._username+" has left the chat."});
59 // switch the input form back to login mode
60 this.joining.className='';
61 this.joined.className='hidden';
62 this.username.focus();
67 // summary: publish a text message to the room
68 if(text != null && text.length>0){
69 // lame attempt to prevent markup
70 text=text.replace(/</g,'<');
71 text=text.replace(/>/g,'>');
72 dojox.cometd.publish("/chat/demo/" + this.roomId, { user: this._username, chat: text});
76 _chat: function(message){
77 // summary: process an incoming message
79 console.warn("bad message format "+message);
82 var from=message.data.user;
83 var special=message.data.join || message.data.leave;
84 var text=message.data.chat;
86 if(!special && from == this._last ){ from="...";
93 this.chatNode.innerHTML += "<span class=\"alert\"><span class=\"from\">"+from+" </span><span class=\"text\">"+text+"</span></span><br/>";
96 this.chatNode.innerHTML += "<span class=\"from\">"+from+" </span><span class=\"text\">"+text+"</span><br/>";
97 this.chatNode.scrollTop = this.chatNode.scrollHeight - this.chatNode.clientHeight;
103 this.joining.className='';
104 this.joined.className='hidden';
105 //this.username.focus();
106 this.username.setAttribute("autocomplete","OFF");
107 if (this.registeredAs) { this.join(this.registeredAs); }
108 this.inherited("startup",arguments);
111 _cleanInput: function(/* Event */e){
112 var key = (e.charCode == dojo.keys.SPACE ? dojo.keys.SPACE : e.keyCode);
113 if(key == dojo.keys.ENTER || key == 13){
114 this.chat(this.phrase.value);
115 this.phrase.value='';
119 _sendPhrase: function(/* Event */e){
120 if (this.phrase.value){
121 this.chat(this.phrase.value);
122 this.phrase.value='';