Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial add

  • Loading branch information...
commit 9a45dbbad575235a79effd68aca1a6098a89c479 0 parents
@alexkingorg authored
Showing with 113 additions and 0 deletions.
  1. +103 −0 index.html
  2. +10 −0 js/jquery.caret.min.js
103 index.html
@@ -0,0 +1,103 @@
+<!--
+
+Copyright 2011 Alex King, All rights reserved.
+
+TODO:
+
+- styling
+- html storage to save page locally
+- ability to delete rows
+- redo row numbering in inverse order
+- show row numbers
+- auto-scroll rows up
+
+-->
+
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8" />
+ <title>SimpleMath</title>
+</head>
+<body>
+
+<div class="equations">
+
+<div class="equation" data-row="1">
+ <p><input type="text" class="math"></p>
+ <p>Result: <input type="number" class="result"></p>
+</div>
+
+</div>
+
+<p><input type="button" name="new-eq" value="New" class="new-eq"></p>
+
+<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
+<script type="text/javascript" src="./js/jquery.caret.min.js"></script>
+<script type="text/javascript">
+$(function() {
+ var keyUp = true;
+ $('.equation .math').live('keydown', function(e) {
+// handle insertion of values from previous lines
+ keyUp = true;
+ if (e.ctrlKey) {
+ var row = e.keyCode - 48;
+ if (row >= 0 && row <= 9) {
+ $result = $('.equations .equation[data-row=' + row + '] .result');
+ if ($result.size()) {
+ var eq = $(this).val();
+ var a = eq.substring(0, $(this).caret().start);
+ var b = eq.substring($(this).caret().end, eq.length);
+ var c = $result.val();
+ var pos = ('' + a + c).length;
+ $(this).val('' + a + c + b).caret({ start: pos, end: pos});
+ keyUp = false;
+ }
+ }
+ }
+ }).live('keyup', function(e) {
+// do math
+ if (!keyUp) {
+ return;
+ }
+ var eq = $(this).val();
+ var val = null;
+ var valError = '...';
+ var $result = $(this).closest('.equation').find('.result');
+ eq = eq.replace(/[^0-9 \.\)\(\+\-\*\/]+/g, '');
+ try {
+ eval('val = ' + eq + ';');
+ }
+ 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();
+ }
+ }).filter(':first').focus();
+ $('.equation .result').live('keyup', function(e) {
+// auto-create new line
+ if (e.which === 13) {
+ e.stopPropagation();
+ $('.new-eq').click();
+ }
+ });
+ $('.new-eq').click(function() {
+// create new line
+ var $eqs = $('.equation');
+ var $eq = $('.equation:first').clone();
+ var row = $eqs.size() + 1;
+ $eq.attr('data-row', row).find(':input').val('').end().appendTo('.equations');
+ $('.equation:last :input:first').focus();
+ });
+});
+</script>
+
+</body>
+</html>
10 js/jquery.caret.min.js
@@ -0,0 +1,10 @@
+/*
+ *
+ * Copyright (c) 2010 C. F., Wong (<a href="http://cloudgen.w0ng.hk">Cloudgen Examplet Store</a>)
+ * Licensed under the MIT License:
+ * http://www.opensource.org/licenses/mit-license.php
+ *
+ */
+(function(k,e,i,j){k.fn.caret=function(b,l){var a,c,f=this[0],d=k.browser.msie;if(typeof b==="object"&&typeof b.start==="number"&&typeof b.end==="number"){a=b.start;c=b.end}else if(typeof b==="number"&&typeof l==="number"){a=b;c=l}else if(typeof b==="string")if((a=f.value.indexOf(b))>-1)c=a+b[e];else a=null;else if(Object.prototype.toString.call(b)==="[object RegExp]"){b=b.exec(f.value);if(b!=null){a=b.index;c=a+b[0][e]}}if(typeof a!="undefined"){if(d){d=this[0].createTextRange();d.collapse(true);
+d.moveStart("character",a);d.moveEnd("character",c-a);d.select()}else{this[0].selectionStart=a;this[0].selectionEnd=c}this[0].focus();return this}else{if(d){c=document.selection;if(this[0].tagName.toLowerCase()!="textarea"){d=this.val();a=c[i]()[j]();a.moveEnd("character",d[e]);var g=a.text==""?d[e]:d.lastIndexOf(a.text);a=c[i]()[j]();a.moveStart("character",-d[e]);var h=a.text[e]}else{a=c[i]();c=a[j]();c.moveToElementText(this[0]);c.setEndPoint("EndToEnd",a);g=c.text[e]-a.text[e];h=g+a.text[e]}}else{g=
+f.selectionStart;h=f.selectionEnd}a=f.value.substring(g,h);return{start:g,end:h,text:a,replace:function(m){return f.value.substring(0,g)+m+f.value.substring(h,f.value[e])}}}}})(jQuery,"length","createRange","duplicate");
Please sign in to comment.
Something went wrong with that request. Please try again.