From 2d823c8f8557f0a49e4307717e99a90b24052e9c Mon Sep 17 00:00:00 2001 From: lassulus Date: Fri, 8 Nov 2013 13:15:52 +0100 Subject: webchat: refactored code --- webchat/public/functions.js | 117 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 webchat/public/functions.js (limited to 'webchat/public/functions.js') diff --git a/webchat/public/functions.js b/webchat/public/functions.js new file mode 100644 index 00000000..ddd3aad6 --- /dev/null +++ b/webchat/public/functions.js @@ -0,0 +1,117 @@ +function inputParser (str) { + var match = /^\/([a-z]+)(?:\s+(.*\S))?\s*$/.exec(str) + if (match) { + return { method: match[1], params: match[2] } + } else { + return { method: 'say', params: str } + } +} + + +function clientParser(object) { + console.log(object) + switch (object.type) { + case 'message': + return printMessage(object); + case 'join': + return handleJoin(object); + case 'quit': + return handleQuit(object); + case 'nicklist': + return handleNicklist(object); + case 'nickchange': + return handleNickchange(object); + } +}; + +function handleJoin(object) { + var safe_from = $('
').text(object.from).html(); + $(''+getCurTime()+''+safe_from+'joined').insertBefore('#foot'); + $('#nicklist').append('
' + safe_from + '
') ; +}; + +function handleQuit(object) { + var safe_from = $('
').text(object.from).html(); + $(''+getCurTime()+''+safe_from+'quit').insertBefore('#foot'); + console.log('removing', safe_from); + $(getNicklistElement(safe_from)).remove(); +}; + +function handleNicklist(object) { + Object.keys(object.message).forEach(function (nick) { +// console.log('nick',nick); + var hash_from = btoa(nick).replace(/=/g,'_'); +// $('.name').each(function (i,e) { console.log(i,e); if (e.innerHTML === 'kweb') { $(e).attr("style", "color:green") } }) + $('#nicklist').append('
' + nick + '
') ; + }); +}; + +function handleNickchange(object) { + var safe_from = $('
').text(object.nick).html(); + var safe_newnick = $('
').text(object.newnick).html(); + $(''+getCurTime()+''+safe_from+'is now known as '+object.newnick+'').insertBefore('#foot'); + $(getNicklistElement(safe_from)).remove(); + $('#nicklist').append('
' + safe_from + '
') ; +}; + +function replaceURLWithHTMLLinks (text) { + var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; + return text.replace(exp,"$1"); +} + +function setMaybeNick (input) { + if (match) { + nick = match[1]; + $('#nick').html(nick); + } +} +function sortNicklist () { +}; + +function getNicklistElement(name) { + var el; + $('.name').each(function (i,e) { + if (e.innerHTML === name) { + if (typeof el !== 'undefined') { + throw new Error('duplicate name: ' + name); + }; + el = e; + }; + }); + return el; +} + +function chatboxAppend (chat_from, chat_msg, type) { + type = type||'chat' + $(''+getCurTime()+''+chat_from+''+chat_msg+'').insertBefore('#foot'); + + var elem = document.getElementById('chatter'); + elem.scrollTop = elem.scrollHeight; +}; + +function printMessage(object) { + var safe_message = $('
').text(object.message).html(); + safe_message = replaceURLWithHTMLLinks(safe_message); + var safe_from = $('
').text(object.nick).html(); + return chatboxAppend(safe_from, safe_message) +}; + +function getCurTime () { + date = new Date; + h = date.getHours(); + if(h<10) + { + h = "0"+h; + } + m = date.getMinutes(); + if(m<10) + { + m = "0"+m; + } + s = date.getSeconds(); + if(s<10) + { + s = "0"+s; + } + return ''+h+':'+m+':'+s; +}; -- cgit v1.2.3 From 24efdc59cb58c5bbfa49516088fe124bc9f431c1 Mon Sep 17 00:00:00 2001 From: lassulus Date: Mon, 11 Nov 2013 17:46:27 +0100 Subject: webchat: still refactoring --- webchat/public/functions.js | 63 ++++++++++----------------------------------- 1 file changed, 14 insertions(+), 49 deletions(-) (limited to 'webchat/public/functions.js') diff --git a/webchat/public/functions.js b/webchat/public/functions.js index ddd3aad6..adcf3169 100644 --- a/webchat/public/functions.js +++ b/webchat/public/functions.js @@ -9,50 +9,22 @@ function inputParser (str) { function clientParser(object) { - console.log(object) - switch (object.type) { - case 'message': - return printMessage(object); - case 'join': - return handleJoin(object); - case 'quit': - return handleQuit(object); - case 'nicklist': - return handleNicklist(object); - case 'nickchange': - return handleNickchange(object); - } -}; - -function handleJoin(object) { - var safe_from = $('
').text(object.from).html(); - $(''+getCurTime()+''+safe_from+'joined').insertBefore('#foot'); - $('#nicklist').append('
' + safe_from + '
') ; + console.log('parser: ',object) + return (handler[object.method] || console.log)(object.params) +//switch (object.method) { +// case 'message': +// return handler.message(object.params); +// case 'join': +// return handler.join(object.params); +// case 'quit': +// return handler.quit(object.params); +// case 'nicklist': +// return handler.nicklist(object.params); +// case 'nickchange': +// return handler.nickchange(object.params); +//} }; -function handleQuit(object) { - var safe_from = $('
').text(object.from).html(); - $(''+getCurTime()+''+safe_from+'quit').insertBefore('#foot'); - console.log('removing', safe_from); - $(getNicklistElement(safe_from)).remove(); -}; - -function handleNicklist(object) { - Object.keys(object.message).forEach(function (nick) { -// console.log('nick',nick); - var hash_from = btoa(nick).replace(/=/g,'_'); -// $('.name').each(function (i,e) { console.log(i,e); if (e.innerHTML === 'kweb') { $(e).attr("style", "color:green") } }) - $('#nicklist').append('
' + nick + '
') ; - }); -}; - -function handleNickchange(object) { - var safe_from = $('
').text(object.nick).html(); - var safe_newnick = $('
').text(object.newnick).html(); - $(''+getCurTime()+''+safe_from+'is now known as '+object.newnick+'').insertBefore('#foot'); - $(getNicklistElement(safe_from)).remove(); - $('#nicklist').append('
' + safe_from + '
') ; -}; function replaceURLWithHTMLLinks (text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; @@ -89,13 +61,6 @@ function chatboxAppend (chat_from, chat_msg, type) { elem.scrollTop = elem.scrollHeight; }; -function printMessage(object) { - var safe_message = $('
').text(object.message).html(); - safe_message = replaceURLWithHTMLLinks(safe_message); - var safe_from = $('
').text(object.nick).html(); - return chatboxAppend(safe_from, safe_message) -}; - function getCurTime () { date = new Date; h = date.getHours(); -- cgit v1.2.3 From 2197882bc5a131ed7c877505ac66cbfa02679273 Mon Sep 17 00:00:00 2001 From: lassulus Date: Mon, 11 Nov 2013 18:28:25 +0100 Subject: webchat: join and quit color via css --- webchat/public/functions.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'webchat/public/functions.js') diff --git a/webchat/public/functions.js b/webchat/public/functions.js index adcf3169..0b8f2d49 100644 --- a/webchat/public/functions.js +++ b/webchat/public/functions.js @@ -54,8 +54,8 @@ function getNicklistElement(name) { } function chatboxAppend (chat_from, chat_msg, type) { - type = type||'chat' - $(''+getCurTime()+''+chat_from+''+chat_msg+'').insertBefore('#foot'); + type = type||'msg' + $(''+getCurTime()+''+chat_from+''+chat_msg+'').insertBefore('#foot'); var elem = document.getElementById('chatter'); elem.scrollTop = elem.scrollHeight; -- cgit v1.2.3 From 2be3c286f5151ca319700dd4bd7c6d16cec6c2c4 Mon Sep 17 00:00:00 2001 From: lassulus Date: Mon, 11 Nov 2013 18:31:04 +0100 Subject: webchat: cleanup code --- webchat/public/functions.js | 12 ------------ 1 file changed, 12 deletions(-) (limited to 'webchat/public/functions.js') diff --git a/webchat/public/functions.js b/webchat/public/functions.js index 0b8f2d49..b669a928 100644 --- a/webchat/public/functions.js +++ b/webchat/public/functions.js @@ -11,18 +11,6 @@ function inputParser (str) { function clientParser(object) { console.log('parser: ',object) return (handler[object.method] || console.log)(object.params) -//switch (object.method) { -// case 'message': -// return handler.message(object.params); -// case 'join': -// return handler.join(object.params); -// case 'quit': -// return handler.quit(object.params); -// case 'nicklist': -// return handler.nicklist(object.params); -// case 'nickchange': -// return handler.nickchange(object.params); -//} }; -- cgit v1.2.3 From c6a7cd9027150fca42500b3bbc78be01993856f6 Mon Sep 17 00:00:00 2001 From: lassulus Date: Mon, 11 Nov 2013 20:16:45 +0100 Subject: webchat: request handling --- webchat/public/functions.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'webchat/public/functions.js') diff --git a/webchat/public/functions.js b/webchat/public/functions.js index b669a928..45c8ad3f 100644 --- a/webchat/public/functions.js +++ b/webchat/public/functions.js @@ -8,9 +8,18 @@ function inputParser (str) { } -function clientParser(object) { +function methodDispatcher (settings, object) { console.log('parser: ',object) - return (handler[object.method] || console.log)(object.params) + return (handler[object.method] || console.log)(settings, object.params) +}; + +function resultDispatcher (settings, object) { + console.log('parser: ',object) + var callback = settings.waiting_callbacks[object.id] + delete settings.waiting_callbacks[object.id] + if (typeof callback === 'function') { + callback(object.error, object.result) + } }; -- cgit v1.2.3 From 3531d46cc3644a64b990f904e40e4846b8242a07 Mon Sep 17 00:00:00 2001 From: lassulus Date: Tue, 12 Nov 2013 02:01:29 +0100 Subject: webchat: refactor to rpc --- webchat/public/functions.js | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) (limited to 'webchat/public/functions.js') diff --git a/webchat/public/functions.js b/webchat/public/functions.js index 45c8ad3f..244af67b 100644 --- a/webchat/public/functions.js +++ b/webchat/public/functions.js @@ -3,25 +3,11 @@ function inputParser (str) { if (match) { return { method: match[1], params: match[2] } } else { - return { method: 'say', params: str } + return { method: 'msg', params: str } } } -function methodDispatcher (settings, object) { - console.log('parser: ',object) - return (handler[object.method] || console.log)(settings, object.params) -}; - -function resultDispatcher (settings, object) { - console.log('parser: ',object) - var callback = settings.waiting_callbacks[object.id] - delete settings.waiting_callbacks[object.id] - if (typeof callback === 'function') { - callback(object.error, object.result) - } -}; - function replaceURLWithHTMLLinks (text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; @@ -37,9 +23,9 @@ function setMaybeNick (input) { function sortNicklist () { }; -function getNicklistElement(name) { +function getNicklistElement(name, type) { var el; - $('.name').each(function (i,e) { + $('.'+type+'_name').each(function (i,e) { if (e.innerHTML === name) { if (typeof el !== 'undefined') { throw new Error('duplicate name: ' + name); -- cgit v1.2.3 From fc72fda5fc46daea25a991ba6b907a793e52ed06 Mon Sep 17 00:00:00 2001 From: lassulus Date: Tue, 12 Nov 2013 02:06:24 +0100 Subject: webchat: remove cruft --- webchat/public/functions.js | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'webchat/public/functions.js') diff --git a/webchat/public/functions.js b/webchat/public/functions.js index 244af67b..781fafce 100644 --- a/webchat/public/functions.js +++ b/webchat/public/functions.js @@ -7,22 +7,11 @@ function inputParser (str) { } } - - function replaceURLWithHTMLLinks (text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; return text.replace(exp,"$1"); } -function setMaybeNick (input) { - if (match) { - nick = match[1]; - $('#nick').html(nick); - } -} -function sortNicklist () { -}; - function getNicklistElement(name, type) { var el; $('.'+type+'_name').each(function (i,e) { -- cgit v1.2.3 From 721b4fb1b32ee47f158fb3b9a1ad0747ff579044 Mon Sep 17 00:00:00 2001 From: lassulus Date: Tue, 12 Nov 2013 02:59:28 +0100 Subject: webchat: remove input parser --- webchat/public/functions.js | 8 -------- 1 file changed, 8 deletions(-) (limited to 'webchat/public/functions.js') diff --git a/webchat/public/functions.js b/webchat/public/functions.js index 781fafce..318d0865 100644 --- a/webchat/public/functions.js +++ b/webchat/public/functions.js @@ -1,11 +1,3 @@ -function inputParser (str) { - var match = /^\/([a-z]+)(?:\s+(.*\S))?\s*$/.exec(str) - if (match) { - return { method: match[1], params: match[2] } - } else { - return { method: 'msg', params: str } - } -} function replaceURLWithHTMLLinks (text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; -- cgit v1.2.3 From b749c216ae84f02330da7396135be11259ec12d9 Mon Sep 17 00:00:00 2001 From: lassulus Date: Tue, 12 Nov 2013 04:08:38 +0100 Subject: webchat: fix nicklist --- webchat/public/functions.js | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'webchat/public/functions.js') diff --git a/webchat/public/functions.js b/webchat/public/functions.js index 318d0865..781fafce 100644 --- a/webchat/public/functions.js +++ b/webchat/public/functions.js @@ -1,3 +1,11 @@ +function inputParser (str) { + var match = /^\/([a-z]+)(?:\s+(.*\S))?\s*$/.exec(str) + if (match) { + return { method: match[1], params: match[2] } + } else { + return { method: 'msg', params: str } + } +} function replaceURLWithHTMLLinks (text) { var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; -- cgit v1.2.3