Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #8368 from brave/urlbar-redux-2
Browse files Browse the repository at this point in the history
first pass at action consolidation for urlbar
  • Loading branch information
bsclifton authored May 4, 2017
2 parents 0da7b64 + e5d2de4 commit 4751448
Show file tree
Hide file tree
Showing 15 changed files with 786 additions and 749 deletions.
24 changes: 18 additions & 6 deletions app/common/state/frameState.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ const api = {
// in WindowState
const index = state.get('frames').findIndex((frame) => frame.get('key') === frameKey)
if (index === -1) {
return path.concat('nosuchframe')
return null
}
return path.concat(['frames', index])
} else {
// in AppState
const index = state.get('tabs').findIndex((tab) => tab.getIn(['frame', 'key']) === frameKey)
if (index === -1) {
return path.concat('nosuchtab')
return null
}
return makeImmutable(['tabs', index, 'frame'])
}
Expand All @@ -61,21 +61,33 @@ const api = {
// in WindowState
const index = state.get('frames').findIndex((frame) => frame.get('tabId') === tabId)
if (index === -1) {
return makeImmutable(['nosuchframe'])
return null
}
return path.concat(['frames', index])
},

getByFrameKey: (state, frameKey) => {
return state.getIn(api.getPathByFrameKey(state, frameKey))
const path = api.getPathByFrameKey(state, frameKey)
if (path == null) {
return null
}
return state.getIn(path)
},

getByTabId: (state, tabId) => {
return state.getIn(api.getPathByTabId(state, tabId))
const path = api.getPathByTabId(state, tabId)
if (path == null) {
return null
}
return state.getIn(path)
},

getTabIdByFrameKey: (state, frameKey) => {
return state.getIn(api.getPathByFrameKey(state, frameKey).concat('tabId')) || -1
const path = api.getPathByFrameKey(state, frameKey)
if (path == null) {
return null
}
return state.getIn(path.concat('tabId')) || -1
}
}

Expand Down
109 changes: 65 additions & 44 deletions app/common/state/navigationBarState.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ const api = {
state = validateState(state)
tabState.validateTabId(tabId)

return tabState.getFramePathByTabId(state, tabId).concat('navbar')
const path = tabState.getFramePathByTabId(state, tabId)
if (path == null) {
return null
}
return path.push('navbar')
},

getNavigationBar: (state, tabId) => {
Expand All @@ -51,15 +55,25 @@ const api = {
return defaultState
}

return state.getIn(api.getNavigationBarPath(state, tabId)) || defaultState
const path = api.getNavigationBarPath(state, tabId)
if (path == null) {
return defaultState
}

return state.getIn(path) || defaultState
},

getUrlBarPath: (state, tabId) => {
return api.getNavigationBarPath(state, tabId).concat('urlbar')
const path = api.getNavigationBarPath(state, tabId)
return path == null ? null : path.push('urlbar')
},

getUrlBar: (state, tabId) => {
return state.getIn(api.getUrlBarPath(state, tabId)) || defaultState.get('urlbar')
const path = api.getUrlBarPath(state, tabId)
if (path == null) {
return defaultState.get('urlbar')
}
return state.getIn(path) || defaultState.get('urlbar')
},

