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') 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 From 5d1b563de8f504c4175f4c203534955b97496281 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 18 May 2014 12:47:50 +0100 Subject: Strip top-level chrome folders. --- background_scripts/completion.coffee | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'background_scripts') diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index afaec4ea..fb3356da 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -140,7 +140,17 @@ class BookmarkCompleter # Traverses the bookmark hierarchy, and returns a flattened list of all bookmarks. traverseBookmarks: (bookmarks) -> results = [] - bookmarks.forEach (folder) => @traverseBookmarksRecursive folder, results + bookmarks.forEach (folder) => + # folder is a chrome virtual root folder. + folder.children.forEach((folder) => + # folder is one of the chrome, built-in folders ("Other Bookmarks", "Mobile Bookmarks", ...). + folder.children.forEach((bookmark) => + # bookmark is a user-defined folder, or itself a bookmark. + @traverseBookmarksRecursive bookmark, results)) + # + # We do not add the (possible) bookmark itself. The user has used the "/" key to begin searching + # within bookmark folders. It's not clear what that even means for top-level bookmarks. + # results.push bookmark results # Recursive helper for `traverseBookmarks`. -- cgit v1.2.3 From a1467b3f0ff2f7e3703edb032909980454763f1b Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 18 May 2014 12:48:55 +0100 Subject: Require bookmark folder separator to be first character of a query term. --- background_scripts/completion.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'background_scripts') diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index fb3356da..54b94eb5 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -114,9 +114,9 @@ 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. + # If the folder separator character the first character in any query term, 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 + usePathAndTitle = @currentSearch.queryTerms.reduce ((prev,term) => prev || term.indexOf(@folderSeparator) == 0), false results = if @currentSearch.queryTerms.length > 0 @bookmarks.filter (bookmark) => -- cgit v1.2.3 From c16d0974cd7c688050c9f2f7e539d5db3077a567 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 18 May 2014 13:09:39 +0100 Subject: Just ignore known and enumerated top-level folders. --- background_scripts/completion.coffee | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'background_scripts') diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index 54b94eb5..3e80526a 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -137,25 +137,25 @@ class BookmarkCompleter @bookmarks = @traverseBookmarks(bookmarks).filter((bookmark) -> bookmark.url?) @onBookmarksLoaded() + ignoreTopLevel: + 'Other Bookmarks': true + 'Mobile Bookmarks': true + 'Bookmarks Bar': true + # Traverses the bookmark hierarchy, and returns a flattened list of all bookmarks. traverseBookmarks: (bookmarks) -> results = [] bookmarks.forEach (folder) => - # folder is a chrome virtual root folder. - folder.children.forEach((folder) => - # folder is one of the chrome, built-in folders ("Other Bookmarks", "Mobile Bookmarks", ...). - folder.children.forEach((bookmark) => - # bookmark is a user-defined folder, or itself a bookmark. - @traverseBookmarksRecursive bookmark, results)) - # - # We do not add the (possible) bookmark itself. The user has used the "/" key to begin searching - # within bookmark folders. It's not clear what that even means for top-level bookmarks. - # results.push bookmark + @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 + bookmark.pathAndTitle = + if bookmark.title and not (parent.pathAndTitle == "" and @ignoreTopLevel[bookmark.title]) + parent.pathAndTitle + @folderSeparator + bookmark.title + else + parent.pathAndTitle results.push bookmark bookmark.children.forEach((child) => @traverseBookmarksRecursive child, results, bookmark) if bookmark.children -- cgit v1.2.3 From d85686ac5e0c35a95d0dd17f08a2b6b269c9349c Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 18 May 2014 14:19:52 +0100 Subject: Document top-level folders. --- background_scripts/completion.coffee | 1 + 1 file changed, 1 insertion(+) (limited to 'background_scripts') diff --git a/background_scripts/completion.coffee b/background_scripts/completion.coffee index 3e80526a..07f97322 100644 --- a/background_scripts/completion.coffee +++ b/background_scripts/completion.coffee @@ -137,6 +137,7 @@ class BookmarkCompleter @bookmarks = @traverseBookmarks(bookmarks).filter((bookmark) -> bookmark.url?) @onBookmarksLoaded() + # If these names occur as top-level bookmark names, then they are not included in the names of bookmark folders. ignoreTopLevel: 'Other Bookmarks': true 'Mobile Bookmarks': true -- cgit v1.2.3