Emit "mkwsFacet" class rather than just "facet"
[mkws-moved-to-github.git] / src / mkws-widget-termlists.js
1 mkws.registerWidgetType('Termlists', function() {
2     var that = this;
3     var M = mkws.M;
4
5     this.team.queue("termlists").subscribe(function(data) {
6         // no facets: this should never happen
7         var facets = that.config.facets;
8         if (!facets || facets.length == 0) {
9             alert("onTerm called even though we have no facets: " + $.toJSON(data));
10             $(that.node).hide();
11             return;
12         }
13
14         // display if we first got results
15         $(that.node).show();
16
17         var acc = [];
18         acc.push('<div class="title">' + M('Termlists') + '</div>');
19
20         for (var i = 0; i < facets.length; i++) {
21             if (facets[i] == "xtargets") {
22                 addSingleFacet(acc, "Sources",  data.xtargets, 16, null);
23             } else if (facets[i] == "subject") {
24                 addSingleFacet(acc, "Subjects", data.subject,  10, "subject");
25             } else if (facets[i] == "author") {
26                 addSingleFacet(acc, "Authors",  data.author,   10, "author");
27             } else {
28                 alert("bad facet configuration: '" + facets[i] + "'");
29             }
30         }
31
32         $(that.node).html(acc.join(''));
33
34         function addSingleFacet(acc, caption, data, max, pzIndex) {
35             var teamName = that.team.name();
36             acc.push('<div class="mkwsFacet mkwsFacet' + caption + ' mkwsTeam_' + teamName + '">');
37             acc.push('<div class="termtitle">' + M(caption) + '</div>');
38             for (var i = 0; i < data.length && i < max; i++) {
39                 acc.push('<div class="term">');
40                 acc.push('<a href="#" ');
41                 var action = '';
42                 if (!pzIndex) {
43                     // Special case: target selection
44                     acc.push('target_id='+data[i].id+' ');
45                     if (!that.team.targetFiltered(data[i].id)) {
46                         action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
47                     }
48                 } else {
49                     action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
50                 }
51                 acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
52                          + ' <span>' + data[i].freq + '</span>');
53                 acc.push('</div>');
54             }
55             acc.push('</div>');
56         }
57     });
58
59     widget.autosearch(that);
60 });