Eliminate the addSingleScaffold function, inlining its code.
[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 output = {};
24         var acc = [];
25         acc.push('<div class="title">' + M('Termlists') + '</div>');
26
27         for (var i = 0; i < facets.length; i++) {
28             var name = facets[i]
29             var ref = facetConfig[name];
30             if (!ref) {
31                 alert("bad facet configuration: '" + name + "'");
32             } else {
33                 acc.push('<div class="mkwsFacet mkwsFacet' + ref[0] + ' mkwsTeam_' + that.team.name() + '">');
34                 acc.push('</div>');
35             }
36         }
37
38         for (var i = 0; i < facets.length; i++) {
39             var name = facets[i]
40             var ref = facetConfig[name];
41             if (!ref) {
42                 alert("bad facet configuration: '" + name + "'");
43             } else {
44                 output[name] = makeSingleFacet(ref[0], data[name], ref[1], ref[2] ? name : null);
45             }
46         }
47
48         $(that.node).html(acc.join(''));
49
50         for (var i = 0; i < facets.length; i++) {
51             var name = facets[i]
52             var ref = facetConfig[name];
53             var caption = ref[0];
54
55             that.log("setting HTML for facet '" + name + "'");
56             that.team.findnode('.mkwsFacet' + caption).html(output[name]);
57         }
58
59
60         function makeSingleFacet(caption, data, max, pzIndex) {
61             var teamName = that.team.name();
62
63             var a2 = [];
64             a2.push('<div class="termtitle">' + M(caption) + '</div>');
65             for (var i = 0; i < data.length && i < max; i++) {
66                 a2.push('<div class="term">');
67                 a2.push('<a href="#" ');
68                 var action = '';
69                 if (!pzIndex) {
70                     // Special case: target selection
71                     a2.push('target_id='+data[i].id+' ');
72                     if (!that.team.targetFiltered(data[i].id)) {
73                         action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
74                     }
75                 } else {
76                     action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
77                 }
78                 a2.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
79                          + ' <span>' + data[i].freq + '</span>');
80                 a2.push('</div>');
81             }
82             return a2.join('');
83         }
84     });
85
86     widget.autosearch(that);
87 });