diff --git a/src/background/components/background.js b/src/background/components/background.js index 3667f14..86e96fa 100644 --- a/src/background/components/background.js +++ b/src/background/components/background.js @@ -1,6 +1,5 @@ import messages from 'shared/messages'; import * as commandActions from 'background/actions/command'; -import * as tabActions from 'background/actions/tab'; export default class BackgroundComponent { constructor(store) { @@ -22,14 +21,6 @@ export default class BackgroundComponent { let settings = this.store.getState().setting; switch (message.type) { - case messages.OPEN_URL: - if (message.newTab) { - let action = tabActions.openNewTab( - message.url, sender.tab.id, message.background, - settings.value.properties.adjacenttab); - return this.store.dispatch(action); - } - return this.store.dispatch(tabActions.openToTab(message.url, sender.tab)); case messages.CONSOLE_ENTER_COMMAND: this.store.dispatch( commandActions.exec(sender.tab, message.text, settings.value), diff --git a/src/background/controllers/link.js b/src/background/controllers/link.js new file mode 100644 index 0000000..7ebbb34 --- /dev/null +++ b/src/background/controllers/link.js @@ -0,0 +1,15 @@ +import LinkInteractor from '../usecases/link'; + +export default class LinkController { + constructor() { + this.linkInteractor = new LinkInteractor(); + } + + openToTab(url, tabId) { + this.linkInteractor.openToTab(url, tabId); + } + + openNewTab(url, openerId, background) { + this.linkInteractor.openNewTab(url, openerId, background); + } +} diff --git a/src/background/infrastructures/content-message-listener.js b/src/background/infrastructures/content-message-listener.js index 8a361c1..f16804f 100644 --- a/src/background/infrastructures/content-message-listener.js +++ b/src/background/infrastructures/content-message-listener.js @@ -3,6 +3,7 @@ import CompletionsController from '../controllers/completions'; import SettingController from '../controllers/setting'; import FindController from '../controllers/find'; import AddonEnabledController from '../controllers/addon-enabled'; +import LinkController from '../controllers/link'; export default class ContentMessageListener { constructor() { @@ -10,6 +11,7 @@ export default class ContentMessageListener { this.completionsController = new CompletionsController(); this.findController = new FindController(); this.addonEnabledController = new AddonEnabledController(); + this.linkController = new LinkController(); } run() { @@ -25,7 +27,7 @@ export default class ContentMessageListener { }); } - onMessage(message) { + onMessage(message, sender) { switch (message.type) { case messages.CONSOLE_QUERY_COMPLETIONS: return this.onConsoleQueryCompletions(message.text); @@ -39,6 +41,9 @@ export default class ContentMessageListener { return this.onFindSetKeyword(message.keyword); case messages.ADDON_ENABLED_RESPONSE: return this.onAddonEnabledResponse(message.enabled); + case messages.OPEN_URL: + return this.onOpenUrl( + message.newTab, message.url, sender.tab.id, message.background); } } @@ -66,4 +71,11 @@ export default class ContentMessageListener { onAddonEnabledResponse(enabled) { return this.addonEnabledController.indicate(enabled); } + + onOpenUrl(newTab, url, openerId, background) { + if (newTab) { + return this.linkController.openNewTab(url, openerId, background); + } + return this.linkController.openToTab(url, openerId); + } } diff --git a/src/background/presenters/tab.js b/src/background/presenters/tab.js index 6120f6e..66a207f 100644 --- a/src/background/presenters/tab.js +++ b/src/background/presenters/tab.js @@ -1,6 +1,22 @@ export default class TabPresenter { - create(url) { - browser.tabs.create({ url, }); + open(url, tabId) { + return browser.tabs.update(tabId, { url }); + } + + create(url, { openerTabId, active }) { + return browser.tabs.create({ url, openerTabId, active }); + } + + async createAdjacent(url, { openerTabId, active }) { + let tabs = await browser.tabs.query({ + active: true, currentWindow: true + }); + return browser.tabs.create({ + url, + openerTabId, + active, + index: tabs[0].index + 1 + }); } onSelected(listener) { diff --git a/src/background/usecases/link.js b/src/background/usecases/link.js new file mode 100644 index 0000000..f9e474a --- /dev/null +++ b/src/background/usecases/link.js @@ -0,0 +1,27 @@ +import SettingRepository from '../repositories/setting'; +import TabPresenter from '../presenters/tab'; + +export default class LinkInteractor { + constructor() { + this.settingRepository = new SettingRepository(); + this.tabPresenter = new TabPresenter(); + } + + openToTab(url, tabId) { + this.tabPresenter.open(url, tabId); + + } + + async openNewTab(url, openerId, background) { + let settings = await this.settingRepository.get(); + let { adjacenttab } = settings.properties; + if (adjacenttab) { + return this.tabPresenter.create(url, { + openerTabId: openerId, active: !background + }); + } + return this.tabPresenter.create(url, { + openerTabId: openerId, active: !background + }); + } +}