This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 975
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converts bookmarksToolbar and bookmarkToolbarButton into redux
Resolves #9421 Auditors: @bsclifton @bridiver Test Plan:
- Loading branch information
Showing
5 changed files
with
265 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const Immutable = require('immutable') | ||
|
||
// Constants | ||
const dragTypes = require('../../../js/constants/dragTypes') | ||
|
||
// Utils | ||
const bookmarkUtil = require('../lib/bookmarkUtil') | ||
const domUtil = require('../../renderer/lib/domUtil') | ||
const siteUtil = require('../../../js/state/siteUtil') | ||
const {calculateTextWidth} = require('../../../js/lib/textCalculator') | ||
const {iconSize} = require('../../../js/constants/config') | ||
|
||
const bookmarksState = { | ||
getDNDBookmarkData: (state, bookmark) => { | ||
const data = (state.getIn(['dragData', 'dragOverData', 'draggingOverType']) === dragTypes.BOOKMARK && | ||
state.getIn(['dragData', 'dragOverData'], Immutable.Map())) || Immutable.Map() | ||
|
||
return (Immutable.is(data.get('draggingOverKey'), bookmark)) ? data : Immutable.Map() | ||
}, | ||
|
||
getToolbarBookmarks: (state) => { | ||
const sites = state.get('sites', Immutable.List()) | ||
|
||
const noParentItems = siteUtil.getBookmarks(sites) | ||
.sort(siteUtil.siteSort) | ||
.filter((bookmark) => !bookmark.get('parentFolderId')) | ||
let widthAccountedFor = 0 | ||
const overflowButtonWidth = 25 | ||
const showOnlyFavicon = bookmarkUtil.showOnlyFavicon() | ||
const showFavicon = bookmarkUtil.showFavicon() | ||
|
||
// Dynamically calculate how many bookmark items should appear on the toolbar | ||
// before it is actually rendered. | ||
const maxWidth = domUtil.getStyleConstants('bookmark-item-max-width') | ||
const padding = domUtil.getStyleConstants('bookmark-item-padding') * 2 | ||
// Toolbar padding is only on the left | ||
const toolbarPadding = domUtil.getStyleConstants('bookmarks-toolbar-padding') | ||
const bookmarkItemMargin = domUtil.getStyleConstants('bookmark-item-margin') * 2 | ||
// No margin for show only favicons | ||
const chevronMargin = domUtil.getStyleConstants('bookmark-item-chevron-margin') | ||
const fontSize = domUtil.getStyleConstants('bookmark-item-font-size') | ||
const fontFamily = domUtil.getStyleConstants('default-font-family') | ||
const chevronWidth = chevronMargin + fontSize | ||
const margin = showFavicon && showOnlyFavicon ? 0 : bookmarkItemMargin | ||
const windowWidth = window.innerWidth | ||
widthAccountedFor += toolbarPadding | ||
|
||
// Loop through until we fill up the entire bookmark toolbar width | ||
let i = 0 | ||
for (let bookmark of noParentItems) { | ||
const current = bookmark[1] | ||
let iconWidth = showFavicon ? iconSize : 0 | ||
// font-awesome file icons are 3px smaller | ||
if (showFavicon && !current.get('folderId') && !current.get('favicon')) { | ||
iconWidth -= 3 | ||
} | ||
const currentChevronWidth = showFavicon && current.get('folderId') ? chevronWidth : 0 | ||
if (showFavicon && showOnlyFavicon) { | ||
widthAccountedFor += padding + iconWidth + currentChevronWidth | ||
} else { | ||
const text = current.get('customTitle') || current.get('title') || current.get('location') | ||
widthAccountedFor += Math.min(calculateTextWidth(text, `${fontSize} ${fontFamily}`) + padding + iconWidth + currentChevronWidth, maxWidth) | ||
} | ||
widthAccountedFor += margin | ||
if (widthAccountedFor >= windowWidth - overflowButtonWidth) { | ||
break | ||
} | ||
i++ | ||
} | ||
|
||
return { | ||
visibleBookmarks: noParentItems.take(i), | ||
// Show at most 100 items in the overflow menu | ||
hiddneBookmarks: noParentItems.skip(i).take(100) | ||
} | ||
} | ||
} | ||
|
||
module.exports = bookmarksState |
Oops, something went wrong.