From 86589a8f0cc592e404ab2d0ed0f86b6d9640c8b0 Mon Sep 17 00:00:00 2001 From: sirDonovan Date: Tue, 6 Jan 2015 03:52:38 -0600 Subject: [PATCH] Refactor |unlink| message This refactor adds a class referencing users to their chat messages, enabling both the original and new |unlink| messages to be handled using the same jQuery object, as well as removing the need for adding a similar class to links in Tools.parseMessage. The new |unlink|hide| message allows chat messages to be hidden from view with the option to restore them by clicking on the added notice. --- js/battle.js | 13 ++++++++++--- js/battledata.js | 19 +++++++------------ js/client-battle.js | 5 +++++ js/client-chat.js | 27 ++++++++++++++++++--------- js/client-mainmenu.js | 8 ++++---- 5 files changed, 44 insertions(+), 28 deletions(-) diff --git a/js/battle.js b/js/battle.js index abb70423c..f041938d8 100644 --- a/js/battle.js +++ b/js/battle.js @@ -5260,9 +5260,9 @@ var Battle = (function () { } var message = args.join('|'); if (message.substr(0,2) === '//') { - this.log('
' + clickableName + ': ' + Tools.parseMessage(message.substr(1), name) + '
', preempt); + this.log('
' + clickableName + ': ' + Tools.parseMessage(message.substr(1)) + '
', preempt); } else if (message.substr(0,4).toLowerCase() === '/me ') { - this.log('
• ' + clickableName + ' ' + Tools.parseMessage(message.substr(4), name) + '
', preempt); + this.log('
• ' + clickableName + ' ' + Tools.parseMessage(message.substr(4)) + '
', preempt); } else if (message.substr(0,14).toLowerCase() === '/data-pokemon ') { if (window.Chart) this.log('
', preempt); } else if (message.substr(0,11).toLowerCase() === '/data-item ') { @@ -5272,7 +5272,7 @@ var Battle = (function () { } else if (message.substr(0,11).toLowerCase() === '/data-move ') { if (window.Chart) this.log('
', preempt); } else { - this.log('
' + clickableName + ': ' + Tools.parseMessage(message, name) + '
', preempt); + this.log('
' + clickableName + ': ' + Tools.parseMessage(message) + '
', preempt); } break; case 'chatmsg': @@ -5458,6 +5458,13 @@ var Battle = (function () { this.log('
[DEBUG] ' + Tools.escapeHTML(name) + '.
', preempt); break; case 'unlink': + var user = toId(args[2]) || toId(args[1]); + var $messages = $('.chatmessage-' + user); + $messages.find('a').contents().unwrap(); + if (args[2]) { + $messages.hide(); + this.log('
'); + } break; default: this.logConsole('unknown command: ' + args[0]); diff --git a/js/battledata.js b/js/battledata.js index 51fa65480..43d505adc 100644 --- a/js/battledata.js +++ b/js/battledata.js @@ -313,7 +313,7 @@ var Tools = { return Tools.escapeHTML(formatid); }, - parseMessage: function(str, linkclass) { + parseMessage: function(str) { str = Tools.escapeHTML(str); // Don't format console commands (>>). if (str.substr(0, 8) === '>>') return str; @@ -330,13 +330,9 @@ var Tools = { options.hidestrikethrough ? '$1' : '$1'); // linking of URIs if (!options.hidelinks) { - var classbit = ''; - if (linkclass) { - classbit = ' class="message-link-' + toId(linkclass) + '"'; - } str = str.replace(/https?\:\/\/[a-z0-9-.]+(?:\:[0-9]+)?(?:\/(?:[^\s]*[^\s?.,])?)?|[a-z0-9.]+\@[a-z0-9.]+\.[a-z0-9]{2,3}|(?:[a-z0-9](?:[a-z0-9-\.]*[a-z0-9])?\.(?:com|org|net|edu|us|jp)(?:\:[0-9]+)?|qmark\.tk)(?:(?:\/(?:[^\s]*[^\s?.,])?)?)\b/ig, function(uri) { if (/[a-z0-9.]+\@[a-z0-9.]+\.[a-z0-9]{2,3}/ig.test(uri)) { - return ''+uri+''; + return ''+uri+''; } // Insert http:// before URIs without a URI scheme specified. var fulluri = uri.replace(/^([a-z]*[^a-z:])/g, 'http://$1'); @@ -361,8 +357,7 @@ var Tools = { event + '\', \'' + Tools.escapeQuotes(fulluri) + '\');'; } return '' + uri + ''; + '" target="_blank" onclick="' + onclick + '">' + uri + ''; }); // google [blah] // google[blah] @@ -370,7 +365,7 @@ var Tools = { str = str.replace(/\bgoogle ?\[([^\]<]+)\]/ig, function(p0, p1) { p1 = Tools.escapeHTML(encodeURIComponent(Tools.unescapeHTML(p1))); return '' + p0 + ''; + '" target="_blank">' + p0 + ''; }); // gl [blah] // gl[blah] @@ -378,21 +373,21 @@ var Tools = { str = str.replace(/\bgl ?\[([^\]<]+)\]/ig, function(p0, p1) { p1 = Tools.escapeHTML(encodeURIComponent(Tools.unescapeHTML(p1))); return '' + p0 + ''; + '" target="_blank">' + p0 + ''; }); // wiki [blah] // Search Wikipedia for 'blah' (and visit the article for 'blah' if it exists) str = str.replace(/\bwiki ?\[([^\]<]+)\]/ig, function(p0, p1) { p1 = Tools.escapeHTML(encodeURIComponent(Tools.unescapeHTML(p1))); return '' + p0 + ''; + p1 + '" target="_blank">' + p0 + ''; }); // [[blah]] // Short form of gl[blah] str = str.replace(/\[\[([^< ](?:[^<`]*?[^< ])?)\]\]/ig, function(p0, p1) { var q = Tools.escapeHTML(encodeURIComponent(Tools.unescapeHTML(p1))); return '' + p1 +''; + '" target="_blank">' + p1 +''; }); } // __italics__ diff --git a/js/client-battle.js b/js/client-battle.js index 0a52a42d2..b5ad2fcf6 100644 --- a/js/client-battle.js +++ b/js/client-battle.js @@ -133,6 +133,11 @@ this.battle.add('', Tools.prefs('noanim')); this.updateControls(); }, + revealMessages: function(user) { + var $messages = $('.chatmessage-' + user); + $messages.show(); + $messages.find('button').parent().remove(); + }, /********************************************************* * Battle stuff diff --git a/js/client-chat.js b/js/client-chat.js index 9cc92edcd..2d190a8f6 100644 --- a/js/client-chat.js +++ b/js/client-chat.js @@ -953,9 +953,13 @@ case 'unlink': // note: this message has global effects, but it's handled here // so that it can be included in the scrollback buffer. - $('.message-link-' + toId(row[1])).each(function() { - $(this).replaceWith($(this).html()); - }); + var user = toId(row[2]) || toId(row[1]); + var $messages = $('.chatmessage-' + user); + $messages.find('a').contents().unwrap(); + if (row[2]) { + $messages.hide(); + this.$chat.append('
'); + } break; case 'tournament': @@ -975,6 +979,11 @@ } } }, + revealMessages: function(user) { + var $messages = $('.chatmessage-' + user); + $messages.show(); + $messages.find('button').parent().remove(); + }, tournamentButton: function(val, button) { if (this.tournamentBox) this.tournamentBox[$(button).data('type')](val, button); }, @@ -1112,23 +1121,23 @@ } } var highlight = isHighlighted ? ' highlighted' : ''; - var chatDiv = '
'; + var chatDiv = '
'; var timestamp = ChatRoom.getTimestamp('lobby', deltatime); if (name.charAt(0) !== ' ') clickableName = '' + Tools.escapeHTML(name.charAt(0)) + ''+clickableName; var self = this; var outputChat = function() { - self.$chat.append(chatDiv + timestamp + '' + clickableName + ': ' + Tools.parseMessage(message, name) + '
'); + self.$chat.append(chatDiv + timestamp + '' + clickableName + ': ' + Tools.parseMessage(message) + '
'); }; var showme = !((Tools.prefs('chatformatting') || {}).hideme); if (pm) { var pmuserid = toUserid(pm); var oName = pm; if (pmuserid === app.user.get('userid')) oName = name; - this.$chat.append('
' + timestamp + '' + clickableName + ': (Private to ' + Tools.escapeHTML(pm) + ') ' + Tools.parseMessage(message, name) + '
'); + this.$chat.append('
' + timestamp + '' + clickableName + ': (Private to ' + Tools.escapeHTML(pm) + ') ' + Tools.parseMessage(message) + '
'); } else if (message.substr(0,4) === '/me ') { message = message.substr(4); if (showme) { - this.$chat.append(chatDiv + timestamp + '• ' + clickableName + ' ' + Tools.parseMessage(message, name) + ''); + this.$chat.append(chatDiv + timestamp + '• ' + clickableName + ' ' + Tools.parseMessage(message) + ''); } else { outputChat(); } @@ -1136,13 +1145,13 @@ } else if (message.substr(0,5) === '/mee ') { message = message.substr(5); if (showme) { - this.$chat.append(chatDiv + timestamp + '• ' + clickableName + '' + Tools.parseMessage(message, name) + ''); + this.$chat.append(chatDiv + timestamp + '• ' + clickableName + '' + Tools.parseMessage(message) + ''); } else { outputChat(); } Storage.logChat(this.id, '* '+name+message); } else if (message.substr(0,10) === '/announce ') { - this.$chat.append(chatDiv + timestamp + '' + clickableName + ': ' + Tools.parseMessage(message.substr(10), name) + ''); + this.$chat.append(chatDiv + timestamp + '' + clickableName + ': ' + Tools.parseMessage(message.substr(10)) + ''); Storage.logChat(this.id, ''+name+': /announce '+message); } else if (message.substr(0,14) === '/data-pokemon ') { this.$chat.append('
    '+Chart.pokemonRow(Tools.getTemplate(message.substr(14)),'',{})+'
'); diff --git a/js/client-mainmenu.js b/js/client-mainmenu.js index c261f4ed8..27bba6d47 100644 --- a/js/client-mainmenu.js +++ b/js/client-mainmenu.js @@ -140,15 +140,15 @@ if (name.substr(0, 1) !== ' ') clickableName = '' + Tools.escapeHTML(name.substr(0, 1)) + ''+clickableName; if (message.substr(0,4) === '/me ') { - $chat.append('
' + timestamp + '• ' + clickableName + ' ' + Tools.parseMessage(message.substr(4), name) + '
'); + $chat.append('
' + timestamp + '• ' + clickableName + ' ' + Tools.parseMessage(message.substr(4)) + '
'); } else if (message.substr(0,5) === '/mee ') { - $chat.append('
' + timestamp + '• ' + clickableName + '' + Tools.parseMessage(message.substr(5), name) + '
'); + $chat.append('
' + timestamp + '• ' + clickableName + '' + Tools.parseMessage(message.substr(5)) + '
'); } else if (message.substr(0,8) === '/invite ') { var roomid = toRoomid(message.substr(8)); $chat.append('
' + timestamp + '' + clickableName + ' invited you to join the room "'+roomid+'"
'); $chat.append('
'); } else if (message.substr(0,10) === '/announce ') { - $chat.append('
' + timestamp + '' + clickableName + ': ' + Tools.parseMessage(message.substr(10), name) + '
'); + $chat.append('
' + timestamp + '' + clickableName + ': ' + Tools.parseMessage(message.substr(10)) + '
'); } else if (message.substr(0,14) === '/data-pokemon ') { $chat.append('
    '+Chart.pokemonRow(Tools.getTemplate(message.substr(14)),'',{})+'
'); } else if (message.substr(0,11) === '/data-item ') { @@ -164,7 +164,7 @@ } else { // Normal chat message. if (message.substr(0,2) === '//') message = message.substr(1); - $chat.append('
' + timestamp + '' + clickableName + ': ' + Tools.parseMessage(message, name) + '
'); + $chat.append('
' + timestamp + '' + clickableName + ': ' + Tools.parseMessage(message) + '
'); } if (autoscroll) {