/ src / reopen-project-list-view.js
reopen-project-list-view.js
 1  const SelectListView = require('atom-select-list');
 2  
 3  module.exports = class ReopenProjectListView {
 4    constructor(callback) {
 5      this.callback = callback;
 6      this.selectListView = new SelectListView({
 7        emptyMessage: 'No projects in history.',
 8        itemsClassList: ['mark-active'],
 9        items: [],
10        filterKeyForItem: project => project.name,
11        elementForItem: project => {
12          let element = document.createElement('li');
13          if (project.name === this.currentProjectName) {
14            element.classList.add('active');
15          }
16          element.textContent = project.name;
17          return element;
18        },
19        didConfirmSelection: project => {
20          this.cancel();
21          this.callback(project.value);
22        },
23        didCancelSelection: () => {
24          this.cancel();
25        }
26      });
27      this.selectListView.element.classList.add('reopen-project');
28    }
29  
30    get element() {
31      return this.selectListView.element;
32    }
33  
34    dispose() {
35      this.cancel();
36      return this.selectListView.destroy();
37    }
38  
39    cancel() {
40      if (this.panel != null) {
41        this.panel.destroy();
42      }
43      this.panel = null;
44      this.currentProjectName = null;
45      if (this.previouslyFocusedElement) {
46        this.previouslyFocusedElement.focus();
47        this.previouslyFocusedElement = null;
48      }
49    }
50  
51    attach() {
52      this.previouslyFocusedElement = document.activeElement;
53      if (this.panel == null) {
54        this.panel = atom.workspace.addModalPanel({ item: this });
55      }
56      this.selectListView.focus();
57      this.selectListView.reset();
58    }
59  
60    async toggle() {
61      if (this.panel != null) {
62        this.cancel();
63      } else {
64        this.currentProjectName =
65          atom.project != null ? this.makeName(atom.project.getPaths()) : null;
66        const projects = atom.history
67          .getProjects()
68          .map(p => ({ name: this.makeName(p.paths), value: p.paths }));
69        await this.selectListView.update({ items: projects });
70        this.attach();
71      }
72    }
73  
74    makeName(paths) {
75      return paths.join(', ');
76    }
77  };