]> asedeno.scripts.mit.edu Git - bluechips.git/commitdiff
Added javascript to calculate the dollar value of each share on-the-fly.
authorRay Speth <speth@mit.edu>
Wed, 17 Mar 2010 19:50:09 +0000 (15:50 -0400)
committerEvan Broder <broder@mit.edu>
Fri, 26 Mar 2010 14:25:48 +0000 (10:25 -0400)
bluechips/public/js/calculator.js [new file with mode: 0644]
bluechips/templates/spend/index.mako

diff --git a/bluechips/public/js/calculator.js b/bluechips/public/js/calculator.js
new file mode 100644 (file)
index 0000000..3c5509f
--- /dev/null
@@ -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;
index 1b0c6e3c493279f5f25c78ea4b96fcd7a8804582..916a307648449b1c6b4bffa889578c18a0bcfa80 100644 (file)
@@ -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>
       <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>
@@ -44,3 +47,4 @@
     </tr>
   </table>
 </form>
+${h.javascript_link('%s/js/calculator.js' % request.script_name)}