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

Commit

Permalink
Removes a lot of functions out of siteUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Jul 16, 2017
1 parent 7a5f000 commit bdc93ce
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 303 deletions.
4 changes: 2 additions & 2 deletions app/browser/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const frameStateUtil = require('../../js/state/frameStateUtil')
const menuUtil = require('../common/lib/menuUtil')
const {getSetting} = require('../../js/settings')
const locale = require('../locale')
const {isLocationBookmarked} = require('../../js/state/siteUtil')
const platformUtil = require('../common/lib/platformUtil')
const bookmarkUtil = require('../common/lib/bookmarkUtil')
const isDarwin = platformUtil.isDarwin()
const isLinux = platformUtil.isLinux()

Expand Down Expand Up @@ -371,7 +371,7 @@ const updateRecentlyClosedMenuItems = (state) => {
}

const isCurrentLocationBookmarked = (state) => {
return isLocationBookmarked(state, currentLocation)
return bookmarkUtil.isLocationBookmarked(state, currentLocation)
}

const createBookmarksSubmenu = (state) => {
Expand Down
6 changes: 4 additions & 2 deletions app/browser/reducers/sitesReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const filtering = require('../../filtering')
const siteUtil = require('../../../js/state/siteUtil')
const syncUtil = require('../../../js/state/syncUtil')
const pinnedSitesUtil = require('../../common/lib/pinnedSitesUtil')
const bookmarkFoldersUtil = require('../../common/lib/bookmarkFoldersUtil')
const bookmarkUtil = require('../../common/lib/bookmarkUtil')
const {getSetting} = require('../../../js/settings')
const writeActions = require('../../../js/constants/sync/proto').actions

Expand All @@ -36,7 +38,7 @@ const updateTabBookmarked = (state, tabValue) => {
if (!tabValue || !tabValue.get('tabId')) {
return state
}
const bookmarked = siteUtil.isLocationBookmarked(state, tabValue.get('url'))
const bookmarked = bookmarkUtil.isLocationBookmarked(state, tabValue.get('url'))
return tabState.updateTabValue(state, tabValue.set('bookmarked', bookmarked))
}

Expand Down Expand Up @@ -197,7 +199,7 @@ const sitesReducer = (state, action, immutableAction) => {
}
break
case appConstants.APP_APPLY_SITE_RECORDS:
let nextFolderId = siteUtil.getNextFolderId(state.get('sites'))
let nextFolderId = bookmarkFoldersUtil.getNextFolderId(state.get('bookmarkFolders'))
// Ensure that all folders are assigned folderIds
action.records.forEach((record, i) => {
if (record.action !== writeActions.DELETE &&
Expand Down
81 changes: 48 additions & 33 deletions app/common/lib/bookmarkFoldersUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,12 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const siteUtil = require('../../../js/state/siteUtil')
const bookmarkFoldersState = require('../state/bookmarkFoldersState')

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

/**
* Obtains an array of folders
*/
const getFolders = (sites, folderId, parentId, labelPrefix) => {
parentId = parentId || 0
let folders = []
const results = sites
.filter(site => site.get('parentFolderId', 0) === parentId)
.toList()
.sort(siteUtil.siteSort)

const resultSize = results.size
for (let i = 0; i < resultSize; i++) {
const site = results.get(i)
if (site.get('folderId') === folderId) {
continue
}

const label = (labelPrefix || '') + site.get('title')
folders.push({
folderId: site.get('folderId'),
parentFolderId: site.get('parentFolderId'),
label
})
const subSites = getFolders(sites, folderId, site.get('folderId'), (label || '') + ' / ')
folders = folders.concat(subSites)
}

return folders
}

const getNextFolderIdItem = (sites) =>
sites.max((siteA, siteB) => {
const folderIdA = siteA.get('folderId')
Expand All @@ -63,8 +33,53 @@ const getNextFolderId = (sites) => {
return (maxIdItem ? (maxIdItem.get('folderId') || 0) : 0) + 1
}

const getNextFolderName = (folders, name) => {
if (!folders) {
return name
}
const site = folders.find((site) => site.get('customTitle') === name)
if (!site) {
return name
}
const filenameFormat = /(.*) \((\d+)\)/
let result = filenameFormat.exec(name)
if (!result) {
return getNextFolderName(folders, name + ' (1)')
}

const nextNum = parseInt(result[2]) + 1
return getNextFolderName(folders, result[1] + ' (' + nextNum + ')')
}

const getFoldersWithoutKey = (state, folderKey, parentId = 0, labelPrefix = '') => {
let folders = []
const results = bookmarkFoldersState.getFolders(state)
.filter(site => site.get('parentFolderId', 0) === parentId)
.toList()
.sort(siteUtil.siteSort)

const resultSize = results.size
for (let i = 0; i < resultSize; i++) {
const folder = results.get(i)
if (folder.get('folderId') === folderKey) {
continue
}

const label = labelPrefix + folder.get('title')
folders.push({
folderId: folder.get('folderId'),
label
})
const subSites = getFoldersWithoutKey(state, folderKey, folder.get('folderId'), (label || '') + ' / ')
folders = folders.concat(subSites)
}

return folders
}

module.exports = {
isFolderNameValid,
getFolders,
getNextFolderId
getNextFolderId,
getNextFolderName,
getFoldersWithoutKey
}
53 changes: 52 additions & 1 deletion app/common/lib/bookmarkUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

const Immutable = require('immutable')

// State
const bookmarksState = require('../state/bookmarksState')

// Constants
const dragTypes = require('../../../js/constants/dragTypes')
const {bookmarksToolbarMode} = require('../constants/settingsEnums')
Expand All @@ -12,6 +15,7 @@ const settings = require('../../../js/constants/settings')
// Utils
const domUtil = require('../../renderer/lib/domUtil')
const siteUtil = require('../../../js/state/siteUtil')
const siteCache = require('../state/siteCache')
const {calculateTextWidth} = require('../../../js/lib/textCalculator')
const {iconSize} = require('../../../js/constants/config')
const {getSetting} = require('../../../js/settings')
Expand Down Expand Up @@ -123,6 +127,50 @@ const getDetailFromFrame = (frame) => {
})
}

/**
* Checks if a location is bookmarked.
*
* @param state The application state Immutable map
* @param {string} location
* @return {boolean}
*/
const isLocationBookmarked = (state, location) => {
const bookmarks = bookmarksState.getBookmarks(state)
const siteKeys = siteCache.getLocationSiteKeys(state, location)

if (!siteKeys || siteKeys.length === 0) {
return false
}

return siteKeys.some(key => bookmarks.has(key))
}

/**
* Converts a siteDetail to createProperties format
* @param {Object} bookmark - A bookmark detail as per app state
* @return {Object} A createProperties plain JS object, not ImmutableJS
*/
const toCreateProperties = (bookmark) => {
return {
url: bookmark.get('location'),
partitionNumber: bookmark.get('partitionNumber')
}
}

/**
* Filters bookmarks relative to a parent folder
* @param state - The application state
* @param folderKey The folder key to filter to
*/
const getBookmarksByParentId = (state, folderKey) => {
const bookmarks = bookmarksState.getBookmarks(state)
if (!folderKey) {
return bookmarks
}

return bookmarks.filter((bookmark) => bookmark.get('parentFolderId') === folderKey)
}

module.exports = {
bookmarkHangerHeading,
displayBookmarkName,
Expand All @@ -131,5 +179,8 @@ module.exports = {
showFavicon,
getDNDBookmarkData,
getToolbarBookmarks,
getDetailFromFrame
getDetailFromFrame,
isLocationBookmarked,
toCreateProperties,
getBookmarksByParentId
}
2 changes: 1 addition & 1 deletion app/common/state/bookmarkFoldersState.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const validateState = function (state) {
const bookmarkFoldersState = {
getFolders: (state) => {
state = validateState(state)
return state.get('bookmarkFolders')
return state.get('bookmarkFolders', Immutable.Map())
},

getFolder: (state, folderKey) => {
Expand Down
25 changes: 25 additions & 0 deletions app/common/state/bookmarksState.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ const bookmarksState = {
bookmarks = bookmarks.filter(bookmark => bookmark.get('parentId') !== parentId)

return state.set('bookmarks', bookmarks)
},

/**
* Update the favicon URL for all entries in the state sites
* which match a given location. Currently, there should only be
* one match, but this will handle multiple.
*
* @param state The application state
* @param location URL for the entry needing an update
* @param favicon favicon URL
*/
updateSiteFavicon: (state, location, favicon) => {
if (UrlUtil.isNotURL(location)) {
return state
}

const siteKeys = siteCache.getLocationSiteKeys(state, location)
if (!siteKeys || siteKeys.length === 0) {
return state
}

siteKeys.forEach((siteKey) => {
state = state.setIn(['bookmarks', siteKey, 'favicon'], favicon)
})
return state
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/common/state/tabState.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const windowState = require('./windowState')
const { makeImmutable, isMap, isList } = require('./immutableUtil')
// this file should eventually replace frameStateUtil
const frameStateUtil = require('../../../js/state/frameStateUtil')
const {isLocationBookmarked} = require('../../../js/state/siteUtil')
const bookmarkUtil = require('../lib/bookmarkUtil')

const validateId = function (propName, id) {
assert.ok(id, `${propName} cannot be null`)
Expand Down Expand Up @@ -455,7 +455,7 @@ const tabState = {
}

const frameLocation = action.getIn(['frame', 'location'])
const frameBookmarked = isLocationBookmarked(state, frameLocation)
const frameBookmarked = bookmarkUtil.isLocationBookmarked(state, frameLocation)
const frameValue = action.get('frame').set('bookmarked', frameBookmarked)
tabValue = tabValue.set('frame', makeImmutable(frameValue))
return tabState.updateTabValue(state, tabValue)
Expand Down
5 changes: 3 additions & 2 deletions app/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const locale = require('./locale')
const tabMessageBox = require('./browser/tabMessageBox')
const {makeImmutable} = require('./common/state/immutableUtil')
const tabState = require('./common/state/tabState')
const bookmarkFoldersUtil = require('./common/lib/bookmarkFoldersUtil')

var isImportingBookmarks = false
var hasBookmarks
Expand Down Expand Up @@ -109,14 +110,14 @@ const getParentFolderId = (path, pathMap, sites, topLevelFolderId, nextFolderIdO
}

importer.on('add-bookmarks', (e, bookmarks, topLevelFolder) => {
let nextFolderId = siteUtil.getNextFolderId(AppStore.getState().get('sites'))
let nextFolderId = bookmarkFoldersUtil.getNextFolderId(AppStore.getState().get('sites'))
let nextFolderIdObject = { id: nextFolderId }
let pathMap = {}
let sites = []
let topLevelFolderId = 0
topLevelFolderId = nextFolderIdObject.id++
sites.push({
customTitle: siteUtil.getNextFolderName(AppStore.getState().get('sites'), topLevelFolder),
title: bookmarkFoldersUtil.getNextFolderName(AppStore.getState().get('bookmarkFolders'), topLevelFolder),
folderId: topLevelFolderId,
parentFolderId: 0,
lastAccessedTime: 0,
Expand Down
16 changes: 10 additions & 6 deletions app/renderer/components/bookmarks/addEditBookmarkFolder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

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

// Components
const ReduxComponent = require('../reduxComponent')
const Dialog = require('../common/dialog')
const AddEditBookmarkFolderForm = require('./addEditBookmarkFolderForm')
const {CommonFormBookmarkHanger} = require('../common/commonForm')

// State
const bookmarkFoldersState = require('../../../common/state/bookmarkFoldersState')
const bookmarksState = require('../../../common/state/bookmarksState')

// Actions
const windowActions = require('../../../../js/actions/windowActions')
Expand All @@ -17,12 +23,10 @@ const windowActions = require('../../../../js/actions/windowActions')
const cx = require('../../../../js/lib/classSet')
const bookmarkFoldersUtil = require('../../../common/lib/bookmarkFoldersUtil')

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

const {
CommonFormBookmarkHanger
} = require('../common/commonForm')


class AddEditBookmarkFolder extends React.Component {
constructor (props) {
Expand Down Expand Up @@ -54,10 +58,10 @@ class AddEditBookmarkFolder extends React.Component {
props.partitionNumber = folderDetails.get('partitionNumber')
props.folderName = folderDetails.get('title')
props.isFolderNameValid = bookmarkFoldersUtil.isFolderNameValid(folderDetails.get('title'))
props.folders = bookmarkFoldersUtil.getFolders(state.get('bookmarkFolders'), folderDetails.get('folderId')) // TODO (nejc) improve, primitives only
props.folders = bookmarkFoldersUtil.getFoldersWithoutKey(state, folderDetails.get('folderId')) // TODO (nejc) improve, primitives only
props.editKey = bookmarkDetail.get('editKey', null)
props.closestKey = bookmarkDetail.get('closestKey', null)
props.hasBookmarks = state.get('sites').size > 0 || state.get('bookmarkFolders').size > 0 // TODO change to bookmark from sites
props.hasBookmarks = bookmarksState.getBookmarks(state).size > 0 || bookmarkFoldersState.getFolders(state).size > 0

return props
}
Expand Down
Loading

0 comments on commit bdc93ce

Please sign in to comment.