From 7ba53dd6bff2d193e89dc6de0072ff795dbc2192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20Norman=20Hauk=C3=A5s?= Date: Sun, 22 Nov 2015 18:00:25 +0100 Subject: [PATCH] added uuid generation so that tags can be truly unique. Application now supports deleting tags from page. --- Gulpfile.js | 1 + package.json | 4 +++- src/index.interfaces.ts | 1 + src/services/tagStorage.service.ts | 11 +++++++++++ src/services/webpage.service.ts | 29 ++++++++++++++--------------- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/Gulpfile.js b/Gulpfile.js index aa0c31a..33ccfd6 100644 --- a/Gulpfile.js +++ b/Gulpfile.js @@ -70,6 +70,7 @@ gulp.task('dist-node-modules', function () { 'node_modules/jquery/dist/jquery.js', 'node_modules/lodash/index.js', 'node_modules/ngstorage/ngStorage.js', + 'node_modules/node-uuid/uuid.js', 'node_modules/rangy/lib/rangy-core.js', 'node_modules/rangy/lib/rangy-serializer.js' ], {base: 'node_modules'}) diff --git a/package.json b/package.json index 6c92714..b07f56a 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,8 @@ "lodash": "^3.10.1", "merge2": "^0.3.6", "ngstorage": "^0.3.10", - "rangy": "^1.3.0" + "node-uuid": "^1.4.7", + "rangy": "^1.3.0", + "uuid": "^2.0.1" } } diff --git a/src/index.interfaces.ts b/src/index.interfaces.ts index 5a3ad69..bcce7ed 100644 --- a/src/index.interfaces.ts +++ b/src/index.interfaces.ts @@ -27,6 +27,7 @@ module tagIt { selectionRange: selectionRangeObject */ export interface ISenseTag { + id: string, userEmail: string; sense: ISense; context: string; diff --git a/src/services/tagStorage.service.ts b/src/services/tagStorage.service.ts index e412c90..1bad22c 100644 --- a/src/services/tagStorage.service.ts +++ b/src/services/tagStorage.service.ts @@ -21,6 +21,17 @@ module tagIt { // this.deleteTags(); // reset tag storage } + deleteTagById (uuid: string) { + this.$log.debug('deleting tag from localstorage with uuid: ' + uuid); + var newList : ISenseTag[] = []; + angular.forEach(this.$localStorage.tagStorage, function(element) { + if(element.id !== uuid) { + this.push(element); + } + }, newList); + this.$localStorage.tagStorage = newList; + } + deleteTags () { this.$log.debug('deleting all tags from localstorage'); delete this.$localStorage.tagStorage; diff --git a/src/services/webpage.service.ts b/src/services/webpage.service.ts index e2e9788..6885816 100644 --- a/src/services/webpage.service.ts +++ b/src/services/webpage.service.ts @@ -7,6 +7,7 @@ module tagIt { */ declare var rangy: any; + declare var uuid: any; export class WebPageService { @@ -32,7 +33,8 @@ module tagIt { } else if (wasRemoveTagButtonClicked(evt)) { this.$log.debug('remove tag button was clicked'); - removeTagFromText(evt); + removeTagFromWebpage(evt); + this.tagStorageService.deleteTagById(evt.target.parentElement.id); } else if(this.findSelectedText()) { this.currentSelectionRange = this.getClonedSelectionRange(); @@ -47,16 +49,11 @@ module tagIt { function wasRemoveTagButtonClicked (evt : any) { return evt.target.className === 'js-tagit-remove-tag'; } - function removeTagFromText (evt) { - debugger; - - //get text - - //replace span with text - - //notify tag storage of removal - - + function removeTagFromWebpage (evt: any) { + var theOriginalTextNode = evt.target.previousSibling; + var theSurroundingSpanElement = evt.target.parentElement; + theSurroundingSpanElement.parentNode + .replaceChild(theOriginalTextNode, theSurroundingSpanElement); } } @@ -84,9 +81,11 @@ module tagIt { var range = this.currentSelectionRange; var serializedRange = rangy.serializeRange( range, false, document.getElementById('tagit-body')); - this.surroundRangeWithSpan(sense, range); + var generatedUuid : string = uuid.v4(); + this.surroundRangeWithSpan(sense, range, generatedUuid); return { + id: generatedUuid, userEmail: 'testEmail', sense: sense, context: range.commonAncestorContainer.innerText, @@ -119,13 +118,13 @@ module tagIt { //tag that text with a span and a remove button. this.surroundRangeWithSpan(tagToLoad.sense, - selection.getRangeAt(0).cloneRange()); + selection.getRangeAt(0).cloneRange(), tagToLoad.id); } - private surroundRangeWithSpan (sense: ISense, range: Range) { + private surroundRangeWithSpan (sense: ISense, range: Range, uuid: string) { // add span around content var span : HTMLSpanElement = document.createElement('span'); - span.id = sense.senseid; + span.id = uuid; span.title = sense.explanation; span.className = 'tagit-tag'; range.surroundContents(span);