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