diff --git a/file/js/be.bastelstu.Chat.litcoffee b/file/js/be.bastelstu.Chat.litcoffee index d2c704f..a429fb6 100644 --- a/file/js/be.bastelstu.Chat.litcoffee +++ b/file/js/be.bastelstu.Chat.litcoffee @@ -33,7 +33,9 @@ exposed by a function if necessary. newMessageCount = 0 scrollUpNotifications = off chatSession = Date.now() + errorVisible = false + inputErrorHidingTimer = null lastMessage = null openChannel = 0 @@ -109,15 +111,15 @@ 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 @@ -126,7 +128,7 @@ and afterwards sent to the server by an AJAX request. text = "/whisper #{$("#timsChatMessageContainer#{openChannel}").data 'username'}, #{text}" # Free the fish! - freeTheFish() if text.toLowerCase() is '/free the fish' + do freeTheFish if text.toLowerCase() is '/free the fish' text = do (text) -> obj = @@ -145,16 +147,13 @@ and afterwards sent to the server by an AJAX request. enableSmilies: $('#timsChatSmilies').data 'status' showLoadingOverlay: false success: -> - $('#timsChatInputContainer').removeClass('formError').find('.innerError').hide() - getMessages() + do hideInputError + + do getMessages failure: (data) -> return true unless (data?.returnValues?.errorType?) or (data?.message?) - $('#timsChatInputContainer').addClass('formError').find('.innerError').show().html (data?.returnValues?.errorType) ? data.message - - setTimeout -> - $('#timsChatInputContainer').removeClass('formError').find('.innerError').hide() - , 5e3 + showInputError (data?.returnValues?.errorType) ? data.message false @@ -163,11 +162,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 ' ' @@ -183,7 +182,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) @@ -201,7 +200,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. @@ -214,14 +213,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() - $('.timsChatMessageContainer.active .timsChatMessage').remove() - $('.timsChatMessageContainer.active').scrollTop $('.timsChatMessageContainer.active').prop('scrollHeight') + do event.preventDefault + do $('.timsChatMessageContainer.active .timsChatMessage').remove + $('.timsChatMessageContainer.active').scrollTop $('.timsChatMessageContainer.active').prop 'scrollHeight' Handle toggling of the toggleable buttons. @@ -236,7 +235,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. @@ -282,7 +281,7 @@ Scroll down when autoscroll is being activated. $('#timsChatAutoscroll').click (event) -> if $('#timsChatAutoscroll').data 'status' - $('.timsChatMessageContainer.active').scrollTop $('.timsChatMessageContainer.active').prop('scrollHeight') + $('.timsChatMessageContainer.active').scrollTop $('.timsChatMessageContainer.active').prop 'scrollHeight' $('.timsChatMessageContainer.active').on 'scroll', (event) -> element = $ @ @@ -293,13 +292,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. @@ -332,11 +331,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 @@ -345,18 +344,37 @@ 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" true +Shows an error message below the input. + + showInputError = (message) -> + $('#timsChatInputContainer').addClass('formError').find('.innerError').show().html message + + clearTimeout inputErrorHidingTimer if inputErrorHidingTimer? + inputErrorHidingTimer = setTimeout -> + do hideInputError + , 5e3 + +Hides the error message below the input. + + hideInputError = -> + clearTimeout inputErrorHidingTimer if inputErrorHidingTimer? + inputErrorHidingTimer = null + + do $('#timsChatInputContainer').removeClass('formError').find('.innerError').hide + Free the fish. freeTheFish = -> return if $.wcfIsset 'fish' console.warn 'Freeing the fish' fish = $ """