1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-12-22 21:40:08 +00:00

Adding autocompleter

This commit is contained in:
Tim Düsterhus 2012-01-28 14:08:51 +01:00
parent 9a1c8072cd
commit 8f6b7c5e12

View File

@ -15,6 +15,9 @@ TimWolla.WCF ?= {}
titleTemplate: null titleTemplate: null
messageTemplate: null messageTemplate: null
newMessageCount: null newMessageCount: null
isActive: true
autocompleteOffset: 0
autocompleteValue: null
events: events:
newMessage: $.Callbacks() newMessage: $.Callbacks()
userMenu: $.Callbacks() userMenu: $.Callbacks()
@ -30,10 +33,20 @@ TimWolla.WCF ?= {}
console.log '[TimWolla.WCF.Chat] Finished initializing' console.log '[TimWolla.WCF.Chat] Finished initializing'
### ###
# Autocompletes a username
###
autocomplete: (firstChars, offset = @autocompleteOffset) ->
users = []
for user in $ '.chatUser'
username = $(user).data('username');
if username.indexOf(firstChars) is 0
users.push username
return if users.length is 0 then firstChars else users[offset % users.length]
###
# Binds all the events needed for Tims Chat. # Binds all the events needed for Tims Chat.
### ###
bindEvents: () -> bindEvents: () ->
@isActive = true
$(window).focus $.proxy () -> $(window).focus $.proxy () ->
document.title = @titleTemplate.fetch({ title: $('#chatRoomList .activeMenuItem a').text() }) document.title = @titleTemplate.fetch({ title: $('#chatRoomList .activeMenuItem a').text() })
@newMessageCount = 0 @newMessageCount = 0
@ -58,6 +71,23 @@ TimWolla.WCF ?= {}
@submit $ event.target @submit $ event.target
, @ , @
$('#chatInput').keydown $.proxy (event) ->
if event.keyCode is 9
event.preventDefault()
if @autocompleteValue is null
@autocompleteValue = $('#chatInput').val()
firstChars = @autocompleteValue.substring(@autocompleteValue.lastIndexOf(' ')+1)
console.log '[TimWolla.WCF.Chat] Autocompleting "' + firstChars + '"'
return if firstChars.length is 0
$('#chatInput').val(@autocompleteValue.substring(0, @autocompleteValue.lastIndexOf(' ')+1) + @autocomplete(firstChars) + ', ')
@autocompleteOffset++
else
@autocompleteOffset = 0
@autocompleteValue = null
, @
$('#chatClear').click (event) -> $('#chatClear').click (event) ->
event.preventDefault() event.preventDefault()
$('.chatMessage').remove() $('.chatMessage').remove()
@ -198,6 +228,7 @@ TimWolla.WCF ?= {}
li = $ '<li></li>' li = $ '<li></li>'
li.attr 'id', id li.attr 'id', id
li.addClass 'chatUser' li.addClass 'chatUser'
li.data 'username', user.username
a = $ '<a href="javascript:;">'+user.username+'</a>' a = $ '<a href="javascript:;">'+user.username+'</a>'
a.click $.proxy (event) -> a.click $.proxy (event) ->
event.preventDefault() event.preventDefault()