accurator/commit

Added listener for pressing save/done button

authorChris Dijkshoorn
Sun Apr 17 12:38:51 2016 +0200
committerChris Dijkshoorn
Sun Apr 17 12:38:51 2016 +0200
commit677d84436ddc4907cf34aee78b7a353a5063bcae
tree4203098fd7cc85aa2ed2c4c1b2ac1d19eef59279
parent91e2b80d8a261fc3c8d070e15fa621dcca63f7ea
Diff style: patch stat
diff --git a/web/js/components/deniche-plugin.js b/web/js/components/deniche-plugin.js
index eca743a..943391f 100644
--- a/web/js/components/deniche-plugin.js
+++ b/web/js/components/deniche-plugin.js
@@ -68,8 +68,10 @@ annotorious.plugin.DenichePlugin.prototype.onInitAnnotator = function(annotator)
 	// get existing annotations on init annotorious
     if (this._anno.fields) {
 		var fields = this._anno.fields[imageId][fieldsId];
+
 		for (var i in fields) {
 			var field = fields[i];
+
 			if (field.showAnnotations) field.getAnnotations();
 		}
     }
@@ -127,6 +129,7 @@ annotorious.plugin.DenichePlugin.prototype.onFragmentCancel = function(ev) {
 }
 
 annotorious.plugin.DenichePlugin.prototype.addAnnotation = function (annotation, update) {
+	// possibly get old tag
 	var old = this._dirtytag;
 	if (!old) old = this._cleantags[ annotation.targetId ];
 
@@ -139,6 +142,7 @@ annotorious.plugin.DenichePlugin.prototype.addAnnotation = function (annotation,
 		annotation.compound_text = [ annotation.text ];
 	}
 
+	// if update is true or instantiated add to the rendered tags
 	if (update) {
 		this._cleantags[annotation.targetId] = annotation;
 		this._anno.addAnnotation(annotation, old);
@@ -152,7 +156,8 @@ annotorious.plugin.DenichePlugin.prototype.addAnnotation = function (annotation,
 
 annotorious.plugin.DenichePlugin.prototype.flushDirtyAnnotation = function(original) {
 	var dirty = this._dirtytag;
-	this._dirtytag = null;
+	this._dirtytag = null; // set to null since it will be processed
+
 	if (dirty) {
 		if (dirty.text) {
 			this._anno.addAnnotation(dirty,original);
@@ -166,6 +171,30 @@ annotorious.plugin.DenichePlugin.prototype.flushDirtyAnnotation = function(origi
 
 annotorious.plugin.DenichePlugin.prototype.installHandlers = function() {
 	var oSelf = this;
+	var node = $(oSelf.annotator.element);
+	var fieldsId = this.annotator.element.getElementsByTagName('img')[0].getAttribute('fields');
+	var imageId  = this.annotator.element.getElementsByTagName('img')[0].getAttribute('id');
+	var fields = this._anno.fields[imageId][fieldsId];
+
+	node.find(".annotorious-editor-button-save").on("click", function() {
+		// itterate through fields, saving values
+		for (var i=0; i<fields.length; i++) {
+			var inputField = $(fields[i].node).find("#" + fields[i].inputId);
+
+			if (inputField.val()) {
+				var annotation = inputField.val();
+
+				fields[i].submitAnnotation(
+					fields[i].MOTIVATION.tagging,
+					fields[i].target,
+					{'@value':annotation},
+					annotation
+				);
+
+				inputField.typeahead('val', ''); // clear input
+			}
+		}
+	});
 
 	this._anno.addHandler('onSelectionCompleted', function(event) {
 		oSelf.currentShape = event.shape;
@@ -174,10 +203,7 @@ annotorious.plugin.DenichePlugin.prototype.installHandlers = function() {
 	});
 
 	this._anno.addHandler('onEditorShown', function(annotation) {
-		// get the annotorious save and cancel button so we can manipulate them:
-		var node = $(oSelf.annotator.element);
-
-		// store buttons, change labels and change styling
+		// get the annotorious save and cancel button so we can manipulate them, change labels and change styling
 		oSelf._saveButtons[annotation.fieldsId] = node.find(".annotorious-editor-button-save").get(0);
 		oSelf._saveButtons[annotation.fieldsId].innerHTML = oSelf._labels.annoBtnDone;
 		oSelf._saveButtons[annotation.fieldsId].className += " btn btn-primary btn-sm";
@@ -205,6 +231,4 @@ annotorious.plugin.DenichePlugin.prototype.installHandlers = function() {
 	this._anno.addHandler('onAnnotationUpdated', function(original) {
 		oSelf.flushDirtyAnnotation(original);
 	});
-
-	// $(".annotorious-editor-button-save").get(0)
 }
diff --git a/web/js/components/field.js b/web/js/components/field.js
index b7057e1..8014af6 100644
--- a/web/js/components/field.js
+++ b/web/js/components/field.js
@@ -89,22 +89,20 @@ Field.prototype.submitAnnotation = function(motiv, target, body, label, graph) {
 		var targetImage = this.targetImage;
 
 		if (targetImage && target != targetImage) {
-			// Another annotation on selector with existing id (target id is the selector, not the image)
+			// another annotation on selector with existing id (target id is the selector, not the image)
 			targetObject = [{hasSource:targetImage, hasSelector:{value:shape}}, {'@id':target}];
 		} else {
-			// Annotation on new selector, id will be generated server-side
+			// annotation on new selector, id will be generated server-side
 			targetObject = [{hasSource:target, hasSelector:{value:shape}}];
 		}
 	} else {
-		// Annotation without fragment, on entire target image
+		// annotation without fragment, on entire target image
 		targetObject = [{'@id':target}];
 	}
 
 	var targetString = JSON.stringify(targetObject);
 	var bodyString = JSON.stringify(body);
 
-	// console.log("Saving the following ", "field: ", this.field, "hasTarget: ", targetString, "hasBody: ", bodyString, "label: ", label, "motivatedBy: ", motiv, "graph: ", graph);
-
 	$.ajax({type: "POST",
 			url: "api/annotation/add",
 			data: {
@@ -116,7 +114,7 @@ Field.prototype.submitAnnotation = function(motiv, target, body, label, graph) {
 				graph: graph
 			}})
 	.then(function(data) {
-		//Add annotation to list of annotations
+		// add annotation to list of annotations
 		_field.annotationList.add(data.annotation);
 		_field.addAnnotationFragment(data.annotation, false); // add but do not update open editor
 	});