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
|
|
|
|
2014-04-06 08:59:22 +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];
|
2014-04-06 08:59:22 +00:00
|
|
|
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];
|
2014-04-09 05:52:56 +00:00
|
|
|
$('<li class="message incoming container">').
|
2014-04-06 08:59:22 +00:00
|
|
|
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
|
|
|
}
|
2014-04-09 05:52:56 +00:00
|
|
|
var button = $('<button id="button' + i + '">').text('Send');
|
|
|
|
var input = $('<input id="text' + i + '">');
|
2014-04-06 08:59:22 +00:00
|
|
|
$('<li>').
|
|
|
|
append(messages).
|
2014-04-09 05:52:56 +00:00
|
|
|
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];
|
2014-04-06 21:13:27 +00:00
|
|
|
if (conversation[0].group) {
|
|
|
|
sendDestinations = conversation[0].group.members;
|
|
|
|
}
|
2014-03-25 19:27:19 +00:00
|
|
|
|
|
|
|
var messageProto = new PushMessageContentProtobuf();
|
2014-04-09 05:52:56 +00:00
|
|
|
messageProto.body = input.val();
|
2014-03-25 19:27:19 +00:00
|
|
|
|
|
|
|
sendMessageToNumbers(sendDestinations, messageProto, function(result) {
|
|
|
|
console.log("Sent message: " + result);
|
2014-01-22 06:23:41 +00:00
|
|
|
});
|
2014-01-13 01:52:58 +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();
|
2014-04-02 08:52:29 +00:00
|
|
|
$('.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: ""});
|
2014-04-02 08:52:29 +00:00
|
|
|
$("#me").click(function() {
|
|
|
|
$('#popup_send_numbers').val($('.my-number').text());
|
|
|
|
});
|
2014-03-25 19:27:19 +00:00
|
|
|
|
|
|
|
$("#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();
|
2014-03-25 19:27:19 +00:00
|
|
|
sendMessageToNumbers(numbers, messageProto,
|
|
|
|
//TODO: Handle result
|
|
|
|
function(thing) {console.log(thing);});
|
|
|
|
});
|
2014-01-22 06:23:41 +00:00
|
|
|
}
|
|
|
|
});
|