Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions +/autocomplete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<script>$(document).ready(function() {
var $payee = $('.payee .font');
var $amount = $('.amount .font');
var $options = $('.payee .options');
let currentIndex = -1;

populateUserslist();

$payee.on('input', function() {
let inputValue = $(this).text().trim().toLowerCase();
if (inputValue.length > 0) {
filterOptions(inputValue);
} else {
closeOptions();
showAllOptions();
}
currentIndex = -1;
});

$options.on('mousedown', '.option', function(e) {
e.preventDefault();
selectOption($(this).text());
});

$payee.on('keydown', function(e) {
var $visibleOptions = $options.find('.option').filter(function() {
return $(this).css('display') !== 'none';
});
if (e.which === 40) { // down
e.preventDefault();
if (!$options.hasClass('show')) {
$options.addClass('show');
}
navigateOptions(1, $visibleOptions);
moveCursorToEnd($payee[0]);
} else if (e.which === 38) { // up
e.preventDefault();
if ($options.hasClass('show')) {
navigateOptions(-1, $visibleOptions);
moveCursorToEnd($payee[0]);
}
} else if (e.which === 13) { // enter
e.preventDefault();
if (currentIndex >= 0) {
selectOption($visibleOptions.eq(currentIndex).text());
}
}
});

function navigateOptions(direction, $visibleOptions) {
if ($visibleOptions.length === 0) return;

if (currentIndex === -1) {
currentIndex = direction > 0 ? 0 : $visibleOptions.length - 1;
} else {
currentIndex += direction;
if (currentIndex < 0) currentIndex = $visibleOptions.length - 1;
if (currentIndex >= $visibleOptions.length) currentIndex = 0;
}

$options.find('.option').removeClass('highlighted');
$visibleOptions.eq(currentIndex).addClass('highlighted');
$('.payee label').hide();

$payee.text($visibleOptions.eq(currentIndex).text());
}

async function selectOption(text) {
$payee.text(text);
closeOptions();
moveCursorToEnd($payee[0]);

// Populate amount if available
let users = await getUsers();
let selectedUser = users.find(user => user.name === text);
if (selectedUser && selectedUser.amount) {
$amount.text(selectedUser.amount);
$('.amount label').hide();
$('.payee').keyup();
$('.amount').keyup();


console.log('--->>>',selectedUser.amount);
$('.amounts .font').text(english(selectedUser.amount.replaceAll(',','').split('.')[0], selectedUser.amount.split('.')[1])).trigger('input');
}
}

function closeOptions() {
$options.removeClass('show');
$options.find('.option').removeClass('highlighted');
currentIndex = -1;
}

function moveCursorToEnd(el) {
let range = document.createRange();
let sel = window.getSelection();
range.setStart(el, 1);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
el.focus();
}

function filterOptions(inputValue) {
let hasMatches = false;
$options.find('.option').each(function() {
let optionText = $(this).text().toLowerCase();
if (optionText.indexOf(inputValue) !== -1) {
$(this).css('display', '');
hasMatches = true;
} else {
$(this).css('display', 'none');
}
});

if (hasMatches) {
$options.addClass('show');
} else {
$options.removeClass('show');
}
}

function showAllOptions() {
$options.find('.option').css('display', '');
}

$(document).on('click', function(e) {
if (!$(e.target).closest('.payee').length) {
closeOptions();
}
});

});

async function addUser(user) {
if (user && user.name) {
let users = await getUsers();
if (!users.some(savedUser => savedUser.name === user.name)) {
users.push(user);
localStorage.setItem('users', JSON.stringify(await Promise.all(users.map(u => SEA.encrypt(JSON.stringify(u), 'rest')))));
await populateUserslist();
}
}
}

async function getUsers() {
let storedUsers = localStorage.getItem('users');
return storedUsers ? Promise.all(JSON.parse(storedUsers).map(async item => await SEA.decrypt(item, 'rest'))) : [];
}

async function populateUserslist() {
let users = await getUsers();
let $options = $('.payee .options');
$options.empty();
users.forEach((user) => {
$options.append(`<div class="option">${user.name}</div>`);
});
}


</script>
155 changes: 79 additions & 76 deletions +/autoconvert.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,90 +27,93 @@

// modified from creative commons share-alike source authored by 'bpbutti'
// https://codereview.stackexchange.com/questions/90349/changing-number-to-words-in-javascript
function english(dollars, cents) {

if(dollars === 0){ // penny check, forget printing dollar amount
return cents + ' cents'
}
// trigger autoconvert for paylinks:
if((location+'').indexOf('&amount') >= 0){ $('.amount .font').trigger('blur') }
})();

//Creates an array with the number's digits and
//adds the necessary amount of 0 to make it fully
//divisible by 3
var digits = String(dollars).split('');
var digitsNeeded = 3 - digits.length % 3;
if (digitsNeeded !== 3) { //prevents this : (123) ---> (000123)
while (digitsNeeded > 0) {
digits.unshift('0');
digitsNeeded--;
}
}

//Groups the digits in groups of three
var digitsGroup = [];
var numberOfGroups = digits.length / 3;
for (var i = 0; i < numberOfGroups; i++) {
digitsGroup[i] = digits.splice(0, 3);
}
// console.log(digitsGroup) //debug

