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