From f3f5d7b281fe561556132f22a6da3578e7d7a5a0 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 18 May 2014 12:16:41 +0100 Subject: Import of bookmark-folder-search code and tests. --- background_scripts/completion.coffee | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'background_scripts/completion.coffee') diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index 0b2e8b0b..afaec4ea 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -102,6 +102,7 @@ class Suggestion class BookmarkCompleter + folderSeparator: "/" currentSearch: null # These bookmarks are loaded asynchronously when refresh() is called. bookmarks: null @@ -113,14 +114,19 @@ class BookmarkCompleter onBookmarksLoaded: -> @performSearch() if @currentSearch performSearch: -> + # If the folder separator character is in the query, then we'll use the bookmark's full path as its title. + # Otherwise, we'll just use the its regular title. + usePathAndTitle = @currentSearch.queryTerms.reduce ((prev,term) => prev || term.indexOf(@folderSeparator) != -1), false results = if @currentSearch.queryTerms.length > 0 @bookmarks.filter (bookmark) => - RankingUtils.matches(@currentSearch.queryTerms, bookmark.url, bookmark.title) + suggestionTitle = if usePathAndTitle then bookmark.pathAndTitle else bookmark.title + RankingUtils.matches(@currentSearch.queryTerms, bookmark.url, suggestionTitle) else [] suggestions = results.map (bookmark) => - new Suggestion(@currentSearch.queryTerms, "bookmark", bookmark.url, bookmark.title, @computeRelevancy) + suggestionTitle = if usePathAndTitle then bookmark.pathAndTitle else bookmark.title + new Suggestion(@currentSearch.queryTerms, "bookmark", bookmark.url, suggestionTitle, @computeRelevancy) onComplete = @currentSearch.onComplete @currentSearch = null onComplete(suggestions) @@ -131,16 +137,18 @@ class BookmarkCompleter @bookmarks = @traverseBookmarks(bookmarks).filter((bookmark) -> bookmark.url?) @onBookmarksLoaded() - # Traverses the bookmark hierarchy, and retuns a flattened list of all bookmarks in the tree. + # Traverses the bookmark hierarchy, and returns a flattened list of all bookmarks. traverseBookmarks: (bookmarks) -> results = [] - toVisit = bookmarks.reverse() - while toVisit.length > 0 - bookmark = toVisit.pop() - results.push(bookmark) - toVisit.push.apply(toVisit, bookmark.children.reverse()) if (bookmark.children) + bookmarks.forEach (folder) => @traverseBookmarksRecursive folder, results results + # Recursive helper for `traverseBookmarks`. + traverseBookmarksRecursive: (bookmark, results, parent={pathAndTitle:""}) -> + bookmark.pathAndTitle = if bookmark.title then parent.pathAndTitle + @folderSeparator + bookmark.title else parent.pathAndTitle + results.push bookmark + bookmark.children.forEach((child) => @traverseBookmarksRecursive child, results, bookmark) if bookmark.children + computeRelevancy: (suggestion) -> RankingUtils.wordRelevancy(suggestion.queryTerms, suggestion.url, suggestion.title) -- cgit v1.2.3