From d5f5aba1aa773071863db3b4a3f76ff98a191ea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Norman=20Hauk=C3=A5s?= Date: Sat, 19 Mar 2016 11:36:45 +0100 Subject: [PATCH] added options for downloading only for current page or for all pages in that domain. Fixed a bug where loading the menu would try to load all tags for the domain and not just the page-specific tags. Fixed by adding filter. Also added option for deleting tags for the current page that is viewed. --- src/index-angular-app.html | 6 ++++- src/index.interfaces.ts | 1 + src/menu/menu.controller.ts | 39 +++++++++++++++++++--------- src/plugin-specific/manifest.json | 2 +- src/services/file.service.ts | 41 +++++++++++++++++------------- src/services/tagStorage.service.ts | 35 +++++++++++++++++-------- src/services/webpage.service.ts | 3 ++- 7 files changed, 83 insertions(+), 44 deletions(-) diff --git a/src/index-angular-app.html b/src/index-angular-app.html index 8257d7c..d4d9899 100644 --- a/src/index-angular-app.html +++ b/src/index-angular-app.html @@ -31,7 +31,11 @@

Tag you're it

-

Download tags

+

Menu: + Download tags (current page) | + Download all tags (domain) | + Delete tags from current page +

Mark one or two words on the page to the right. And we'll supply you with possible definitions. Select a definition and the diff --git a/src/index.interfaces.ts b/src/index.interfaces.ts index 6d20015..fbaa9d4 100644 --- a/src/index.interfaces.ts +++ b/src/index.interfaces.ts @@ -35,5 +35,6 @@ module tagIt { wordThatWasTagged: string; serializedSelectionRange: string; deserializedRange?: Range; + urlOfPageThatWasTagged: string; } } diff --git a/src/menu/menu.controller.ts b/src/menu/menu.controller.ts index 0377443..1f97198 100644 --- a/src/menu/menu.controller.ts +++ b/src/menu/menu.controller.ts @@ -39,7 +39,7 @@ module tagIt { }); // Reload existing tags - var tagsToLoad = this.tagStorageService.loadTags(); + var tagsToLoad = this.tagStorageService.loadTagsForCurrentPage(); this.$log.debug('these tags were found in storage'); this.$log.debug(tagsToLoad); @@ -58,11 +58,13 @@ module tagIt { //initialize and save the new tag var senseTag = this.webPageService.initializeNewTag(sense); this.tagStorageService.saveTag(senseTag); - this.backendService.sendTaggedDataToServer(senseTag); + + // deactivate backendService for now. + // this.backendService.sendTaggedDataToServer(senseTag); //re-add tags, with new tag. Clear menu options. this.webPageService.readdTagsToPage( - this.tagStorageService.loadTags() + this.tagStorageService.loadTagsForCurrentPage() ); this.clearMenuVariables(); }); @@ -71,13 +73,31 @@ module tagIt { /** * Enables a clickable button to download tags as a json file. */ - downloadTags() { + downloadTagsForPage() { if (typeof chrome === 'undefined') { this.$log.debug('Did not find chrome facilities. Can\'t download.') return; //do nothing } - var tags : ISenseTag[] = this.tagStorageService.loadTags(); - this.fileService.saveFile(tags); + this.fileService.saveFile(this.tagStorageService.loadTagsForCurrentPage()); + } + + downloadAllTagsForDomain() { + if (typeof chrome === 'undefined') { + this.$log.debug('Did not find chrome facilities. Can\'t download.') + return; //do nothing + } + this.fileService.saveFile(this.tagStorageService.loadAllTagsInLocalStorage()); + } + + /** + * Enables clickable link for removing tags. + */ + removeTagsFromLocalStorage() { + if (confirm('Really delete tags from the current page?')) { + this.webPageService.removeAllTagsFromPage(() => { + this.tagStorageService.deleteTagsFromCurrentPage() + }) + } } onWordSelectedEvent = (newWord: string) => { @@ -93,7 +113,7 @@ module tagIt { this.backendService.callServer(newWord) .then((synsets: any) => { this.$log.debug(synsets); - this.senses = synsets.data.senses;; + this.senses = synsets.data.senses; }); } @@ -112,10 +132,5 @@ module tagIt { this.senses = []; } - deleteTags() { - this.tagStorageService.deleteTags(); - location.reload(); - } - } } diff --git a/src/plugin-specific/manifest.json b/src/plugin-specific/manifest.json index b1f3497..bc26b98 100644 --- a/src/plugin-specific/manifest.json +++ b/src/plugin-specific/manifest.json @@ -1,7 +1,7 @@ { "name": "Tag you're it", "description": "A browser tool for tagging words with exact definitions.", - "version": "0.0.8", + "version": "0.0.9", "manifest_version": 2, "icons": { "16": "icon16.png", diff --git a/src/services/file.service.ts b/src/services/file.service.ts index 412a6c9..a572cd3 100644 --- a/src/services/file.service.ts +++ b/src/services/file.service.ts @@ -5,25 +5,30 @@ //Responsible for saving. module tagIt { - export class FileService { + export class FileService { - $log: ng.ILogService; + $log: ng.ILogService; + + /* @ngInject */ + constructor($log: ng.ILogService) { + this.$log = $log; + } + + saveFile(content: ISenseTag[]) { + var json = JSON.stringify(content, null, 2); + var blob = new Blob([json], { type: "application/json" }); + var url = URL.createObjectURL(blob); + var a = document.createElement('a'); + var date = new Date(); + a.download = ` + tags downloaded for ${window.location.hostname} + time of download ${date.toLocaleString()} + .json` + .replace('\n', '') + a.href = url; + a.click(); + URL.revokeObjectURL(url); //Explicitly release obj from memory. + } - /* @ngInject */ - constructor($log: ng.ILogService) { - this.$log = $log; } - - saveFile(content: ISenseTag[]) { - var json = JSON.stringify(content, null, 2); - var blob = new Blob([json], { type: "application/json" }); - var url = URL.createObjectURL(blob); - var a = document.createElement('a'); - var date = new Date(); - a.download = `tags downloaded for ${window.location.hostname} time of download ${date.toLocaleString()}.json`; - a.href = url; - a.click(); - } - - } } \ No newline at end of file diff --git a/src/services/tagStorage.service.ts b/src/services/tagStorage.service.ts index e5489de..dc407ce 100644 --- a/src/services/tagStorage.service.ts +++ b/src/services/tagStorage.service.ts @@ -10,7 +10,7 @@ module tagIt { $http: ng.IHttpService; $log: ng.ILogService; - $localStorage: any; + $localStorage: { tagStorage: ISenseTag[] }; /* @ngInject */ constructor($http: ng.IHttpService, $log: ng.ILogService, @@ -18,11 +18,11 @@ module tagIt { this.$http = $http; this.$log = $log; this.$localStorage = $localStorage; - + if (window.location.href.indexOf("tagitreset") !== -1) { - this.deleteTags(); // reset tag storage - this.$log.debug("Resetting tags for this page"); - } + this.deleteTagsFromCurrentPage(); // reset tag storage + this.$log.debug("Resetting tags for this page"); + } if (!this.$localStorage.tagStorage) { this.$localStorage.tagStorage = []; @@ -42,9 +42,11 @@ module tagIt { this.$localStorage.tagStorage = newList; } - deleteTags() { - this.$log.debug('deleting all tags from localstorage'); - delete this.$localStorage.tagStorage; + deleteTagsFromCurrentPage() { + this.$log.debug('deleting tags'); + this.$localStorage.tagStorage = + this.$localStorage.tagStorage.filter( + (tag: ISenseTag) => tag.urlOfPageThatWasTagged !== window.location.href) } saveTag(tagToSave: ISenseTag) { @@ -53,9 +55,20 @@ module tagIt { this.$localStorage.tagStorage.push(tagToSave); } - loadTags() { - this.$log.debug('loadTags'); - return this.$localStorage.tagStorage; + loadTagsForCurrentPage(): ISenseTag[] { + this.$log.debug('loadTagsForCurrentPage'); + return this.$localStorage.tagStorage + .filter((tag: ISenseTag) => + tag.urlOfPageThatWasTagged === window.location.href + ); + } + + /** + * Loads all tags in localstorage (for the current domain). + */ + loadAllTagsInLocalStorage() { + this.$log.debug('loadAllTagsInLocalStorage'); + return this.$localStorage.tagStorage } } diff --git a/src/services/webpage.service.ts b/src/services/webpage.service.ts index f5d3130..fbaa6dc 100644 --- a/src/services/webpage.service.ts +++ b/src/services/webpage.service.ts @@ -228,7 +228,8 @@ module tagIt { wordThatWasTagged: selection.toString(), iframeIndex: getIframeIndex(this.listOfFramesWithContent, iframeOfInterest), context: parentElement.textContent, - serializedSelectionRange: serializedRange + serializedSelectionRange: serializedRange, + urlOfPageThatWasTagged: window.location.href } /**