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