aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/SelectionManager.ts
blob: 43865910843883500042a40a1eeed84ad043cc6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { observable, action } from "mobx";
import { DocumentView } from "../views/nodes/DocumentView";
import { Document } from "../../fields/Document";

export namespace SelectionManager {
  class Manager {
    @observable
    SelectedDocuments: Array<DocumentView> = [];

    @action
    SelectDoc(doc: DocumentView, ctrlPressed: boolean): void {
      // if doc is not in SelectedDocuments, add it
      if (!ctrlPressed) {
        manager.SelectedDocuments = [];
      }

      if (manager.SelectedDocuments.indexOf(doc) === -1) {
        manager.SelectedDocuments.push(doc);
      }
    }
  }

  const manager = new Manager();

  export function SelectDoc(doc: DocumentView, ctrlPressed: boolean): void {
    if (!doc.isMinimized()) {
      manager.SelectDoc(doc, ctrlPressed);
    }
  }

  export function IsSelected(doc: DocumentView): boolean {
    return manager.SelectedDocuments.indexOf(doc) !== -1;
  }

  export function DeselectAll(except?: Document): void {
    let found: DocumentView | undefined = undefined;
    if (except) {
      for (let i = 0; i < manager.SelectedDocuments.length; i++) {
        let view = manager.SelectedDocuments[i];
        if (view.props.Document == except) found = view;
      }
    }
    manager.SelectedDocuments.length = 0;
    if (found) manager.SelectedDocuments.push(found);
  }

  export function SelectedDocuments(): Array<DocumentView> {
    return manager.SelectedDocuments;
  }
}