aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/TabUseCase.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-05-07 21:16:47 +0900
committerGitHub <noreply@github.com>2019-05-07 21:16:47 +0900
commit05ef6a8ca35aaa801c11eb6b4896caa3690058af (patch)
tree2c7708ca91ac2b462cc86aa28612e3d3943496f3 /src/background/usecases/TabUseCase.ts
parent457d954e08923b4accd28a919c72d0b61db1bb98 (diff)
parent27d0a7f37d24a0ad68a8ccb7dee18fc1d00eea58 (diff)
Merge pull request #578 from ueokande/move-to-typescript
Move to TypeScript
Diffstat (limited to 'src/background/usecases/TabUseCase.ts')
-rw-r--r--src/background/usecases/TabUseCase.ts81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/background/usecases/TabUseCase.ts b/src/background/usecases/TabUseCase.ts
new file mode 100644
index 0000000..1615333
--- /dev/null
+++ b/src/background/usecases/TabUseCase.ts
@@ -0,0 +1,81 @@
+import TabPresenter from '../presenters/TabPresenter';
+import BrowserSettingRepository from '../repositories/BrowserSettingRepository';
+
+export default class TabUseCase {
+ private tabPresenter: TabPresenter;
+
+ private browserSettingRepository: BrowserSettingRepository;
+
+ constructor() {
+ this.tabPresenter = new TabPresenter();
+ this.browserSettingRepository = new BrowserSettingRepository();
+ }
+
+ async close(force: boolean): Promise<any> {
+ let tab = await this.tabPresenter.getCurrent();
+ if (!force && tab.pinned) {
+ return Promise.resolve();
+ }
+ return this.tabPresenter.remove([tab.id as number]);
+ }
+
+ async closeRight(): Promise<any> {
+ let tabs = await this.tabPresenter.getAll();
+ tabs.sort((t1, t2) => t1.index - t2.index);
+ let index = tabs.findIndex(t => t.active);
+ if (index < 0) {
+ return;
+ }
+ for (let i = index + 1; i < tabs.length; ++i) {
+ let tab = tabs[i];
+ if (!tab.pinned) {
+ this.tabPresenter.remove([tab.id as number]);
+ }
+ }
+ }
+
+ reopen(): Promise<any> {
+ return this.tabPresenter.reopen();
+ }
+
+ async reload(cache: boolean): Promise<any> {
+ let tab = await this.tabPresenter.getCurrent();
+ return this.tabPresenter.reload(tab.id as number, cache);
+ }
+
+ async setPinned(pinned: boolean): Promise<any> {
+ let tab = await this.tabPresenter.getCurrent();
+ return this.tabPresenter.setPinned(tab.id as number, pinned);
+ }
+
+ async togglePinned(): Promise<any> {
+ let tab = await this.tabPresenter.getCurrent();
+ return this.tabPresenter.setPinned(tab.id as number, !tab.pinned);
+ }
+
+ async duplicate(): Promise<any> {
+ let tab = await this.tabPresenter.getCurrent();
+ return this.tabPresenter.duplicate(tab.id as number);
+ }
+
+ async openPageSource(): Promise<any> {
+ let tab = await this.tabPresenter.getCurrent();
+ let url = 'view-source:' + tab.url;
+ return this.tabPresenter.create(url);
+ }
+
+ async openHome(newTab: boolean): Promise<any> {
+ let tab = await this.tabPresenter.getCurrent();
+ let urls = await this.browserSettingRepository.getHomepageUrls();
+ if (urls.length === 1 && urls[0] === 'about:home') {
+ // eslint-disable-next-line max-len
+ throw new Error('Cannot open Firefox Home (about:home) by WebExtensions, set your custom URLs');
+ }
+ if (urls.length === 1 && !newTab) {
+ return this.tabPresenter.open(urls[0], tab.id);
+ }
+ for (let url of urls) {
+ this.tabPresenter.create(url);
+ }
+ }
+}