signal-desktop/js/popup.js
lilia 68027dadd4 Fix click handler
var i was being bound in the closure of this click handler, then
incremented by the for loop, such that its value was 1 by the time the
handler was called, so we were grabbing the message body from, e.g.
$("#input1") when we wanted $("#input0").
2014-04-08 23:03:34 -07:00

96 lines
3.1 KiB
JavaScript

$('#inbox_link').click(function() {
$('#inbox').show();
$('#send').hide();
});
$('#send_link').click(function() {
$('#inbox').hide();
$('#send').show();
});
registerOnLoadFunction(function() {
if (storage.getUnencrypted("number_id") === undefined) {
chrome.tabs.create({url: "options.html"});
} else {
function fillMessages() {
var MAX_MESSAGES_PER_CONVERSATION = 4;
var MAX_CONVERSATIONS = 5;
var conversations = [];
var messageMap = getMessageMap();
for (conversation in messageMap) {
var messages = messageMap[conversation];
messages.sort(function(a, b) { return b.timestamp - a.timestamp; });
conversations[conversations.length] = messages;
}
conversations.sort(function(a, b) { return b[0].timestamp - a[0].timestamp });
var ul = $('#conversations');
ul.html('');
for (var i = 0; i < MAX_CONVERSATIONS && i < conversations.length; i++) {
var conversation = conversations[i];
var messages = $('<ul class="conversation">');
for (var j = 0; j < MAX_MESSAGES_PER_CONVERSATION && j < conversation.length; j++) {
var message = conversation[j];
$('<li class="message incoming container">').
append($('<div class="avatar">')).
append($('<div class="bubble">').
append($('<span class="message-text">').text(message.message)).
append($('<span class="metadata">').text("From: " + message.sender + ", at: " + timestampToHumanReadable(message.timestamp)))
).appendTo(messages);
}
var button = $('<button id="button' + i + '">').text('Send');
var input = $('<input id="text' + i + '">');
$('<li>').
append(messages).
append($("<form class='container'>").append(input).append(button)).
appendTo(ul);
button.click(function() {
var sendDestinations = [conversation[0].sender];
if (conversation[0].group) {
sendDestinations = conversation[0].group.members;
}
var messageProto = new PushMessageContentProtobuf();
messageProto.body = input.val();
sendMessageToNumbers(sendDestinations, messageProto, function(result) {
console.log("Sent message: " + result);
});
});
}
}
$(window).bind('storage', function(e) {
console.log("Got localStorage update for key " + e.key);
if (event.key == "emessageMap")//TODO: Fix when we get actual encryption
fillMessages();
});
fillMessages();
$('.my-number').text(storage.getUnencrypted("number_id").split(".")[0]);
storage.putUnencrypted("unreadCount", 0);
chrome.browserAction.setBadgeText({text: ""});
$("#me").click(function() {
$('#popup_send_numbers').val($('.my-number').text());
});
$("#popup_send_button").click(function() {
var numbers = [];
var splitString = $("#popup_send_numbers").val().split(",");
for (var i = 0; i < splitString.length; i++) {
try {
numbers.push(verifyNumber(splitString[i]));
} catch (numberError) {
//TODO
alert(numberError);
}
}
var messageProto = new PushMessageContentProtobuf();
messageProto.body = $("#popup_send_text").val();
sendMessageToNumbers(numbers, messageProto,
//TODO: Handle result
function(thing) {console.log(thing);});
});
}
});