locationValueSuffix: (state, tabId) => {
Expand Down Expand Up @@ -103,7 +117,11 @@ const api = {

setSelectedIndex: (state, tabId, index = -1) => {
state = validateState(state)
return state.setIn(api.getUrlBarPath(state, tabId).concat(['suggestions', 'selectedIndex']), index)
const path = api.getUrlBarPath(state, tabId)
if (path == null) {
return state
}
return state.setIn(path.concat(['suggestions', 'selectedIndex']), index)
},

getSelectedIndex: (state, tabId) => {
Expand All @@ -113,8 +131,12 @@ const api = {

setSuggestionList: (state, tabId, suggestionList = []) => {
state = validateState(state)
const path = api.getUrlBarPath(state, tabId)
if (path == null) {
return state
}
suggestionList = makeImmutable(suggestionList)
return state.setIn(api.getUrlBarPath(state, tabId).concat(['suggestions', 'suggestionList']), suggestionList)
return state.setIn(path.concat(['suggestions', 'suggestionList']), suggestionList)
},

getSuggestionList: (state, tabId) => {
Expand All @@ -133,7 +155,11 @@ const api = {

setAutocompleteEnabled: (state, tabId, enabled = false) => {
state = validateState(state)
return state.setIn(api.getUrlBarPath(state, tabId).concat(['suggestions', 'autocompleteEnabled']), enabled)
const path = api.getUrlBarPath(state, tabId)
if (path == null) {
return state
}
return state.setIn(path.concat(['suggestions', 'autocompleteEnabled']), enabled)
},

isAutocompleteEnabled: (state, tabId) => {
Expand All @@ -142,16 +168,25 @@ const api = {

setLocation: (state, tabId, location, counter) => {
state = validateState(state)
return state.setIn(api.getUrlBarPath(state, tabId).push('location'), location)
const path = api.getUrlBarPath(state, tabId)
if (path == null) {
return state
}
return state.setIn(path.push('location'), location)
},

getLocation: (state, tabId) => {
return api.getUrlBar(state, tabId).get('location')
},

setKeyCounter: (state, tabId, counter) => {
const path = api.getUrlBarPath(state, tabId)
if (path == null) {
return state
}

if (counter) {
state = state.setIn(api.getUrlBarPath(state, tabId).push('keyCounter'), counter)
state = state.setIn(path.push('keyCounter'), counter)
}
return state
},
Expand All @@ -162,9 +197,13 @@ const api = {

setShouldRenderUrlBarSuggestions: (state, tabId, enabled = false) => {
state = validateState(state)
state = state.setIn(api.getUrlBarPath(state, tabId).concat(['suggestions', 'shouldRender']), enabled)
const path = api.getUrlBarPath(state, tabId)
if (path == null) {
return state
}
state = state.setIn(path.concat(['suggestions', 'shouldRender']), enabled)
if (!enabled) {
state = state.mergeIn(api.getUrlBarPath(state, tabId).push('suggestions'), defaultState.getIn(['urlbar', 'suggestions']))
state = state.mergeIn(path.push('suggestions'), defaultState.getIn(['urlbar', 'suggestions']))
}
return state
},
Expand All @@ -175,39 +214,13 @@ const api = {
suggestionList && suggestionList.size > 0
},

setPreviousUrlBarSuggestionSelected: (state, tabId) => {
if (api.shouldRenderUrlBarSuggestions(state, tabId)) {
const selectedIndex = api.getSelectedIndex(state, tabId)
const lastSuffix = api.locationValueSuffix(state, tabId)

if (selectedIndex !== 0 && !lastSuffix) {
state = api.setSelectedIndex(state, tabId, 0)
} else if (selectedIndex > 0) {
state = api.setSelectedIndex(state, tabId, selectedIndex - 1)
}
// state = updateUrlSuffix(state, state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'suggestionList']), suggestionList))
}
return state
},

setNextUrlBarSuggestionSelected: (state, tabId) => {
if (api.shouldRenderUrlBarSuggestions(state, tabId)) {
const selectedIndex = api.getSelectedIndex(state, tabId)
const lastSuffix = api.locationValueSuffix(state, tabId)

if (selectedIndex !== 0 && !lastSuffix) {
state = api.setSelectedIndex(state, tabId, 0)
} else if (selectedIndex > 0) {
state = api.setSelectedIndex(state, tabId, selectedIndex - 1)
}
// state = updateUrlSuffix(state, state.getIn(activeFrameStatePath(state).concat(['navbar', 'urlbar', 'suggestions', 'suggestionList']), suggestionList))
}
return state
},

setActive: (state, tabId, active = false) => {
state = validateState(state)
return state.setIn(api.getUrlBarPath(state, tabId).push('active'), active)
const path = api.getUrlBarPath(state, tabId)
if (path == null) {
return state
}
return state.setIn(path.push('active'), active)
},

isActive: (state, tabId) => {
Expand All @@ -216,7 +229,11 @@ const api = {

setFocused: (state, tabId, focused = false) => {
state = validateState(state)
return state.setIn(api.getUrlBarPath(state, tabId).push('focused'), focused)
const path = api.getUrlBarPath(state, tabId)
if (path == null) {
return state
}
return state.setIn(path.push('focused'), focused)
},

isFocused: (state, tabId) => {
Expand All @@ -225,11 +242,15 @@ const api = {

setSelected: (state, tabId, selected = false) => {
state = validateState(state)
const path = api.getUrlBarPath(state, tabId)
if (path == null) {
return state
}
if (selected) {
// selection implies focus
state = state = api.setFocused(state, tabId, true)
}
return state.setIn(api.getUrlBarPath(state, tabId).push('selected'), selected)
return state.setIn(path.push('selected'), selected)
},

isSelected: (state, tabId) => {
Expand Down
37 changes: 27 additions & 10 deletions app/common/state/tabState.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ const tabState = {
state = validateState(state)

const index = state.get('tabs').findIndex((tab) => tab.get('tabId') === tabId)
if (index === -1) {
return makeImmutable(['nosuchpath'])
if (index === tabState.TAB_ID_NONE) {
return null
}
return makeImmutable(['tabs', index])
},
Expand All @@ -185,7 +185,11 @@ const tabState = {
return null
}

return state.getIn(tabState.getPathByTabId(state, tabId))
const path = tabState.getPathByTabId(state, tabId)
if (path == null) {
return null
}
return state.getIn(path)
},

getTab: (state, tabValue) => {
Expand Down Expand Up @@ -305,8 +309,17 @@ const tabState = {
getFramePathByTabId: (state, tabId) => {
state = makeImmutable(state)
tabId = validateId('tabId', tabId)
if (state.get('tabs')) {
return tabState.getPathByTabId(state, tabId).push('frame')

if (tabId === tabState.TAB_ID_NONE) {
return null
}

if (state.get('tabs') && !state.get('frames')) {
const path = tabState.getPathByTabId(state, tabId)
if (path == null) {
return null
}
return path.push('frame')
} else {
return frameState.getPathByTabId(state, tabId)
}
Expand All @@ -324,32 +337,36 @@ const tabState = {
},

getFrameByTabId: (state, tabId) => {
const path = tabState.getFramePathByTabId(state, tabId)
if (path == null) {
return null
}
return state.getIn(tabState.getFramePathByTabId(state, tabId))
},

isSecure: (state, tabId) => {
const frame = tabState.getFrameByTabId(state, tabId)
return frameStateUtil.isFrameSecure(frame)
return frame ? frameStateUtil.isFrameSecure(frame) : null
},

isLoading: (state, tabId) => {
const frame = tabState.getFrameByTabId(state, tabId)
return frameStateUtil.isFrameLoading(frame)
return frame ? frameStateUtil.isFrameLoading(frame) : null
},

startLoadTime: (state, tabId) => {
const frame = tabState.getFrameByTabId(state, tabId)
return frameStateUtil.startLoadTime(frame)
return frame ? frameStateUtil.startLoadTime(frame) : null
},

endLoadTime: (state, tabId) => {
const frame = tabState.getFrameByTabId(state, tabId)
return frameStateUtil.endLoadTime(frame)
return frame ? frameStateUtil.endLoadTime(frame) : null
},

getHistory: (state, tabId) => {
const frame = tabState.getFrameByTabId(state, tabId)
return frameStateUtil.getHistory(frame)
return frame ? frameStateUtil.getHistory(frame) : null
},

getLocation: (state, tabId) => {
Expand Down
Loading

0 comments on commit 4751448

Please sign in to comment.