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