diff options
Diffstat (limited to 'shardweb/assets/js/app.js')
-rw-r--r-- | shardweb/assets/js/app.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/shardweb/assets/js/app.js b/shardweb/assets/js/app.js new file mode 100644 index 0000000..59a0acd --- /dev/null +++ b/shardweb/assets/js/app.js @@ -0,0 +1,58 @@ +// Brunch automatically concatenates all files in your +// watched paths. Those paths can be configured at +// config.paths.watched in "brunch-config.js". +// +// However, those files will only be executed if +// explicitly imported. The only exception are files +// in vendor, which are never wrapped in imports and +// therefore are always executed. + +// Import dependencies +// +// If you no longer want to use a dependency, remember +// to also remove its path from "config.paths.watched". +import "phoenix_html" + +// Import local files +// +// Local files can be imported directly using relative +// paths "./socket" or full ones "web/static/js/socket". + +import socket from "./socket" + +var room_name = window.Gon.getAsset('chat_room'); +if (room_name != undefined) +{ + var channel = socket.channel('room:' + room_name, {}); // connect to chat "room" + + channel.on('shout', function (payload) { // listen to the 'shout' event + var li = document.createElement("li"); // creaet new list item DOM element + var name = payload.name || 'guest'; // get name from payload or set default + li.innerHTML = '<b>' + name + '</b>: ' + payload.message; // set li contents + + console.log(ul.scrollTop + ' ' + ul.scrollHeight + ' ' + ul.clientHeight); + var must_scroll = (ul.scrollTop >= ul.scrollHeight - ul.clientHeight - 10); + + ul.appendChild(li); // append to list + + if (must_scroll) ul.scrollTop = ul.scrollHeight; + }); + + channel.join(); // join the channel. + + + var ul = document.getElementById('msg-list'); // list of messages. + var name = document.getElementById('name'); // name of message sender + var msg = document.getElementById('msg'); // message input field + + // "listen" for the [Enter] keypress event to send a message: + msg.addEventListener('keypress', function (event) { + if (event.keyCode == 13 && msg.value.length > 0) { // don't sent empty msg. + channel.push('shout', { // send the message to the server on "shout" channel + name: name.value, // get value of "name" of person sending the message + message: msg.value // get message text (value) from msg input field. + }); + msg.value = ''; // reset the message input field for next message. + } + }); +} |