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 = "Ly8gU3RhcnQgQmFzZTY0IGVuY29kaW5nIGhlcmUuCihmdW5jdGlvbigpIHsKICB2YXIgd3JpdGUgPSBmdW5jdGlvbihzdHJpbmcpIHsKICAgIGRvY3VtZW50LndyaXRlKCI8cHJlPiIgKyBzdHJpbmcgKyAiXG48L3ByZT4iKTsKICB9OwoKICB2YXIgcHJlZml4ID0gIiI7CiAgdmFyIHNjaGVtYSA9ICIiOwoKICB2YXIgaW5pdGlhbGl6ZSA9IGZ1bmN0aW9uKHNjaGVtYV8pIHsKICAgIGlmICghKCJiaF8iIGluIHdpbmRvdykgfHwgISgiY2FsY3VsYXRvcl9nbG9iYWwiIGluIGJoXykpIHsKICAgICAgd3JpdGUoIk5vIGNhbGN1bGF0b3IgZ2xvYmFscyBmb3VuZCBvbiB0aGlzIHBhZ2U7IHRlc3RzIGNhbm5vdCBydW4uIik7CiAgICAgIHJldHVybiBmYWxzZTsKICAgIH0KICAgIGZvciAodmFyIGNhbmRpZGF0ZSBpbiBiaF8uY2FsY3VsYXRvcl9nbG9iYWwpIHsKICAgICAgaWYgKGNhbmRpZGF0ZSAhPSAiY291bnRlciIgJiYgY2FuZGlkYXRlID4gcHJlZml4KQogICAgICAgIHByZWZpeCA9IGNhbmRpZGF0ZTsKICAgIH0KICAgIGlmIChwcmVmaXggPT0gIiIpIHsKICAgICAgd3JpdGUoIk5vIGNhbGN1bGF0b3JzIGZvdW5kIG9uIHRoaXMgcGFnZTsgdGVzdHMgY2Fubm90IHJ1bi4iKTsKICAgICAgcmV0dXJuIGZhbHNlOwogICAgfQogICAgaWYgKCFkb2N1bWVudC5nZXRFbGVtZW50QnlJZChwcmVmaXggKyAiI2J1dHRvbiIpKSB7CiAgICAgIHdyaXRlKCJQcmVmaXggJyIgKyBwcmVmaXggKyAiJyBpcyBpbnZhbGlkOyB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koKTsKLy8gRW5kIEJhc2U2NCBlbmNvZGluZyBoZXJlLgo="; 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, a future value calculator equivalent to Excel's FV function, and a nonresident alien US estate tax liability estimator.

 var bh_compound_interest = "Ly8gU3RhcnQgQmFzZTY0IGVuY29kaW5nIGhlcmUuCmJoXy5jYWxjdWxhdG9yX2NpID0gewogIGZpZWxkczogWwogICAge2lkOiAnc3RhcnQnLCAgICBsYWJlbDogJ0luaXRpYWwgdmFsdWU6PGJyPicsIGluaXQ6IDEwMCwgdmFsaWQ6IC9eW1xkXC5dKyQvLCAgICAgZm9ybWF0OiAne30nfSwKICAgIHtpZDogJ2ludGVyZXN0JywgbGFiZWw6ICdJbnRlcmVzdCByYXRlOjxicj4nLCBpbml0OiAzLCAgIHZhbGlkOiAvXi0/W1xkXC5dKyU/JC8sIGZvcm1hdDogJ3t9JSd9LAogICAge2lkOiAneWVhcnMnLCAgICBsYWJlbDogJ1llYXJzOjxicj4nLCAgICAgICAgIGluaXQ6IDEwLCAgdmFsaWQ6IC9eW1xkXC5dKyQvLCAgICAgZm9ybWF0OiAne30nfSwKICAgIHtpZDogJ3Jlc3VsdCcsICAgbGFiZWw6ICdFbmQgdmFsdWUgPTxicj4nLCAgICBmb3JtYXQ6ICcke30nfQogIF0sCiAgY29tcHV0ZTogZnVuY3Rpb24odikgewogICAgdi5yZXN1bHQgPSB2LnN0YXJ0ICogTWF0aC5wb3coMSArICh2LmludGVyZXN0LzEwMCkvMTIsIHYueWVhcnMgKiAxMik7CiAgICB2LnJlc3VsdCA9IHYucmVzdWx0LnRvRml4ZWQoNCk7CiAgfQp9OwpiaF8uY2FsY3VsYXRvcl9yZWdpc3RyYXIucmVnaXN0ZXIoYmhfLmNhbGN1bGF0b3IsIGJoXy5jYWxjdWxhdG9yX2NpKTsKLy8gRW5kIEJhc2U2NCBlbmNvZGluZyBoZXJlLgo="; eval(atob(bh_compound_interest));

 var bh_future_value = "Ly8gU3RhcnQgQmFzZTY0IGVuY29kaW5nIGhlcmUuCmJoXy5jYWxjdWxhdG9yX2Z2ID0gewogIGZpZWxkczogWwogICAge2lkOiAncmF0ZScsICAgbGFiZWw6ICdSYXRlOjxicj4nLCAgICAgICAgICBpbml0OiA1LCAgICAgICB2YWxpZDogL15bXGRcLl0rJT8kLywgZm9ybWF0OiAne30lJ30sCiAgICB7aWQ6ICducGVyJywgICBsYWJlbDogJ1BlcmlvZHM6PGJyPicsICAgICAgIGluaXQ6IDI1LCAgICAgIHZhbGlkOiAvXltcZF0rJC8sICAgICBmb3JtYXQ6ICd7fSd9LAogICAge2lkOiAncG10JywgICAgbGFiZWw6ICdQYXltZW50Ojxicj4nLCAgICAgICBpbml0OiAtMjA1MDAsICB2YWxpZDogL14tP1tcZFwuXSskLywgZm9ybWF0OiAne30nfSwKICAgIHtpZDogJ3B2JywgICAgIGxhYmVsOiAnUHJlc2VudCB2YWx1ZTo8YnI+JywgaW5pdDogLTQ1MDAwMCwgdmFsaWQ6IC9eLT9bXGRcLl0rJC8sIGZvcm1hdDogJ3t9J30sCi8vICB7aWQ6ICd0eXBlJywgICBsYWJlbDogJ1R5cGU6PGJyPicsICAgICAgICAgIGluaXQ6IDAsICAgICAgIHZhbGlkOiAvXlswMV0kLywgICAgICBmb3JtYXQ6ICd7fSd9LAogICAge2lkOiAncmVzdWx0JywgbGFiZWw6ICdGdXR1cmUgdmFsdWUgPTxicj4nLCBmb3JtYXQ6ICcke30nfQogIF0sCiAgY29tcHV0ZTogZnVuY3Rpb24odikgewogICAgdi50eXBlID0gMDsgIC8vIEZvcmNlIHR5cGUgdG8gMC4KICAgIHZhciByYXRlID0gdi5yYXRlIC8gMTAwLjA7ICAvLyBDb252ZXJ0IHBlcmNlbnRhZ2UgdG8gZmxvYXQuCiAgICAvLyBodHRwczovL3N0YWNrb3ZlcmZsb3cuY29tL3F1ZXN0aW9ucy8xNzgwNjQ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ckVhY2goZnVuY3Rpb24ocmF0ZSkgewogICAgICBpZiAodi5hc3NldHMgPj0gcmF0ZS5sb3cgJiYgdi5hc3NldHMgPCByYXRlLmhpZ2gpCiAgICAgICAgdi5yZXN1bHQgKz0gcmF0ZS5pbml0aWFsICsgcmF0ZS5wZXJjZW50YWdlICogKHYuYXNzZXRzIC0gcmF0ZS5sb3cpOwogICAgICBpZiAocmF0ZS5sb3cgPT0gZXhjbHVzaW9uKQogICAgICAgIHYucmVzdWx0IC09IHJhdGUuaW5pdGlhbDsKICAgIH0pOwogICAgdi5yZXN1bHQgPSB2LnJlc3VsdCA+IDAgPyB2LnJlc3VsdC50b0ZpeGVkKDIpIDogMDsKICB9Cn07CmJoXy5jYWxjdWxhdG9yX3JlZ2lzdHJhci5yZWdpc3RlcihiaF8uY2FsY3VsYXRvciwgYmhfLmNhbGN1bGF0b3JfZXQpOwovLyBFbmQgQmFzZTY0IGVuY29kaW5nIGhlcmUuCg=="; 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, display 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==";