Added support of chat identifier.

This commit is contained in:
Martin Edenhofer 2015-11-25 10:35:57 +01:00
parent 39b37e048b
commit 2a85b39493
3 changed files with 27 additions and 13 deletions

View file

@ -2,16 +2,16 @@ do($ = window.jQuery, window) ->
scripts = document.getElementsByTagName('script') scripts = document.getElementsByTagName('script')
myScript = scripts[scripts.length - 1] myScript = scripts[scripts.length - 1]
scriptHost = myScript.src.match(".*://([^:/]*).*")[1] scriptHost = myScript.src.match('.*://([^:/]*).*')[1]
# Define the plugin class # Define the plugin class
class ZammadChat class ZammadChat
defaults: defaults:
chat_id: undefined
show: true show: true
target: $('body') target: $('body')
host: '' host: ''
port: 6042
debug: false debug: false
flat: false flat: false
fontSize: undefined fontSize: undefined
@ -80,6 +80,11 @@ do($ = window.jQuery, window) ->
@log 'notice', 'Chat: Browser not supported!' @log 'notice', 'Chat: Browser not supported!'
return return
if !options.chat_id
@state = 'unsupported'
@log 'error', 'Chat: need chat id as option!'
return
@options = $.extend {}, @defaults, options @options = $.extend {}, @defaults, options
@el = $(@view('chat')( @el = $(@view('chat')(
title: @options.title title: @options.title
@ -105,7 +110,8 @@ do($ = window.jQuery, window) ->
event.preventDefault() event.preventDefault()
@sendMessage() @sendMessage()
send: (event, data) => send: (event, data = {}) =>
data.chat_id = @options.chat_id
@log 'debug', 'ws:send', event, data @log 'debug', 'ws:send', event, data
pipe = JSON.stringify pipe = JSON.stringify
event: event event: event
@ -118,6 +124,8 @@ do($ = window.jQuery, window) ->
for pipe in pipes for pipe in pipes
@log 'debug', 'ws:onmessage', pipe @log 'debug', 'ws:onmessage', pipe
switch pipe.event switch pipe.event
when 'chat_error'
@log 'error', pipe.data
when 'chat_session_message' when 'chat_session_message'
return if pipe.data.self_written return if pipe.data.self_written
@receiveMessage pipe.data @receiveMessage pipe.data
@ -149,7 +157,7 @@ do($ = window.jQuery, window) ->
@hide() @hide()
@wsClose() @wsClose()
when 'no_seats_available' when 'no_seats_available'
@onError 'Zammad Chat: Too many clients in queue. Clients in queue: ', pipe.data.queue @onError "Zammad Chat: Too many clients in queue. Clients in queue: #{pipe.data.queue}"
@state = 'off' @state = 'off'
@hide() @hide()
@wsClose() @wsClose()
@ -269,7 +277,7 @@ do($ = window.jQuery, window) ->
renderMessage: (data) => renderMessage: (data) =>
@lastAddedType = "message--#{ data.from }" @lastAddedType = "message--#{ data.from }"
unread = document.hidden ? " zammad-chat-message--unread" : "" unread = document.hidden ? ' zammad-chat-message--unread' : ''
@el.find('.zammad-chat-body').append @view('message')(data) @el.find('.zammad-chat-body').append @view('message')(data)
@scrollToBottom() @scrollToBottom()
@ -294,7 +302,7 @@ do($ = window.jQuery, window) ->
if !@sessionId if !@sessionId
@session_init() @session_init()
onOpenAnimationEnd: => onOpenAnimationEnd: ->
#@showTimeout() #@showTimeout()
close: (event) => close: (event) =>
@ -436,13 +444,16 @@ do($ = window.jQuery, window) ->
@send('chat_session_init') @send('chat_session_init')
detectHost: -> detectHost: ->
@options.host = "ws://#{ scriptHost }" protocol = 'ws://'
if window.location.protocol is 'https:'
protocol = 'wss://'
@options.host = "#{ protocol }#{ scriptHost }"
wsConnect: => wsConnect: =>
@detectHost() if !@options.host @detectHost() if !@options.host
@log 'notice', "Connecting to #{@options.host}:#{@options.port}" @log 'notice', "Connecting to #{@options.host}"
@ws = new window.WebSocket("#{@options.host}:#{@options.port}") @ws = new window.WebSocket("#{@options.host}")
@ws.onopen = @onWebSocketOpen @ws.onopen = @onWebSocketOpen
@ws.onmessage = @onWebSocketMessage @ws.onmessage = @onWebSocketMessage
@ -491,6 +502,7 @@ do($ = window.jQuery, window) ->
onSessionClosed: (data) -> onSessionClosed: (data) ->
@addStatus @T('Chat closed by %s', data.realname) @addStatus @T('Chat closed by %s', data.realname)
@disableInput() @disableInput()
@setAgentOnlineState 'offline'
disconnect: -> disconnect: ->
@showLoader() @showLoader()
@ -527,6 +539,8 @@ do($ = window.jQuery, window) ->
@el.find('.zammad-chat-agent-status').removeClass('zammad-chat-is-hidden') @el.find('.zammad-chat-agent-status').removeClass('zammad-chat-is-hidden')
@input.focus() @input.focus()
@setAgentOnlineState 'online'
showTimeout: -> showTimeout: ->
@el.find('.zammad-chat-body').html @view('timeout') @el.find('.zammad-chat-body').html @view('timeout')
agent: @agent.name agent: @agent.name

View file

@ -138,8 +138,8 @@
--> -->
<script> <script>
var chat = new ZammadChat({ var chat = new ZammadChat({
host: 'ws://localhost', chat_id: 1,
port: 6042, host: 'ws://localhost:6042',
debug: true debug: true
}); });

View file

@ -105,8 +105,8 @@
<script src="chat.js"></script> <script src="chat.js"></script>
<script> <script>
var chat = new ZammadChat({ var chat = new ZammadChat({
host: 'ws://localhost', chat_id: 1,
port: 6042, host: 'ws://localhost:6042',
debug: true, debug: true,
background: '#494d52', background: '#494d52',
flat: true, flat: true,