More refactoring. Reset menu after having tagged a word.

This commit is contained in:
Nils Norman Haukås 2015-11-14 14:51:31 +01:00
parent f44177a938
commit a54af6a323
2 changed files with 39 additions and 38 deletions

View file

@ -8,7 +8,7 @@ module tagIt {
selectedWord = "";
senses : Object[];
backendService : BackendService;
selectedWordService : WebPageService;
webPageService : WebPageService;
$log : ng.ILogService;
$scope: ng.IScope;
@ -20,15 +20,16 @@ module tagIt {
this.$log = $log;
this.$scope = $scope;
this.backendService = BackendService;
this.selectedWordService = WebPageService;
this.webPageService = WebPageService;
// Wire up clicklistener
this.selectedWordService.wireUpListener(this.onWordSelected,
this.webPageService.wireUpListener(this.onWordSelected,
this.onWordDeSelected);
}
onTagSelect (sense: ISense) {
this.selectedWordService.addTagToPage(sense);
this.webPageService.addTagToPage(sense);
this.clearMenuVariables();
this.backendService.storeTaggingInformation({});
}
@ -43,9 +44,16 @@ module tagIt {
onWordDeSelected = () => {
this.$log.debug("onWordDeSelected");
this.clearMenuVariables()
// since the click did not originate from
// an ng-click or the like we need to
// do an explicit view refresh
this.$scope.$apply();
}
clearMenuVariables = () {
this.selectedWord = "";
this.senses = [];
this.$scope.$apply();
}
selectWord (sense : ISense) {

View file

@ -8,6 +8,8 @@ module tagIt {
export class WebPageService {
$log : ng.ILogService;
// when clicking the menu to select a synset
// we need to remember what the currently selected word was
currentSelectionRange : any;
/* @ngInject */
@ -23,24 +25,40 @@ module tagIt {
if (!document.hasFocus()) {
return true;
}
var selection = that.findSelection();
if(selection) {
this.currentSelectionRange = selection.getRangeAt(0).cloneRange();
callbackOnSelectFunc(joinLongWords(selection.toString()));
// call callbackOnSelectFunc if there was a word selected
if(this.findSelectedText()) {
this.currentSelectionRange = this.getClonedSelectionRange();
callbackOnSelectFunc(joinLongWords(this.findSelectedText()));
// call callbackOnDeSelectFunc if there was a word selected
} else {
callbackOnDeSelectFunc();
}
// clicks should propagate upwards to other things
// evt.stopPropagation();
// evt.preventDefault();
}, false);
function joinLongWords (possiblyLongWord: string) {
return possiblyLongWord.replace(" ","_");
}
}
getClonedSelectionRange () {
return this.findSelection().getRangeAt(0).cloneRange();
}
findSelection () {
return window.getSelection();
}
findSelectedText () {
var selectedText = this.findSelection().toString();
if (selectedText) {
this.$log.debug('text that was selected: ' + selectedText);
return selectedText;
} else {
return;
}
}
// place spans around a tagged word.
addTagToPage (sense : ISense) {
addTagToPage = (sense : ISense) => {
var windowSelection = window.getSelection();
var range = this.currentSelectionRange;
var span : HTMLSpanElement = document.createElement('span');
@ -52,30 +70,5 @@ module tagIt {
// windowSelection.addRange(range);
this.$log.debug('addTagToPage');
}
private findSelection () {
var focused : any = document.activeElement;
var selectedText : string;
// try grabbing text from an input or textarea field
// commenting this until we need to figure out tagging of editable fields
// if (focused) {
// try {
// selectedText = focused.value.substring(
// focused.selectionStart, focused.selectionEnd);
// } catch (err) {
// }
// }
// if previous method did not work ask window for selection
if (selectedText == undefined) {
var currentSelection : Selection = window.getSelection();
var selectedText = currentSelection.toString();
}
if (selectedText) {
this.$log.debug('text that was selected: ' + selectedText);
return currentSelection;
} else {
return;
}
}
}
}