-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use autocomplete feature for generating tags
Introduce tagsService which will handle the necessary filtering of tags and saving them into the local storage in an appropriate format.
- Loading branch information
1 parent
ada5012
commit 90c782d
Showing
10 changed files
with
397 additions
and
76 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
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
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,58 @@ | ||
createTagHelpers = ['localstorage', (localstorage) -> | ||
TAGS_LIST_KEY = 'hypothesis.user.tags.list' | ||
TAGS_MAP_KEY = 'hypothesis.user.tags.map' | ||
|
||
filterTags: (query) -> | ||
savedTags = localstorage.getObject TAGS_LIST_KEY | ||
savedTags ?= [] | ||
|
||
# Only show tags having query as a substring | ||
filterFn = (e) -> | ||
e.toLowerCase().indexOf(query.toLowerCase()) > -1 | ||
|
||
savedTags.filter(filterFn) | ||
|
||
# Add newly added tags from an annotation to the stored ones and refresh | ||
# timestamp for every tags used. | ||
refreshTags: (existingTags, allTags) -> | ||
savedTags = localstorage.getObject TAGS_MAP_KEY | ||
savedTags ?= {} | ||
|
||
for tag in allTags | ||
if existingTags? and (tag.text in existingTags) and savedTags[tag.text]? | ||
# We've already counted this tag, just update the timestamp | ||
savedTags[tag.text].updated = Date.now() | ||
else | ||
# Update the tag counter too | ||
if savedTags[tag.text]? | ||
savedTags[tag.text].count += 1 | ||
savedTags[tag.text].updated = Date.now() | ||
else | ||
# Brand new tag, create an entry for it | ||
savedTags[tag.text] = { | ||
text: tag.text | ||
count: 1 | ||
updated: Date.now() | ||
} | ||
|
||
localstorage.setObject TAGS_MAP_KEY, savedTags | ||
|
||
tagsList = [] | ||
for tag of savedTags | ||
tagsList[tagsList.length] = tag | ||
|
||
# Now produce TAGS_LIST, ordered by (count desc, lexical asc) | ||
compareFn = (t1, t2) -> | ||
if savedTags[t1].count != savedTags[t2].count | ||
return savedTags[t2].count - savedTags[t1].count | ||
else | ||
return -1 if t1 < t2 | ||
return 1 if t1 > t2 | ||
return 0 | ||
|
||
tagsList = tagsList.sort(compareFn) | ||
localstorage.setObject TAGS_LIST_KEY, tagsList | ||
] | ||
|
||
angular.module('h.helpers') | ||
.service('tagHelpers', createTagHelpers) |
Oops, something went wrong.