aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/SelectionManager.ts
blob: 8c92c202378e560f6ac02b5d60695b4087eeaad7 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { observable, action } from "mobx";
import { Doc } from "../../new_fields/Doc";
import { DocumentView } from "../views/nodes/DocumentView";
import { FormattedTextBox } from "../views/nodes/FormattedTextBox";
import { NumCast } from "../../new_fields/Types";

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) {
                this.DeselectAll();
            }

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

        @action
        DeselectAll(): void {
            manager.SelectedDocuments.map(dv => dv.props.whenActiveChanged(false));
            manager.SelectedDocuments = [];
            FormattedTextBox.InputBoxOverlay = undefined;
        }
        @action
        ReselectAll() {
            let sdocs = manager.SelectedDocuments.map(d => d);
            manager.SelectedDocuments = [];
            return sdocs;
        }
        @action
        ReselectAll2(sdocs: DocumentView[]) {
            sdocs.map(s => SelectionManager.SelectDoc(s, true));
        }
    }

    const manager = new Manager();

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

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

    export function DeselectAll(except?: Doc): void {
        let found: DocumentView | undefined = undefined;
        if (except) {
            for (const view of manager.SelectedDocuments) {
                if (view.props.Document === except) found = view;
            }
        }

        manager.DeselectAll();
        if (found) manager.SelectDoc(found, false);
    }

    export function ReselectAll() {
        let sdocs = manager.ReselectAll();
        setTimeout(() => manager.ReselectAll2(sdocs), 0);
    }
    export function SelectedDocuments(): Array<DocumentView> {
        return manager.SelectedDocuments;
    }
    export function ViewsSortedVertically(): DocumentView[] {
        let sorted = SelectionManager.SelectedDocuments().slice().sort((doc1, doc2) => {
            if (NumCast(doc1.props.Document.x) > NumCast(doc2.props.Document.x)) return 1;
            if (NumCast(doc1.props.Document.x) < NumCast(doc2.props.Document.x)) return -1;
            return 0;
        });
        return sorted;
    }
    export function ViewsSortedHorizontally(): DocumentView[] {
        let sorted = SelectionManager.SelectedDocuments().slice().sort((doc1, doc2) => {
            if (NumCast(doc1.props.Document.y) > NumCast(doc2.props.Document.y)) return 1;
            if (NumCast(doc1.props.Document.y) < NumCast(doc2.props.Document.y)) return -1;
            return 0;
        });
        return sorted;
    }
}