For now, use jsnlog-DEV.js
[mkws-moved-to-github.git] / src / mkws-widget-facets.js
1 mkws.registerWidgetType('facets', function() {
2   // Initially hide the facets; display when we get results
3   var that = this;
4   var team = this.team;
5   team.queue("facets").subscribe(function(data) {
6     that.node.addClass("active");
7   });
8
9   var template = team.loadTemplate(this.config.template || "facets");
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("facets").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         field: data[i].id,
56         count: data[i].freq,
57         linkdata: linkdata
58       }); 
59     }
60     // configured template > facet specific template > default facet template
61     var template;
62     if (that.config.template) {
63       template = team.loadTemplate(that.config.template);
64     } else {
65       template = team.loadTemplate("facet-" + name);
66       if (template) {
67         that.info("Using facet-" + name + " template.")
68       } else {
69         that.info("No " + name + " specific template, using default.")
70         template = team.loadTemplate("facet");
71       }
72     }
73     that.node.html(template({
74       name: name,
75       caption: caption,
76       query: that.config.query,
77       terms: terms
78     }));
79   });
80   this.autosearch();
81 });