CustomNumberQuestion
The file customnumberquestion.js implements the class CustomNumberQuestion, which descends from ITPQuestion. It registers this class with the itpelementfactory and also registers a mapping between the element type question_number and the class type ITPElement.ITPPageElement.ITPQuestion.CustomNumberQuestion.
CustomNumberQuestion adds four methods; thousands, countseparators, internal2screen and screen2internal, that implement the thousands separator logic. The details of this implementation are not relevant to this example.
More importantly, CustomNumberQuestion overrides the following methods:
- initScreenElements, this method builds up a map of screen elements, adding to the list that is produced by the base class ITPQuestion. It finds the standard input element that is produced by the Xslt and hides this. It adds a new input, binding thousands separator logic to it.
- setInternalAnswer, this method makes sure that the original hidden input element is synchronized with the answer provided by the end user. The content of the hidden element will be posted when the form is submitted. Because this element is now guaranteed to be in sync with the answer on the screen, no additional work is required in the method prepareForSubmission.
- getScreenAnswer, this method return the answer that is currently on the screen, in internal format, by removing all thousands separator characters.
- setScreenAnswer, this method puts an answer on the screen, after adding thousands separators add the right locations.
- validate, this method returns whether an answer is valid and acceptable. It does not accept values that cannot be interpreted numerically and validates numerical values against the amount of allowed digits. If the answer is invalid, it sets a feedback text, which will be presented to the end user as an error.
The combination of these overrides is sufficient to implement the custom thousands separator behavior. During initialization the custom implementation initScreenElements will manipulate the screen and bind the thousands separator logic. During form entry and submission the other overrides will combine to make sure the answer is interpreted and validated correctly.