diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-07 21:16:47 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-07 21:16:47 +0900 |
commit | 05ef6a8ca35aaa801c11eb6b4896caa3690058af (patch) | |
tree | 2c7708ca91ac2b462cc86aa28612e3d3943496f3 /src/background/usecases/TabSelectUseCase.ts | |
parent | 457d954e08923b4accd28a919c72d0b61db1bb98 (diff) | |
parent | 27d0a7f37d24a0ad68a8ccb7dee18fc1d00eea58 (diff) |
Merge pull request #578 from ueokande/move-to-typescript
Move to TypeScript
Diffstat (limited to 'src/background/usecases/TabSelectUseCase.ts')
-rw-r--r-- | src/background/usecases/TabSelectUseCase.ts | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/background/usecases/TabSelectUseCase.ts b/src/background/usecases/TabSelectUseCase.ts new file mode 100644 index 0000000..a0b52f0 --- /dev/null +++ b/src/background/usecases/TabSelectUseCase.ts @@ -0,0 +1,53 @@ +import TabPresenter from '../presenters/TabPresenter'; + +export default class TabSelectUseCase { + private tabPresenter: TabPresenter; + + constructor() { + this.tabPresenter = new 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); + } +} |