From ce3c5eb909fdcde9c4830924d810e666eb718fcc Mon Sep 17 00:00:00 2001 From: lilia Date: Sat, 7 Jun 2014 15:51:51 -0700 Subject: [PATCH] Refactor conversation list view Pull apart UI classes for displaying and creating threads. Also get rid of ugly alert popup in favor of Whisper.notify. --- js/popup.js | 3 ++ .../{messages.js => conversations/index.js} | 39 +--------------- js/views/conversations/new.js | 45 +++++++++++++++++++ .../show.js} | 0 popup.html | 5 ++- 5 files changed, 53 insertions(+), 39 deletions(-) rename js/views/{messages.js => conversations/index.js} (51%) create mode 100644 js/views/conversations/new.js rename js/views/{conversation.js => conversations/show.js} (100%) diff --git a/js/popup.js b/js/popup.js index 9e7ba2644eb8..72fafa62b7ff 100644 --- a/js/popup.js +++ b/js/popup.js @@ -19,6 +19,9 @@ textsecure.registerOnLoadFunction(function() { if (textsecure.storage.getUnencrypted("number_id") === undefined) { extension.navigator.tabs.create("options.html"); } else { + + new Whisper.ConversationListView(); + new Whisper.ConversationComposeView({el: $('body')}); $('.my-number').text(textsecure.storage.getUnencrypted("number_id").split(".")[0]); textsecure.storage.putUnencrypted("unreadCount", 0); extension.navigator.setBadgeText(""); diff --git a/js/views/messages.js b/js/views/conversations/index.js similarity index 51% rename from js/views/messages.js rename to js/views/conversations/index.js index cc7f85ed2db1..d1b125cc75fa 100644 --- a/js/views/messages.js +++ b/js/views/conversations/index.js @@ -3,7 +3,7 @@ var Whisper = Whisper || {}; (function () { 'use strict'; - Whisper.ConversationListView = new (Backbone.View.extend({ // singleton + Whisper.ConversationListView = Backbone.View.extend({ tagName: 'ul', id: 'conversations', @@ -24,41 +24,6 @@ var Whisper = Whisper || {}; Whisper.Messages.fetch(); this.$el.appendTo($('#inbox')); - - $('#send_link').click(function(e) { - $('#send').fadeIn().find('input[type=text]').focus(); - }); - - $('#send').click(function() { - $('#send input[type=text]').focus(); - }); - - $("#compose-cancel").click(function(e) { - $('#send').hide(); - e.preventDefault(); - }); - $("#send").submit((function(e) { - e.preventDefault(); - var numbers = []; - var splitString = $("#send_numbers").val().split(","); - for (var i = 0; i < splitString.length; i++) { - try { - numbers.push(textsecure.utils.verifyNumber(splitString[i])); - } catch (numberError) { - alert(numberError); - } - } - $("#send_numbers").val(''); - numbers = _.filter(numbers, _.identity); // rm undefined, null, "", etc... - if (numbers.length) { - $('#send').hide(); - Whisper.Threads.findOrCreateForRecipient(numbers).trigger('select'); - } else { - Whisper.notify('recipient missing or invalid'); - $('#send input[type=text]').focus(); - } - }).bind(this)); - }, addThread: function(thread) { @@ -74,5 +39,5 @@ var Whisper = Whisper || {}; addMessage: function(message) { message.thread().trigger('message', message); } - }))(); + }); })(); diff --git a/js/views/conversations/new.js b/js/views/conversations/new.js new file mode 100644 index 000000000000..28651e3d1172 --- /dev/null +++ b/js/views/conversations/new.js @@ -0,0 +1,45 @@ +(function () { + 'use strict'; + + Whisper.ConversationComposeView = Backbone.View.extend({ + events : { + 'click #send_link' : 'show_send', + 'click #send' : 'focus_send', + 'click #compose-cancel' : 'hide_send', + 'submit #send' : 'submit_send' + }, + show_send: function(e) { + $('#send').fadeIn().find('input[type=text]').focus(); + }, + focus_send: function(e) { + $('#send input[type=text]').focus(); + }, + hide_send: function(e) { + $('#send').hide(); + e.preventDefault(); + }, + submit_send: function(e) { + e.preventDefault(); + var numbers = []; + var splitString = $("#send_numbers").val().split(","); + for (var i = 0; i < splitString.length; i++) { + try { + numbers.push(textsecure.utils.verifyNumber(splitString[i])); + } catch (numberError) { + if (!numberError.countryCodeValid) { + Whisper.notify('Invalid country code'); + } + if (!numberError.numberValid) { + Whisper.notify('Invalid number'); + } + $('#send input[type=text]').focus(); + return; + } + } + $("#send_numbers").val(''); + $('#send').hide(); + Whisper.Threads.findOrCreateForRecipient(numbers).trigger('select'); + } + }); +})(); + diff --git a/js/views/conversation.js b/js/views/conversations/show.js similarity index 100% rename from js/views/conversation.js rename to js/views/conversations/show.js diff --git a/popup.html b/popup.html index a36e78a381db..cc7ba8d18337 100644 --- a/popup.html +++ b/popup.html @@ -66,8 +66,9 @@ - - + + +