Template talk:Interactive calculator

Accuracy and regression tests
The following section tests the basic functionality of TWR Calculator. All tests should PASS, and 'Tested!' should appear in the calculator input and output boxes.

 var bh_calculator_test = "Ly8gU3RhcnQgQmFzZTY0IGVuY29kaW5nIGhlcmUuCihmdW5jdGlvbigpIHsKICB2YXIgd3JpdGUgPSBmdW5jdGlvbihzdHJpbmcpIHsKICAgIGRvY3VtZW50LndyaXRlKCI8cHJlPiIgKyBzdHJpbmcgKyAiXG48L3ByZT4iKTsKICB9OwoKICB2YXIgcHJlZml4ID0gIiI7CiAgdmFyIHNjaGVtYSA9ICIiOwoKICB2YXIgaW5pdGlhbGl6ZSA9IGZ1bmN0aW9uKHNjaGVtYV8pIHsKICAgIGlmICghKCJiaF8iIGluIHdpbmRvdykgfHwgISgiY2FsY19nbG9iYWwiIGluIGJoXykpIHsKICAgICAgd3JpdGUoIk5vIGNhbGN1bGF0b3IgZ2xvYmFscyBmb3VuZCBvbiB0aGlzIHBhZ2UuIik7CiAgICAgIHJldHVybiBmYWxzZTsKICAgIH0KICAgIGZvciAodmFyIGNhbmRpZGF0ZSBpbiBiaF8uY2FsY19nbG9iYWwpIHsKICAgICAgaWYgKGNhbmRpZGF0ZSAhPSAiY291bnRlciIgJiYgY2FuZGlkYXRlID4gcHJlZml4KQogICAgICAgIHByZWZpeCA9IGNhbmRpZGF0ZTsKICAgIH0KICAgIGlmIChwcmVmaXggPT0gIiIpIHsKICAgICAgd3JpdGUoIk5vIGNhbGN1bGF0b3JzIGZvdW5kIG9uIHRoaXMgcGFnZS4iKTsKICAgICAgcmV0dXJuIGZhbHNlOwogICAgfQogICAgaWYgKCFkb2N1bWVudC5nZXRFbGVtZW50QnlJZChwcmVmaXggKyAiI2J1dHRvbiIpKSB7CiAgICAgIHdyaXRlKCJJbnZhbGlkIHByZWZpeCBmb3VuZDsgdGVzdHMgY2Fubm90IHJ1bi4iKTsKICAgICAgcmV0dXJuIGZhbHNlOwogICAgfQogICAgaWYgKCEoc2NoZW1hXyBpb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Ci8vIEVuZCBCYXNlNjQgZW5jb2RpbmcgaGVyZS4="; eval(atob(bh_calculator_test));

Examples of adapting the code
The code is table-driven. Supplying an alternative table can adapt the calculator to a different purpose. The following examples show a simple monthly compounding interest calculator and a nonresident alien US estate tax liability estimator.

 var bh_compound_interest = "Ly8gU3RhcnQgQmFzZTY0IGVuY29kaW5nIGhlcmUuCmJoXy5jYWxjX2NpID0gewogIGZpZWxkczogWwogICAgICB7aWQ6ICdzdGFydCcsICAgIGxhYmVsOiAnSW5pdGlhbCB2YWx1ZTo8YnI+JywgaW5pdDogMTAwLCBtYXRjaDovXltcZFwuXSskLywgICAgIGZvcm1hdDogJ3t9J30sCiAgICAgIHtpZDogJ2ludGVyZXN0JywgbGFiZWw6ICdJbnRlcmVzdCByYXRlOjxicj4nLCBpbml0OiAzLCAgIG1hdGNoOi9eLT9bXGRcLl0rJT8kLywgZm9ybWF0OiAne30lJ30sCiAgICAgIHtpZDogJ3llYXJzJywgICAgbGFiZWw6ICdZZWFyczo8YnI+JywgICAgICAgICBpbml0OiAxMCwgIG1hdGNoOi9eW1xkXC5dKyQvLCAgICAgZm9ybWF0OiAne30nfSwKICAgICAge2lkOiAncmVzdWx0JywgICBsYWJlbDogJ0VuZCB2YWx1ZSA9PGJyPicsICAgIGZvcm1hdDogJyR7fSd9CiAgICBdLAogIGNvbXB1dGU6IGZ1bmN0aW9uKHYpIHsKICAgIHYucmVzdWx0ID0gdi5zdGFydCAqIE1hdGgucG93KDEgKyAodi5pbnRlcmVzdC8xMDApLzEyLCB2LnllYXJzICogMTIpOwogICAgdi5yZXN1bHQgPSB2LnJlc3VsdC50b0ZpeGVkKDQpOwogIH0KfTsKYmhfLmNhbGNfcmVnaXN0cmFyLnJlZ2lzdGVyKGJoXy5jYWxjX3YxLCBiaF8uY2FsY19jaSk7Ci8vIEVuZCBCYXNlNjQgZW5jb2RpbmcgaGVyZS4K"; eval(atob(bh_compound_interest));

 var bh_estate_tax_estimator = "Ly8gU3RhcnQgQ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wLCAgaGlnaDogICAyNTAwMDAsIGluaXRpYWw6ICAzODgwMCwgcGVyY2VudGFnZTogMC4zMn0sCiAgICAgIHtsb3c6ICAyNTAwMDAsICBoaWdoOiAgIDUwMDAwMCwgaW5pdGlhbDogIDcwODAwLCBwZXJjZW50YWdlOiAwLjM0fSwKICAgICAge2xvdzogIDUwMDAwMCwgIGhpZ2g6ICAgNzUwMDAwLCBpbml0aWFsOiAxNTU4MDAsIHBlcmNlbnRhZ2U6IDAuMzd9LAogICAgICB7bG93OiAgNzUwMDAwLCAgaGlnaDogIDEwMDAwMDAsIGluaXRpYWw6IDI0ODMwMCwgcGVyY2VudGFnZTogMC4zOX0sCiAgICAgIHtsb3c6IDEwMDAwMDAsICBoaWdoOiBJbmZpbml0eSwgaW5pdGlhbDogMzQ1ODAwLCBwZXJjZW50YWdlOiAwLjQwfQogICAgXTsKICAgIHYucmVzdWx0ID0gMDsKICAgIHJhdGVzLmZvckVhY2goZnVuY3Rpb24ocmF0ZSkgewogICAgICBpZiAodi5hc3NldHMgPj0gcmF0ZS5sb3cgJiYgdi5hc3NldHMgPCByYXRlLmhpZ2gpCiAgICAgICAgdi5yZXN1bHQgKz0gcmF0ZS5pbml0aWFsICsgcmF0ZS5wZXJjZW50YWdlICogKHYuYXNzZXRzIC0gcmF0ZS5sb3cpOwogICAgICBpZiAocmF0ZS5sb3cgPT0gZXhjbHVzaW9uKQogICAgICAgIHYucmVzdWx0IC09IHJhdGUuaW5pdGlhbDsKICAgIH0pOwogICAgdi5yZXN1bHQgPSB2LnJlc3VsdCA+IDAgPyB2LnJlc3VsdC50b0ZpeGVkKDIpIDogMDsKICB9Cn07CmJoXy5jYWxjX3JlZ2lzdHJhci5yZWdpc3RlcihiaF8uY2FsY192MSwgYmhfLmNhbGNfZXQpOwovLyBFbmQgQmFzZTY0IGVuY29kaW5nIGhlcmUu"; eval(atob(bh_estate_tax_estimator));

