Isn't this whole JS code already used in by the file autocompletion?
Maybe we should extract it into e saparate object that can be reused. I guess we're going to use autocompletion a  lot in phase 2. What do you think?