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 : '
',
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, '>');
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 += ""
+ from
+ " "
+ text + "
";
this._last = "";
} else {
this.chatNode.innerHTML += "" + from
+ " " + text
+ "
";
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 = '';
}
}
});
}