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