meshenger/web/main.js

326 lines
8.4 KiB
JavaScript
Raw Normal View History

2014-11-07 01:40:19 +01:00
localStorage.clear();
2014-11-07 16:21:10 +01:00
//These need to be obtained from the node
var ownId, ownColor, ownAlias;
2014-11-07 16:21:10 +01:00
2015-05-19 20:58:43 +02:00
/*
* INIT
*/
document.addEventListener('DOMContentLoaded', function(){
function update(){
if ( !ownId ){
getOwnId();
}
if ( !ownAlias){
getOwnAlias();
}
checkInbox();
// also check for outbox items on interval,
// necessary in case connection is lost and messages are not yet sent
checkOutbox();
}
2015-05-19 23:41:50 +02:00
2015-05-19 23:13:41 +02:00
document.getElementById('message-form').onsubmit = onSubmitMessage;
2015-05-19 20:58:43 +02:00
//update everything to initialize
updateInboxView();
update();
//check for new messages every 7 seconds
window.setInterval( update, 7000 );
2015-05-19 21:07:49 +02:00
initState();
initPhotoStuff(); //all photostuff is found in photostuff.js
2015-05-19 20:58:43 +02:00
});
/*
* STATE CHANGES
*/
2015-05-19 21:07:49 +02:00
function initState(){
2015-05-19 21:36:23 +02:00
showOverview();
2015-05-19 21:07:49 +02:00
2015-05-19 21:36:23 +02:00
document.getElementById('new-photo').onclick = showNewPhoto;
document.getElementById('new-message').onclick = showNewMessage;
document.getElementById('message-back').onclick = showOverview;
document.getElementById('photo-back').onclick = showOverview;
2015-05-19 20:58:43 +02:00
}
2015-05-19 21:36:23 +02:00
function showNewPhoto(){
2015-05-19 20:58:43 +02:00
document.getElementById('photo-page').style.display = "block";
document.getElementById('overview-page').style.display = "none";
document.getElementById('message-page').style.display = "none";
2015-05-19 21:56:01 +02:00
document.getElementById('fileInput').click();
2015-05-19 20:58:43 +02:00
}
2015-05-19 21:36:23 +02:00
function showNewMessage(){
2015-05-19 20:58:43 +02:00
document.getElementById('photo-page').style.display = "none";
document.getElementById('overview-page').style.display = "none";
document.getElementById('message-page').style.display = "block";
}
2015-05-19 21:36:23 +02:00
function showOverview(){
2015-05-19 20:58:43 +02:00
document.getElementById('photo-page').style.display = "none";
document.getElementById('overview-page').style.display = "block";
document.getElementById('message-page').style.display = "none";
}
2014-11-07 01:40:19 +01:00
/*
* OUTBOX STUFF
*/
2015-05-19 23:13:41 +02:00
function onSubmitMessage(){
var msg = document.getElementById('message').value.replace(/\r?\n/g, "<br />");
if ( !msg || msg === "" ) { /* prevent sending of empty messages */
return false;
}
2015-05-20 01:36:01 +02:00
var namm = document.getElementById('name').value;
2015-05-22 17:56:48 +02:00
if ( !namm || namm === "" || namm.length > 20) { /* prevent too long usernames */
2015-05-20 01:36:01 +02:00
namm = "anonymous";
}
addOutboxItem( namm, "<p class='text-message'>"+msg+"</p>" );
2015-05-20 01:36:01 +02:00
return false;
2015-05-19 23:41:50 +02:00
}
2015-05-19 23:13:41 +02:00
2015-05-19 23:41:50 +02:00
function addOutboxItem( namm, message ){
2015-05-19 23:13:41 +02:00
var outStr = localStorage.getItem( 'outbox' ) || '';
2014-11-07 16:21:10 +01:00
var newMsgs = {};
var ddata = new Date().getTime();
2015-05-19 23:13:41 +02:00
var alias = ownAlias;
2014-11-07 16:21:10 +01:00
var contento = {
"time" : ddata,
2015-05-19 23:41:50 +02:00
"message" : message,
2014-11-07 16:21:10 +01:00
"name" : namm,
"node" : "local",
"alias": alias,
2014-11-07 16:21:10 +01:00
"hops" : "0"
2015-05-19 21:36:23 +02:00
};
2014-11-07 16:21:10 +01:00
newMsgs.message = contento;
2015-05-19 21:36:23 +02:00
localStorage.setItem( 'outbox', JSON.stringify(newMsgs) );
2014-11-07 16:21:10 +01:00
checkOutbox();
document.getElementById('message').value = '';
2015-05-19 21:36:23 +02:00
showOverview();
2015-05-19 23:13:41 +02:00
}
2014-11-07 01:40:19 +01:00
function checkOutbox() {
2014-11-07 16:21:10 +01:00
var outStr = localStorage.getItem( 'outbox' );
2015-05-19 20:24:39 +02:00
if ( ! outStr ) {
return;
}
var lines = outStr.split( /\n/ );
for ( var i in lines ) {
if ( lines[i].length === 0 ) {
continue;
}
var obj = JSON.parse(lines[i]);
var ts = obj.message.time;
delete obj.message.time;
var msg = JSON.stringify(obj.message);
sendMessage( ts, msg );
}
2014-11-07 01:40:19 +01:00
}
function sendMessage( timestamp, message ) {
2015-05-19 20:24:39 +02:00
var xhr = new XMLHttpRequest();
var data = 'time=' + encodeURIComponent( timestamp ) +
'&message=' + encodeURIComponent( message );
xhr.onreadystatechange = function(){
if ( xhr.readyState == 4){
if ( xhr.status == 200 ) {
removeOutboxItem( timestamp );
}
}
};
xhr.open('POST', 'send', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2005 00:00:00 GMT');
xhr.send(data);
2014-11-07 01:40:19 +01:00
}
function removeOutboxItem( timestamp ) {
2015-05-19 20:24:39 +02:00
var outStr = localStorage.getItem( 'outbox' ) || '';
var lines = outStr.split( /\n/ );
for ( var i in lines ) {
2014-11-07 01:40:19 +01:00
var obj = JSON.parse(lines[i]);
2015-05-19 20:24:39 +02:00
var ts = obj.message.time;
if ( ts === timestamp ) {
lines.splice( i, 1 );
break;
}
}
var newOutStr = lines.join('\n');
localStorage.setItem('outbox', newOutStr);
2014-11-07 01:40:19 +01:00
}
/*
* INBOX STUFF
*/
function updateInboxView() {
2015-05-19 23:41:50 +02:00
var localStorageArray = [];
2014-11-07 16:21:10 +01:00
var contentString = '';
2014-11-07 01:40:19 +01:00
2014-11-07 16:21:10 +01:00
if (localStorage.length>0) {
for (i=0;i<localStorage.length;i++){
2014-11-07 01:40:19 +01:00
2014-11-07 16:21:10 +01:00
element=localStorage.getItem(localStorage.key(i));
if ( localStorage.key(i).length < 10 || element === 'outbox' ) {
continue;
}
try {
2014-11-07 01:40:19 +01:00
elementj = JSON.parse(element);
2014-11-07 16:21:10 +01:00
} catch (e) {
2014-11-07 01:40:19 +01:00
continue;
2014-11-07 16:21:10 +01:00
}
2014-11-07 01:40:19 +01:00
2014-11-07 16:21:10 +01:00
localStorageArray[i] = {
time:localStorage.key(i),
user:elementj.name,
message:elementj.message,
node:elementj.node,
hops:elementj.hops,
alias:elementj.alias
2014-11-07 16:21:10 +01:00
};
}
}
2015-05-19 23:41:50 +02:00
orderStorage = localStorageArray.sort(function(a,b) { return b.time - a.time; } );
2014-11-07 16:21:10 +01:00
for(var i in orderStorage) {
if ( i.length === 0 || i === 'outbox' ) {
continue;
}
2014-11-07 16:21:10 +01:00
var datereadable = getReadableDate( new Date(parseInt(orderStorage[i].time)) );
var color = getNodeColor( orderStorage[i].node );
contentString += '<li><div class="message-block" style="background-color:'+color+'">'+
'<div class="date-sender">On ' + datereadable +
2015-05-21 17:17:31 +02:00
' <b>' + parseEmoticons(orderStorage[i].user) +'</b> wrote:</div>' +
'<div class="message-text">' + parseEmoticons( orderStorage[i].message ) + '</div>' + //parseEmoticons is found in emoji.js
' <div class="nodehops"><div class="node '+orderStorage[i].node+'">from '+orderStorage[i].alias + '</div>' +
' <div class="hops '+orderStorage[i].hops+'">via '+orderStorage[i].hops+' nodes</div></div></div></li>';
2014-11-07 16:21:10 +01:00
}
document.getElementById( 'inbox' ).innerHTML = contentString;
}
2014-11-07 16:21:10 +01:00
function getReadableDate( date ) {
var day = date.getDate();
if (day < 10) day = '0' + day;
var month = date.getMonth()+1;
if (month < 10) month = '0' + month;
var year = date.getFullYear();
var hrs = date.getHours();
if (hrs < 10) hrs = '0' + hrs;
var min = date.getMinutes();
if (min < 10) min = '0' + min;
return day + '/' + month + '/' + year + ' ' + hrs + ':' + min;
}
function getNodeColor( nodeId ) {
if( nodeId === 'local' ) {
return ownColor || '#fff';
}
return colorLuminance(nodeId.substr(0,6), 0.5);
}
function colorLuminance(hex, lum) {
2015-05-19 20:24:39 +02:00
// validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '');
if (hex.length < 6) {
hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
}
lum = lum || 0;
// convert to decimal and change luminosity
var rgb = "#", c, i;
for (i = 0; i < 3; i++) {
c = parseInt(hex.substr(i*2,2), 16);
c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
rgb += ("00"+c).substr(c.length);
}
return rgb;
2014-11-07 01:40:19 +01:00
}
function onMessageDownload( msg, filename ) {
2015-05-19 20:24:39 +02:00
if ( localStorage.getItem( filename ) === null ) {
localStorage.setItem( filename, msg );
}
updateInboxView();
2014-11-07 01:40:19 +01:00
}
function onIndex( index ) {
2015-05-19 23:41:50 +02:00
var lines = index.split( /\n/ );
var k,i,l,f;
for( k in localStorage){
l = 1;
for ( i in lines ) {
f = lines[i];
2015-05-19 20:24:39 +02:00
if (f == k){ l = 0; }
}
2015-05-19 23:41:50 +02:00
if (l == 1){
localStorage.removeItem(k);
}
2015-05-19 20:24:39 +02:00
}
2015-05-19 23:41:50 +02:00
updateInboxView();
2014-11-07 01:40:19 +01:00
2015-05-19 23:41:50 +02:00
for ( i in lines ) {
2015-05-19 20:24:39 +02:00
var fname = lines[i];
if ( localStorage.getItem( fname ) === null ) {
//localStorage.setItem( ts, lines[i].substr(lines[i].indexOf(' ')) );
downloadMessage( fname );
}
2014-11-07 01:40:19 +01:00
2015-05-19 20:24:39 +02:00
}
2014-11-07 01:40:19 +01:00
}
function downloadMessage(filename) {
2014-11-07 16:21:10 +01:00
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (xhr.readyState == 4 && xhr.status == 200){
onMessageDownload( xhr.responseText, filename );
}
2015-05-19 23:13:41 +02:00
};
2014-11-07 16:21:10 +01:00
xhr.open( "GET", 'msg/'+filename, true);
xhr.send();
2014-11-07 01:40:19 +01:00
}
function checkInbox() {
2014-11-07 16:21:10 +01:00
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (xhr.readyState == 4 && xhr.status == 200){
onIndex( xhr.responseText );
}
2015-05-19 23:13:41 +02:00
};
2014-11-07 16:21:10 +01:00
xhr.open( "GET", 'index', true);
xhr.send();
}
function getOwnId() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (xhr.readyState == 4 && xhr.status == 200){
ownId = xhr.responseText;
ownColor = getNodeColor( ownId );
2014-11-07 01:40:19 +01:00
}
2015-05-19 23:13:41 +02:00
};
2014-11-07 16:21:10 +01:00
xhr.open( "GET", 'id', true);
xhr.send();
2014-11-07 01:40:19 +01:00
}
function getOwnAlias() {
2015-05-19 23:41:50 +02:00
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (xhr.readyState == 4 && xhr.status == 200){
ownAlias = xhr.responseText;
}
};
xhr.open( "GET", 'alias', true);
xhr.send();
2015-05-19 20:24:39 +02:00
}