refactor js dependencies

This commit is contained in:
snogcel 2016-12-04 17:55:20 -07:00
parent 0fc8b3ae46
commit abcf4f7b8d
25 changed files with 10648 additions and 607 deletions

23
js/formHandler.js Executable file
View file

@ -0,0 +1,23 @@
function setFormEditable(edit) {
$('.createProposal input').each(function() {
$(this).attr("disabled", edit);
});
$('.createProposal select').each(function() {
$(this).attr("disabled", edit);
});
if (edit === true) {
$('#btnPrepare').addClass('hidden');
$('#btnEdit').removeClass('hidden');
$('#btnNew').removeClass('hidden');
} else {
$('#btnPrepare').removeClass('hidden');
$('#btnEdit').addClass('hidden');
$('#btnNew').addClass('hidden');
}
}
var copyToClipboard = function(id) {
document.getElementById(id).select();
document.execCommand('copy');
};

158
js/paymentCycle.js Executable file
View file

@ -0,0 +1,158 @@
/***
* Payment Cycle Generator
*
* @param gov
* @constructor
*/
function PaymentCycle(gov) {
var self = this;
this.network = gov.network;
this.paymentCycle = 16616; // mainnet
this.budgetCycles = 24;
if (this.network == 'testnet') this.paymentCycle = 24;
if (this.network == 'testnet') this.budgetCycles = 96;
this.blockHeight = null;
this.Messages = {
paymentCycle: {
months: "Months",
month: "Month",
days: "Days",
day: "Day",
hours: "Hours",
hour: "Hour",
minutes: "Minutes",
minute: "Minute",
seconds: "Seconds",
second: "Second"
}
};
this.getInfo(function(err,res) {
console.log("current blockheight: " + res.info.blocks);
self.blockHeight = res.info.blocks;
});
}
PaymentCycle.prototype.getNextSuperblock = function(block) {
return (Math.floor((block/this.paymentCycle)) * this.paymentCycle + this.paymentCycle);
};
PaymentCycle.prototype.getBlockTimestamp = function(block) {
var blocks = block - this.blockHeight;
var now = Math.floor(Date.now());
return (now + (blocks * (155 * 1000))); // 155 seconds per block x 1000 = ms per block
};
PaymentCycle.prototype.getTimeDifference = function(opts, start, end) {
var precision = opts.precision;
var millisec = end - start;
var seconds = (millisec / 1000).toFixed(precision);
var minutes = (millisec / (1000 * 60)).toFixed(precision);
var hours = (millisec / (1000 * 60 * 60)).toFixed(precision);
var days = (millisec / (1000 * 60 * 60 * 24)).toFixed(precision);
var months = (millisec / (1000 * 60 * 60 * 24 * 30)).toFixed(precision);
if (seconds < 60) {
if (seconds <= 1) return seconds + " " + this.Messages.paymentCycle.second; // singular
return seconds + " " + this.Messages.paymentCycle.seconds;
} else if (minutes < 60) {
if (minutes <= 1) return minutes + " " + this.Messages.paymentCycle.minute; // singular
return minutes + " " + this.Messages.paymentCycle.minutes;
} else if (hours < 24) {
if (hours <= 1) return hours + " " + this.Messages.paymentCycle.hour; // singular
return hours + " " + this.Messages.paymentCycle.hours;
} else if (days < 30) {
if (days <= 1) return days + " " + this.Messages.paymentCycle.day; // singular
return days + " " + this.Messages.paymentCycle.days;
} else {
if (months <= 1) return months + " " + this.Messages.paymentCycle.month; // singular
return months + " " + this.Messages.paymentCycle.months;
}
};
PaymentCycle.prototype.updateDropdowns = function() {
var self = this;
var blockHeight = this.blockHeight;
var startDate = [];
var endDate = [];
for (i = 0; i < this.budgetCycles + 1; i++) {
var superblock = this.getNextSuperblock(blockHeight);
var timestamp = this.getBlockTimestamp(superblock);
var label = new Date(timestamp).toLocaleDateString();
if (this.network == 'testnet') label = new Date(timestamp).toLocaleString();
var superblockDate = {
superblock: superblock,
timestamp: timestamp,
label: label
};
startDate.push(superblockDate);
endDate.push(superblockDate);
blockHeight = superblock;
}
endDate.shift(); // remove first element of endDate
startDate.pop(); // remove last element of startDate to keep length even
var now = Math.floor(Date.now());
var opts = {
precision: 2
}; // 2 unit of precision for eta formatting
// calculate the amount of time between start and stop, show: e.g. 5 Months or 5 Hours
var start_epoch = $("#start_epoch");
start_epoch.find('option').remove();
$.each(startDate, function(index) {
var eta = self.getTimeDifference(opts, now, this.timestamp);
var time = this.timestamp - now;
var option = $("<option />").val((Math.floor(this.timestamp / 1000))).text(this.label).attr('data-index', index).attr('data-time', time).attr('data-eta', eta).attr('data-block', this.superblock);
start_epoch.append(option);
});
opts.precision = null; // 0 units of precision for eta formatting
var end_epoch = $("#end_epoch");
end_epoch.find('option').remove();
$.each(endDate, function(index) {
var eta = self.getTimeDifference(opts, startDate[0].timestamp, this.timestamp);
var time = this.timestamp - startDate[0].timestamp;
var option = $("<option />").val((Math.floor(this.timestamp / 1000))).text(eta + " (" + this.label + ")").attr('data-index', index).attr('data-time', time).attr('data-eta', eta).attr('data-block', this.superblock);
end_epoch.append(option);
});
};
PaymentCycle.prototype.getInfo = function(cb) {
$.getJSON(provider + "insight-api-dash/status?q=getinfo", function( data ) {
cb(null, data);
});
};

