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