Each widget now has its own config object, inheriting from the team
[mkws-moved-to-github.git] / src / mkws-widgets.js
1 // Factory function for widget objects.
2 function widget($, team, type, node) {
3     var that = {
4         team: team,
5         type: type,
6         node: node,
7         config: Object.create(team.config())
8     };
9
10     function log(s) {
11         team.log(s);
12     }
13     that.log = log;
14
15     that.toString = function() {
16         return '[Widget ' + team.name() + ':' + type + ']';
17     };
18
19     var fn = mkws.promotionFunction(type);
20     if (fn) {
21         fn.call(that);
22         log("made " + type + " widget(node=" + node + ")");
23     } else {
24         log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
25     }
26
27     return that;
28 }
29
30
31 // Functions follow for promoting the regular widget object into
32 // widgets of specific types. These could be moved into their own
33 // source files.
34
35
36 mkws.registerWidgetType('Targets', function() {
37     var that = this;
38     var M = mkws.M;
39
40     this.team.queue("targets").subscribe(function(data) {
41         var table ='<table><thead><tr>' +
42             '<td>' + M('Target ID') + '</td>' +
43             '<td>' + M('Hits') + '</td>' +
44             '<td>' + M('Diags') + '</td>' +
45             '<td>' + M('Records') + '</td>' +
46             '<td>' + M('State') + '</td>' +
47             '</tr></thead><tbody>';
48
49         for (var i = 0; i < data.length; i++) {
50             table += "<tr><td>" + data[i].id +
51                 "</td><td>" + data[i].hits +
52                 "</td><td>" + data[i].diagnostic +
53                 "</td><td>" + data[i].records +
54                 "</td><td>" + data[i].state + "</td></tr>";
55         }
56
57         table += '</tbody></table>';
58         var subnode = $(that.node).children('.mkwsBytarget');
59         subnode.html(table);
60     });
61 });
62
63
64 mkws.registerWidgetType('Stat', function() {
65     var that = this;
66     var M = mkws.M;
67
68     this.team.queue("stat").subscribe(function(data) {
69         if (that.node.length === 0)  alert("huh?!");
70
71         $(that.node).html('<span class="head">' + M('Status info') + '</span>' +
72             ' -- ' +
73             '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
74             ' -- ' +
75             '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
76     });
77 });
78
79
80 mkws.registerWidgetType('Termlists', function() {
81     var that = this;
82     var M = mkws.M;
83
84     this.team.queue("termlists").subscribe(function(data) {
85         if (!that.node) {
86             alert("termlists event when there are no termlists");
87             return;
88         }
89
90         // no facets: this should never happen
91         var facets = that.config.facets;
92         if (!facets || facets.length == 0) {
93             alert("onTerm called even though we have no facets: " + $.toJSON(data));
94             $(that.node).hide();
95             return;
96         }
97
98         // display if we first got results
99         $(that.node).show();
100
101         var acc = [];
102         acc.push('<div class="title">' + M('Termlists') + '</div>');
103
104         for (var i = 0; i < facets.length; i++) {
105             if (facets[i] == "xtargets") {
106                 addSingleFacet(acc, "Sources",  data.xtargets, 16, null);
107             } else if (facets[i] == "subject") {
108                 addSingleFacet(acc, "Subjects", data.subject,  10, "subject");
109             } else if (facets[i] == "author") {
110                 addSingleFacet(acc, "Authors",  data.author,   10, "author");
111             } else {
112                 alert("bad facet configuration: '" + facets[i] + "'");
113             }
114         }
115
116         $(that.node).html(acc.join(''));
117
118         function addSingleFacet(acc, caption, data, max, pzIndex) {
119             var teamName = that.team.name();
120             acc.push('<div class="facet mkwsFacet' + caption + ' mkwsTeam_' + teamName + '">');
121             acc.push('<div class="termtitle">' + M(caption) + '</div>');
122             for (var i = 0; i < data.length && i < max; i++) {
123                 acc.push('<div class="term">');
124                 acc.push('<a href="#" ');
125                 var action = '';
126                 if (!pzIndex) {
127                     // Special case: target selection
128                     acc.push('target_id='+data[i].id+' ');
129                     if (!that.team.targetFiltered(data[i].id)) {
130                         action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
131                     }
132                 } else {
133                     action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
134                 }
135                 acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
136                          + ' <span>' + data[i].freq + '</span>');
137                 acc.push('</div>');
138             }
139             acc.push('</div>');
140         }
141     });
142 });
143
144
145 mkws.registerWidgetType('Pager', function() {
146     var that = this;
147     var M = mkws.M;
148
149     this.team.queue("pager").subscribe(function(data) {
150         $(that.node).html(drawPager(data))
151
152         function drawPager(data) {
153             var teamName = that.team.name();
154             var s = '<div style="float: right">' + M('Displaying') + ': '
155                 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
156                 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
157                 + data.total + ')</div>';
158
159             //client indexes pages from 1 but pz2 from 0
160             var onsides = 6;
161             var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
162             var currentPage = that.team.currentPage();
163
164             var firstClkbl = (currentPage - onsides > 0)
165                 ? currentPage - onsides
166                 : 1;
167
168             var lastClkbl = firstClkbl + 2*onsides < pages
169                 ? firstClkbl + 2*onsides
170                 : pages;
171
172             var prev = '<span class="mkwsPrev">&#60;&#60; ' + M('Prev') + '</span><b> | </b>';
173             if (currentPage > 1)
174                 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
175                 +'&#60;&#60; ' + M('Prev') + '</a><b> | </b>';
176
177             var middle = '';
178             for(var i = firstClkbl; i <= lastClkbl; i++) {
179                 var numLabel = i;
180                 if(i == currentPage)
181                     numLabel = '<b>' + i + '</b>';
182
183                 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
184                     + numLabel + ' </a>';
185             }
186
187             var next = '<b> | </b><span class="mkwsNext">' + M('Next') + ' &#62;&#62;</span>';
188             if (pages - currentPage > 0)
189                 next = '<b> | </b><a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
190                 + M('Next') + ' &#62;&#62;</a>';
191
192             var predots = '';
193             if (firstClkbl > 1)
194                 predots = '...';
195
196             var postdots = '';
197             if (lastClkbl < pages)
198                 postdots = '...';
199
200             s += '<div style="float: clear">'
201                 + prev + predots + middle + postdots + next + '</div>';
202
203             return s;
204         }
205     });
206 });
207
208
209 mkws.registerWidgetType('Records', function() {
210     var that = this;
211     var team = this.team;
212
213     this.team.queue("records").subscribe(function(data) {
214         var html = [];
215         for (var i = 0; i < data.hits.length; i++) {
216             var hit = data.hits[i];
217             var divId = team.recordElementId(hit.recid[0]);
218             html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
219             // ### At some point, we may be able to move the
220             // m_currentRecordId and m_currentRecordData members
221             // from the team object into this widget.
222             if (hit.recid == team.currentRecordId()) {
223                 if (team.currentRecordData())
224                     html.push(team.renderDetails(team.currentRecordData()));
225             }
226         }
227         $(that.node).html(html.join(''));
228
229         function renderSummary(hit)
230         {
231             var template = team.loadTemplate("Summary");
232             hit._id = team.recordElementId(hit.recid[0]);
233             hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
234             return template(hit);
235         }
236     });
237 });
238
239
240 mkws.registerWidgetType('Navi', function() {
241     var that = this;
242     var teamName = this.team.name();
243     var M = mkws.M;
244
245     this.team.queue("navi").subscribe(function() {
246         var filters = that.team.filters();
247         var text = "";
248
249         for (var i in filters) {
250             if (text) {
251                 text += " | ";
252             }
253             var filter = filters[i];
254             if (filter.id) {
255                 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
256                     "', '" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
257             } else {
258                 text += M(filter.field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
259                     "', '" + filter.field + "', '" + filter.value + "'" +
260                     ');return false;">' + filter.value + '</a>';
261             }
262         }
263
264         $(that.node).html(text);
265     });
266 });
267
268
269 // It seems this and the Perpage widget doen't need to subscribe to
270 // anything, since they produce events rather than consuming them.
271 //
272 mkws.registerWidgetType('Sort', function() {
273     var that = this;
274
275     $(this.node).change(function() {
276         that.team.set_sortOrder($(that.node).val());
277         if (that.team.submitted()) {
278             that.team.resetPage();
279             that.team.reShow();
280         }
281         return false;
282     });
283 });
284
285
286 mkws.registerWidgetType('Perpage', function() {
287     var that = this;
288
289     $(this.node).change(function() {
290         that.team.set_perpage($(that.node).val());
291         if (that.team.submitted()) {
292             that.team.resetPage();
293             that.team.reShow();
294         }
295         return false;
296     });
297 });