119
js/proposalGenerator.js Executable file
View file

@ -0,0 +1,119 @@
function ProposalGenerator(gov) {
this._mode = 'proposal';
if(!gov.network) gov.network = 'livenet';
this.gov = gov;
// proposal basic fields
this.gov.name = $('#name').val();
this.gov.url = $('#url').val();
this.gov.payment_address = $('#payment_address').val();
this.gov.payment_amount = $('#payment_amount').val();
this.gov.start_epoch = $('#start_epoch').val();
this.gov.end_epoch = $('#end_epoch').val();
// hidden elements
this.gov.type = parseInt($('#type').val());
}
ProposalGenerator.prototype.validate = function() {
try {
var gov = this.gov.serialize();
}
catch (e) {
switch(e.message) {
case 'Invalid Name':
console.log("error: invalid name");
$('#name').addClass('validationError');
$('#name').val("Invalid name. Please enter a name without spaces and weird characters. E.g. can use a '-' or '_' instead of a space.");
break;
case 'Invalid URL':
console.log("Error: invalid url");
$('#url').addClass('validationError');
$('#url').val("There is a formatting error in your URL. Did you forget the leading 'http://'?");
break;
case 'Invalid Payment Amount':
console.log("Error: invalid payment amount");
$('#payment_amount').addClass('validationError');
$('#payment_amount').val("Invalid payment amount. Please enter a number from 1 - 7500");
break;
case 'Invalid Timespan':
console.log("Error: invalid timespan");
$('#start_epoch, #end_epoch').addClass('validationError');
break;
case 'Invalid Start Date':
console.log("Error: invalid start date");
$('#start_epoch').addClass('validationError');
break;
case 'Invalid End Date':
console.log("Error: invalid end date");
$('#end_epoch').addClass('validationError');
break;
case 'Invalid Address':
console.log("Error: invalid address");
$('#payment_address').addClass('validationError');
$('#payment_address').val("Invalid Dash Address. Please just copy & paste from wallet.");
break;
default:
console.log(e);
break;
}
return false;
}
return true;
};
ProposalGenerator.prototype.walletCommands = function() {
var gov = this.gov;
var prepCommand = "gobject prepare "+$('#parentHash').val() + " " + $('#revision').val() +" " + $('#time').val() +" " + gov.serialize();
console.log(prepCommand);
$("textarea#prepareProposal").val(prepCommand);
if(this._mode == 'proposal') {
setFormEditable(true);
$('.walletCommands#walletCommandsHeader').removeClass('hidden');
$('.walletCommands#walletCommandsPrepare').removeClass('hidden');
$('.walletCommands#walletCommandsTx').removeClass('hidden');
//$('.walletCommands#walletCommandsProgress').removeClass('hidden');
//$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
this._mode = 'command';
}
};
ProposalGenerator.prototype.createProposal = function() {
$('#feeTxid').val("");
$('#submitProposal').val("");
if(this._mode == 'command') {
setFormEditable(false);
$('.walletCommands#walletCommandsHeader').addClass('hidden');
$('.walletCommands#walletCommandsPrepare').addClass('hidden');
$('.walletCommands#walletCommandsTx').addClass('hidden');
$('.walletCommands#walletCommandsProgress').addClass('hidden');
$('.walletCommands#walletCommandsSubmit').addClass('hidden');
this._mode = 'proposal';
}
};
ProposalGenerator.prototype.resetProposal = function() {
$('.createProposal input').each(function() {
$(this).val('');
});
setFormEditable(true);
};

