signal-desktop/js/popup.js

97 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-03-11 05:52:18 +00:00
$('#inbox_link').click(function() {
2014-01-12 14:07:13 +00:00
$('#inbox').show();
$('#send').hide();
2014-03-11 05:52:18 +00:00
});
$('#send_link').click(function() {
2014-01-12 14:07:13 +00:00
$('#inbox').hide();
$('#send').show();
2014-03-11 05:52:18 +00:00
});
2014-01-12 07:32:13 +00:00
2014-01-22 06:23:41 +00:00
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;
2014-01-12 14:07:13 +00:00
2014-01-22 06:23:41 +00:00
var conversations = [];
2014-01-12 14:07:13 +00:00
2014-01-22 06:23:41 +00:00
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;
}
2014-01-12 14:07:13 +00:00
2014-01-22 06:23:41 +00:00
conversations.sort(function(a, b) { return b[0].timestamp - a[0].timestamp });
2014-01-12 14:07:13 +00:00
var ul = $('#conversations');
2014-01-22 06:23:41 +00:00
ul.html('');
for (var i = 0; i < MAX_CONVERSATIONS && i < conversations.length; i++) {
var conversation = conversations[i];
var messages = $('<ul class="conversation">');
2014-01-22 06:23:41 +00:00
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);
2014-01-22 06:23:41 +00:00
}
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() {
2014-01-22 06:23:41 +00:00
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);
2014-01-22 06:23:41 +00:00
});
});
2014-01-22 06:23:41 +00:00
}
2014-01-12 14:07:13 +00:00
}
2014-01-22 06:23:41 +00:00
$(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]);
2014-01-22 06:23:41 +00:00
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();
2014-03-26 19:05:09 +00:00
messageProto.body = $("#popup_send_text").val();
sendMessageToNumbers(numbers, messageProto,
//TODO: Handle result
function(thing) {console.log(thing);});
});
2014-01-22 06:23:41 +00:00
}
});