From e44a7e37b6c7b5961adaffc62b9042b8d442938e Mon Sep 17 00:00:00 2001 From: mensonge Date: Thu, 13 Nov 2008 09:49:11 +0000 Subject: New feature: basic Ajax suggestion for tags and implementation of Dojo toolkit git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f --- includes/js/dijit/demos/chat/room.js | 127 +++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 includes/js/dijit/demos/chat/room.js (limited to 'includes/js/dijit/demos/chat/room.js') diff --git a/includes/js/dijit/demos/chat/room.js b/includes/js/dijit/demos/chat/room.js new file mode 100644 index 0000000..b1847f4 --- /dev/null +++ b/includes/js/dijit/demos/chat/room.js @@ -0,0 +1,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: '
' + +'
' + +'
' + +'
' + +'${prompt} ' + +'
' + +'' + +'
' + +'
', + + 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=''; + } + } +}); + +} -- cgit v1.2.3