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

View file

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

File diff suppressed because one or more lines are too long

View file

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