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