Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Added javascript to calculate the dollar value of each share on-the-fly.
Browse files Browse the repository at this point in the history
  • Loading branch information
speth authored and ebroder committed Mar 26, 2010
1 parent 6a7b564 commit 9d82b09
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
38 changes: 38 additions & 0 deletions bluechips/public/js/calculator.js
@@ -0,0 +1,38 @@
function validateSplit(input) {
if (!input.match(/^[\d\+\/\*\-\(\)\. ]*$/)) {
return Number.NaN;
}
if (input.match(/([\+\/\*\-])\1/)) {
return Number.NaN;
}
try {
v = eval(input);
} catch (err) {
return Number.NaN;
}
if (v == null) {
return 0;
}
return v;
}

function calcSplit() {
amount = document.getElementById("amount").value;
total = 0;
var values = new Array();
textvals = document.getElementsByClassName("share-text");
for (i=0; i<textvals.length; i++) {
v = validateSplit(textvals[i].value);
if (!isNaN(v)) {
total += v;
}
values[i] = v;
}
for (i=0; i<textvals.length; i++) {
id = textvals[i].id+'-calc';
val = (amount*values[i]/total).toFixed(2);
document.getElementById(id).innerHTML = val;
}
}

window.onload=calcSplit;
8 changes: 6 additions & 2 deletions bluechips/templates/spend/index.mako
Expand Up @@ -9,7 +9,7 @@
</tr>
<tr>
<th><label for="amount">Amount</label></th>
<td>${h.currency('amount', c.expenditure.amount, size=8)}</td>
<td>${h.currency('amount', c.expenditure.amount, size=8, onkeyup="calcSplit();")}</td>
</tr>
<tr>
<th><label for="date">Date</label></th>
Expand All @@ -32,9 +32,12 @@
<tr>
<th><label for="shares-${ii}amount">${user.name}</label></th>
<td>
${h.text('shares-%d.amount' % ii, percent)}
${h.text('shares-%d.amount' % ii, percent, class_="share-text", onkeyup="calcSplit();")}
${h.hidden('shares-%d.user_id' % ii, user.id)}
</td>
<td id="shares-${ii}amount-calc" align="right">
0.00
</td>
</tr>
% endfor
<tr>
Expand All @@ -44,3 +47,4 @@
</tr>
</table>
</form>
${h.javascript_link('%s/js/calculator.js' % request.script_name)}

0 comments on commit 9d82b09

Please sign in to comment.