2006-09-25 10:49:26 -04:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
*
|
2007-08-11 17:29:21 -04:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2006-09-25 10:49:26 -04:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2005-12-12 12:53:59 -05:00
|
|
|
|
2006-01-27 09:05:02 -05:00
|
|
|
var room =
|
|
|
|
{
|
2006-01-31 04:12:44 -05:00
|
|
|
_last: "",
|
|
|
|
_username: null,
|
2006-05-09 13:41:47 -04:00
|
|
|
_chatTopic: "topic://CHAT.DEMO",
|
2006-01-27 09:05:02 -05:00
|
|
|
|
|
|
|
join: function()
|
|
|
|
{
|
|
|
|
var name = $('username').value;
|
|
|
|
if (name == null || name.length==0 )
|
|
|
|
{
|
2006-01-31 04:12:44 -05:00
|
|
|
alert('Please enter a username!');
|
2006-01-27 09:05:02 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-31 04:12:44 -05:00
|
|
|
this._username=name;
|
2006-01-27 09:05:02 -05:00
|
|
|
|
2006-05-09 13:41:47 -04:00
|
|
|
amq.addListener('chat',this._chatTopic,room._chat);
|
2006-01-27 09:05:02 -05:00
|
|
|
$('join').className='hidden';
|
|
|
|
$('joined').className='';
|
|
|
|
$('phrase').focus();
|
|
|
|
Behaviour.apply();
|
2006-05-09 13:41:47 -04:00
|
|
|
amq.sendMessage(this._chatTopic, "<message type='join' from='" + room._username + "'/>");
|
2006-01-27 09:05:02 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
leave: function()
|
|
|
|
{
|
2006-05-09 13:41:47 -04:00
|
|
|
amq.removeListener('chat',this._chatTopic);
|
2006-01-27 09:05:02 -05:00
|
|
|
// switch the input form
|
|
|
|
$('join').className='';
|
|
|
|
$('joined').className='hidden';
|
|
|
|
$('username').focus();
|
2006-01-31 04:12:44 -05:00
|
|
|
Behaviour.apply();
|
2006-05-09 13:41:47 -04:00
|
|
|
amq.sendMessage(this._chatTopic, "<message type='leave' from='" + room._username + "'/>");
|
2006-01-31 04:32:54 -05:00
|
|
|
room._username=null;
|
2006-01-27 09:05:02 -05:00
|
|
|
},
|
|
|
|
|
2006-05-09 13:41:47 -04:00
|
|
|
chat: function(text)
|
2006-01-27 09:05:02 -05:00
|
|
|
{
|
|
|
|
if (text != null && text.length>0 )
|
|
|
|
{
|
|
|
|
// TODO more encoding?
|
|
|
|
text=text.replace('<','<');
|
|
|
|
text=text.replace('>','>');
|
2006-05-09 13:41:47 -04:00
|
|
|
amq.sendMessage(this._chatTopic, "<message type='chat' from='" + room._username + "'>" + text + "</message>");
|
2006-01-27 09:05:02 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2006-01-30 08:18:18 -05:00
|
|
|
_chat: function(message)
|
2006-01-27 09:05:02 -05:00
|
|
|
{
|
|
|
|
var chat=$('chat');
|
2006-01-31 04:12:44 -05:00
|
|
|
var type=message.getAttribute('type');
|
|
|
|
var from=message.getAttribute('from');
|
2006-01-27 09:05:02 -05:00
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case 'chat' :
|
|
|
|
{
|
|
|
|
var text=message.childNodes[0].data;
|
|
|
|
|
2006-01-31 04:12:44 -05:00
|
|
|
if ( from == room._last )
|
2006-01-27 09:05:02 -05:00
|
|
|
from="...";
|
|
|
|
else
|
|
|
|
{
|
2006-01-31 04:12:44 -05:00
|
|
|
room._last=from;
|
2006-01-27 09:05:02 -05:00
|
|
|
from+=":";
|
|
|
|
}
|
|
|
|
|
|
|
|
chat.innerHTML += "<span class=\"from\">"+from+" </span><span class=\"text\">"+text+"</span><br/>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'ping' :
|
|
|
|
{
|
|
|
|
$('members').innerHTML+="<span class=\"member\">"+from+"</span><br/>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'join' :
|
|
|
|
{
|
|
|
|
$('members').innerHTML="";
|
2006-01-31 04:12:44 -05:00
|
|
|
if (room._username!=null)
|
2006-05-09 13:41:47 -04:00
|
|
|
amq.sendMessage(this._chatTopic, "<message type='ping' from='" + room._username + "'/>");
|
2006-01-27 09:05:02 -05:00
|
|
|
chat.innerHTML += "<span class=\"alert\"><span class=\"from\">"+from+" </span><span class=\"text\">has joined the room!</span></span><br/>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'leave':
|
|
|
|
{
|
|
|
|
$('members').innerHTML="";
|
2006-01-31 04:12:44 -05:00
|
|
|
if (room._username!=null)
|
2006-05-09 13:41:47 -04:00
|
|
|
amq.sendMessage(this._chatTopic, "<message type='ping' from='" + room._username + "'/>");
|
2006-01-27 09:05:02 -05:00
|
|
|
chat.innerHTML += "<span class=\"alert\"><span class=\"from\">"+from+" </span><span class=\"text\">has left the room!</span></span><br/>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chat.scrollTop = chat.scrollHeight - chat.clientHeight;
|
|
|
|
|
2006-01-31 04:12:44 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
_poll: function(first)
|
|
|
|
{
|
|
|
|
if (first || $('join').className=='hidden' && $('joined').className=='hidden')
|
|
|
|
{
|
2006-01-27 09:05:02 -05:00
|
|
|
$('join').className='';
|
|
|
|
$('joined').className='hidden';
|
|
|
|
$('username').focus();
|
|
|
|
Behaviour.apply();
|
2006-01-31 04:12:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2006-01-27 09:05:02 -05:00
|
|
|
|
2006-01-31 04:12:44 -05:00
|
|
|
amq.addPollHandler(room._poll);
|
2006-01-27 09:05:02 -05:00
|
|
|
|
2006-01-31 04:12:44 -05:00
|
|
|
var chatBehaviours =
|
2006-01-27 09:05:02 -05:00
|
|
|
{
|
|
|
|
'#username' : function(element)
|
|
|
|
{
|
|
|
|
element.setAttribute("autocomplete","OFF");
|
2006-01-31 04:12:44 -05:00
|
|
|
element.onkeyup = function(ev)
|
|
|
|
{
|
2006-03-08 04:17:14 -05:00
|
|
|
var keyc=getKeyCode(ev);
|
2006-01-31 04:12:44 -05:00
|
|
|
if (keyc==13 || keyc==10)
|
2006-01-27 09:05:02 -05:00
|
|
|
{
|
|
|
|
room.join();
|
2006-01-31 04:12:44 -05:00
|
|
|
return false;
|
2006-01-27 09:05:02 -05:00
|
|
|
}
|
2006-01-31 04:12:44 -05:00
|
|
|
return true;
|
2006-01-27 09:05:02 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'#joinB' : function(element)
|
|
|
|
{
|
2006-01-31 04:12:44 -05:00
|
|
|
element.onclick = function(event)
|
2006-01-27 09:05:02 -05:00
|
|
|
{
|
|
|
|
room.join();
|
2006-01-31 04:12:44 -05:00
|
|
|
return true;
|
2006-01-27 09:05:02 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'#phrase' : function(element)
|
|
|
|
{
|
|
|
|
element.setAttribute("autocomplete","OFF");
|
2006-01-31 04:12:44 -05:00
|
|
|
element.onkeyup = function(ev)
|
|
|
|
{
|
2006-03-08 04:17:14 -05:00
|
|
|
var keyc=getKeyCode(ev);
|
2006-01-31 04:12:44 -05:00
|
|
|
|
|
|
|
if (keyc==13 || keyc==10)
|
2006-01-27 09:05:02 -05:00
|
|
|
{
|
2006-05-09 13:41:47 -04:00
|
|
|
var text = $F('phrase');
|
|
|
|
$('phrase').value='';
|
|
|
|
room.chat(text);
|
2006-01-27 09:05:02 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
'#sendB' : function(element)
|
|
|
|
{
|
2006-01-31 04:12:44 -05:00
|
|
|
element.onclick = function(event)
|
2006-01-27 09:05:02 -05:00
|
|
|
{
|
2006-05-09 13:41:47 -04:00
|
|
|
var text = $F('phrase');
|
|
|
|
$('phrase').value='';
|
|
|
|
room.chat(text);
|
2006-01-27 09:05:02 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
'#leaveB' : function(element)
|
|
|
|
{
|
|
|
|
element.onclick = function()
|
|
|
|
{
|
|
|
|
room.leave();
|
2006-01-31 04:12:44 -05:00
|
|
|
return false;
|
2006-01-27 09:05:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-01-31 04:12:44 -05:00
|
|
|
Behaviour.register(chatBehaviours);
|
2006-01-27 09:05:02 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|