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

Clean up coffeescript a bit

This commit is contained in:
Tim Düsterhus 2013-07-09 21:48:40 +02:00
parent 9c27cd5429
commit abcae3e129

View File

@ -110,21 +110,21 @@ Make the user leave the chat when **Tims Chat** is about to be unloaded.
Insert the appropriate smiley code into the input when a smiley is clicked.
$('#smilies').on 'click', 'img', ->
insertText ' ' + $(@).attr('alt') + ' '
insertText " #{$(@).attr('alt')} "
Handle submitting the form. The message will be validated by some basic checks, passed to the `submit` eventlisteners
and afterwards sent to the server by an AJAX request.
$('#timsChatForm').submit (event) ->
event.preventDefault()
do event.preventDefault
text = $('#timsChatInput').val().trim()
text = do $('#timsChatInput').val().trim
$('#timsChatInput').val('').focus().keyup()
return false if text.length is 0
# Free the fish!
freeTheFish() if text.toLowerCase() is '/free the fish'
do freeTheFish if text.toLowerCase() is '/free the fish'
text = do (text) ->
obj =
@ -143,9 +143,9 @@ and afterwards sent to the server by an AJAX request.
enableSmilies: $('#timsChatSmilies').data 'status'
showLoadingOverlay: false
success: ->
hideInputError()
do hideInputError
getMessages()
do getMessages
failure: (data) ->
return true unless (data?.returnValues?.errorType?) or (data?.message?)
@ -158,11 +158,11 @@ The the word the caret is in will be passed to `autocomplete` and replaced if a
$('#timsChatInput').keydown (event) ->
if event.keyCode is $.ui.keyCode.TAB
input = $(event.currentTarget)
event.preventDefault()
do event.preventDefault
input = $ @
autocomplete.value ?= input.val()
autocomplete.caret ?= input.getCaret()
autocomplete.value ?= do input.val
autocomplete.caret ?= do input.getCaret
beforeCaret = autocomplete.value.substring 0, autocomplete.caret
lastSpace = beforeCaret.lastIndexOf ' '
@ -178,7 +178,7 @@ The the word the caret is in will be passed to `autocomplete` and replaced if a
return if toComplete.length is 0
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"
# TODO: Proper command list
commands = (command for command in v.config.installedCommands when regex.test command)
@ -196,7 +196,7 @@ The the word the caret is in will be passed to `autocomplete` and replaced if a
Reset autocompleter to default status, when a key is pressed that is not TAB.
else
$('#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.
@ -209,14 +209,14 @@ Reset autocompleter to default status, when the input is `click`ed, as the posit
Refresh the room list when the associated button is `click`ed.
$('#timsChatRoomList button').click ->
refreshRoomList()
do refreshRoomList
Clear the chat by removing every single message once the clear button is `clicked`.
$('#timsChatClear').click (event) ->
event.preventDefault()
$('.timsChatMessage').remove()
$('#timsChatMessageContainer').scrollTop $('#timsChatMessageContainer').prop('scrollHeight')
do event.preventDefault
do $('.timsChatMessage').remove
$('#timsChatMessageContainer').scrollTop $('#timsChatMessageContainer').prop 'scrollHeight'
Handle toggling of the toggleable buttons.
@ -231,7 +231,7 @@ Handle toggling of the toggleable buttons.
element.addClass 'active'
element.attr 'title', element.data 'disableMessage'
$('#timsChatInput').focus()
do $('#timsChatInput').focus
Mark smilies as disabled when they are disabled.
@ -277,7 +277,7 @@ Scroll down when autoscroll is being activated.
$('#timsChatAutoscroll').click (event) ->
if $('#timsChatAutoscroll').data 'status'
$('#timsChatMessageContainer').scrollTop $('#timsChatMessageContainer').prop('scrollHeight')
$('#timsChatMessageContainer').scrollTop $('#timsChatMessageContainer').prop 'scrollHeight'
$('#timsChatMessageContainer').on 'scroll', (event) ->
element = $ @
@ -288,13 +288,13 @@ Scroll down when autoscroll is being activated.
if scrollTop < scrollHeight - height - 25
if $('#timsChatAutoscroll').data('status') is 1
scrollUpNotifications = on
$('#timsChatAutoscroll').click()
do $('#timsChatAutoscroll').click
if scrollTop > scrollHeight - height - 10
if $('#timsChatAutoscroll').data('status') is 0
scrollUpNotifications = off
$(@).removeClass 'notification'
$('#timsChatAutoscroll').click()
do $('#timsChatAutoscroll').click
Enable duplicate tab detection.
@ -327,11 +327,11 @@ load messages if the appropriate event arrives.
do ->
be.bastelstu.wcf.nodePush.onConnect ->
console.log 'Disabling periodic loading'
pe.getMessages.stop()
do pe.getMessages.stop
be.bastelstu.wcf.nodePush.onDisconnect ->
console.log 'Enabling periodic loading'
getMessages()
do getMessages
pe.getMessages = new WCF.PeriodicalExecuter getMessages, v.config.reloadTime * 1e3
be.bastelstu.wcf.nodePush.onMessage 'be.bastelstu.chat.newMessage', getMessages
@ -340,7 +340,7 @@ load messages if the appropriate event arrives.
Finished! Enable the input now and join the chat.
join roomID
$('#timsChatInput').enable().jCounter().focus();
do $('#timsChatInput').enable().jCounter().focus
console.log "Finished initializing"
@ -353,7 +353,7 @@ Shows an error message below the input.
clearTimeout inputErrorHidingTimer if inputErrorHidingTimer?
inputErrorHidingTimer = setTimeout ->
hideInputError()
do hideInputError
, 5e3
Hides the error message below the input.
@ -362,7 +362,7 @@ Hides the error message below the input.
clearTimeout inputErrorHidingTimer if inputErrorHidingTimer?
inputErrorHidingTimer = null
$('#timsChatInputContainer').removeClass('formError').find('.innerError').hide()
do $('#timsChatInputContainer').removeClass('formError').find('.innerError').hide
Free the fish.
@ -370,6 +370,7 @@ Free the fish.
return if $.wcfIsset 'fish'
console.warn 'Freeing the fish'
fish = $ """<div id="fish">#{WCF.String.escapeHTML('><((((\u00B0>')}</div>"""
fish.css
position: 'absolute'
top: '150px'
@ -413,7 +414,7 @@ Fetch new messages from the server and pass them to `handleMessages`. The userli
error: ->
console.error "Message loading failed, #{--remainingFailures} remaining"
if remainingFailures <= 0
freeTheFish()
do freeTheFish
console.error 'To many failures, aborting'
showError WCF.Language.get 'chat.error.onMessageLoad'
@ -531,7 +532,7 @@ Remove all users that left the chat.
$('.timsChatUser').each ->
unless foundUsers[$(@).attr('id')]?
console.log "Removing User: '#{$(@).data('username')}'"
$(@).remove();
do $(@).remove
$('#toggleUsers .badge').text $('.timsChatUser').length
@ -547,12 +548,12 @@ the existing text. If `options.submit` is true the message will be sent to the s
text = $('#timsChatInput').val() + text if options.append
$('#timsChatInput').val text
$('#timsChatInput').keyup()
do $('#timsChatInput').keyup
if (options.submit)
$('#timsChatForm').submit()
if options.submit
do $('#timsChatForm').submit
else
$('#timsChatInput').focus()
do $('#timsChatInput').focus
Send out notifications for the given `message`. The number of unread messages will be prepended to `document.title` and if available desktop notifications will be sent.
@ -574,9 +575,9 @@ Send out notifications for the given `message`. The number of unread messages wi
notification = new window.Notification title,
body: content
onclick: ->
notification.close()
do notification.close
setTimeout ->
notification.close()
do notification.close
, 5e3
Fetch the roomlist from the server and update it in the GUI.
@ -592,7 +593,7 @@ Fetch the roomlist from the server and update it in the GUI.
showLoadingOverlay: false
suppressErrors: true
success: (data) ->
$('.timsChatRoom').remove()
do $('.timsChatRoom').remove
$('#toggleRooms .badge').text data.returnValues.length
for room in data.returnValues
@ -603,8 +604,8 @@ Fetch the roomlist from the server and update it in the GUI.
if window.history?.replaceState?
$('.timsChatRoom').click (event) ->
event.preventDefault()
target = $(@)
do event.preventDefault
target = $ @
window.history.replaceState {}, '', target.attr 'href'
@ -622,8 +623,8 @@ Shows an unrecoverable error with the given text.
loading = true
pe.refreshRoomList.stop()
pe.getMessages.stop()
do pe.refreshRoomList.stop
do pe.getMessages.stop
errorDialog = $("""
<div id="timsChatLoadingErrorDialog">
@ -634,7 +635,7 @@ Shows an unrecoverable error with the given text.
formSubmit = $("""<div class="formSubmit"></div>""").appendTo errorDialog
reloadButton = $("""<button class="buttonPrimary">#{WCF.Language.get 'chat.error.reload'}</button>""").appendTo formSubmit
reloadButton.on 'click', ->
window.location.reload()
do window.location.reload
$('#timsChatLoadingErrorDialog').wcfDialog
closable: false
@ -666,8 +667,8 @@ Joins a room.
document.title = v.titleTemplate.fetch data.returnValues
handleMessages data.returnValues.messages
getMessages()
refreshRoomList()
do getMessages
do refreshRoomList
failure: ->
showError WCF.Language.get 'chat.error.join'
@ -675,16 +676,18 @@ Bind the given callback to the given event.
addListener = (event, callback) ->
return false unless events[event]?
events[event].add callback
true
Remove the given callback from the given event.
removeListener = (event, callback) ->
return false unless events[event]?
events[event].remove callback
true
And finally export the public methods and variables.
Chat =