diff options
Diffstat (limited to 'src/reducers')
-rw-r--r-- | src/reducers/background.js | 53 | ||||
-rw-r--r-- | src/reducers/command.js | 24 | ||||
-rw-r--r-- | src/reducers/console.js | 39 | ||||
-rw-r--r-- | src/reducers/content.js | 48 | ||||
-rw-r--r-- | src/reducers/input.js | 23 |
5 files changed, 187 insertions, 0 deletions
diff --git a/src/reducers/background.js b/src/reducers/background.js new file mode 100644 index 0000000..d7d7860 --- /dev/null +++ b/src/reducers/background.js @@ -0,0 +1,53 @@ +import * as tabs from '../background/tabs'; +import * as zooms from '../background/zooms'; +import * as consoleActions from '../actions/console'; +import actions from '../actions'; + +const doCompletion = (command, keywords, sender) => { + if (command === 'buffer') { + return tabs.getCompletions(keywords).then((tabs) => { + let items = tabs.map((tab) => { + return { + caption: tab.title, + content: tab.title, + url: tab.url, + icon: tab.favIconUrl + } + }); + let completions = { + name: "Buffers", + items: items + }; + return browser.tabs.sendMessage( + sender, + consoleActions.setCompletions([completions])); + }); + } + return Promise.resolve(); +}; + +export default function reducer(state, action = {}, sender) { + // TODO hide sender object + switch (action.type) { + case actions.BACKGROUND_REQUEST_COMPLETIONS: + return doCompletion(action.command, action.keywords, sender.tab.id); + case actions.TABS_CLOSE: + return tabs.closeTab(sender.tab.id); + case actions.TABS_REOPEN: + return tabs.reopenTab(); + case actions.TABS_PREV: + return tabs.selectPrevTab(sender.tab.index, action.count); + case actions.TABS_NEXT: + return tabs.selectNextTab(sender.tab.index, action.count); + case actions.TABS_RELOAD: + return tabs.reload(sender.tab, action.cache); + case actions.ZOOM_IN: + return zooms.zoomIn(); + case actions.ZOOM_OUT: + return zooms.zoomOut(); + case actions.ZOOM_NEUTRAL: + return zooms.neutral(); + default: + return Promise.resolve(); + } +} diff --git a/src/reducers/command.js b/src/reducers/command.js new file mode 100644 index 0000000..7e03593 --- /dev/null +++ b/src/reducers/command.js @@ -0,0 +1,24 @@ +import * as tabs from '../background/tabs'; +import actions from '../actions'; + +const cmdBuffer = (sender, arg) => { + if (isNaN(arg)) { + return tabs.selectByKeyword(sender.tab, arg); + } else { + let index = parseInt(arg, 10) - 1; + return tabs.selectAt(index); + } +} + +export default function reducer(state, action, sender) { + switch (action.type) { + case actions.COMMAND_OPEN_URL: + return browser.tabs.update(sender.tab.id, { url: action.url }); + case actions.COMMAND_TABOPEN_URL: + return browser.tabs.create({ url: action.url }); + case actions.COMMAND_BUFFER: + return cmdBuffer(sender, action.keywords); + default: + return Promise.resolve(); + } +} diff --git a/src/reducers/console.js b/src/reducers/console.js new file mode 100644 index 0000000..3303802 --- /dev/null +++ b/src/reducers/console.js @@ -0,0 +1,39 @@ +import actions from '../actions'; + +const defaultState = { + errorShown: false, + errorText: '', + commandShown: false, + commandText: '', + completions: [], +}; + +export default function reducer(state = defaultState, action = {}) { + switch (action.type) { + case actions.CONSOLE_SHOW_COMMAND: + return Object.assign({}, state, { + commandShown: true, + commandText: action.text, + errorShown: false, + completions: [] + }); + case actions.CONSOLE_SET_COMPLETIONS: + return Object.assign({}, state, { + completions: action.completions + }); + case actions.CONSOLE_SHOW_ERROR: + return Object.assign({}, state, { + errorText: action.text, + errorShown: true, + commandShown: false, + }); + case actions.CONSOLE_HIDE: + return Object.assign({}, state, { + errorShown: false, + commandShown: false + + }); + default: + return state; + } +} diff --git a/src/reducers/content.js b/src/reducers/content.js new file mode 100644 index 0000000..bcf1160 --- /dev/null +++ b/src/reducers/content.js @@ -0,0 +1,48 @@ +import * as consoleFrames from '../console/frames'; +import * as histories from '../content/histories'; +import * as scrolls from '../content/scrolls'; +import Follow from '../content/follow'; +import actions from '../actions'; + +export default function reducer(state, action = {}) { + switch (action.type) { + case actions.CMD_OPEN: + return consoleFrames.showCommand(''); + case actions.CMD_TABS_OPEN: + if (action.alter) { + // alter url + return consoleFrames.showCommand('open ' + window.location.href); + } else { + return consoleFrames.showCommand('open '); + } + case actions.CMD_BUFFER: + return consoleFrames.showCommand('buffer '); + case actions.SCROLL_LINES: + scrolls.scrollLines(window, action.count); + break; + case actions.SCROLL_PAGES: + scrolls.scrollPages(window, action.count); + break; + case actions.SCROLL_TOP: + scrolls.scrollTop(window); + break; + case actions.SCROLL_BOTTOM: + scrolls.scrollBottom(window); + break; + case actions.SCROLL_LEFT: + scrolls.scrollLeft(window); + break; + case actions.SCROLL_RIGHT: + scrolls.scrollRight(window); + break; + case actions.FOLLOW_START: + new Follow(window.document, action.newTab); + break; + case actions.HISTORY_PREV: + histories.prev(window); + break; + case actions.HISTORY_NEXT: + histories.next(window); + break; + } +} diff --git a/src/reducers/input.js b/src/reducers/input.js new file mode 100644 index 0000000..25ff1a3 --- /dev/null +++ b/src/reducers/input.js @@ -0,0 +1,23 @@ +import actions from '../actions'; + +const defaultState = { + keys: [], +}; + +export default function reducer(state = defaultState, action = {}) { + switch (action.type) { + case actions.INPUT_KEY_PRESS: + return Object.assign({}, state, { + keys: state.keys.concat([{ + code: action.code, + ctrl: action.ctrl + }]) + }); + case actions.INPUT_CLEAR_KEYS: + return Object.assign({}, state, { + keys: [], + }); + default: + return state; + } +} |