Refactor Javascript into widgets