1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-10-31 14:10:08 +00:00

Fix autocomplete cycling in Google Chrome

This commit is contained in:
Tim Düsterhus 2014-08-02 21:40:38 +02:00
parent c4952705c3
commit 44a4353fcf

View File

@ -229,48 +229,51 @@ Autocomplete a username when TAB is pressed. The name to autocomplete is based o
The the word the caret is in will be passed to `autocomplete` and replaced if a match was found. The the word the caret is in will be passed to `autocomplete` and replaced if a match was found.
$('#timsChatInput').keydown (event) -> $('#timsChatInput').keydown (event) ->
if event.keyCode is $.ui.keyCode.TAB switch event.keyCode
do event.preventDefault when 229
input = $ @ return
when $.ui.keyCode.TAB
do event.preventDefault
input = $ @
autocomplete.value ?= do input.val autocomplete.value ?= do input.val
autocomplete.caret ?= do input.getCaret autocomplete.caret ?= do input.getCaret
beforeCaret = autocomplete.value.substring 0, autocomplete.caret beforeCaret = autocomplete.value.substring 0, autocomplete.caret
lastSpace = beforeCaret.lastIndexOf ' ' lastSpace = beforeCaret.lastIndexOf ' '
beforeComplete = autocomplete.value.substring 0, lastSpace + 1 beforeComplete = autocomplete.value.substring 0, lastSpace + 1
toComplete = autocomplete.value.substring lastSpace + 1 toComplete = autocomplete.value.substring lastSpace + 1
nextSpace = toComplete.indexOf ' ' nextSpace = toComplete.indexOf ' '
if nextSpace is -1 if nextSpace is -1
afterComplete = ''; afterComplete = '';
else else
afterComplete = toComplete.substring nextSpace + 1 afterComplete = toComplete.substring nextSpace + 1
toComplete = toComplete.substring 0, nextSpace toComplete = toComplete.substring 0, nextSpace
return if toComplete.length is 0 return if toComplete.length is 0
console.log "Autocompleting '#{toComplete}'" console.log "Autocompleting '#{toComplete}'"
if beforeComplete is '' and (toComplete.substring 0, 1) is '/' if beforeComplete is '' and (toComplete.substring 0, 1) is '/'
regex = new RegExp "^#{WCF.String.escapeRegExp toComplete.substring 1}", "i" regex = new RegExp "^#{WCF.String.escapeRegExp toComplete.substring 1}", "i"
commands = (command for command in v.config.installedCommands when regex.test command) commands = (command for command in v.config.installedCommands when regex.test command)
toComplete = '/' + commands[autocomplete.offset++ % commands.length] + ' ' if commands.length isnt 0 toComplete = '/' + commands[autocomplete.offset++ % commands.length] + ' ' if commands.length isnt 0
else else
regex = new RegExp "^#{WCF.String.escapeRegExp toComplete}", "i" regex = new RegExp "^#{WCF.String.escapeRegExp toComplete}", "i"
users = [ ] users = [ ]
for userID, user of userList.current for userID, user of userList.current
users.push user.username if regex.test user.username users.push user.username if regex.test user.username
toComplete = users[autocomplete.offset++ % users.length] + ', ' if users.length isnt 0 toComplete = users[autocomplete.offset++ % users.length] + ', ' if users.length isnt 0
input.val "#{beforeComplete}#{toComplete}#{afterComplete}" input.val "#{beforeComplete}#{toComplete}#{afterComplete}"
input.setCaret (beforeComplete + toComplete).length input.setCaret (beforeComplete + toComplete).length
Reset autocompleter to default status, when a key is pressed that is not TAB. Reset autocompleter to default status, when a key is pressed that is not TAB.
else else
do $('#timsChatInput').click do $('#timsChatInput').click
Reset autocompleter to default status, when the input is `click`ed, as the position of the caret may have changed. Reset autocompleter to default status, when the input is `click`ed, as the position of the caret may have changed.