From 9d82b09b61eb6e838e1cbb7b5a72426b5a6b4727 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 17 Mar 2010 15:50:09 -0400 Subject: [PATCH] Added javascript to calculate the dollar value of each share on-the-fly. --- bluechips/public/js/calculator.js | 38 ++++++++++++++++++++++++++++ bluechips/templates/spend/index.mako | 8 ++++-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 bluechips/public/js/calculator.js diff --git a/bluechips/public/js/calculator.js b/bluechips/public/js/calculator.js new file mode 100644 index 0000000..3c5509f --- /dev/null +++ b/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 - ${h.currency('amount', c.expenditure.amount, size=8)} + ${h.currency('amount', c.expenditure.amount, size=8, onkeyup="calcSplit();")} @@ -32,9 +32,12 @@ - ${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)} + + 0.00 + % endfor @@ -44,3 +47,4 @@ +${h.javascript_link('%s/js/calculator.js' % request.script_name)} -- 2.45.2