Remove obsolete logging.
[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.team.findnode('.mkwsFacet' + caption).html(output);
48             }
49         }
50
51
52         function makeSingleFacet(caption, data, max, pzIndex) {
53             var teamName = that.team.name();
54
55             var a2 = [];
56             a2.push('<div class="termtitle">' + M(caption) + '</div>');
57             for (var i = 0; i < data.length && i < max; i++) {
58                 a2.push('<div class="term">');
59                 a2.push('<a href="#" ');
60                 var action = '';
61                 if (!pzIndex) {
62                     // Special case: target selection
63                     a2.push('target_id='+data[i].id+' ');
64                     if (!that.team.targetFiltered(data[i].id)) {
65                         action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
66                     }
67                 } else {
68                     action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
69                 }
70                 a2.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
71                          + ' <span>' + data[i].freq + '</span>');
72                 a2.push('</div>');
73             }
74             return a2.join('');
75         }
76     });
77
78     widget.autosearch(that);
79 });