22972059bc859e73a1cd10fa50a5a2c531313b1a
[mkws-moved-to-github.git] / examples / htdocs / mkws-widget-credo.js
1 // The Google Images database returns links like:
2 //      http://images.google.com/url?q=http://eofdreams.com/fish.html&sa=U&ei=RAB-U9XNDo2Dqga1o4L4Bw&ved=0CC4Q9QEwAA&usg=AFQjCNFhRtn6GMevHbpITZ6kfx6rsHV2ow
3 // This Handlebars helper avoids a pointless redirect by transforming
4 // this to the URL of the underling page, in this case
5 //      http://eofdreams.com/fish.html
6 //
7 Handlebars.registerHelper('mkws-googleurl', function(obj) {
8   if (!obj) {
9     return "obj undefined";
10   } else if (!obj[0]) {
11     return "obj[0] undefined, JSON=" + $.toJSON(obj);
12   } else {
13     return mkws.getParameterByName('q', obj[0]);
14   }
15 });
16
17
18 // ### This works inefficiently by having multiple teams all run the
19 // same search against different sets of targets. A much better
20 // approach would be run a single search, with all these panels
21 // members of the same team, but picking out only the results relevant
22 // to them. That will be more work.
23
24 mkws.registerWidgetType('Credo', function() {
25   var that = this;
26
27   this.team.registerTemplate('CredoImage', '\
28       <div>\
29        <a href="{{mkws-googleurl md-electronic-url}}" target="_blank">\
30         {{#mkws-first md-thumburl}}\
31           <img src="{{this}}" alt="{{../md-title}}"/>\
32         {{/mkws-first}}\
33         <br/>\
34        </a>\
35        <p>{{md-title}}</p>\
36       </div>\
37 ');
38
39   var s = []
40   s.push('<table>');
41
42   // Main panel: encylopaedia and images on the left, topics on the right
43   s.push('<tr class="front">');
44
45   s.push('<td class="main">');
46   s.push(section('encyclopaedia', 'Topic Page: <span class="x-mkws-title"/>',
47                  this.subwidget('Reference', { _team: 'ref', paragraphs: 1 })));
48   // The Images widget needs to be in our team so we can set its template
49   s.push(section('image', 'Images',
50                  this.subwidget('GoogleImage', { maxrecs: 4, template: 'CredoImage' })));
51   s.push('</td>');
52
53   s.push('<td class="side">');
54   s.push(section('topics', 'Related Topics',
55                  this.subwidget('Facet', { _team: 'main', facet: 'subject' })));
56   s.push('</td>');
57
58   s.push('</tr>');
59
60   s.push('<tr><td colspan="2"><hr class="divider"/></td></tr>');
61
62   s.push(sectionRow('entries', 'News',
63                     this.subwidget('Records', { _team: 'news', targetfilter: 'categories=news', perpage: 10 })));
64   s.push(sectionRow('articles', 'Articles',
65                     this.subwidget('Records', { _team: 'articles', targetfilter: 'categories=articles', perpage: 10 })));
66   s.push(sectionRow('books', 'Books',
67                     this.subwidget('Records', { _team: 'books', targetfilter: 'categories=books', perpage: 10 })));
68   s.push(sectionRow('news', 'Results from all targets',
69                     this.subwidget('Records', { _team: 'main' })));
70
71   s.push('</table>');
72
73   this.node.html(s.join(''));
74
75   // Fill in the titles from the query once widgets have all been prepared
76   var that = this;
77   this.team.queue("ready").subscribe(function() {
78     var query = toTitleCase(that.config.query);
79     that.log("got query '" + query + "' from team config");
80     mkws.$('.x-mkws-title').html(query);
81     mkws.$('title').html("MKWS: " + query);
82
83     // Derived from http://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript
84     function toTitleCase(str) {
85       return str.replace(/\w\S*/g, function(txt) {
86         return txt.charAt(0).toUpperCase() + txt.substr(1);
87       });
88     }
89   });
90
91
92   function section(xclass, title, content) {
93     var s = [];
94     s.push('<div class="' + xclass + ' section">');
95     s.push('<div class="title">' + title + '</div>');
96     s.push('<div class="content">' + content + '</div>');
97     s.push('</div>');
98     return s.join('');
99   }
100
101   function sectionRow(xclass, title, content) {
102     var s = [];
103     s.push('<tr>');
104     s.push('<td colspan="2">');
105     s.push(section(xclass, title, content));
106     s.push('</td>');
107     s.push('</tr>');
108     return s.join('');
109   }
110 });