Factor out duplicate author/subject facet-generation code into new
authorMike Taylor <mike@indexdata.com>
Sun, 30 Jun 2013 08:01:31 +0000 (09:01 +0100)
committerMike Taylor <mike@indexdata.com>
Sun, 30 Jun 2013 08:01:31 +0000 (09:01 +0100)
add_single_facet() function. Target facet is still (for now) done
separately.

experiments/spclient/mkws.js

index e7217ad..3bf3069 100644 (file)
@@ -175,30 +175,24 @@ function my_onterm(data) {
     }
     termlists.push('</div>');
 
-    termlists.push('<div class="facet">');
-    termlists.push('<div class="termtitle">' + M('Subjects') + '</div>');
-    for (var i = 0; i < data.subject.length && i < SubjectMax; i++ ) {
-        termlists.push('<a href="#" onclick="limitQuery(\'su\', this.firstChild.nodeValue);return false;">'
-                      + data.subject[i].name
-                      + '</a><span> ('
-                      + data.subject[i].freq
-                      + ')</span><br/>');
-    }
-    termlists.push('</div>');
+    add_single_facet(termlists, "Subjects", data.subject, SubjectMax, "su");
+    add_single_facet(termlists, "Authors", data.author, AuthorMax, "au");
+
+    var termlist = document.getElementById("termlist");
+    replaceHtml(termlist, termlists.join(''));
+}
 
+function add_single_facet(termlists, caption, data, max, cclIndex) {
     termlists.push('<div class="facet">');
-    termlists.push('<div class="termtitle">' + M('Authors') + '</div>');
-    for (var i = 0; i < data.author.length && i < AuthorMax; i++ ) {
-        termlists.push('<a href="#" onclick="limitQuery(\'au\', this.firstChild.nodeValue);return false;">'
-                      + data.author[i].name
+    termlists.push('<div class="termtitle">' + M(caption) + '</div>');
+    for (var i = 0; i < data.length && i < max; i++ ) {
+        termlists.push('<a href="#" onclick="limitQuery(\'' + cclIndex + '\', this.firstChild.nodeValue);return false;">'
+                      + data[i].name
                       + '</a><span> ('
-                      + data.author[i].freq
+                      + data[i].freq
                       + ')</span><br/>');
     }
     termlists.push('</div>');
-
-    var termlist = document.getElementById("termlist");
-    replaceHtml(termlist, termlists.join(''));
 }
 
 function my_onrecord(data) {