# alexkingorg/SimpleMath

better abstracted behaviors

1 parent 3ed06ea commit 64fee6611f9d37cdba5a4eb77615ad94fc9a4a6d committed Jan 28, 2011
Showing with 21 additions and 17 deletions.
1. +21 −17 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 @@

-

Result:

+

Result:

@@ -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,43 +69,45 @@ 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; } 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(); }); });