+(function ($) {
var pvuis
var audio = new Audio('/sounds/bell.mp3')
function addULI (uname, cxids) {
$('
')
.data('name', uname)
.data('cxids', cxids)
.css('cursor', 'pointer')
.click(function () { setPrivate(this); })
.appendTo('#userlist')
}
function getUsers () {
$('#userlist').empty()
$('#to').empty()
$.get('/api/chat/users').done(
function (users) {
$.each(users, function () {
var user = this
var existent = $('#userlist li').filterByData('name', user.UserName)
if (existent.length > 0) existent.remove()
var cxids = []
$.each(user.Connections, function () {
cxids.push(this.ConnectionId)
})
addULI(user.UserName, cxids)
})
}
)
}
function onCx () {
setTimeout(function () {
getUsers()
}, 120),
$('#chatview').removeClass('disabled')
$('#sendmessage').prop('disabled',false);
$('#sendpv').prop('disabled',false);
}
function onDisCx () {
$('#chatview').addClass('disabled');
$('#sendmessage').prop('disabled',true);
$('#sendpv').prop('disabled',true);
}
// This optional function html-encodes messages for display in the page.
function htmlEncode (value) {
var encodedValue = $('').text(value).html()
return encodedValue
}
var setPrivate = function (li) {
$('#sendmessagebox').addClass('hidden')
$('#sendpvbox').removeClass('hidden')
pvuis = { CXs: $(li).data('cxids'), UserName: $(li).data('name') }
$('#sendpvdest').html(pvuis.UserName)
$('#pv').focus()
}
var setPublic = function () {
$('#sendmessagebox').removeClass('hidden')
$('#sendpvbox').addClass('hidden')
$('#message').focus()
}
$('#pubChan').css('cursor', 'pointer')
$('#pubChan').click(setPublic)
setPublic()
// Reference the auto-generated proxy for the hub.
var chat = $.connection.chatHub
// Create a function that the hub can call back to display messages.
chat.client.addMessage = function (name, message) {
// Add the message to the page.
$('#discussion').append('
')
}
chat.client.addPV = function (name, message) {
if (!$('#mute').prop('checked')) {
audio.play()
}
// Add the pv to the page.
$('#discussion').append('
')
}
}
}
var sendMessage = function () {
chat.server.send($('#displayname').val(), $('#message').val())
// Clear text box and reset focus for next comment.
$('#message').val('')
}
var sendPV = function () {
var msg = $('#pv').val()
// Call the Send method on the hub.
$.each(pvuis.CXs, function () {
chat.server.sendPV(this, msg)
})
$('#discussion').append('