accurator/commit

Flushing when promissed.

authorChris Dijkshoorn
Thu Apr 21 15:57:15 2016 +0200
committerChris Dijkshoorn
Thu Apr 21 15:57:15 2016 +0200
commit14dffc7b7afdaf1e8f8d2add6a57ec23f1d13ada
treed0f067a217cc7beda765750118eed46bbd3b7b4d
parenta627c9e4919baf6abdf1b7f85fe492c35e96c528
Diff style: patch stat
diff --git a/web/js/components/annotations.js b/web/js/components/annotations.js
index 84b9f11..fabf36f 100644
--- a/web/js/components/annotations.js
+++ b/web/js/components/annotations.js
@@ -26,8 +26,8 @@ AnnotationList.prototype.remove = function(annotation) {
 }
 
 AnnotationList.prototype.render = function() {
-	var node = $(this.node).find("#" + this.divId);
-
+	var node = $(this.node);
+	console.log(node);
 	// empty the field before showing all annotations
 	node.empty();
 
diff --git a/web/js/components/deniche-plugin.js b/web/js/components/deniche-plugin.js
index 943391f..905ba7e 100644
--- a/web/js/components/deniche-plugin.js
+++ b/web/js/components/deniche-plugin.js
@@ -176,26 +176,6 @@ annotorious.plugin.DenichePlugin.prototype.installHandlers = function() {
 	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;
 		var currentFieldsId = event.mouseEvent.target.parentNode.getElementsByTagName('img')[0].getAttribute('fields');
@@ -225,7 +205,38 @@ annotorious.plugin.DenichePlugin.prototype.installHandlers = function() {
 	});
 
 	this._anno.addHandler('onAnnotationCreated', function(original) {
-		oSelf.flushDirtyAnnotation(original);
+		var counter = 0;
+		var promises =[];
+
+		// 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();
+
+				promises[counter] = fields[i].submitAnnotation(
+					fields[i].MOTIVATION.tagging,
+					fields[i].target,
+					{'@value':annotation},
+					annotation
+				);
+
+				inputField.typeahead('val', ''); // clear input
+				counter++;
+			}
+		}
+
+		if (promises.length > 0) {
+			// wait for all the annotatoins to be added before flushing
+			$.when.apply($, promises)
+			.then(function() {
+				oSelf.flushDirtyAnnotation(original);
+			});
+		} else {
+			// no promises to wait for
+			oSelf.flushDirtyAnnotation(original);
+		}
 	});
 
 	this._anno.addHandler('onAnnotationUpdated', function(original) {
diff --git a/web/js/components/field.js b/web/js/components/field.js
index 8014af6..04f3381 100644
--- a/web/js/components/field.js
+++ b/web/js/components/field.js
@@ -103,7 +103,7 @@ Field.prototype.submitAnnotation = function(motiv, target, body, label, graph) {
 	var targetString = JSON.stringify(targetObject);
 	var bodyString = JSON.stringify(body);
 
-	$.ajax({type: "POST",
+	return $.ajax({type: "POST",
 			url: "api/annotation/add",
 			data: {
 				field: this.field,
@@ -112,7 +112,7 @@ Field.prototype.submitAnnotation = function(motiv, target, body, label, graph) {
 				label: label,
 			   	motivatedBy: motiv,
 				graph: graph
-			}})
+	}})
 	.then(function(data) {
 		// add annotation to list of annotations
 		_field.annotationList.add(data.annotation);