d3dc14998559b3d9fcf7638361c393fa45e2cde1
[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:   [ "Authors",  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
24         var acc = [];
25         acc.push('<div class="title">' + M('Termlists') + '</div>');
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                 acc.push('<div class="mkwsFacet mkwsFacet' + ref[0] + ' mkwsTeam_' + that.team.name() + '">');
33                 acc.push('</div>');
34             }
35         }
36         $(that.node).html(acc.join(''));
37
38
39         for (var i = 0; i < facets.length; i++) {
40             var name = facets[i]
41             var ref = facetConfig[name];
42             var caption = ref[0];
43             if (!ref) {
44                 alert("bad facet configuration: '" + name + "'");
45             } else {
46                 var output = makeSingleFacet(ref[0], data[name], ref[1], ref[2] ? name : null);
47                 that.log("setting HTML for facet '" + name + "'");
48                 that.team.findnode('.mkwsFacet' + caption).html(output);
49             }
50         }
51
52
53         function makeSingleFacet(caption, data, max, pzIndex) {
54             var teamName = that.team.name();
55
56             var a2 = [];
57             a2.push('<div class="termtitle">' + M(caption) + '</div>');
58             for (var i = 0; i < data.length && i < max; i++) {
59                 a2.push('<div class="term">');
60                 a2.push('<a href="#" ');
61                 var action = '';
62                 if (!pzIndex) {
63                     // Special case: target selection
64                     a2.push('target_id='+data[i].id+' ');
65                     if (!that.team.targetFiltered(data[i].id)) {
66                         action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
67                     }
68                 } else {
69                     action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
70                 }
71                 a2.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
72                          + ' <span>' + data[i].freq + '</span>');
73                 a2.push('</div>');
74             }
75             return a2.join('');
76         }
77     });
78
79     widget.autosearch(that);
80 });