Move mkws.facetConfig into mkws-widget-termlists.js, the only place that needs it.
[mkws-moved-to-github.git] / src / mkws-widget-termlists.js
1 mkws.facetConfig = {
2     xtargets: [ "Sources",  16, false ],
3     subject:  [ "Subjects", 10, true ],
4     author:   [ "Authors",  10, true ]
5 }
6
7 mkws.registerWidgetType('Termlists', function() {
8     var that = this;
9     var facets = that.config.facets;
10
11     this.team.queue("termlists").subscribe(function(data) {
12         // display if we first got results
13         $(that.node).show();
14     });
15
16     for (var i = 0; i < facets.length; i++) {
17         var name = facets[i]
18         var ref = mkws.facetConfig[name];
19         if (!ref) {
20             alert("bad facet configuration: '" + name + "'");
21         }
22     }
23     
24     widget.autosearch(that);
25 });
26
27
28 mkws.registerWidgetType('Facet', function() {
29     var that = this;
30     var teamName = that.team.name();
31     var name = that.config.facet;
32     var ref = mkws.facetConfig[name] || alert("no facet definition for '" + name + "'");
33     var caption = ref[0];
34     var max = ref[1];
35     var pzIndex = ref[2] ? name : null;
36
37     that.team.queue("termlists").subscribe(function(data) {
38         data = data[name];
39
40         var acc = [];
41         acc.push('<div class="termtitle">' + mkws.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
60         $(that.node).html(acc.join(''));
61     });
62 });