diff options
author | Joe Robinson <joe@lc8n.com> | 2016-10-13 22:34:21 +0100 |
---|---|---|
committer | Joe Robinson <joe@lc8n.com> | 2016-10-13 22:34:21 +0100 |
commit | 40b1c978b00911cf17dc1a7b45c10b82ce57641d (patch) | |
tree | b8bdb5a380fb250ea490869ec85dd3f81bec4afa | |
parent | 97fa9bf68f5e3aa50f094fa7e7bb4c427f2702aa (diff) |
Fix PM sending/receiving, some cleanup for messages
-rw-r--r-- | handlers.js | 58 | ||||
-rw-r--r-- | index.html | 2 | ||||
-rw-r--r-- | index.js | 51 | ||||
-rw-r--r-- | main.js | 2 |
4 files changed, 55 insertions, 58 deletions
diff --git a/handlers.js b/handlers.js index 938c2e2..e43a250 100644 --- a/handlers.js +++ b/handlers.js @@ -6,29 +6,24 @@ var numChans = 1; var channels; var buffers = []; var client; +var myNick = "" module.exports.setChannels = function(myChannels) { - console.log("setting channels: " + myChannels); channels = myChannels; } module.exports.handleCommands = function(myClient) { client = myClient; client.on('registered', function(event) { - console.log(event); - console.log("channels: " + channels); channels.forEach(function(channelStr) { - console.log(channelStr); joinChannel(channelStr, true); }); }); client.on('message', function(event) { - console.log(event); receiveMsg(event.target, event.nick, event.message) }) client.on('userlist', function(event) { - channel = event.channel.replace("#","") - if($(".ui.tab[data-tab=chan-"+channel+"]").length == 0) { + if($(".ui.tab[data-tab='"+channel+"']").length == 0) { joinChannel(channel, false) } event.users.forEach(function (user) { @@ -37,15 +32,14 @@ module.exports.handleCommands = function(myClient) { user.modes.forEach(function (modeChar) { if (modeChar === "o") { mode = "@" - console.log(mode) } else if (modeChar === "h") { mode = "%" } else if (modeChar === "v") { mode = "+" } }) - if($(".ui.users[data-tab=chan-"+channel+"] [data-nick="+nick+"]").length == 0) { - $(".ui.users[data-tab=chan-"+channel+"]").append("<div class=item data-nick="+nick+">"+mode+nick+"</div>") + if($(".ui.users[data-tab='"+channel+"'] [data-nick="+nick+"]").length == 0) { + $(".ui.users[data-tab='"+channel+"']").append("<div class=item data-nick="+nick+">"+mode+nick+"</div>") } }) }); @@ -53,7 +47,6 @@ module.exports.handleCommands = function(myClient) { } function joinChannel(channel, sendJoin) { - console.log("here"); var channelObj = client.channel(channel); if (sendJoin) { @@ -61,26 +54,25 @@ function joinChannel(channel, sendJoin) { channelObj.join(); channelObj.say("beep"); } - channel = channel.replace("#",""); chanId = $(".active.chat").attr("data-id") $(".active").removeClass("active"); - $(".ui.menu").append("<a class='active item' data-id='"+numChans+"' data-tab='chan-"+channel+"'>#"+channel+"</a>"); - $("#chan-container").append("<div class='ui active tab' data-id='"+numChans+"' data-tab='chan-"+channel+"'></div>"); + $(".ui.menu").append("<a class='active item' data-id='"+numChans+"' data-tab='"+channel+"'>"+channel+"</a>"); + $("#chan-container").append("<div class='ui active tab' data-id='"+numChans+"' data-tab='"+channel+"'></div>"); - var chanTab = $("[data-tab=chan-"+ channel +"].tab"); + var chanTab = $("[data-tab='"+ channel +"'].tab"); - chanTab.append("<div class='ui selection list users' data-id='"+numChans+"' data-tab='chan-"+channel+"'></div>"); - chanTab.append("<div class='ui segment chat' data-id='"+numChans+"' data-tab='chan-"+channel+"'></div>"); - chanTab.append("<div class='ui topic' data-id='"+numChans+"' data-tab='chan-"+channel+"'></div>"); + chanTab.append("<div class='ui selection list users' data-id='"+numChans+"' data-tab='"+channel+"'></div>"); + chanTab.append("<div class='ui segment chat' data-id='"+numChans+"' data-tab='"+channel+"'></div>"); + chanTab.append("<div class='ui topic' data-id='"+numChans+"' data-tab='"+channel+"'></div>"); $('.menu .item').tab({history:false}); numChans++; } -function receiveMsg(channel, from, message) { +function receiveMsg(target, from, message) { // message = message.autoLink( { // callback: function(url) { // return /\.(gif|png|jpe?g)$/i.test(url) ? '<img src="' + url + '">' : null; @@ -88,13 +80,26 @@ function receiveMsg(channel, from, message) { // target: "_blank" // }); // console.log(message.autoLink()); - channel = channel.replace("#","") - console.log(from + ' => #'+ channel +': ' + message); - console.log($("[data-tab=chan-"+ channel +"].chat")); - var chatTab = $("[data-tab=chan-"+ channel +"].chat"); - console.log(chatTab); + + console.log(from + ' => '+ target +': ' + message); + + if (target === client.user.nick) { + target = from + } + var chatTab = $("[data-tab='"+ target +"'].chat"); + + if (chatTab.length == 0) { + $(".ui.menu").append("<a class='item' data-id='"+numChans+"' data-tab='"+target+"'>"+target+"</a>"); + $("#chan-container").append("<div class='ui tab' data-id='"+numChans+"' data-tab='"+target+"'></div>"); + chanTab = $("[data-tab="+target+"].tab"); + chanTab.append("<div class='ui segment chat' data-id='"+numChans+"' data-tab='"+target+"'></div>"); + + $('.menu .item').tab({history:false}); + numChans++; + chatTab = $("[data-tab="+ target +"].chat"); + } + message = linkifyHtml(message); - console.log(from + ' => #'+ channel +': ' + message); chatTab.append("<p class='chat-line'><" + from + "> " + message +"</p>"); var imgMatch = message.match(/[a-z0-9\-]+[\.:]\S+\.(gif|png|jpe?g|bmp)/); @@ -102,7 +107,7 @@ function receiveMsg(channel, from, message) { var audioMatch = message.match(/[a-z0-9\-]+[\.:]\S+\.(mp3|wav|ogg)/); var youtubeMatch = message.match(/(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/(watch\?v=)?[a-z0-9A-Z_\-]+/); if (imgMatch != null && imgMatch.length > 0) { - var image = util.imagify(imgMatch[0],channel); + var image = util.imagify(imgMatch[0],target); chatTab.append("<p class=chat-line>"+image+"</p>"); } if (vidMatch != null && vidMatch.length > 0) { @@ -112,7 +117,6 @@ function receiveMsg(channel, from, message) { chatTab.append("<p class=chat-line><audio controls><source src="+audioMatch[0]+"></audio></p>"); } if (youtubeMatch != null && youtubeMatch.length > 0) { - console.log(youtubeMatch) youtubeParts = youtubeMatch[0].split("/"); youtubeId = youtubeParts[youtubeParts.length-1]; youtubeId = youtubeId.replace("watch?v=",""); @@ -53,7 +53,7 @@ window.$ = window.jQuery = require('./lib/jquery-2.2.2.js'); blachat = require("./index.js"); function scrollToTop(channel) { - var chatTab = $("[data-tab=chan-"+ channel +"].chat"); + var chatTab = $("[data-tab="+ channel +"].chat"); chatTab.scrollTop(chatTab.prop('scrollHeight')); } </script> @@ -72,10 +72,6 @@ console.log($("#channel-input").val()); }); - - - - function imagify(url, channel) { console.log(url); $.ajax({ @@ -111,7 +107,7 @@ var numChans = 1; $("#send-message").keypress(function(e) { if(e.which == 13) { var message = $("#send-message").val(); - var channel = $(".tab.active").attr("data-tab").replace("chan-", "#") + var channel = $(".tab.active").attr("data-tab") if (message.indexOf("/join") == 0) { joinChannel(message.substring(6, message.length), true); } else if (message.indexOf("/part") == 0) { @@ -130,7 +126,7 @@ $("#send-message").keypress(function(e) { message = message.substring(6+nick.length, message.length); sendPm(message, nick); } else if (message.indexOf("/topic") == 0) { - var channel = $(".tab.active").attr("data-tab").replace("chan-", "#") + var channel = $(".tab.active").attr("data-tab") topic = message.substring(7, message.length); setTopic(channel, topic); } else if (message.indexOf("/") == 0) { @@ -139,11 +135,11 @@ $("#send-message").keypress(function(e) { client.send("kick", ["#wtest", "wjoe__", "test"]); } else { var tab = $(".tab.active").attr("data-tab"); - if (tab.startsWith("chan-")) { + if (tab.startsWith("#")) { sendMsg(message, channel); - } else if(tab.startsWith("pm-")){ - sendPm(message, tab.substring(3, tab.length)); + } else { + sendPm(message, tab); } } $("#send-message").val(''); @@ -152,26 +148,26 @@ $("#send-message").keypress(function(e) { function sendMsg(message, channel) { client.say(channel, message); - var chanTab = $("[data-tab=chan-"+ channel.replace("#","") +"].chat"); + var chanTab = $("[data-tab='"+ channel +"'].chat"); chanTab.append("<p class='chat-line'><" + myNick + "> " + message +"</p>"); chanTab.scrollTop(chanTab.prop("scrollHeight")); } function sendPm(message, nick) { - var chanTab = $("[data-tab=pm-"+nick+"].tab"); + var chanTab = $("[data-tab="+nick+"].tab"); $(".active").removeClass("active"); if (chanTab.length == 0) { - $(".ui.menu").append("<a class='active item' data-id='"+numChans+"' data-tab='pm-"+nick+"'>"+nick+"</a>"); - $("#chan-container").append("<div class='ui active tab' data-id='"+numChans+"' data-tab='pm-"+nick+"'></div>"); - chanTab = $("[data-tab=pm-"+nick+"].tab"); - chanTab.append("<div class='ui segment chat' data-id='"+numChans+"' data-tab='pm-"+nick+"'></div>"); + $(".ui.menu").append("<a class='active item' data-id='"+numChans+"' data-tab='"+nick+"'>"+nick+"</a>"); + $("#chan-container").append("<div class='ui active tab' data-id='"+numChans+"' data-tab='"+nick+"'></div>"); + chanTab = $("[data-tab="+nick+"].tab"); + chanTab.append("<div class='ui segment chat' data-id='"+numChans+"' data-tab='"+nick+"'></div>"); $('.menu .item').tab({history:false}); numChans++; } - $("[data-tab=pm-"+nick+"]").addClass("active"); - var chatTab = $("[data-tab=pm-"+nick+"].chat"); + $("[data-tab="+nick+"]").addClass("active"); + var chatTab = $("[data-tab="+nick+"].chat"); console.log(nick + ': ' + message); client.say(nick, message); chatTab.append("<p class='chat-line'><" + myNick + "> " + message +"</p>"); @@ -183,34 +179,31 @@ function setTopic(channel, topic) { } function joinChannel(channel, sendJoin) { - console.log("here"); if (sendJoin) { client.join(channel); } - channel = channel.replace("#",""); chanId = $(".active.chat").attr("data-id") $(".active").removeClass("active"); - $(".ui.menu").append("<a class='active item' data-id='"+numChans+"' data-tab='chan-"+channel+"'>#"+channel+"</a>"); - $("#chan-container").append("<div class='ui active tab' data-id='"+numChans+"' data-tab='chan-"+channel+"'></div>"); + $(".ui.menu").append("<a class='active item' data-id='"+numChans+"' data-tab='"+channel+"'>"+channel+"</a>"); + $("#chan-container").append("<div class='ui active tab' data-id='"+numChans+"' data-tab='"+channel+"'></div>"); - var chanTab = $("[data-tab=chan-"+ channel +"].tab"); + var chanTab = $("[data-tab="+ channel +"].tab"); - chanTab.append("<div class='ui selection list users' data-id='"+numChans+"' data-tab='chan-"+channel+"'></div>"); - chanTab.append("<div class='ui segment chat' data-id='"+numChans+"' data-tab='chan-"+channel+"'></div>"); - chanTab.append("<div class='ui topic' data-id='"+numChans+"' data-tab='chan-"+channel+"'></div>"); + chanTab.append("<div class='ui selection list users' data-id='"+numChans+"' data-tab='"+channel+"'></div>"); + chanTab.append("<div class='ui segment chat' data-id='"+numChans+"' data-tab='"+channel+"'></div>"); + chanTab.append("<div class='ui topic' data-id='"+numChans+"' data-tab='"+channel+"'></div>"); $('.menu .item').tab({history:false}); } function removeChannel(channel, sendPart) { - channel = channel.replace("#",""); - chanId = $("[data-tab=chan-"+channel+"].chat").attr("data-id"); - $("[data-tab=chan-"+channel+"]").remove(); + chanId = $("[data-tab="+channel+"].chat").attr("data-id"); + $("[data-tab="+channel+"]").remove(); if (sendPart) { - client.part("#"+channel); + client.part(channel); } while ($("[data-id="+(chanId-1)+"]").length == 0 && chanId >=0) { chanId--; @@ -24,7 +24,7 @@ app.on('window-all-closed', function() { // initialization and is ready to create browser windows. app.on('ready', function() { // Create the browser window. - mainWindow = new BrowserWindow({width: 800, height: 600, title: "blachat"}); + mainWindow = new BrowserWindow({width: 1000, height: 800, title: "blachat"}); // and load the index.html of the app. mainWindow.loadURL('file://' + __dirname + '/index.html'); |