//Change the group's numerical values to text
var digitsGroupLen = digitsGroup.length;
var numTxt = [
[null, 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'], //hundreds
[null, 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'], //tens
[null, 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] //ones
];
var tenthsDifferent = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']

// j maps the groups in the digitsGroup
// k maps the element's position in the group to the numTxt equivalent
// k values: 0 = hundreds, 1 = tens, 2 = ones
for (var j = 0; j < digitsGroupLen; j++) {
for (var k = 0; k < 3; k++) {
var currentValue = digitsGroup[j][k];
digitsGroup[j][k] = numTxt[k][currentValue]
if (k === 0 && currentValue !== '0') { // !==0 avoids creating a string "null hundred"
digitsGroup[j][k] += ' hundred ';
}
else if (k === 1 && currentValue === '1') { //Changes the value in the tens place and erases the value in the ones place
digitsGroup[j][k] = tenthsDifferent[digitsGroup[j][2]];
digitsGroup[j][2] = 0; //Sets to null. Because it sets the next k to be evaluated, setting this to null doesn't work.
}
}
}
function english(dollars, cents) {

// console.log(digitsGroup) //debug
if(dollars === 0){ // penny check, forget printing dollar amount
return cents + ' cents'
}

//Adds '-' for grammar, cleans all null values, joins the group's elements into a string
for (var l = 0; l < digitsGroupLen; l++) {
if (digitsGroup[l][1] && digitsGroup[l][2]) {
digitsGroup[l][1] += '-';
}
digitsGroup[l].filter(function (e) { return e !== null });
digitsGroup[l] = digitsGroup[l].join('');
}
//Creates an array with the number's digits and
//adds the necessary amount of 0 to make it fully
//divisible by 3
var digits = String(dollars).split('');
var digitsNeeded = 3 - digits.length % 3;
if (digitsNeeded !== 3) { //prevents this : (123) ---> (000123)
while (digitsNeeded > 0) {
digits.unshift('0');
digitsNeeded--;
}
}

// console.log(digitsGroup) //debug

//Adds thousand, millions, billion and etc to the respective string.
var posfix = [null, 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion'];
if (digitsGroupLen > 1) {
var posfixRange = posfix.splice(0, digitsGroupLen).reverse();
for (var m = 0; m < digitsGroupLen - 1; m++) { //'-1' prevents adding a null posfix to the last group
if (digitsGroup[m]) { // avoids 10000000 being read (one billion million)
digitsGroup[m] += ' ' + posfixRange[m];
}
}
}
//Groups the digits in groups of three
var digitsGroup = [];
var numberOfGroups = digits.length / 3;
for (var i = 0; i < numberOfGroups; i++) {
digitsGroup[i] = digits.splice(0, 3);
}
// console.log(digitsGroup) //debug

// drop the and if its not needed
//Change the group's numerical values to text
var digitsGroupLen = digitsGroup.length;
var numTxt = [
[null, 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'], //hundreds
[null, 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'], //tens
[null, 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] //ones
];
var tenthsDifferent = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']

//Joins all the string into one and returns it
return digitsGroup.join(' ').replace(/\s+/g, ' ') + ' and ' + cents + '/100'
// j maps the groups in the digitsGroup
// k maps the element's position in the group to the numTxt equivalent
// k values: 0 = hundreds, 1 = tens, 2 = ones
for (var j = 0; j < digitsGroupLen; j++) {
for (var k = 0; k < 3; k++) {
var currentValue = digitsGroup[j][k];
digitsGroup[j][k] = numTxt[k][currentValue]
if (k === 0 && currentValue !== '0') { // !==0 avoids creating a string "null hundred"
digitsGroup[j][k] += ' hundred ';
}
else if (k === 1 && currentValue === '1') { //Changes the value in the tens place and erases the value in the ones place
digitsGroup[j][k] = tenthsDifferent[digitsGroup[j][2]];
digitsGroup[j][2] = 0; //Sets to null. Because it sets the next k to be evaluated, setting this to null doesn't work.
}
}
}

}; //End of numToWords function
// console.log(digitsGroup) //debug

//Adds '-' for grammar, cleans all null values, joins the group's elements into a string
for (var l = 0; l < digitsGroupLen; l++) {
if (digitsGroup[l][1] && digitsGroup[l][2]) {
digitsGroup[l][1] += '-';
}
digitsGroup[l].filter(function (e) { return e !== null });
digitsGroup[l] = digitsGroup[l].join('');
}

// console.log(digitsGroup) //debug

//Adds thousand, millions, billion and etc to the respective string.
var posfix = [null, 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion'];
if (digitsGroupLen > 1) {
var posfixRange = posfix.splice(0, digitsGroupLen).reverse();
for (var m = 0; m < digitsGroupLen - 1; m++) { //'-1' prevents adding a null posfix to the last group
if (digitsGroup[m]) { // avoids 10000000 being read (one billion million)
digitsGroup[m] += ' ' + posfixRange[m];
}
}
}

// drop the and if its not needed

//Joins all the string into one and returns it
return digitsGroup.join(' ').replace(/\s+/g, ' ') + ' and ' + cents + '/100'

} //End of numToWords function

// trigger autoconvert for paylinks:
if((location+'').indexOf('&amount') >= 0){ $('.amount .font').trigger('blur') }
})();
</script>
Loading