Tims Chat 3 =========== This is the main javascript file for [**Tims Chat**](https://github.com/wbbaddons/Tims-Chat). It handles everything that happens in the GUI of **Tims Chat**. ### Copyright Information # @author Tim Düsterhus # @copyright 2010-2014 Tim Düsterhus # @license Creative Commons Attribution-NonCommercial-ShareAlike # @package be.bastelstu.chat ### ## Code We start by setting up our environment by ensuring some sane values for both `$` and `window`, enabling EMCAScript 5 strict mode and overwriting console to prepend the name of the class. (($, window) -> "use strict"; console = log: (message) -> window.console.log "[be.bastelstu.Chat] #{message}" unless production? warn: (message) -> window.console.warn "[be.bastelstu.Chat] #{message}" unless production? error: (message) -> window.console.error "[be.bastelstu.Chat] #{message}" unless production? Continue with defining the needed variables. All variables are local to our closure and will be exposed by a function if necessary. isActive = true newMessageCount = 0 scrollUpNotifications = off chatSession = Date.now() userList = current: {} allTime: {} roomList = active: {} available: {} hiddenTopics = {} fileUploaded = no errorVisible = false inputErrorHidingTimer = null lastMessage = null openChannel = 0 messageContainerSize = 0 userListSize = 0 remainingFailures = 3 overlaySmileyList = null markedMessages = {} events = newMessage: $.Callbacks() userMenu: $.Callbacks() submit: $.Callbacks() pe = getMessages: null refreshRoomList: null fish: null loading = false autocomplete = offset: 0 value: null caret: 0 v = titleTemplate: null messageTemplate: null userTemplate: null config: null Initialize **Tims Chat**. Bind needed DOM events and initialize data structures. initialized = false init = (roomID, config, titleTemplate, messageTemplate, userTemplate, userMenuTemplate) -> return false if initialized initialized = true userListSize = $('#timsChatUserList').height() v.config = config v.titleTemplate = titleTemplate v.messageTemplate = messageTemplate v.userTemplate = userTemplate v.userMenuTemplate = userMenuTemplate console.log 'Initializing' When **Tims Chat** becomes focused mark the chat as active and remove the number of new messages from the title. $(window).focus -> document.title = v.titleTemplate.fetch(roomList.active) if roomList.active?.title? and roomList.active.topic.trim() isnt '' newMessageCount = 0 isActive = true When **Tims Chat** loses the focus mark the chat as inactive. $(window).blur -> isActive = false Make the user leave the chat when **Tims Chat** is about to be unloaded. $(window).on 'beforeunload', -> return undefined if errorVisible new WCF.Action.Proxy autoSend: true data: actionName: 'leave' className: 'chat\\data\\room\\RoomAction' showLoadingOverlay: false async: false suppressErrors: true undefined $(window).resize -> if $('html').hasClass 'fullscreen' do -> verticalContentPadding = $('#content').innerHeight() - $('#content').height() verticalSizeOfContentElements = do -> height = 0 $('#content > *:visible').each (k, v) -> height += $(v).outerHeight() height return if verticalSizeOfContentElements is 0 freeSpace = $('body').height() - verticalContentPadding - verticalSizeOfContentElements $('.timsChatMessageContainer').height $('.timsChatMessageContainer').height() + freeSpace do -> verticalSidebarPadding = $('.sidebar').innerHeight() - $('.sidebar').height() verticalUserListContainerPadding = $('#timsChatUserListContainer').innerHeight() - $('#timsChatUserListContainer').height() sidebarHeight = $('.sidebar > div').height() freeSpace = $('body').height() - verticalSidebarPadding - verticalUserListContainerPadding - sidebarHeight $('#timsChatUserList').height $('#timsChatUserList').height() + freeSpace if $('#timsChatAutoscroll').data 'status' $('.timsChatMessageContainer.active').scrollTop $('.timsChatMessageContainer.active').prop 'scrollHeight' $('.mobileSidebarToggleButton').on 'click', -> do $(window).resize Insert the appropriate smiley code into the input when a smiley is clicked. $('#smilies').on 'click', 'img', -> insertText " #{$(@).attr('alt')} " Copy the first loaded category of smilies so it won't get detached by wcfDialog overlaySmileyList = $('