aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/operation.js
blob: ed6403201118d48e5c8967cc1fb667ae615043cb (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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import TabPresenter from '../presenters/tab';
import ConsolePresenter from '../presenters/console';
import * as urls from '../../shared/urls';

const ZOOM_SETTINGS = [
  0.33, 0.50, 0.66, 0.75, 0.80, 0.90, 1.00,
  1.10, 1.25, 1.50, 1.75, 2.00, 2.50, 3.00
];

export default class OperationInteractor {
  constructor() {
    this.tabPresenter = new TabPresenter();
    this.consolePresenter = new ConsolePresenter();
  }

  async close(force) {
    let tab = await this.tabPresenter.getCurrent();
    if (!force && tab.pinned) {
      return;
    }
    return this.tabPresenter.remove([tab.id]);
  }

  async closeRight() {
    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);
      }
    }
  }

  reopen() {
    return this.tabPresenter.reopen();
  }

  async selectPrev(count) {
    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);
  }

  async selectNext(count) {
    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);
  }

  async selectFirst() {
    let tabs = await this.tabPresenter.getAll();
    return this.tabPresenter.select(tabs[0].id);
  }

  async selectLast() {
    let tabs = await this.tabPresenter.getAll();
    return this.tabPresenter.select(tabs[tabs.length - 1].id);
  }

  async selectPrevSelected() {
    let tabId = await this.tabPresenter.getLastSelectedId();
    if (tabId === null || typeof tabId === 'undefined') {
      return;
    }
    this.tabPresenter.select(tabId);
  }

  async reload(cache) {
    let tab = await this.tabPresenter.getCurrent();
    return this.tabPresenter.reload(tab.id, cache);
  }

  async setPinned(pinned) {
    let tab = await this.tabPresenter.getCurrent();
    return this.tabPresenter.setPinned(tab.id, pinned);
  }

  async togglePinned() {
    let tab = await this.tabPresenter.getCurrent();
    return this.tabPresenter.setPinned(tab.id, !tab.pinned);
  }

  async duplicate() {
    let tab = await this.tabPresenter.getCurrent();
    return this.tabPresenter.duplicate(tab.id);
  }

  async openPageSource() {
    let tab = await this.tabPresenter.getCurrent();
    let url = 'view-source:' + tab.url;
    return this.tabPresenter.create(url);
  }

  async zoomIn(tabId) {
    let tab = await this.tabPresenter.getCurrent();
    let current = await this.tabPresenter.getZoom(tab.id);
    let factor = ZOOM_SETTINGS.find(f => f > current);
    if (factor) {
      return this.tabPresenter.setZoom(tabId, factor);
    }
  }

  async zoomOut(tabId) {
    let tab = await this.tabPresenter.getCurrent();
    let current = await this.tabPresenter.getZoom(tab.id);
    let factor = [].concat(ZOOM_SETTINGS).reverse().find(f => f < current);
    if (factor) {
      return this.tabPresenter.setZoom(tabId, factor);
    }
  }

  zoomNutoral(tabId) {
    return this.tabPresenter.setZoom(tabId, 1);
  }

  async showCommand() {
    let tab = await this.tabPresenter.getCurrent();
    return this.consolePresenter.showCommand(tab.id, '');
  }

  async showOpenCommand(alter) {
    let tab = await this.tabPresenter.getCurrent();
    let command = 'open ';
    if (alter) {
      command += tab.url;
    }
    return this.consolePresenter.showCommand(tab.id, command);
  }

  async showTabopenCommand(alter) {
    let tab = await this.tabPresenter.getCurrent();
    let command = 'tabopen ';
    if (alter) {
      command += tab.url;
    }
    return this.consolePresenter.showCommand(tab.id, command);
  }

  async showWinopenCommand(alter) {
    let tab = await this.tabPresenter.getCurrent();
    let command = 'winopen ';
    if (alter) {
      command += tab.url;
    }
    return this.consolePresenter.showCommand(tab.id, command);
  }

  async showBufferCommand() {
    let tab = await this.tabPresenter.getCurrent();
    let command = 'buffer ';
    return this.consolePresenter.showCommand(tab.id, command);
  }

  async showAddbookmarkCommand(alter) {
    let tab = await this.tabPresenter.getCurrent();
    let command = 'addbookmark ';
    if (alter) {
      command += tab.title;
    }
    return this.consolePresenter.showCommand(tab.id, command);
  }

  async findStart() {
    let tab = await this.tabPresenter.getCurrent();
    return this.consolePresenter.showFind(tab.id);
  }

  async hideConsole() {
    let tab = await this.tabPresenter.getCurrent();
    return this.consolePresenter.hide(tab.id);
  }

  async openHome(newTab) {
    let tab = await this.tabPresenter.getCurrent();
    let result = await browser.browserSettings.homepageOverride.get({});
    let us = urls.homepageUrls(result.value);
    if (us.length === 1 && us[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 (us.length === 1 && !newTab) {
      return this.tabPresenter.open(us[0], tab.id);
    }
    for (let u of us) {
      this.tabPresenter.create(u);
    }
  }
}