diff --git a/src/js/main.js b/src/js/main.js index cd59adf..ef0b60e 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -23,8 +23,6 @@ $(function() { var $inputMessage = $('.inputMessage'); // Input message input box var $key = $('.key'); var $genKey = $('.new_key'); - var $removeKey = $('#remove_key'); - var encryptionEnabled; var $participants = $('#participants'); var $chatPage = $('.chat.page'); // The chatroom page @@ -37,6 +35,7 @@ $(function() { var typing = false; var lastTypingTime; var $currentInput = $usernameInput.focus(); + var encryptionKey; var clipboard = new Clipboard('.copyable'); @@ -79,6 +78,9 @@ $(function() { // Sends a chat message function sendMessage () { + // Don't allow sending if key is empty + if (!encryptionKey.trim().length) return; + var message = $inputMessage.val(); // Prevent markup from being injected into the message message = cleanInput(message); @@ -244,8 +246,13 @@ $(function() { } // If enter is pressed on key input then close key modal - if (event.which === 13 && $('input#key').is(':focus')) { - $('#key-modal').modal('hide'); + if (event.which === 13 && $('#join-modal input').is(':focus')) { + checkJoinKey(); + } + + // If enter is pressed on edit key input + if (event.which === 13 && $('#settings-modal .edit-key input.key').is(':focus')) { + saveKey(); } }); @@ -260,12 +267,7 @@ $(function() { $genKey.click(function () { var key = (Math.random() * Math.random()).toString(36).substring(7); - $key.val(key); - keyInputChanged(key); - }); - - $removeKey.click(function() { - $key.val(''); + updateKeyVal(key); }); // Socket events @@ -282,17 +284,14 @@ $(function() { if (data.numUsers > 1) { $('#join-modal').modal('show'); key = ''; - encryptionEnabled = false; - $('.chat .warning-sign').show(); - } else { - encryptionEnabled = true; - $('.chat .warning-sign').hide(); } - $key.val(key); + updateKeyVal(key); }); // Whenever the server emits 'new message', update the chat body socket.on('new message', function (data) { + // Don't show messages if no key + if (!isActive) { newMessages++; favicon.badge(newMessages); @@ -337,10 +336,6 @@ $(function() { setUsername(); - $('span.key-btn').click(function() { - $('#settings-modal').modal('show'); - }); - window.onfocus = function () { isActive = true; newMessages = 0; @@ -390,44 +385,17 @@ $(function() { $('[data-toggle="tooltip"]').tooltip(); - function keyInputChanged(val) { + function joinKeyInputChanged(val) { if (!val.trim().length) { - encryptionEnabled = false; - $('.modal-footer button.encryption-inactive').show(); - $('.modal-footer button.encryption-active').hide(); - $('.chat .warning-sign').show(); - $('.inputMessage').addClass('encryption-disabled'); + $('#join-modal .modal-footer button').attr('disabled', 'disabled'); } else { - encryptionEnabled = true; - $('.modal-footer button.encryption-active').show(); - $('.modal-footer button.encryption-inactive').hide(); - $('.chat .warning-sign').hide(); - $('.inputMessage').removeClass('encryption-disabled'); + $('#join-modal .modal-footer button').removeAttr('disabled'); } } - $('.key').on('input propertychange paste change', function() { + $('#join-modal .key').on('input propertychange paste change', function() { var val = $(this).val(); - $('.key').val(val); - keyInputChanged(val); - }); - - $('.modal-footer button.encryption-inactive').click(function() { - var n = noty({ - text: 'Encryption is OFF. Anyone with this URL can read your messages. Turn on encryption in Settings.', - theme: 'relax', - type: 'error', - timeout: 5000 - }); - }); - - $('.chat .warning-sign').click(function() { - var n = noty({ - text: 'Encryption is OFF. Anyone with this URL can read your messages. Turn on encryption in Settings.', - theme: 'relax', - type: 'error', - timeout: 5000 - }); + joinKeyInputChanged(val); }); $('.navbar .participants').click(function() { @@ -450,4 +418,49 @@ $(function() { }); } + function updateKeyVal(val) { + $('.key').val(val); + $('.key').text(val); + encryptionKey = val; + } + + // Prevent closing join-modal + $('#join-modal').modal({ + backdrop: 'static', + show: false + }); + + $('.read-key').click(function() { + $('.edit-key').show(); + $('.edit-key input').focus(); + $(this).hide(); + }); + + $('.edit-key #cancel-key-edit').click(function() { + $('.edit-key').hide(); + $('.read-key').show(); + updateKeyVal(encryptionKey); + }); + + $('.edit-key #save-key-edit').click(function() { + saveKey(); + }); + + function saveKey() { + $('.edit-key').hide(); + $('.read-key').show(); + updateKeyVal($('.edit-key input.key').val()); + } + + $('#join-modal .modal-footer button').click(function() { + checkJoinKey(); + }); + + function checkJoinKey() { + var key = $('#join-modal input').val().trim(); + if (!key.length) return; + updateKeyVal(key); + $('#join-modal').modal('hide'); + } + }); diff --git a/src/public/favicon.ico b/src/public/favicon.ico index d49da2f..2f7fbc5 100644 Binary files a/src/public/favicon.ico and b/src/public/favicon.ico differ diff --git a/src/public/style.css b/src/public/style.css index ecb2e98..db06e25 100644 --- a/src/public/style.css +++ b/src/public/style.css @@ -41,6 +41,10 @@ p { font-weight: 700; } +.italic { + font-style: italic; +} + /* Pages */ .pages { @@ -167,14 +171,6 @@ input { display: none; } -#first-modal .modal-footer button.encryption-inactive { - display: none; -} - -#join-modal .modal-footer button.encryption-active { - display: none; -} - .chat #input-icons { position: absolute; bottom: 15px; @@ -223,4 +219,18 @@ input { #participants-modal ul.users li .you { color: #333; font-weight: 400; -} \ No newline at end of file +} + +.read-key { + cursor: pointer; +} + +.read-key span.key { + border-bottom: 1px solid #333; +} + +.edit-key { + display: none; +} + + diff --git a/src/views/index.mustache b/src/views/index.mustache index aecc3f8..52f02fc 100644 --- a/src/views/index.mustache +++ b/src/views/index.mustache @@ -88,18 +88,22 @@ @@ -144,26 +135,14 @@