dojo.provide("demos.babelChat.src");
dojo.require("dojox.cometd");
dojo.require("dojox.rpc.Service");
dojo.require("dojox.io.scriptFrame");
dojo.require("dojox.widget.Dialog");
dojo.require("dojo.fx");
dojo.require("dijit._base.place");
dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("dojox.analytics.Urchin");
(function(d){
var user, good, comet = dojox.cometd,
chatroom = "/babelchat/messages";
d.declare("chat.Message", [dijit._Widget, dijit._Templated],{
messageLang:"",
message:"",
lang:"",
user:"",
templateString:
'
'+
'${user}:
' +
''+
'',
postCreate: function(){
this.inherited(arguments);
this._origMessage = this.message;
if(this.lang !== this.messageLang){
this.connect(this.domNode, "ondblclick", "_toggle");
// doing this because google API says not to bombard them:
setTimeout(d.hitch(this,"_translate"), 700);
}else{
this._localMessage = this.message;
}
},
_toggle: function(e){
this.messageNode.innerHTML = this._translated ? this._origMessage : this._localMessage;
this._translated = !this._translated;
},
_translate: function(){
var pair = this.messageLang + "|" + this.lang;
goog.translate({ q: this._origMessage, langpair: pair })
.addBoth(d.hitch(this,"_tcallback"));
},
_tcallback: function(response){
if(response && response.responseData){
this._localMessage = response.responseData.translatedText;
this.messageNode.innerHTML = this._localMessage;
this._translated = true;
}
}
});
d.declare("chat.User", null, {
constructor: function(args){
d.mixin(this, args);
this.subscriptions = [];
this.subscriptions.push(comet.subscribe(chatroom, this, "incomingMessage"));
},
setLang: function(lang){
this.lang = lang;
},
sendMessage: function(message){
comet.publish(chatroom, {
lang: this.lang,
message: message,
user: this.name
});
},
incomingMessage: function(message){
var data = message.data;
var msg = new chat.Message({
lang: this.lang,
messageLang: data.lang,
message: data.message.replace(/