From 777808182320e0584a38e2ae9fdfb6e8e7d99973 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Sat, 15 Oct 2016 20:59:27 +0100 Subject: Move set topic and join channel functions to commands.js --- commands.js | 34 ++++++++++++++++++++++++++++++++++ handlers.js | 32 ++++---------------------------- index.js | 40 ++++++---------------------------------- 3 files changed, 44 insertions(+), 62 deletions(-) create mode 100644 commands.js diff --git a/commands.js b/commands.js new file mode 100644 index 0000000..b8f53f5 --- /dev/null +++ b/commands.js @@ -0,0 +1,34 @@ +var client + +module.exports.init = function(myClient) { + client = myClient +} + +module.exports.setTopic = function(channel, topic) { + client.raw("TOPIC", channel, topic); +} + +module.exports.joinChannel = function (channel, sendJoin) { + + var channelObj = client.channel(channel); + if (sendJoin) { + channelObj.join(); + channelObj.say("beep"); + } + chanId = $(".active.chat").attr("data-id") + + $(".active").removeClass("active"); + + $(".ui.menu").append(""+channel+""); + $("#chan-container").append("
"); + + var chanTab = $("[data-tab='"+ channel +"'].tab"); + + chanTab.append("
"); + chanTab.append("
"); + chanTab.append("
"); + + $('.menu .item').tab({history:false}); + + numChans++; +} diff --git a/handlers.js b/handlers.js index 0e27c25..9b956c3 100644 --- a/handlers.js +++ b/handlers.js @@ -1,6 +1,7 @@ var linkify = require("linkifyjs"); var linkifyHtml = require('linkifyjs/html'); var util = require("./util.js") +var commands = require("./commands.js") var numChans = 1; var channels; @@ -13,7 +14,7 @@ module.exports.handleCommands = function(myClient) { client = myClient; client.on('registered', function(event) { channels.forEach(function(channelStr) { - joinChannel(channelStr, true); + commands.joinChannel(channelStr, true); }); }); client.on('message', function(event) { @@ -23,7 +24,7 @@ module.exports.handleCommands = function(myClient) { client.on('userlist', function(event) { channel = event.channel if($(".ui.tab[data-tab='"+channel+"']").length == 0) { - joinChannel(channel, false) + commands.joinChannel(channel, false) } event.users.forEach(function (user) { var nick = user.nick @@ -51,7 +52,7 @@ module.exports.handleCommands = function(myClient) { time = "vov" var chatTab = $("[data-tab='"+ channel +"'].chat"); if($(".ui.tab[data-tab='"+channel+"']").length == 0) { - joinChannel(channel, false) + commands.joinChannel(channel, false) } chatTab.append("

Topic for "+channel+" is "+topic+" - set by "+nick+" at "+time+"

") var topicTab = $("[data-tab='"+ channel +"'].topic"); @@ -60,31 +61,6 @@ module.exports.handleCommands = function(myClient) { } -function joinChannel(channel, sendJoin) { - - var channelObj = client.channel(channel); - if (sendJoin) { - channelObj.join(); - channelObj.say("beep"); - } - chanId = $(".active.chat").attr("data-id") - - $(".active").removeClass("active"); - - $(".ui.menu").append(""+channel+""); - $("#chan-container").append("
"); - - var chanTab = $("[data-tab='"+ channel +"'].tab"); - - chanTab.append("
"); - chanTab.append("
"); - chanTab.append("
"); - - $('.menu .item').tab({history:false}); - - numChans++; -} - function receiveMsg(target, from, message) { // message = message.autoLink( { // callback: function(url) { diff --git a/index.js b/index.js index 7b11a23..d6d0a62 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ var shell = require('electron').shell; var config = require('./config.json') var pjson = require('./package.json'); var handlers = require('./handlers.js'); - +var commands = require('./commands.js'); var debugmiddleware = require('./debugmiddleware.js'); const progName = pjson.name; const version = pjson.version; @@ -61,8 +61,9 @@ console.log($("#channel-input").val()); version: "blachat 0.1.3" }); - handlers.setChannels(channels); - handlers.handleCommands(client); + commands.init(client) + handlers.setChannels(channels) + handlers.handleCommands(client) @@ -109,7 +110,7 @@ $("#send-message").keypress(function(e) { var message = $("#send-message").val(); var channel = $(".tab.active").attr("data-tab") if (message.indexOf("/join") == 0) { - joinChannel(message.substring(6, message.length), true); + commands.joinChannel(message.substring(6, message.length), true); } else if (message.indexOf("/part") == 0) { if (message.length > 6) { channel = message.substring(6, message.length); @@ -127,7 +128,7 @@ $("#send-message").keypress(function(e) { } else if (message.indexOf("/topic") == 0) { var channel = $(".tab.active").attr("data-tab") topic = message.substring(7, message.length); - setTopic(channel, topic); + commands.setTopic(channel, topic); } else if (message.indexOf("/") == 0) { command = message.split(" ")[0].replace("/",""); commandArgs = message.replace(command + " ", ""); @@ -173,35 +174,6 @@ function sendPm(message, nick) { chatTab.scrollTop(chanTab.prop("scrollHeight")); } -function setTopic(channel, topic) { - client.raw("TOPIC", channel, topic); -} - -function joinChannel(channel, sendJoin) { - - var channelObj = client.channel(channel); - if (sendJoin) { - channelObj.join(); - channelObj.say("beep"); - } - chanId = $(".active.chat").attr("data-id") - - $(".active").removeClass("active"); - - $(".ui.menu").append(""+channel+""); - $("#chan-container").append("
"); - - var chanTab = $("[data-tab='"+ channel +"'].tab"); - - chanTab.append("
"); - chanTab.append("
"); - chanTab.append("
"); - - $('.menu .item').tab({history:false}); - - numChans++; -} - function removeChannel(channel, sendPart) { chanId = $("[data-tab='"+channel+"'].chat").attr("data-id"); $("[data-tab='"+channel+"']").remove(); -- cgit v1.2.3