X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Fmkws-widget-termlists.js;h=fef616f71ae8e6991ce7416ffcbcdfdd0a60267a;hb=99f12f4cbd8c7a89b5b976fe2c1a7d40e4c5441e;hp=3762600e057bc0010d2f74ac4d177b9f43c0380e;hpb=8cb43727c2afcd64660f9ce155084a7f41ab6858;p=mkws-moved-to-github.git
diff --git a/src/mkws-widget-termlists.js b/src/mkws-widget-termlists.js
index 3762600..fef616f 100644
--- a/src/mkws-widget-termlists.js
+++ b/src/mkws-widget-termlists.js
@@ -1,60 +1,82 @@
mkws.registerWidgetType('Termlists', function() {
- var that = this;
- var M = mkws.M;
-
- this.team.queue("termlists").subscribe(function(data) {
- // no facets: this should never happen
- var facets = that.config.facets;
- if (!facets || facets.length == 0) {
- alert("onTerm called even though we have no facets: " + $.toJSON(data));
- $(that.node).hide();
- return;
- }
-
- // display if we first got results
- $(that.node).show();
-
- var acc = [];
- acc.push('
' + M('Termlists') + '
');
-
- for (var i = 0; i < facets.length; i++) {
- if (facets[i] == "xtargets") {
- addSingleFacet(acc, "Sources", data.xtargets, 16, null);
- } else if (facets[i] == "subject") {
- addSingleFacet(acc, "Subjects", data.subject, 10, "subject");
- } else if (facets[i] == "author") {
- addSingleFacet(acc, "Authors", data.author, 10, "author");
- } else {
- alert("bad facet configuration: '" + facets[i] + "'");
- }
- }
-
- $(that.node).html(acc.join(''));
-
- function addSingleFacet(acc, caption, data, max, pzIndex) {
- var teamName = that.team.name();
- acc.push('');
- acc.push('
' + M(caption) + '
');
- for (var i = 0; i < data.length && i < max; i++) {
- acc.push('
');
- }
- acc.push('
');
- }
- });
-
- widget.autosearch(that);
+ var that = this;
+
+ // Initially hide the termlists; display when we get results
+ mkws.$(document).ready(function() {
+ that.node.hide();
+ });
+ this.team.queue("termlists").subscribe(function(data) {
+ that.node.show();
+ });
+
+ var acc = [];
+ var facets = this.config.facets;
+ acc.push('' + mkws.M('Termlists') + '
');
+ for (var i = 0; i < facets.length; i++) {
+ acc.push('', '
');
+ }
+ this.node.html(acc.join(''));
+
+ this.autosearch();
+});
+
+
+mkws.registerWidgetType('Facet', function() {
+ var facetConfig = {
+ xtargets: [ "Sources", 16, false ],
+ subject: [ "Subjects", 10, true ],
+ author: [ "Authors", 10, true ]
+ }
+
+ var that = this;
+ var name = that.config.facet;
+ var ref = facetConfig[name] || [ "Unknown", 10, true ];
+ var caption = this.config['facet_caption_' + name] || ref[0];
+ var max = parseInt(this.config['facet_max_' + name] || ref[1]);
+ var pzIndex = ref[2] ? name : null;
+
+ that.toString = function() {
+ return '[Widget ' + that.team.name() + ':' + that.type + '(' + name + ')]';
+ };
+
+ var t2 = that.team.loadTemplate('Facet-' + caption);
+ that.log("template for Facet-" + caption + " is " + !!t2);
+ if (!t2) {
+ that.log("no " + caption + "-specific template, falling back");
+ t2 = that.team.loadTemplate('Facet');
+ }
+ that.log("template for Facet is " + !!t2);
+
+ that.team.queue("termlists").subscribe(function(data) {
+ data = data[name];
+ var t1 = that.team.loadTemplate('facetTitle-' + caption, mkws.M(caption))
+ var title = t1({ query: that.config.query });
+ var acc = [];
+ acc.push('', title, '
');
+
+ var teamName = that.team.name();
+ for (var i = 0; i < data.length && i < max; i++) {
+ var fn, field;
+ // General case modifies the query; special case selects a target
+ if (pzIndex) {
+ fn = 'limitQuery'; field = pzIndex;
+ } else if (!that.team.targetFiltered(data[i].id)) {
+ fn = 'limitTarget'; field = data[i].id;
+ }
+
+ var s = t2({
+ team: teamName,
+ fn: fn,
+ field: field,
+ term: data[i].name,
+ count: data[i].freq,
+ query: that.config.query
+ });
+ acc.push('', s, '
');
+ }
+
+ that.node.html(acc.join(''));
+ });
+
+ this.autosearch();
});