diff --git a/examples/molhokwai/app/static/js/app.js b/examples/molhokwai/app/static/js/app.js new file mode 100644 index 0000000..ef59fa6 --- /dev/null +++ b/examples/molhokwai/app/static/js/app.js @@ -0,0 +1,187 @@ +// File: static/js/app.js +(function() { + 'use strict'; + + var pusher = new Pusher('1ed4f14aec1b394a4ed4', { + authEndpoint: '/emelit/plugin_pusher/pusher_auth.json', + cluster: 'eu', + encrypted: true + }); + + // ---------------------------------------------------- + // Chat Details + // ---------------------------------------------------- + + let chat = { + name: undefined, + email: undefined, + myChannel: undefined, + } + + + // ---------------------------------------------------- + // Targeted Elements + // ---------------------------------------------------- + + const chatPage = $(document) + const chatWindow = $('.chatbubble') + const chatHeader = chatWindow.find('.unexpanded') + const chatBody = chatWindow.find('.chat-window') + + + // ---------------------------------------------------- + // Register helpers + // ---------------------------------------------------- + + let helpers = { + + // ---------------------------------------------------- + // Toggles the display of the chat window. + // ---------------------------------------------------- + + ToggleChatWindow: function () { + chatWindow.toggleClass('opened') + chatHeader.find('.title').text( + chatWindow.hasClass('opened') ? 'Minimize Chat Window' : 'Chat with Support' + ) + }, + + // -------------------------------------------------------------------- + // Show the appropriate display screen. Login screen or Chat screen. + // -------------------------------------------------------------------- + + ShowAppropriateChatDisplay: function () { + (chat.name) ? helpers.ShowChatRoomDisplay() : helpers.ShowChatInitiationDisplay() + }, + + // ---------------------------------------------------- + // Show the enter details form. + // ---------------------------------------------------- + + ShowChatInitiationDisplay: function () { + chatBody.find('.chats').removeClass('active') + chatBody.find('.login-screen').addClass('active') + }, + + // ---------------------------------------------------- + // Show the chat room messages display. + // ---------------------------------------------------- + + ShowChatRoomDisplay: function () { + chatBody.find('.chats').addClass('active') + chatBody.find('.login-screen').removeClass('active') + + setTimeout(function(){ + chatBody.find('.loader-wrapper').hide() + chatBody.find('.input, .messages').show() + }, 2000) + }, + + // ---------------------------------------------------- + // Append a message to the chat messages UI. + // ---------------------------------------------------- + + NewChatMessage: function (message) { + if (message !== undefined) { + const messageClass = message.sender !== chat.email ? 'support' : 'user' + + chatBody.find('ul.messages').append( + `
` + ) + + + chatBody.scrollTop(chatBody[0].scrollHeight) + } + }, + + // ---------------------------------------------------- + // Send a message to the chat channel. + // ---------------------------------------------------- + + SendMessageToSupport: function (evt) { + + evt.preventDefault() + + let createdAt = new Date() + createdAt = createdAt.toLocaleString() + + const message = $('#newMessage').val().trim() + + if(false){ + // removed client side trigerring + chat.myChannel.trigger('client-guest-new-message', { + 'sender': chat.name, + 'email': chat.email, + 'text': message, + 'createdAt': createdAt + }); + } + else{ + // added server side trigerring for event handling + let sender = chat.name + let email = chat.email + let text = message + axios.post('/emelit/plugin_pusher/message.json', {sender, email, text, createdAt}).then(response => { + // received: sender, email, text, createdAt + // pass + }) + } + + helpers.NewChatMessage({ + 'text': message, + 'name': chat.name, + 'sender': chat.email + }) + + console.log("Message added!") + + $('#newMessage').val('') + }, + + // ---------------------------------------------------- + // Logs user into a chat session. + // ---------------------------------------------------- + + LogIntoChatSession: function (evt) { + const name = $('#fullname').val().trim() + const email = $('#email').val().trim().toLowerCase() + + // Disable the form + chatBody.find('#loginScreenForm input, #loginScreenForm button').attr('disabled', true) + + if ((name !== '' && name.length >= 3) && (email !== '' && email.length >= 5)) { + axios.post('/emelit/plugin_pusher/guest.json/new', {name, email}).then(response => { + chat.name = name + chat.email = email + chat.myChannel = pusher.subscribe('private-' + response.data.email); + helpers.ShowAppropriateChatDisplay() + }) + } else { + alert('Enter a valid name and email.') + } + + evt.preventDefault() + } + } + + // ------------------------------------------------------------------ + // Listen for a new message event from the admin + // ------------------------------------------------------------------ + + pusher.bind('client-support-new-message', function(data){ + helpers.NewChatMessage(data) + }) + + + // ---------------------------------------------------- + // Register page event listeners + // ---------------------------------------------------- + + chatPage.ready(helpers.ShowAppropriateChatDisplay) + chatHeader.on('click', helpers.ToggleChatWindow) + chatBody.find('#loginScreenForm').on('submit', helpers.LogIntoChatSession) + chatBody.find('#messageSupport').on('submit', helpers.SendMessageToSupport) +}()) diff --git a/examples/molhokwai/script/plugin_pusher.py b/examples/molhokwai/script/plugin_pusher.py new file mode 100644 index 0000000..c4204b9 --- /dev/null +++ b/examples/molhokwai/script/plugin_pusher.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python + +import sys +sys.path.append('..') + +import time + +import pusherclient +from plugin_pusher import settings + +# Add a logging handler so we can see the raw communication data +import logging +root = logging.getLogger() +root.setLevel(logging.INFO) +ch = logging.StreamHandler(sys.stdout) +root.addHandler(ch) + +global pusher + +def print_usage(filename): + print("Usage: python %s