Merge branch 'master' into templateallthemarkup
[mkws-moved-to-github.git] / src / mkws-widget-termlists.js
1 mkws.registerWidgetType('Termlists', function() {
2   // Initially hide the termlists; display when we get results
3   var that = this;
4   var team = this.team;
5   mkws.$(document).ready(function() {
6     that.node.hide();
7   });
8   team.queue("termlists").subscribe(function(data) {
9     that.node.show();
10   });
11
12   var template = team.loadTemplate(this.config.template || "Termlists");
13   this.node.html(template({
14     team: team.name(),
15     facets: this.config.facets
16   }));
17   this.autosearch();
18 });
19
20
21 mkws.registerWidgetType('Facet', function() {
22   var facetConfig = {
23     xtargets: [ "Sources",  16, false ],
24     subject:  [ "Subjects", 10, true ],
25     author:   [ "Authors",  10, true ]
26   }
27   var that = this;
28   var team = this.team;
29   var name = that.config.facet;
30   var ref = facetConfig[name] || [ "Unknown", 10, true ];
31   var caption = this.config['facet_caption_' + name] || ref[0];
32   var max     = parseInt(this.config['facet_max_' + name] || ref[1]);
33   var pzIndex = ref[2] ? name : null;
34
35   that.toString = function() {
36     return '[Widget ' + team.name() + ':' + that.type + '(' + name + ')]';
37   };
38
39   team.queue("termlists").subscribe(function(data) {
40     data = data[name];
41     var terms = [];
42     var teamName = team.name();
43     for (var i = 0; i < data.length && i < max; i++) {
44       var linkdata = "";
45       var action = "";
46       if (!pzIndex) {
47         // Special case: target selection
48         linkdata += ('target_id='+data[i].id+' ');
49         if (!team.targetFiltered(data[i].id)) {
50           action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
51         }
52       } else {
53         action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
54       }
55       linkdata += 'onclick="' + action + ';return false;"';
56       terms.push({
57         term: data[i].name,
58         count: data[i].freq,
59         linkdata: linkdata
60       }); 
61     }
62     // configured template > facet specific template > default facet template
63     var template;
64     if (that.config.template) {
65       template = team.loadTemplate(that.config.template);
66     } else {
67       template = team.loadTemplate("Facet-" + caption);
68       if (template) {
69         that.log("Using Facet-" + caption + " template.")
70       } else {
71         that.log("No " + caption + " specific template, using default.")
72         template = team.loadTemplate("Facet");
73       }
74     }
75     that.node.html(template({
76       name: name,
77       caption: caption,
78       terms: terms
79     }));
80   });
81   this.autosearch();
82 });