Each facet is now (finally!) its own widget.
[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
5     this.team.queue("termlists").subscribe(function(data) {
6         // display if we first got results
7         $(that.node).show();
8     });
9
10     for (var i = 0; i < facets.length; i++) {
11         var name = facets[i]
12         var ref = mkws.facetConfig[name];
13         if (!ref) {
14             alert("bad facet configuration: '" + name + "'");
15         }
16     }
17     
18     widget.autosearch(that);
19 });
20
21
22 mkws.registerWidgetType('Facet', function() {
23     var that = this;
24     var teamName = that.team.name();
25     var name = that.config.facet;
26     var ref = mkws.facetConfig[name] || alert("no facet definition for '" + name + "'");
27     var caption = ref[0];
28     var max = ref[1];
29     var pzIndex = ref[2] ? name : null;
30
31     that.team.queue("termlists").subscribe(function(data) {
32         data = data[name];
33
34         var acc = [];
35         acc.push('<div class="termtitle">' + mkws.M(caption) + '</div>');
36         for (var i = 0; i < data.length && i < max; i++) {
37             acc.push('<div class="term">');
38             acc.push('<a href="#" ');
39             var action = '';
40             if (!pzIndex) {
41                 // Special case: target selection
42                 acc.push('target_id='+data[i].id+' ');
43                 if (!that.team.targetFiltered(data[i].id)) {
44                     action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
45                 }
46             } else {
47                 action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
48             }
49             acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
50                      + ' <span>' + data[i].freq + '</span>');
51             acc.push('</div>');
52         }
53
54         $(that.node).html(acc.join(''));
55     });
56 });