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