Properly sort array of integers

Otherwise we would end up randomly having units stored in wrong positions.
Allow specifying an arbitrary page for `fetchPages`
Moved context-specific data away from templates into settings
Use unit numbering instead of page numbering

Fixes bug 2215.
Use js-friendly property names