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

Commit bdc93ce

Browse files
committed
Removes a lot of functions out of siteUtil
1 parent 7a5f000 commit bdc93ce

17 files changed

+202
-303
lines changed

app/browser/menu.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ const frameStateUtil = require('../../js/state/frameStateUtil')
3434
const menuUtil = require('../common/lib/menuUtil')
3535
const {getSetting} = require('../../js/settings')
3636
const locale = require('../locale')
37-
const {isLocationBookmarked} = require('../../js/state/siteUtil')
3837
const platformUtil = require('../common/lib/platformUtil')
38+
const bookmarkUtil = require('../common/lib/bookmarkUtil')
3939
const isDarwin = platformUtil.isDarwin()
4040
const isLinux = platformUtil.isLinux()
4141

@@ -371,7 +371,7 @@ const updateRecentlyClosedMenuItems = (state) => {
371371
}
372372

373373
const isCurrentLocationBookmarked = (state) => {
374-
return isLocationBookmarked(state, currentLocation)
374+
return bookmarkUtil.isLocationBookmarked(state, currentLocation)
375375
}
376376

377377
const createBookmarksSubmenu = (state) => {

app/browser/reducers/sitesReducer.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const filtering = require('../../filtering')
2525
const siteUtil = require('../../../js/state/siteUtil')
2626
const syncUtil = require('../../../js/state/syncUtil')
2727
const pinnedSitesUtil = require('../../common/lib/pinnedSitesUtil')
28+
const bookmarkFoldersUtil = require('../../common/lib/bookmarkFoldersUtil')
29+
const bookmarkUtil = require('../../common/lib/bookmarkUtil')
2830
const {getSetting} = require('../../../js/settings')
2931
const writeActions = require('../../../js/constants/sync/proto').actions
3032

@@ -36,7 +38,7 @@ const updateTabBookmarked = (state, tabValue) => {
3638
if (!tabValue || !tabValue.get('tabId')) {
3739
return state
3840
}
39-
const bookmarked = siteUtil.isLocationBookmarked(state, tabValue.get('url'))
41+
const bookmarked = bookmarkUtil.isLocationBookmarked(state, tabValue.get('url'))
4042
return tabState.updateTabValue(state, tabValue.set('bookmarked', bookmarked))
4143
}
4244

@@ -197,7 +199,7 @@ const sitesReducer = (state, action, immutableAction) => {
197199
}
198200
break
199201
case appConstants.APP_APPLY_SITE_RECORDS:
200-
let nextFolderId = siteUtil.getNextFolderId(state.get('sites'))
202+
let nextFolderId = bookmarkFoldersUtil.getNextFolderId(state.get('bookmarkFolders'))
201203
// Ensure that all folders are assigned folderIds
202204
action.records.forEach((record, i) => {
203205
if (record.action !== writeActions.DELETE &&

app/common/lib/bookmarkFoldersUtil.js

+48-33
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,12 @@
22
* You can obtain one at http://mozilla.org/MPL/2.0/. */
33

44
const siteUtil = require('../../../js/state/siteUtil')
5+
const bookmarkFoldersState = require('../state/bookmarkFoldersState')
56

67
const isFolderNameValid = (title) => {
78
return (title != null && title !== 0) && title.trim().length > 0
89
}
910

10-
/**
11-
* Obtains an array of folders
12-
*/
13-
const getFolders = (sites, folderId, parentId, labelPrefix) => {
14-
parentId = parentId || 0
15-
let folders = []
16-
const results = sites
17-
.filter(site => site.get('parentFolderId', 0) === parentId)
18-
.toList()
19-
.sort(siteUtil.siteSort)
20-
21-
const resultSize = results.size
22-
for (let i = 0; i < resultSize; i++) {
23-
const site = results.get(i)
24-
if (site.get('folderId') === folderId) {
25-
continue
26-
}
27-
28-
const label = (labelPrefix || '') + site.get('title')
29-
folders.push({
30-
folderId: site.get('folderId'),
31-
parentFolderId: site.get('parentFolderId'),
32-
label
33-
})
34-
const subSites = getFolders(sites, folderId, site.get('folderId'), (label || '') + ' / ')
35-
folders = folders.concat(subSites)
36-
}
37-
38-
return folders
39-
}
40-
4111
const getNextFolderIdItem = (sites) =>
4212
sites.max((siteA, siteB) => {
4313
const folderIdA = siteA.get('folderId')
@@ -63,8 +33,53 @@ const getNextFolderId = (sites) => {
6333
return (maxIdItem ? (maxIdItem.get('folderId') || 0) : 0) + 1
6434
}
6535

36+
const getNextFolderName = (folders, name) => {
37+
if (!folders) {
38+
return name
39+
}
40+
const site = folders.find((site) => site.get('customTitle') === name)
41+
if (!site) {
42+
return name
43+
}
44+
const filenameFormat = /(.*) \((\d+)\)/
45+
let result = filenameFormat.exec(name)
46+
if (!result) {
47+
return getNextFolderName(folders, name + ' (1)')
48+
}
49+
50+
const nextNum = parseInt(result[2]) + 1
51+
return getNextFolderName(folders, result[1] + ' (' + nextNum + ')')
52+
}
53+
54+
const getFoldersWithoutKey = (state, folderKey, parentId = 0, labelPrefix = '') => {
55+
let folders = []
56+
const results = bookmarkFoldersState.getFolders(state)
57+
.filter(site => site.get('parentFolderId', 0) === parentId)
58+
.toList()
59+
.sort(siteUtil.siteSort)
60+
61+
const resultSize = results.size
62+
for (let i = 0; i < resultSize; i++) {
63+
const folder = results.get(i)
64+
if (folder.get('folderId') === folderKey) {
65+
continue
66+
}
67+
68+
const label = labelPrefix + folder.get('title')
69+
folders.push({
70+
folderId: folder.get('folderId'),
71+
label
72+
})
73+
const subSites = getFoldersWithoutKey(state, folderKey, folder.get('folderId'), (label || '') + ' / ')
74+
folders = folders.concat(subSites)
75+
}
76+
77+
return folders
78+
}
79+
6680
module.exports = {
6781
isFolderNameValid,
68-
getFolders,
69-
getNextFolderId
82+
getNextFolderId,
83+
getNextFolderName,
84+
getFoldersWithoutKey
7085
}

app/common/lib/bookmarkUtil.js

+52-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
const Immutable = require('immutable')
66

7+
// State
8+
const bookmarksState = require('../state/bookmarksState')
9+
710
// Constants
811
const dragTypes = require('../../../js/constants/dragTypes')
912
const {bookmarksToolbarMode} = require('../constants/settingsEnums')
@@ -12,6 +15,7 @@ const settings = require('../../../js/constants/settings')
1215
// Utils
1316
const domUtil = require('../../renderer/lib/domUtil')
1417
const siteUtil = require('../../../js/state/siteUtil')
18+
const siteCache = require('../state/siteCache')
1519
const {calculateTextWidth} = require('../../../js/lib/textCalculator')
1620
const {iconSize} = require('../../../js/constants/config')
1721
const {getSetting} = require('../../../js/settings')
@@ -123,6 +127,50 @@ const getDetailFromFrame = (frame) => {
123127
})
124128
}
125129

130+
/**
131+
* Checks if a location is bookmarked.
132+
*
133+
* @param state The application state Immutable map
134+
* @param {string} location
135+
* @return {boolean}
136+
*/
137+
const isLocationBookmarked = (state, location) => {
138+
const bookmarks = bookmarksState.getBookmarks(state)
139+
const siteKeys = siteCache.getLocationSiteKeys(state, location)
140+
141+
if (!siteKeys || siteKeys.length === 0) {
142+
return false
143+
}
144+
145+
return siteKeys.some(key => bookmarks.has(key))
146+
}
147+
148+
/**
149+
* Converts a siteDetail to createProperties format
150+
* @param {Object} bookmark - A bookmark detail as per app state
151+
* @return {Object} A createProperties plain JS object, not ImmutableJS
152+
*/
153+
const toCreateProperties = (bookmark) => {
154+
return {
155+
url: bookmark.get('location'),
156+
partitionNumber: bookmark.get('partitionNumber')
157+
}
158+
}
159+
160+
/**
161+
* Filters bookmarks relative to a parent folder
162+
* @param state - The application state
163+
* @param folderKey The folder key to filter to
164+
*/
165+
const getBookmarksByParentId = (state, folderKey) => {
166+
const bookmarks = bookmarksState.getBookmarks(state)
167+
if (!folderKey) {
168+
return bookmarks
169+
}
170+
171+
return bookmarks.filter((bookmark) => bookmark.get('parentFolderId') === folderKey)
172+
}
173+
126174
module.exports = {
127175
bookmarkHangerHeading,
128176
displayBookmarkName,
@@ -131,5 +179,8 @@ module.exports = {
131179
showFavicon,
132180
getDNDBookmarkData,
133181
getToolbarBookmarks,
134-
getDetailFromFrame
182+
getDetailFromFrame,
183+
isLocationBookmarked,
184+
toCreateProperties,
185+
getBookmarksByParentId
135186
}

app/common/state/bookmarkFoldersState.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const validateState = function (state) {
2929
const bookmarkFoldersState = {
3030
getFolders: (state) => {
3131
state = validateState(state)
32-
return state.get('bookmarkFolders')
32+
return state.get('bookmarkFolders', Immutable.Map())
3333
},
3434

3535
getFolder: (state, folderKey) => {

app/common/state/bookmarksState.js

+25
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,31 @@ const bookmarksState = {
114114
bookmarks = bookmarks.filter(bookmark => bookmark.get('parentId') !== parentId)
115115

116116
return state.set('bookmarks', bookmarks)
117+
},
118+
119+
/**
120+
* Update the favicon URL for all entries in the state sites
121+
* which match a given location. Currently, there should only be
122+
* one match, but this will handle multiple.
123+
*
124+
* @param state The application state
125+
* @param location URL for the entry needing an update
126+
* @param favicon favicon URL
127+
*/
128+
updateSiteFavicon: (state, location, favicon) => {
129+
if (UrlUtil.isNotURL(location)) {
130+
return state
131+
}
132+
133+
const siteKeys = siteCache.getLocationSiteKeys(state, location)
134+
if (!siteKeys || siteKeys.length === 0) {
135+
return state
136+
}
137+
138+
siteKeys.forEach((siteKey) => {
139+
state = state.setIn(['bookmarks', siteKey, 'favicon'], favicon)
140+
})
141+
return state
117142
}
118143
}
119144

app/common/state/tabState.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const windowState = require('./windowState')
1313
const { makeImmutable, isMap, isList } = require('./immutableUtil')
1414
// this file should eventually replace frameStateUtil
1515
const frameStateUtil = require('../../../js/state/frameStateUtil')
16-
const {isLocationBookmarked} = require('../../../js/state/siteUtil')
16+
const bookmarkUtil = require('../lib/bookmarkUtil')
1717

1818
const validateId = function (propName, id) {
1919
assert.ok(id, `${propName} cannot be null`)
@@ -455,7 +455,7 @@ const tabState = {
455455
}
456456

457457
const frameLocation = action.getIn(['frame', 'location'])
458-
const frameBookmarked = isLocationBookmarked(state, frameLocation)
458+
const frameBookmarked = bookmarkUtil.isLocationBookmarked(state, frameLocation)
459459
const frameValue = action.get('frame').set('bookmarked', frameBookmarked)
460460
tabValue = tabValue.set('frame', makeImmutable(frameValue))
461461
return tabState.updateTabValue(state, tabValue)

app/importer.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const locale = require('./locale')
2121
const tabMessageBox = require('./browser/tabMessageBox')
2222
const {makeImmutable} = require('./common/state/immutableUtil')
2323
const tabState = require('./common/state/tabState')
24+
const bookmarkFoldersUtil = require('./common/lib/bookmarkFoldersUtil')
2425

2526
var isImportingBookmarks = false
2627
var hasBookmarks
@@ -109,14 +110,14 @@ const getParentFolderId = (path, pathMap, sites, topLevelFolderId, nextFolderIdO
109110
}
110111

111112
importer.on('add-bookmarks', (e, bookmarks, topLevelFolder) => {
112-
let nextFolderId = siteUtil.getNextFolderId(AppStore.getState().get('sites'))
113+
let nextFolderId = bookmarkFoldersUtil.getNextFolderId(AppStore.getState().get('sites'))
113114
let nextFolderIdObject = { id: nextFolderId }
114115
let pathMap = {}
115116
let sites = []
116117
let topLevelFolderId = 0
117118
topLevelFolderId = nextFolderIdObject.id++
118119
sites.push({
119-
customTitle: siteUtil.getNextFolderName(AppStore.getState().get('sites'), topLevelFolder),
120+
title: bookmarkFoldersUtil.getNextFolderName(AppStore.getState().get('bookmarkFolders'), topLevelFolder),
120121
folderId: topLevelFolderId,
121122
parentFolderId: 0,
122123
lastAccessedTime: 0,

app/renderer/components/bookmarks/addEditBookmarkFolder.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44

55
const React = require('react')
66
const Immutable = require('immutable')
7+
const {StyleSheet, css} = require('aphrodite/no-important')
78

89
// Components
910
const ReduxComponent = require('../reduxComponent')
1011
const Dialog = require('../common/dialog')
1112
const AddEditBookmarkFolderForm = require('./addEditBookmarkFolderForm')
13+
const {CommonFormBookmarkHanger} = require('../common/commonForm')
14+
15+
// State
16+
const bookmarkFoldersState = require('../../../common/state/bookmarkFoldersState')
17+
const bookmarksState = require('../../../common/state/bookmarksState')
1218

1319
// Actions
1420
const windowActions = require('../../../../js/actions/windowActions')
@@ -17,12 +23,10 @@ const windowActions = require('../../../../js/actions/windowActions')
1723
const cx = require('../../../../js/lib/classSet')
1824
const bookmarkFoldersUtil = require('../../../common/lib/bookmarkFoldersUtil')
1925

20-
const {StyleSheet, css} = require('aphrodite/no-important')
26+
// Styles
2127
const globalStyles = require('../styles/global')
2228

23-
const {
24-
CommonFormBookmarkHanger
25-
} = require('../common/commonForm')
29+
2630

2731
class AddEditBookmarkFolder extends React.Component {
2832
constructor (props) {
@@ -54,10 +58,10 @@ class AddEditBookmarkFolder extends React.Component {
5458
props.partitionNumber = folderDetails.get('partitionNumber')
5559
props.folderName = folderDetails.get('title')
5660
props.isFolderNameValid = bookmarkFoldersUtil.isFolderNameValid(folderDetails.get('title'))
57-
props.folders = bookmarkFoldersUtil.getFolders(state.get('bookmarkFolders'), folderDetails.get('folderId')) // TODO (nejc) improve, primitives only
61+
props.folders = bookmarkFoldersUtil.getFoldersWithoutKey(state, folderDetails.get('folderId')) // TODO (nejc) improve, primitives only
5862
props.editKey = bookmarkDetail.get('editKey', null)
5963
props.closestKey = bookmarkDetail.get('closestKey', null)
60-
props.hasBookmarks = state.get('sites').size > 0 || state.get('bookmarkFolders').size > 0 // TODO change to bookmark from sites
64+
props.hasBookmarks = bookmarksState.getBookmarks(state).size > 0 || bookmarkFoldersState.getFolders(state).size > 0
6165

6266
return props
6367
}

0 commit comments

Comments
 (0)