Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 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");
4
 
5
dojo.require("dojox.cometd");
6
dojo.require("dijit._Widget");
7
dojo.require("dijit._Templated");
8
 
9
dojo.declare("dijit.demos.chat.Room",
10
	[dijit._Widget,dijit._Templated],
11
	{
12
 
13
	_last: "",
14
	_username: null,
15
	roomId: "public",
16
	isPrivate: false,
17
	prompt: "Name:",
18
 
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"/>'
24
					+'</div>'
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"/>'
28
					+'</div>'
29
				+'</div>'
30
			+'</div>',
31
 
32
	join: function(name){
33
		if(name == null || name.length==0){
34
			alert('Please enter a username!');
35
		}else{
36
			if(this.isPrivate){ this.roomId = name; }
37
			this._username=name;
38
			this.joining.className='hidden';
39
			this.joined.className='';
40
			this.phrase.focus();
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 });
45
		}
46
	},
47
 
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);
52
		}
53
	},
54
 
55
	leave: function(){
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."});
58
 
59
		// switch the input form back to login mode
60
		this.joining.className='';
61
		this.joined.className='hidden';
62
		this.username.focus();
63
		this._username=null;
64
	},
65
 
66
	chat: function(text){
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,'&lt;');
71
			text=text.replace(/>/g,'&gt;');
72
			dojox.cometd.publish("/chat/demo/" + this.roomId, { user: this._username, chat: text});
73
		}
74
	},
75
 
76
	_chat: function(message){
77
		// summary: process an incoming message
78
		if (!message.data){
79
			console.warn("bad message format "+message);
80
			return;
81
		}
82
		var from=message.data.user;
83
		var special=message.data.join || message.data.leave;
84
		var text=message.data.chat;
85
		if(text!=null){
86
			if(!special && from == this._last ){ from="...";
87
			}else{
88
				this._last=from;
89
				from+=":";
90
			}
91
 
92
			if(special){
93
				this.chatNode.innerHTML += "<span class=\"alert\"><span class=\"from\">"+from+"&nbsp;</span><span class=\"text\">"+text+"</span></span><br/>";
94
				this._last="";
95
			}else{
96
				this.chatNode.innerHTML += "<span class=\"from\">"+from+"&nbsp;</span><span class=\"text\">"+text+"</span><br/>";
97
				this.chatNode.scrollTop = this.chatNode.scrollHeight - this.chatNode.clientHeight;
98
			}
99
		}
100
	},
101
 
102
	startup: function(){
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);
109
	},
110
 
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='';
116
		}
117
	},
118
 
119
	_sendPhrase: function(/* Event */e){
120
		if (this.phrase.value){
121
			this.chat(this.phrase.value);
122
			this.phrase.value='';
123
		}
124
	}
125
});
126
 
127
}