Extract dialog code in to a module

The dialogs are still tightly coupled to HTML IDs, so the dialog is single-instance still. However, now the code is isolated, so we can get it's templates, styles, and behavior under control. Other than that, this PR changes as little about the dialog code as possible.