Support annotation tooltips in table view for multiple values

Change-Id: Ib314e3cf890fe9e359914df47b0826a369e0dfe2
diff --git a/dev/demo/match-tabledemo.js b/dev/demo/match-tabledemo.js
index fe422ef..91607ea 100644
--- a/dev/demo/match-tabledemo.js
+++ b/dev/demo/match-tabledemo.js
@@ -30,8 +30,10 @@
   "    <span title=\"cnx/syn:@PREMOD\">" +
   "      <span title=\"mate/l:deutlich\">" +
   "        <span title=\"mate/m:degree:pos\">" +
-  "          <span title=\"mate/p:ADJD\">" +
-  "            <span title=\"opennlp/p:ADJD\">deutlich</span>" +
+  "          <span title=\"mate/m:case:nom\">" +
+  "            <span title=\"mate/p:ADJD\">" +
+  "              <span title=\"opennlp/p:ADJD\">deutlich</span>" +
+  "            </span>" +
   "          </span>" +
   "        </span>" +
   "      </span>" +
diff --git a/dev/js/spec/matchSpec.js b/dev/js/spec/matchSpec.js
index 806aee8..ccf32de 100644
--- a/dev/js/spec/matchSpec.js
+++ b/dev/js/spec/matchSpec.js
@@ -167,7 +167,7 @@
   return me.firstChild;
 };
 
-define(['match'], function () {
+define(['match', 'hint/foundries/cnx', 'hint/foundries/mate'], function () {
 
   // Override getMatchInfo API call
   KorAP.API.getMatchInfo = function (x, param, cb) {
@@ -555,6 +555,10 @@
       expect(tr.children[3].firstChild.nodeValue).toEqual('A');
       expect(tr.children[4].firstChild.firstChild.nodeValue).toEqual('A');
       expect(tr.children[4].lastChild.firstChild.nodeValue).toEqual('ADJA');
+
+      expect(tr.children[4].firstChild.getAttribute("title")).toEqual('Adjective');
+      expect(tr.children[2].getAttribute("title")).toEqual('Adverb');
+
     });
   });
 
diff --git a/dev/js/src/hint/foundries.js b/dev/js/src/hint/foundries.js
index 6f2ac02..67a6a88 100644
--- a/dev/js/src/hint/foundries.js
+++ b/dev/js/src/hint/foundries.js
@@ -1,3 +1,81 @@
+window.KorAP = window.KorAP || {};
+KorAP.annotationHelper = KorAP.annotationHelper || { '-' : [] };
+
 define(function () {
-  return (KorAP.annotationHelper = KorAP.annotationHelper || { '-' : [] });
+
+  var ah = KorAP.annotationHelper;
+
+  ah["getDesc"] = function (foundryLayer, value) {
+
+    if (!foundryLayer)
+      return;
+
+    var anno = this[foundryLayer];
+
+    if (!anno)
+      return;
+
+    if (value.indexOf(':') < 0) {
+      value += ' ';
+
+      // Iterate over all annotations and add the descriptions
+      // This is a classic hash-lookup-case, but we have
+      // to deal with lists ...
+      for (var i = 0; i < anno.length; i++) {
+        if (anno[i] &&
+            anno[i][1] == value) {
+          if (anno[i][2])
+            return anno[i][2];
+          else
+            return;
+        };
+      };
+
+      return;
+    }
+    else {
+      var text = '';
+      var v = value.split(":");
+      var l1 = v[0];
+      var l2 = v[1];
+
+      l1 += ':';
+
+      // Add key description
+      for (var i = 0; i < anno.length; i++) {
+        if (anno[i] &&
+            anno[i][1] == l1) {
+          if (anno[i][2])
+            text += anno[i][2];
+          else
+            text += anno[i][0];
+          break;
+        };
+      };
+
+      // Nothinmg found
+      if (text.length === 0)
+        return;
+
+      // Check next level
+      anno = this[foundryLayer + l1];
+
+      l2 += ' ';
+
+      // Add value description
+      for (var i = 0; i < anno.length; i++) {
+        if (anno[i] &&
+            anno[i][1] == l2) {
+          if (anno[i][2])
+            text += ': ' + anno[i][2];
+
+          return text;
+        };
+      };      
+    };
+
+    return '';
+  };
+
+  return ah;
 });
diff --git a/dev/js/src/hint/foundries/cnx.js b/dev/js/src/hint/foundries/cnx.js
index 608d4e6..9316cbd 100644
--- a/dev/js/src/hint/foundries/cnx.js
+++ b/dev/js/src/hint/foundries/cnx.js
@@ -58,6 +58,6 @@
     ["@NH", "@NH ", "Nominal Head"],
     ["@POSTMOD", "@POSTMOD ", "Postmodifier"],
     ["@PREMARK", "@PREMARK ", "Preposed Marker"],
-    ["@PREMOD", "@POSTMOD ", "Premodifier"]
+    ["@PREMOD", "@PREMOD ", "Premodifier"]
   ];
 });
