Simplify to a version that still doesn't work, but in the same way.
[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.newsearch_opacity;
8     if (op !== undefined) { that.node.fadeTo(500, op); }
9   });
10
11   team.queue("facets").subscribe(function(data) {
12     that.node.stop();
13     that.node.css('opacity', 1);
14     that.node.addClass("active");
15   });
16
17   var template = team.loadTemplate(this.config.template || "facets");
18   this.node.html(template({
19     team: team.name(),
20     facets: this.config.facets
21   }));
22   this.autosearch();
23 });
24
25
26 mkws.registerWidgetType('facet', function() {
27   var facetConfig = {
28     xtargets: [ "Sources",  16, false ],
29     subject:  [ "Subjects", 10, true ],
30     author:   [ "Authors",  10, true ]
31   }
32   var that = this;
33   var team = this.team;
34   var name = that.config.facet;
35   var ref = facetConfig[name] || [ "Unknown", 10, true ];
36   var caption = this.config['facet_caption_' + name] || ref[0];
37   var max     = parseInt(this.config['facet_max_' + name] || ref[1]);
38   var pzIndex = ref[2] ? name : null;
39
40   that.toString = function() {
41     return '[Widget ' + team.name() + ':' + that.type + '(' + name + ')]';
42   };
43
44   team.queue("facets").subscribe(function(data) {
45     data = data[name];
46     var terms = [];
47     var teamName = team.name();
48     for (var i = 0; i < data.length && i < max; i++) {
49       var fs = $.extend(true, {}, team.filters());
50       //team.warn("fs = " + mkws.$.toJSON(fs));
51       //team.warn("team.filters = " + mkws.$.toJSON(team.filters()));
52       if (!pzIndex) {
53         // Special case: target selection
54         team.warn("before: team.filters = " + mkws.$.toJSON(team.filters()));
55         fs.add(targetFilter(data[i].id, data[i].name));
56         team.warn("after: team.filters = " + mkws.$.toJSON(team.filters()));
57       } else {
58         fs.add(fieldFilter(pzIndex, data[i].name));
59       }
60       terms.push({
61         term: data[i].name,
62         count: data[i].freq,
63         href: '#' + team.urlFragment({ filters: fs })
64       }); 
65     }
66     // configured template > facet specific template > default facet template
67     var template;
68     if (that.config.template) {
69       template = team.loadTemplate(that.config.template);
70     } else {
71       template = team.loadTemplate("facet-" + name);
72       if (template) {
73         that.info("Using facet-" + name + " template.")
74       } else {
75         that.info("No " + name + " specific template, using default.")
76         template = team.loadTemplate("facet");
77       }
78     }
79     that.node.html(template({
80       name: name,
81       caption: caption,
82       terms: terms
83     }));
84   });
85   this.autosearch();
86 });