var displayAddress = "F6LUnwRRjFuEW97Y4av31eLqqVMK9FrgE2";
var floidToOnion = {};
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
if (!window.indexedDB) {
window.alert("Your browser doesn't support a stable version of IndexedDB.")
}
function convertStringToInt(string){
return parseInt(string,10);
}
let ajax = function (uri, params, req_type, callback) {
let url = `https://livenet.flocha.in/${uri}`;
let response = {};
var http = new XMLHttpRequest();
http.open(req_type, url, true);
http.onreadystatechange = function () {
if (http.readyState == 4 && http.status == 200) {
response.success = true;
response.data = http.responseText;
callback(response.data);
} else {
response.success = false;
}
}
http.send(params);
}
function getTotalPages(address){
var uri = "api/txs/?address="+address;
try {
let res = ajax(uri, null, 'GET', function (response) {
try {
let data = JSON.parse(response);
getTransactionsByPage(address,convertStringToInt(data["pagesTotal"]+''));
} catch (error) {
console.log(error);
}
});
} catch (error) {
console.error(error);
}
}
function getTransactionsByPage(address,totalPages){
var cnt = 0;
for(var i=0;i/g, ">");
//console.log(msg);
var message = buildMessageReceived(msg,moment().format('h:mm A'));
if(msgArray[2] === recipientId){
conversation.appendChild(message);
//add blue tick to the sender of received message
}
else{
//implement badge unread message
var contactListElement = document.getElementById('contact-list');
//console.log(typeof contactListElement.innerHTML);
var items = contactListElement.getElementsByTagName('li');
var itemsLen = items.length;
for(var i=0;i/g, ">");
//console.log(input.value);
var message = buildUnSentMessage(input.value,moment().format('h:mm A')); //Need to change span tag of build
console.log(message);
console.log("APPENDED",message);
conversation.appendChild(message);
//animateMessage(message);
console.log("Network Status",isNetwork);
temp_input = input.value;
if(isNetwork === 0){
console.log("Network Status Offline");
addUnsentChat(temp_input,timing,recipientId);
input.value = '';
conversation.scrollTop = conversation.scrollHeight;
e.preventDefault();
return;
}
//websocket.send(input.value);
/*recipient_websocket.onopen = function(event){
recipient_websocket.send(recipientId+" "+floId+" "+temp_input);
//recipient_websocket.close();
}
recipient_websocket.onerror = function(event){
console.log("Message Not Sent To Recipient!Try Again!");
}*/
recursion_called = 0;
sendMessage(recipientId+" "+floId+" "+temp_input,timing,message);
}
input.value = '';
conversation.scrollTop = conversation.scrollHeight;
e.preventDefault();
}
function sendMessage(msg,timer,message){
// Wait until the state of the socket is not ready and send the message when it is...
var msgArray = msg.split(' ');
var ws,send_check = 0;
console.log('check');
if(msgArray[0] === "id2")
ws = new WebSocket("ws://"+floidToOnion[msgArray[0]]+":8000/ws");
else
ws = new WebSocket("ws://"+floidToOnion[msgArray[0]]+"/ws");
ws.onopen = function(evt){
console.log('open');
ws.send(msg);
send_check = 1;
recursion_called = 0;
addSentChat(msg.substring(2+msgArray[0].length+msgArray[1].length),timer,msgArray[0]);
addTick(message);
}
ws.onclose = function(evt){
console.log("connection closed");
if(network === 1 && send_check === 0 && recursion_called <= 5){
recursion_called++;
sendMessage(msg,timer,message);
return;
}
addUnsentChat(msg.substring(2+msgArray[0].length+msgArray[1].length),timer,msgArray[0]);
}
ws.onerror = function(evt){
console.log('error');
if(isNetwork === 1 && send_check === 0 && recursion_called <= 5){
recursion_called++;
sendMessage(msg,timer,message);
return;
}
addUnsentChat(msg.substring(2+msgArray[0].length+msgArray[1].length),timer,msgArray[0]);
//conversation.innerHTML = "";
//readFromDb(msgArray[0]);
}
}
function buildUnSentMessage(text,time) {
var element = document.createElement('div');
timing = time;
element.classList.add('message', 'sent');
element.innerHTML = text +
'' +
'' + time + '' + '';
return element;
}
function buildMessageSent(text,time) {
var element = document.createElement('div');
timing = time;
element.classList.add('message', 'sent');
element.innerHTML = text +
'' +
'' + time + '' +
'' +
'' +
'' +
'' +
'';
return element;
}
function buildMessageReceived(text,time) {
var element = document.createElement('div');
timing = time;
element.classList.add('message', 'received');
element.innerHTML = text +
'' +
'' + time + '' +
'' +
'' +
'' +
'' +
'';
return element;
}
function blueTickMessage(message) {
setTimeout(function() {
var tick = message.querySelector('.tick');
tick.classList.remove('tick-animation');
}, 500);
}
function addTick(message) {
setTimeout(function() {
var timerElement = message.querySelector('.unsend_msg');
timerElement.outerHTML = '' +
'' +
'' +
'' +
'';
//tick.classList.remove('tick-animation');
}, 500);
}
window.addEventListener('online', function(e) {
console.log('And we\'re back :).');
//conversation.innerHTML = "";
if(recipientId !== '' && recipientId !== undefined){
addNetworkStatusOfRecipient(recipientId);
readFromDb(recipientId);
}
//makeOnline();
isNetwork = 1;
//alert('You Have Been Disconnected!');
//window.location.href = floidToOnion[floId];
}, false);
window.addEventListener('offline', function(e) {
console.log('Connection is down.');
//makeOffline();
if(recipientId !== '' && recipientId !== undefined)
document.getElementsByClassName('status')[0].innerHTML = "Unknown";
isNetwork = 0;
}, false);
function makeOnline(){
console.log(document.getElementsByClassName('status')[0]);
document.getElementsByClassName('status')[0].innerHTML = "Online";
}
function makeOffline(){
document.getElementsByClassName('status')[0].innerHTML = "Offline";
}
//window.addEventListener("load", init, false);
}