diff --git a/dev/js/src/match/table.js b/dev/js/src/match/table.js
index 481e60a..a8ae51d 100644
--- a/dev/js/src/match/table.js
+++ b/dev/js/src/match/table.js
@@ -155,24 +155,41 @@
       var tr = table.appendChild(d.createElement('thead'))
           .appendChild(d.createElement('tr'));
 
+      var ah = KorAP.annotationHelper || { "getDesc" : function () {}};
+
       // Add cell to row
-      var addCell = function (type, name) {
+      var addCell = function (type, key, value) {        
         var c = this.appendChild(d.createElement(type))
-        if (name === undefined)
+        if (value === undefined)
           return c;
 
-        if (name instanceof Array && name[1] !== undefined) {
+        if (key && value instanceof Array && value[1] !== undefined) {
 
           // There are multiple values to add
           c.classList.add('matchkeyvalues');
-          for (var n = 0; n < name.length; n++) {
-            var text = d.createTextNode(name[n]);
+          for (var n = 0; n < value.length; n++) {
+            var text = d.createTextNode(value[n]);
             var e = c.appendChild(d.createElement('div'));
             e.appendChild(text);
+
+            var anno = ah.getDesc(key, value[n]);
+
+            if (anno)
+              e.setAttribute("title", anno);
           };
         }
+
         else {
-          c.appendChild(d.createTextNode(name));
+
+          if (value instanceof Array)
+            value = value[0];
+
+          c.appendChild(d.createTextNode(value));
+
+          // Add tooltip
+          var anno = ah.getDesc(key, value);
+          if (anno)
+            c.setAttribute("title", anno);
         };
 
         return c;
@@ -181,12 +198,12 @@
       tr.addCell = addCell;
 
       // Add header information
-      tr.addCell('th', 'Foundry');
-      tr.addCell('th', 'Layer');
+      tr.addCell('th', undefined, 'Foundry');
+      tr.addCell('th', undefined, 'Layer');
 
       // Add tokens
       for (var i in this._token) {
-        tr.addCell('th', this.getToken(i));
+        tr.addCell('th', undefined, this.getToken(i));
       };
       
       var tbody = table.appendChild(
@@ -195,8 +212,6 @@
 
       var foundryList = Object.keys(this._foundry).sort();
 
-      var ah = KorAP.annotationHelper || {};
-
       for (var f = 0; f < foundryList.length; f++) {
         var foundry = foundryList[f];
         var layerList =
@@ -210,11 +225,10 @@
           tr.setAttribute('tabindex', 0);
           tr.addCell = addCell;
 
-          tr.addCell('th', foundry);
-          tr.addCell('th', layer);
+          tr.addCell('th', undefined, foundry);
+          tr.addCell('th', undefined, layer);
 
           var key = foundry + '/' + layer + '=';
-          var anno = ah[key];
 
           for (var v = 0; v < this.length(); v++) {
 
@@ -224,24 +238,9 @@
             // Add cell to row
             var cell = tr.addCell(
               'td',
+              key,
               value 
             );
-
-            // Iterate over all annotations and add the descriptions
-            if (anno) {
-
-              // This is a classic hash-lookup-case, but we have
-              // to deal with lists ...
-              for (var i = 0; i < anno.length; i++) {
-                if (anno[i] &&
-                    anno[i][0] &&
-                    anno[i][2] &&
-                    anno[i][0] == value) {
-                  cell.setAttribute('title', anno[i][2]);
-                  break;
-                };
-              };
-            };
           };
         };
       };