Rename the two "template" variables to t1 and t2.
[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   mkws.$(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="mkwsTermlistsTitle">' + 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   this.autosearch();
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] || [ "Unknown", 10, true ];
34   var caption = this.config['facet_caption_' + name] || ref[0];
35   var max     = parseInt(this.config['facet_max_' + name] || ref[1]);
36   var pzIndex = ref[2] ? name : null;
37
38   that.toString = function() {
39     return '[Widget ' + that.team.name() + ':' + that.type + '(' + name + ')]';
40   };
41
42   var t2 = that.team.loadTemplate('Facet-' + caption);
43   that.log("template for Facet-" + caption + " is " + !!t2);
44   if (!t2) {
45     that.log("no " + caption + "-specific template, falling back");
46     t2 = that.team.loadTemplate('Facet');
47   }
48   that.log("template for Facet is " + !!t2);
49
50   that.team.queue("termlists").subscribe(function(data) {
51     data = data[name];
52     var t1 = that.team.loadTemplate('facetTitle-' + caption, mkws.M(caption))
53     var title = t1({ query: that.config.query });
54     var acc = [];
55     acc.push('<div class="mkwsFacetTitle">', title, '</div>');
56
57     var teamName = that.team.name();
58     for (var i = 0; i < data.length && i < max; i++) {
59       var fn, field;
60       // General case modifies the query; special case selects a target
61       if (pzIndex) {
62         fn = 'limitQuery'; field = pzIndex;
63       } else if (!that.team.targetFiltered(data[i].id)) {
64         fn = 'limitTarget'; field = data[i].id;
65       }
66
67       var s = t2({ 
68         team: teamName,
69         fn: fn,
70         field: field,
71         term: data[i].name,
72         count: data[i].freq,
73         query: that.config.query
74       });
75       acc.push('<div class="mkwsTerm">', s, '</div>');
76     }
77
78     that.node.html(acc.join(''));
79   });
80
81   this.autosearch();
82 });