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