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