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