|
@ -1,10 +1,15 @@ |
|
|
localStorage.clear(); |
|
|
localStorage.clear(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//These need to be obtained from the node
|
|
|
|
|
|
var ownId, ownColor; |
|
|
|
|
|
|
|
|
/* |
|
|
/* |
|
|
* OUTBOX STUFF |
|
|
* OUTBOX STUFF |
|
|
*/ |
|
|
*/ |
|
|
document.getElementById( 'send' ).onclick = function() { |
|
|
document.getElementById('message-form').onsubmit = function(){ |
|
|
|
|
|
|
|
|
var outStr = localStorage.getItem( 'outbox' ) || ''; |
|
|
var outStr = localStorage.getItem( 'outbox' ) || ''; |
|
|
|
|
|
|
|
|
if (document.getElementById('name').value == ""){ |
|
|
if (document.getElementById('name').value == ""){ |
|
|
var namm = "anonymous"; |
|
|
var namm = "anonymous"; |
|
|
} |
|
|
} |
|
@ -27,7 +32,10 @@ document.getElementById( 'send' ).onclick = function() { |
|
|
updateOutboxView(); |
|
|
updateOutboxView(); |
|
|
checkOutbox(); |
|
|
checkOutbox(); |
|
|
document.getElementById('message').value = ''; |
|
|
document.getElementById('message').value = ''; |
|
|
}; |
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function checkOutbox() { |
|
|
function checkOutbox() { |
|
|
var outStr = localStorage.getItem( 'outbox' ); |
|
|
var outStr = localStorage.getItem( 'outbox' ); |
|
|
if ( ! outStr ) { |
|
|
if ( ! outStr ) { |
|
@ -113,30 +121,76 @@ function updateInboxView() { |
|
|
if ( localStorage.key(i).length < 10 || element === 'outbox' ) { |
|
|
if ( localStorage.key(i).length < 10 || element === 'outbox' ) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
// alert(element);
|
|
|
|
|
|
try { |
|
|
try { |
|
|
elementj = JSON.parse(element); |
|
|
elementj = JSON.parse(element); |
|
|
} catch (e) { |
|
|
} catch (e) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
localStorageArray[i] = { time:localStorage.key(i), user:elementj.name, message:elementj.message, node:elementj.node, hops:elementj.hops }; |
|
|
localStorageArray[i] = { |
|
|
|
|
|
time:localStorage.key(i), |
|
|
|
|
|
user:elementj.name, |
|
|
|
|
|
message:elementj.message, |
|
|
|
|
|
node:elementj.node, |
|
|
|
|
|
hops:elementj.hops |
|
|
|
|
|
}; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
orderStorage = localStorageArray.sort(function(a,b) { return b.time - a.time } ); |
|
|
orderStorage = localStorageArray.sort(function(a,b) { return b.time - a.time } ); |
|
|
|
|
|
|
|
|
for(var i in orderStorage) |
|
|
for(var i in orderStorage) { |
|
|
{ |
|
|
|
|
|
if ( i.length === 0 || i === 'outbox' ) { |
|
|
if ( i.length === 0 || i === 'outbox' ) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
var date = new Date(parseInt(orderStorage[i].time)); |
|
|
var datereadable = getReadableDate( new Date(parseInt(orderStorage[i].time)) ); |
|
|
// date.setHours(date.getHours() + 2);
|
|
|
var color = getNodeColor( orderStorage[i].node ); |
|
|
var datereadable = date.getDate()+"/"+(date.getMonth()+1)+"/"+date.getFullYear()+" "+date.getHours()+":"+date.getMinutes(); |
|
|
contentString += '<li><div class="message-block" style="background-color:'+color+'">'+ |
|
|
contentString += '<li><b>' + datereadable + ' </b>' + ' <i>'+ orderStorage[i].user +'</i><br/> '+orderStorage[i].message+' <span class="node '+orderStorage[i].node+'">'+orderStorage[i].node+'</span> <span class="hops '+orderStorage[i].hops+'">'+orderStorage[i].hops+'</span></li>'; |
|
|
'<div class="date-sender">On ' + datereadable + |
|
|
|
|
|
' <b>' + orderStorage[i].user +'</b> wrote:</div>' + |
|
|
|
|
|
'<div class="message-text">' + orderStorage[i].message + '</div>' + |
|
|
|
|
|
' <span class="node '+orderStorage[i].node+'">from '+orderStorage[i].node + '</span>' + |
|
|
|
|
|
' <span class="hops '+orderStorage[i].hops+'">via '+orderStorage[i].hops+' nodes</span></div></li>'; |
|
|
} |
|
|
} |
|
|
document.getElementById( 'inbox' ).innerHTML = contentString; |
|
|
document.getElementById( 'inbox' ).innerHTML = contentString; |
|
|
} |
|
|
} |
|
|
|
|
|
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) { |
|
|
|
|
|
|
|
|
|
|
|
// 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; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
function onMessageDownload( msg, filename ) { |
|
|
function onMessageDownload( msg, filename ) { |
|
|
if ( localStorage.getItem( filename ) === null ) { |
|
|
if ( localStorage.getItem( filename ) === null ) { |
|
@ -190,15 +244,38 @@ function checkInbox() { |
|
|
xhr.open( "GET", 'index', true); |
|
|
xhr.open( "GET", 'index', true); |
|
|
xhr.send(); |
|
|
xhr.send(); |
|
|
} |
|
|
} |
|
|
|
|
|
function getOwnId() { |
|
|
|
|
|
var xhr = new XMLHttpRequest(); |
|
|
|
|
|
xhr.onreadystatechange = function(){ |
|
|
|
|
|
if (xhr.readyState == 4 && xhr.status == 200){ |
|
|
|
|
|
ownId = xhr.responseText; |
|
|
|
|
|
ownColor = getNodeColor( ownId ); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
xhr.open( "GET", 'id', true); |
|
|
|
|
|
xhr.send(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
/* |
|
|
* INIT |
|
|
* INIT |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
updateInboxView(); |
|
|
function update(){ |
|
|
updateOutboxView(); |
|
|
if ( !ownId ){ |
|
|
window.setInterval( function(){ |
|
|
getOwnId(); |
|
|
|
|
|
} |
|
|
checkInbox(); |
|
|
checkInbox(); |
|
|
|
|
|
// also check for outbox items on interval,
|
|
|
|
|
|
// necessary in case connection is lost and messages are not yet sent
|
|
|
checkOutbox(); |
|
|
checkOutbox(); |
|
|
}, 7000 ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//update everything to initialize
|
|
|
|
|
|
updateInboxView(); |
|
|
|
|
|
updateOutboxView(); |
|
|
|
|
|
update(); |
|
|
|
|
|
|
|
|
|
|
|
//check for new messages every 7 seconds
|
|
|
|
|
|
window.setInterval( update, 7000 ); |
|
|
|
|
|
|
|
|