238
js/transactionListener.js Executable file
View file

@ -0,0 +1,238 @@
function transactionListener(proposal) {
$('#feeTxid').on('input', function() {
if ($(this).val().length > 0) {
var submitCommand = "gobject submit " + $('#parentHash').val() + " " + $('#revision').val() + " " + $('#time').val() + " " + proposal.gov.serialize() + " " + $(this).val();
console.log(submitCommand);
var txidfield = $('#feeTxid');
console.log('value entered: ' + txidfield.val());
$('textarea#submitProposal').val(submitCommand);
txidfield.change(function() {
// Check input( $( this ).val() ) for validity here
console.log('there is something wrong with your transaction ID: ' + $('#feeTxid').val() + ' Please copy and paste it here .');
txidfield.val('')
});
//some checks if feeTxid seems valid before we check the api
if (txidfield.val().length == 64) {
if (isAlphaNumeric(txidfield.val())) {
txidfield.unbind( "change" );
console.log('feeTxid seems good: ' + txidfield.val());
console.log("wait while we check the api!");
// first check if transactionid is already in api
$.getJSON(provider + 'insight-api-dash/tx/' + txidfield.val(), function(data) {
txidfield.attr("disabled", true);
$('.walletCommands#walletCommandsProgress').removeClass('hidden');
var txid = data.tx;
var confirmations = data.confirmations;
var conftxt;
var conftxt2;
var progbarval;
console.log('Transaction has ' + confirmations + ' confirmation(s)');
progbarval = 100/6*confirmations;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval.toPrecision(3) + '%')
.css("display", "block");
if (confirmations != 'undefined' && $.isNumeric(confirmations)) {
var socket = io(provider);
if (confirmations == 0) {
// we have to count the blocks and wait for 6 confirmations
console.log("we have to count the blocks and wait for 6 confirmations");
eventToListenTo = 'block';
room = 'inv';
socket.on('connect', function() {
// Join the room.
socket.emit('subscribe', room);
console.log("listening for '" + eventToListenTo + "' in '" + room + "'");
});
socket.on(eventToListenTo, function(data) {
console.log("New block received: " + data + " time: " + data.time);
blockhash = data;
// let's check if transaction is really in this block or not
if (confirmations == 0) {
$.getJSON(provider + 'insight-api-dash/txs/?block=' + blockhash, function(data) {
var txs = data.txs;
var found;
var numOfTxs = txs.length;
for (var i = 0; i < numOfTxs; i++) {
console.log('txs' + i + ': ' + txs[i].txid);
if (txs[i].txid == txidfield.val()) {
console.log('found tx!');
found = true;
}
}
if (found) {
console.log('all good. Count up confirmations.');
confirmations = confirmations + 1;
progbarval = 100/6*confirmations;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval.toPrecision(3) + '%')
.css("display", "block");
if (confirmations == 1) {
conftxt = 'confirmation';
conftxt2 = 'confirmations';
}
else if (confirmations == 5) {
conftxt = 'confirmations';
conftxt2 = 'confirmation';
}
else {
conftxt = 'confirmations';
conftxt2 = 'confirmations';
}
$("#progresstxt").text("Your transaction has " + confirmations + " " + conftxt + ". Waiting for " + (6 - confirmations) + " more " + conftxt2 + "...");
console.log('we have ' + confirmations + ' confirmations...');
}
else {
console.log('txid not in new block');
}
}).fail(function(jqXHR) {
if (jqXHR.status == 400) {
// there seems to be a problem with your feeTxid because txid is not found in api
console.log('block hash not found in api!');
} else {
console.log('There seems to be a problem with the api connection. Maybe endpoint resyncing?');
}
});
}
else {
// for the time being just count up the confirmations without confirming everytime if the transaction is still inside the previous blocks
confirmations = confirmations + 1;
progbarval = 100/6*confirmations;
if (confirmations == 1) {
conftxt = 'confirmation';
conftxt2 = 'confirmations';
}
else if (confirmations == 5) {
conftxt = 'confirmations';
conftxt2 = 'confirmation';
}
else {
conftxt = 'confirmations';
conftxt2 = 'confirmations';
}
$("#progresstxt").text("Your transaction has " + confirmations + " " + conftxt + ". Waiting for " + (6 - confirmations) + " more " + conftxt2 + "...");
console.log('we have ' + confirmations + ' confirmations...');
}
if (confirmations >= 6) {
progbarval = 100;
$("#progresstxt").text("Your transaction has " + confirmations + " confirmations. You can now submit the proposal.");
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval.toPrecision(3) + '%')
.css("display", "block");
});
}
else if (confirmations > 0 && confirmations <= 5) {
// we have to count the blocks and wait for outstanding confirmations
console.log("we have to count the blocks and wait for outstanding confirmations");
eventToListenTo = 'block';
room = 'inv';
socket.on('connect', function() {
// Join the room.
socket.emit('subscribe', room);
console.log("listening for '" + eventToListenTo + "' in '" + room + "'");
});
socket.on(eventToListenTo, function(data) {
console.log("New block received: " + data + " time: " + data.time);
// for the time being just count up the confirmations without confirming everytime if the transaction is still inside the previous blocks
confirmations = confirmations + 1;
progbarval = 100/6*confirmations;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval.toPrecision(3) + '%')
.css("display", "block");
if (confirmations = 1) {
conftxt = 'confirmation';
conftxt2 = 'confirmations';
}
else if (confirmations = 5) {
conftxt = 'confirmations';
conftxt2 = 'confirmation';
}
else {
conftxt = 'confirmations';
conftxt2 = 'confirmations';
}
$("#progresstxt").text("Your transaction has " + confirmations + " " + conftxt + ". Waiting for " + (6 - confirmations) + " more " + conftxt2 + "...");
console.log('we have ' + confirmations + ' confirmations...');
if (confirmations >= 6) {
progbarval = 100;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval + '%')
.css("display", "block");
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
});
}
else {
// already reached 6 or more confirmations, so we can proceed
progbarval = 100;
$("#progressbar").progressbar({value: progbarval})
.children('.ui-progressbar-value')
.html(progbarval + '%')
.css("display", "block");
$("#progresstxt").text("Your transaction has " + confirmations + " confirmations. You can now submit the proposal.");
console.log("already reached 6 or more confirmations, so we can proceed");
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
}
else {
console.log('Something went terribly wrong. Faulty api data?');
txidfield.attr("disabled", false);
}
}).fail(function(jqXHR) {
if (jqXHR.status == 400) {
// there seems to be a problem with your feeTxid because txid is not found in api
console.log('problem with feeTxid! Ask for new input!');
alert("Check again and please enter your correct TxID!");
txidfield.attr("disabled", false);
} else {
txidfield.attr("disabled", false);
console.log('There seems to be a problem with the api connection');
}
});
}
else {
$('#feeTxid').addClass('validationError');
$('#feeTxid').val('Your transacton ID is invalid. It must be alphanumeric. Please just copy & paste from console.');
console.log("there is something wrong with your transaction ID. It must be alphanumeric!")
}
}
else {
$('#feeTxid').addClass('validationError');
$('#feeTxid').val('Your transacton ID is invalid. Please just copy & paste from console.');
console.log("there is something wrong with your transaction ID. It must be exactly 64 characters!")
}
}
else {
$('textarea#submitProposal').val('');
$('.walletCommands#walletCommandsSubmit').addClass('hidden');
}
});
}
function isAlphaNumeric(str) {
var code, i, len;
for (i = 0, len = str.length; i < len; i++) {
code = str.charCodeAt(i);
if (!(code > 47 && code < 58) && // numeric (0-9)
!(code > 64 && code < 91) && // upper alpha (A-Z)
!(code > 96 && code < 123)) { // lower alpha (a-z)
return false;
}
}
return true;
}