chat client: detect host, init chat in index.html

This commit is contained in:
Felix Niklas 2015-11-13 13:59:15 +01:00
parent ecb1f94c02
commit 6c0e4ba9f3
4 changed files with 37 additions and 17 deletions

View file

@ -1,14 +1,19 @@
do($ = window.jQuery, window) -> do($ = window.jQuery, window) ->
scripts = document.getElementsByTagName('script')
myScript = scripts[scripts.length - 1]
scriptHost = myScript.src.match(".*://([^:/]*).*")[1]
# Define the plugin class # Define the plugin class
class ZammadChat class ZammadChat
defaults: defaults:
invitationPhrase: '<strong>Chat</strong> with us!' invitationPhrase: '<strong>Chat</strong> with us!'
agentPhrase: ' is helping you' agentPhrase: ' is helping you'
show: true show: false
target: $('body') target: $('body')
host: 'ws://localhost:6042' host: ''
port: 6042
_messageCount: 0 _messageCount: 0
isOpen: false isOpen: false
@ -61,7 +66,7 @@ do($ = window.jQuery, window) ->
options.T = @T options.T = @T
return window.zammadChatTemplates[name](options) return window.zammadChatTemplates[name](options)
constructor: (el, options) -> constructor: (options) ->
@options = $.extend {}, @defaults, options @options = $.extend {}, @defaults, options
@el = $(@view('chat')(@options)) @el = $(@view('chat')(@options))
@options.target.append @el @options.target.append @el
@ -395,9 +400,14 @@ do($ = window.jQuery, window) ->
session_init: -> session_init: ->
@send('chat_session_init') @send('chat_session_init')
detectHost: ->
@options.host = "ws://#{ scriptHost }"
wsConnect: => wsConnect: =>
@log 'notice', "Connecting to #{@options.host}" @detectHost() if !@options.host
@ws = new window.WebSocket(@options.host)
@log 'notice', "Connecting to #{@options.host}:#{@options.port}"
@ws = new window.WebSocket("#{@options.host}:#{@options.port}")
@ws.onopen = @onWebSocketOpen @ws.onopen = @onWebSocketOpen
@ws.onmessage = @onWebSocketMessage @ws.onmessage = @onWebSocketMessage
@ -499,5 +509,4 @@ do($ = window.jQuery, window) ->
.toggleClass('zammad-chat-is-online', state) .toggleClass('zammad-chat-is-online', state)
.text if state then @T('Online') else @T('Offline') .text if state then @T('Online') else @T('Offline')
$(document).ready -> window.ZammadChat = ZammadChat
window.zammadChat = new ZammadChat()

View file

@ -2,14 +2,18 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
slice = [].slice; slice = [].slice;
(function($, window) { (function($, window) {
var ZammadChat; var ZammadChat, myScript, scriptHost, scripts;
scripts = document.getElementsByTagName('script');
myScript = scripts[scripts.length - 1];
scriptHost = myScript.src.match(".*://([^:/]*).*")[1];
ZammadChat = (function() { ZammadChat = (function() {
ZammadChat.prototype.defaults = { ZammadChat.prototype.defaults = {
invitationPhrase: '<strong>Chat</strong> with us!', invitationPhrase: '<strong>Chat</strong> with us!',
agentPhrase: ' is helping you', agentPhrase: ' is helping you',
show: true, show: false,
target: $('body'), target: $('body'),
host: 'ws://localhost:6042' host: '',
port: 6042
}; };
ZammadChat.prototype._messageCount = 0; ZammadChat.prototype._messageCount = 0;
@ -93,7 +97,7 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
})(this); })(this);
}; };
function ZammadChat(el, options) { function ZammadChat(options) {
this.setAgentOnlineState = bind(this.setAgentOnlineState, this); this.setAgentOnlineState = bind(this.setAgentOnlineState, this);
this.onConnectionEstablished = bind(this.onConnectionEstablished, this); this.onConnectionEstablished = bind(this.onConnectionEstablished, this);
this.setSessionId = bind(this.setSessionId, this); this.setSessionId = bind(this.setSessionId, this);
@ -498,9 +502,16 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
return this.send('chat_session_init'); return this.send('chat_session_init');
}; };
ZammadChat.prototype.detectHost = function() {
return this.options.host = "ws://" + scriptHost;
};
ZammadChat.prototype.wsConnect = function() { ZammadChat.prototype.wsConnect = function() {
this.log('notice', "Connecting to " + this.options.host); if (!this.options.host) {
this.ws = new window.WebSocket(this.options.host); this.detectHost();
}
this.log('notice', "Connecting to " + this.options.host + ":" + this.options.port);
this.ws = new window.WebSocket(this.options.host + ":" + this.options.port);
this.ws.onopen = this.onWebSocketOpen; this.ws.onopen = this.onWebSocketOpen;
this.ws.onmessage = this.onWebSocketMessage; this.ws.onmessage = this.onWebSocketMessage;
this.ws.onclose = (function(_this) { this.ws.onclose = (function(_this) {
@ -618,9 +629,7 @@ var bind = function(fn, me){ return function(){ return fn.apply(me, arguments);
return ZammadChat; return ZammadChat;
})(); })();
return $(document).ready(function() { return window.ZammadChat = ZammadChat;
return window.zammadChat = new ZammadChat();
});
})(window.jQuery, window); })(window.jQuery, window);
if (!window.zammadChatTemplates) { if (!window.zammadChatTemplates) {

File diff suppressed because one or more lines are too long

View file

@ -134,6 +134,8 @@
} }
--> -->
<script> <script>
var chat = new ZammadChat();
chat.open();
$('.settings :input').on({ $('.settings :input').on({
change: function(){ change: function(){