|
| 1 | +class WebSocketChat { |
| 2 | + |
| 3 | + enterRoom(roomName) { |
| 4 | + let message = { |
| 5 | + room: roomName |
| 6 | + }; |
| 7 | + this._connection.send("chatRoomEnter:" + JSON.stringify(message)); |
| 8 | + } |
| 9 | + |
| 10 | + leaveRoom(roomName) { |
| 11 | + let message = { |
| 12 | + room: roomName |
| 13 | + }; |
| 14 | + this._connection.send("chatRoomLeave:" + JSON.stringify(message)); |
| 15 | + } |
| 16 | + |
| 17 | + createRoom(roomName) { |
| 18 | + if ($("#room-" + roomName ).length > 0) { |
| 19 | + $('[href="#room-' + roomName + '"]').tab("show"); |
| 20 | + return false; |
| 21 | + } |
| 22 | + |
| 23 | + let tab = require("./templates/new_tab.hbs"); |
| 24 | + let tabContent = require("./templates/new_tab_content.hbs"); |
| 25 | + $("#room-tabs").append( |
| 26 | + $(tab({ "room": roomName })) |
| 27 | + ); |
| 28 | + $("#room-tabs-content").append( |
| 29 | + $(tabContent({ "room": roomName })) |
| 30 | + ); |
| 31 | + |
| 32 | + $('[href="#room-' + roomName + '"]').tab("show"); |
| 33 | + |
| 34 | + this.enterRoom(roomName); |
| 35 | + |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + displayNewMessage(message) { |
| 40 | + let messageData; |
| 41 | + try { |
| 42 | + messageData = JSON.parse(message); |
| 43 | + } catch (e) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + let container = $("#" + messageData.room + "-messages"); |
| 48 | + container.prepend( |
| 49 | + $(this._messageTemplate({ "dateTime": this._moment().format("lll"), "comment": messageData.comment })) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + sendMessage(roomName, comment) { |
| 54 | + let message = { |
| 55 | + room: roomName, |
| 56 | + comment: comment |
| 57 | + }; |
| 58 | + this._connection.send("chatMessage:" + JSON.stringify(message)); |
| 59 | + } |
| 60 | + |
| 61 | + constructor(url) { |
| 62 | + this._connection = new WebSocket(url); |
| 63 | + this._moment = require("moment"); |
| 64 | + this._messageTemplate = require("./templates/new_message.hbs"); |
| 65 | + |
| 66 | + let that = this; |
| 67 | + this._connection.onopen = function() { |
| 68 | + let init = { |
| 69 | + room:"default" |
| 70 | + }; |
| 71 | + that._connection.send("chatRoomEnter:" + JSON.stringify(init)); |
| 72 | + }; |
| 73 | + |
| 74 | + this._connection.onmessage = function(event) { |
| 75 | + that.displayNewMessage(event.data); |
| 76 | + }; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +(function($) { |
| 81 | + $.fn.onPressEnter = function(func) { |
| 82 | + this.bind("keypress", function(e) { |
| 83 | + if ( e.keyCode === 13 ) { |
| 84 | + e.preventDefault(); |
| 85 | + func.apply(this, [e]); |
| 86 | + } |
| 87 | + }); |
| 88 | + return this; |
| 89 | + }; |
| 90 | +})(jQuery); |
| 91 | + |
| 92 | +connect = function(url) { |
| 93 | + let chat = new WebSocketChat(url); |
| 94 | + let messageInput = $("input#new-message"); |
| 95 | + let newRoomInput = $("input#new-room-name"); |
| 96 | + let send = function() { |
| 97 | + if ("" === messageInput.val() ) { |
| 98 | + return; |
| 99 | + } |
| 100 | + chat.sendMessage($("li.active").data("chatroom"), messageInput.val()); |
| 101 | + messageInput.val(null); |
| 102 | + }; |
| 103 | + |
| 104 | + let newRoom = function() { |
| 105 | + if ("" === newRoomInput.val() ) { |
| 106 | + return; |
| 107 | + } |
| 108 | + chat.createRoom(newRoomInput.val()); |
| 109 | + newRoomInput.val(null); |
| 110 | + }; |
| 111 | + |
| 112 | + /** |
| 113 | + * Send message |
| 114 | + */ |
| 115 | + messageInput.onPressEnter(function() { |
| 116 | + send(); |
| 117 | + }); |
| 118 | + $("#btn-send").on("click", function() { |
| 119 | + send(); |
| 120 | + }); |
| 121 | + |
| 122 | + /** |
| 123 | + * Create new room |
| 124 | + */ |
| 125 | + newRoomInput.onPressEnter( function() { |
| 126 | + newRoom(); |
| 127 | + }); |
| 128 | + $("#btn-add-room").on("click", function() { |
| 129 | + newRoom(); |
| 130 | + }); |
| 131 | + |
| 132 | + /** |
| 133 | + * Remove a Tab |
| 134 | + */ |
| 135 | + $("#room-tabs").on("click", " li a .close", function() { |
| 136 | + let roomName = $( this ).parents("li.active").data("chatroom"); |
| 137 | + chat.leaveRoom(roomName); |
| 138 | + |
| 139 | + let tabId = $( this ).parents("li").children("a").attr("href"); |
| 140 | + $( this ).parents("li").remove("li"); |
| 141 | + $( tabId ).remove(); |
| 142 | + |
| 143 | + $("#room-tabs a:first").tab("show"); |
| 144 | + }); |
| 145 | + |
| 146 | + /** |
| 147 | + * Click Tab to show its content |
| 148 | + */ |
| 149 | + $("#room-tabs").on("click", "a", function( e ) { |
| 150 | + e.preventDefault(); |
| 151 | + $( this ).tab("show"); |
| 152 | + }); |
| 153 | +}; |
0 commit comments