Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

better abstracted behaviors

  • Loading branch information...
commit 64fee6611f9d37cdba5a4eb77615ad94fc9a4a6d 1 parent 3ed06ea
@alexkingorg authored
Showing with 21 additions and 17 deletions.
  1. +21 −17 index.html
View
38 index.html
@@ -5,6 +5,8 @@
TODO:
- styling
+ - desktop
+ - mobile
- ability to delete rows (cmd+delete to delete row as well as link)
- show row numbers
- auto-scroll rows up
@@ -31,7 +33,7 @@
<div class="equation" data-row="1">
<p><input type="text" class="math"></p>
- <p>Result: <input type="number" class="result"></p>
+ <p>Result: <input type="text" class="result"></p>
</div>
</div>
@@ -57,6 +59,7 @@
var c = $result.val();
var pos = ('' + a + c).length;
$(this).val('' + a + c + b).caret({ start: pos, end: pos});
+ $(this).trigger('calc');
keyUp = false;
}
}
@@ -66,13 +69,18 @@
if (!keyUp) {
return;
}
- var eq = $(this).val();
+ $(this).trigger('calc');
+// if key = enter, set focus to result and select for easy copying
+ if (e.which === 13) {
+ e.stopPropagation();
+ $(this).closest('.equation').find('.result').focus().select();
+ }
+ }).live('calc', function() {
+// simple math
var val = null;
var valError = '...';
- var $result = $(this).closest('.equation').find('.result');
- eq = eq.replace(/[^0-9 \.\)\(\+\-\*\/]+/g, '');
try {
- eval('val = ' + eq + ';');
+ eval('val = ' + $(this).val().replace(/[^0-9 \.\)\(\+\-\*\/]+/g, '') + ';');
}
catch(err) {
val = valError;
@@ -80,29 +88,26 @@
if (isNaN(val)) {
val = valError;
}
- $result.val(val);
-// if key = enter, set focus to result and select for easy copying
- if (e.which === 13) {
- e.stopPropagation();
- $result.focus().select();
- }
+ $(this).closest('.equation').find('.result').val(val);
}).filter(':first').focus();
$('.equation .result').live('keyup', function(e) {
-// auto-create new line
+// auto-create new row
if (e.which === 13) {
e.stopPropagation();
$('.new-eq').click();
}
});
$('.new-eq').click(function() {
-// create new line
+// create new row
var $eqs = $('.equation');
- var $eq = $('.equation:first').clone();
- $eq.find(':input').val('').end().appendTo('.equations');
+ $('.equation:first').clone().find(':input').val('').end().appendTo('.equations');
+ $('.equations').trigger('renumber').find('.equation:last :input:first').focus();
+ });
+ $('.equations').bind('renumber', function() {
+// re-number rows
var i = 0;
var row = '';
$($('.equation').get().reverse()).each(function() {
-// re-number rows
if (i == 10) {
row = 0;
}
@@ -115,7 +120,6 @@
$(this).attr('data-row', row);
i++;
});
- $('.equation:last :input:first').focus();
});
});
</script>
Please sign in to comment.
Something went wrong with that request. Please try again.