Issue #1685140 by frega, seutje, ethanw, nod_: Refactor collapse.js.

PR for https://github.com/backdrop/backdrop-issues/issues/211
Replacement PR for https://github.com/backdrop/backdrop/pull/293/files