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