aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/TabSelectUseCase.ts
blob: df3db94851a56de7ed9aab4c1e302dedcf438af6 (plain) (blame)
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
import { injectable } from 'tsyringe';
import TabPresenter from '../presenters/TabPresenter';

@injectable()
export default class TabSelectUseCase {
  constructor(
    private tabPresenter: TabPresenter,
  ) {
  }

  async selectPrev(count: number): Promise<any> {
    let tabs = await this.tabPresenter.getAll();
    if (tabs.length < 2) {
      return;
    }
    let tab = tabs.find(t => t.active);
    if (!tab) {
      return;
    }
    let select = (tab.index - count + tabs.length) % tabs.length;
    return this.tabPresenter.select(tabs[select].id as number);
  }

  async selectNext(count: number): Promise<any> {
    let tabs = await this.tabPresenter.getAll();
    if (tabs.length < 2) {
      return;
    }
    let tab = tabs.find(t => t.active);
    if (!tab) {
      return;
    }
    let select = (tab.index + count) % tabs.length;
    return this.tabPresenter.select(tabs[select].id as number);
  }

  async selectFirst(): Promise<any> {
    let tabs = await this.tabPresenter.getAll();
    return this.tabPresenter.select(tabs[0].id as number);
  }

  async selectLast(): Promise<any> {
    let tabs = await this.tabPresenter.getAll();
    return this.tabPresenter.select(tabs[tabs.length - 1].id as number);
  }

  async selectPrevSelected(): Promise<any> {
    let tabId = await this.tabPresenter.getLastSelectedId();
    if (tabId === null || typeof tabId === 'undefined') {
      return Promise.resolve();
    }
    return this.tabPresenter.select(tabId);
  }
}