Base64 encoding
MediaWiki can transform a page's text before sending it out. For example, it can change  into. If applied to JavaScript source, the result is no longer valid code, and will not run. To avoid this problem, all JavaScript source code used here is saved as Base64 encoded strings. A small JavaScript shim runs in the browser to decode and then run this code. The shim cannot itself be Base64 encoded, so it must avoid using any JavaScript that MediaWiki might transform.

The browser's JavaScript console (Ctrl+Shift+I in Chrome) can display the source, for example by typing  at the console prompt. Alternatively, there are many web sites that offer Base64 encoding and decoding online, for example base64encode.org. Cut and paste the value of  (that is, everything between but excluding the enclosing quotes) into the 'Decode' tab or text entry box, and click 'Decode', 'Submit' or similar.

To alter the code, edit as necessary in an external text editor, then Base64 encode the edited code and cut and paste the encoding in place of the current  content.

Calculator code design notes
All code elements live in the global. This is to ensure that they do not clash with any other JavaScript shipped by MediaWiki, either now or in future. The calculator code is split into three parts. The part that defines the inputs and the calculation done on them is in. The other parts handle presentation in the browser.

Overview

 * defines the input fields: id, name, initial value, validator pattern, and format; and a single output (result) field. The result must be the last listed, and have no initial value or pattern. It defines the calculation that generates the result.
 * On loading the page,  creates an HTML table element containing input fields and a 'Calculate' button, from data in  . It adds this to the DOM, at the script location. Elements in the table are tagged with an ID that is unique to this invocation.
 * Updating an input field or clicking on 'Calculate' generates a call to . This validates the inputs, runs the required calculation, then updates all the fields. Invalid inputs generate NaN, displayed as 'Error!'.

Multiple transclusion

 * Global (cross-script) data is stored in . Members are a counter, and a data lookup table for use by.
 * The code supports multiple transclusion in a page by generating unique identifiers for HTML input and output elements. These use the counter, which increments on each transclusion.
 * On update or 'Calculate',  receives an argument containing the prefix for the unique html element identifiers. From this, it can deduce which data set it should use and which HTML elements to work with.

Reusability

 * A single instance of  supports multiple instances of transclusion. This is a requirement, since it is effectively redefined each time.
 * It can also support multiple different schemas, where  is one of many possible schemas. This allows the code to be quickly repurposed into a different type of calculator.
 * Alternative implementations or versions of  are possible (for example,  ). Implementations need to hold an   function, have a   that matches their pseudo-namespace, and be able to work with schemas and with.

Portability

 * The code uses a relatively minimal set of JavaScript features. It tries to avoid using features that might not be present in some older browsers.
 * MediaWiki's Mobile frontend/skin creates CSS styles that do not display well with  and   HTML elements. To improve things, these elements are explicitly styled to appear broadly similar in both Desktop and Mobile views.

Source code
Below are source code listings for all of the JavaScript used to create, test, and adapt the calculator.  bh_listing = "ZG9jdW1lbnQud3JpdGUoJzxwcmU+JyArIGF0b2Ioc2NyaXB0KS5yZXBsYWNlKC8mL2csICImYW1wOyIpLnJlcGxhY2UoLzwvZywgIiZsdDsiKS5yZXBsYWNlKC8+L2csICImZ3Q7IikucmVwbGFjZSgvIi9nLCAiJnF1b3Q7IikucmVwbGFjZSgvJy9nLCAiJiMwMzk7IikgKyAnPC9wcmU+Jyk7Cg==";