Do not emit static "Status info" text in the Stat widget: leave it to the CSS.
[mkws-moved-to-github.git] / src / mkws-widget-main.js
1 // Functions follow for promoting the regular widget object into
2 // widgets of specific types. These could be moved into their own
3 // source files.
4
5
6 mkws.registerWidgetType('Targets', function() {
7   var that = this;
8   var M = mkws.M;
9
10   $(this.node).html('No information available yet.');
11   $(this.node).css("display", "none");
12
13   this.team.queue("targets").subscribe(function(data) {
14     var table ='<table><thead><tr>' +
15       '<td>' + M('Target ID') + '</td>' +
16       '<td>' + M('Hits') + '</td>' +
17       '<td>' + M('Diags') + '</td>' +
18       '<td>' + M('Records') + '</td>' +
19       '<td>' + M('State') + '</td>' +
20       '</tr></thead><tbody>';
21
22     for (var i = 0; i < data.length; i++) {
23       table += "<tr><td>" + data[i].id +
24         "</td><td>" + data[i].hits +
25         "</td><td>" + data[i].diagnostic +
26         "</td><td>" + data[i].records +
27         "</td><td>" + data[i].state + "</td></tr>";
28     }
29
30     table += '</tbody></table>';
31     $(that.node).html(table);
32   });
33 });
34
35
36 mkws.registerWidgetType('Stat', function() {
37   var that = this;
38   var M = mkws.M;
39
40   this.team.queue("stat").subscribe(function(data) {
41     if (that.node.length === 0)  alert("huh?!");
42
43     $(that.node).html(' -- ' +
44                       '<span class="mkwsClientCount">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
45                       ' -- ' +
46                       '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
47   });
48 });
49
50
51 mkws.registerWidgetType('Pager', function() {
52   var that = this;
53   var M = mkws.M;
54
55   this.team.queue("pager").subscribe(function(data) {
56     $(that.node).html(drawPager(data))
57
58     function drawPager(data) {
59       var teamName = that.team.name();
60       var s = '<div style="float: right">' + M('Displaying') + ': '
61         + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
62         ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
63         + data.total + ')</div>';
64
65       //client indexes pages from 1 but pz2 from 0
66       var onsides = 6;
67       var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
68       var currentPage = that.team.currentPage();
69
70       var firstClkbl = (currentPage - onsides > 0)
71         ? currentPage - onsides
72         : 1;
73
74       var lastClkbl = firstClkbl + 2*onsides < pages
75         ? firstClkbl + 2*onsides
76         : pages;
77
78       var prev = '<span class="mkwsPrev">&#60;&#60; ' + M('Prev') + '</span> | ';
79       if (currentPage > 1)
80         prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
81         +'&#60;&#60; ' + M('Prev') + '</a> | ';
82
83       var middle = '';
84       for(var i = firstClkbl; i <= lastClkbl; i++) {
85         var numLabel = i;
86         if(i == currentPage)
87           numLabel = '<span class="mkwsCurrentPage">' + i + '</span>';
88
89         middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
90           + numLabel + ' </a>';
91       }
92
93       var next = ' | <span class="mkwsNext">' + M('Next') + ' &#62;&#62;</span>';
94       if (pages - currentPage > 0)
95         next = ' | <a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
96         + M('Next') + ' &#62;&#62;</a>';
97
98       var predots = '';
99       if (firstClkbl > 1)
100         predots = '...';
101
102       var postdots = '';
103       if (lastClkbl < pages)
104         postdots = '...';
105
106       s += '<div style="float: clear">'
107         + prev + predots + middle + postdots + next + '</div>';
108
109       return s;
110     }
111   });
112 });
113
114
115 mkws.registerWidgetType('Records', function() {
116   var that = this;
117   var team = this.team;
118
119   this.team.queue("records").subscribe(function(data) {
120     var html = [];
121     for (var i = 0; i < data.hits.length; i++) {
122       var hit = data.hits[i];
123       that.team.queue("record").publish(hit);
124       var divId = team.recordElementId(hit.recid[0]);
125       html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
126       // ### At some point, we may be able to move the
127       // m_currentRecordId and m_currentRecordData members
128       // from the team object into this widget.
129       if (hit.recid == team.currentRecordId()) {
130         if (team.currentRecordData())
131           html.push(team.renderDetails(team.currentRecordData()));
132       }
133     }
134     $(that.node).html(html.join(''));
135
136     function renderSummary(hit) {
137       var template = team.loadTemplate(that.config.template || "Summary");
138       hit._id = team.recordElementId(hit.recid[0]);
139       hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
140       return template(hit);
141     }
142   });
143
144   widget.autosearch(that);
145 });
146
147
148 mkws.registerWidgetType('Navi', function() {
149   var that = this;
150   var teamName = this.team.name();
151   var M = mkws.M;
152
153   this.team.queue("navi").subscribe(function() {
154     var filters = that.team.filters();
155     var text = "";
156
157     filters.visitTargets(function(id, name) {
158       if (text) text += " | ";
159       text += M('source') + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitTarget(\'' + teamName +
160         "', '" + id + "'" + ');return false;">' + name + '</a>';
161     });
162
163     filters.visitFields(function(field, value) {
164       if (text) text += " | ";
165       text += M(field) + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitQuery(\'' + teamName +
166         "', '" + field + "', '" + value + "'" +
167         ');return false;">' + value + '</a>';
168     });
169
170     $(that.node).html(text);
171   });
172 });
173
174
175 // It seems this and the Perpage widget doen't need to subscribe to
176 // anything, since they produce events rather than consuming them.
177 //
178 mkws.registerWidgetType('Sort', function() {
179   var that = this;
180
181   $(this.node).change(function() {
182     that.team.set_sortOrder($(that.node).val());
183     if (that.team.submitted()) {
184       that.team.reShow();
185     }
186     return false;
187   });
188 });
189
190
191 mkws.registerWidgetType('Perpage', function() {
192   var that = this;
193
194   $(this.node).change(function() {
195     that.team.set_perpage($(that.node).val());
196     if (that.team.submitted()) {
197       that.team.reShow();
198     }
199     return false;
200   });
201 });
202
203
204 mkws.registerWidgetType('Done', function() {
205   var that = this;
206
207   this.team.queue("complete").subscribe(function(n) {
208     $(that.node).html("Search complete: found " + n + " records");
209   });
210 });
211
212
213 mkws.registerWidgetType('Switch', function() {
214   var tname = this.team.name();
215   $(this.node).html('\
216 <a href="#" onclick="mkws.switchView(\'' + tname + '\', \'records\')">Records</a><span> \
217 | \
218 </span><a href="#" onclick="mkws.switchView(\'' + tname + '\', \'targets\')">Targets</a>');
219   widget.hideWhenNarrow(this);
220 });
221
222
223 mkws.registerWidgetType('Search', function() {
224   var tname = this.team.name();
225   var M = mkws.M;
226
227   $(this.node).html('\
228 <form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
229   <input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + this.config.query_width + '" />\
230   <input class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
231 </form>');
232 });
233
234
235 mkws.registerWidgetType('SearchForm', function() {
236   var team = this.team;
237   $(this.node).submit(function() {
238     var val = team.widget('Query').value();
239     team.newSearch(val);
240     return false;
241   });
242 });
243
244
245 mkws.registerWidgetType('Results', function() {
246   var tname = this.team.name();
247
248   $(this.node).html('\
249 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
250   <tr>\
251     <td class="mkwsTermlists-Container-wide mkwsTeam_' + tname + '" width="250" valign="top">\
252       <div class="mkwsTermlists mkwsTeam_' + tname + '"></div>\
253     </td>\
254     <td class="mkwsMOTDContainer mkwsTeam_' + tname + '" valign="top">\
255       <div class="mkwsRanking mkwsTeam_' + tname + '"></div>\
256       <div class="mkwsPager mkwsTeam_' + tname + '"></div>\
257       <div class="mkwsNavi mkwsTeam_' + tname + '"></div>\
258       <div class="mkwsRecords mkwsTeam_' + tname + '"></div>\
259     </td>\
260   </tr>\
261   <tr>\
262     <td colspan="2">\
263       <div class="mkwsTermlists-Container-narrow mkwsTeam_' + tname + '"></div>\
264     </td>\
265   </tr>\
266 </table>');
267
268   widget.autosearch(this);
269 });
270
271
272 mkws.registerWidgetType('Ranking', function() {
273   var tname = this.team.name();
274   var that = this;
275   var M = mkws.M;
276
277   var s = '<form>';
278   if (this.config.show_sort) {
279     s +=  M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
280   }
281   if (this.config.show_perpage) {
282     s += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
283   }
284   s += '</form>';
285
286   $(this.node).html(s);
287
288
289   function mkwsHtmlSort() {
290     var order = that.team.sortOrder();
291
292     that.log("HTML sort, sortOrder = '" + order + "'");
293     var sort_html = '<select class="mkwsSort mkwsTeam_' + tname + '">';
294
295     for(var i = 0; i < that.config.sort_options.length; i++) {
296       var opt = that.config.sort_options[i];
297       var key = opt[0];
298       var val = opt.length == 1 ? opt[0] : opt[1];
299
300       sort_html += '<option value="' + key + '"';
301       if (order == key || order == val) {
302         sort_html += ' selected="selected"';
303       }
304       sort_html += '>' + M(val) + '</option>';
305     }
306     sort_html += '</select>';
307
308     return sort_html;
309   }
310
311   function mkwsHtmlPerpage() {
312     var perpage = that.team.perpage();
313
314     that.log("HTML perpage, perpage = " + perpage);
315     var perpage_html = '<select class="mkwsPerpage mkwsTeam_' + tname + '">';
316
317     for(var i = 0; i < that.config.perpage_options.length; i++) {
318       var key = that.config.perpage_options[i];
319
320       perpage_html += '<option value="' + key + '"';
321       if (key == perpage) {
322         perpage_html += ' selected="selected"';
323       }
324       perpage_html += '>' + key + '</option>';
325     }
326     perpage_html += '</select>';
327
328     return perpage_html;
329   }
330 });
331
332
333 mkws.registerWidgetType('Lang', function() {
334   // dynamic URL or static page? /path/foo?query=test
335   /* create locale language menu */
336   if (!this.config.show_lang) return;
337
338   var lang_default = "en";
339   var lang = this.config.lang || lang_default;
340   var list = [];
341
342   /* display a list of configured languages, or all */
343   var lang_options = this.config.lang_options || [];
344   var toBeIncluded = {};
345   for (var i = 0; i < lang_options.length; i++) {
346     toBeIncluded[lang_options[i]] = true;
347   }
348
349   for (var k in mkws.locale_lang) {
350     if (toBeIncluded[k] || lang_options.length == 0)
351       list.push(k);
352   }
353
354   // add english link
355   if (lang_options.length == 0 || toBeIncluded[lang_default])
356     list.push(lang_default);
357
358   this.log("Language menu for: " + list.join(", "));
359
360   /* the HTML part */
361   var data = "";
362   for (var i = 0; i < list.length; i++) {
363     var l = list[i];
364     if (data)
365       data += ' | ';
366
367     if (lang == l) {
368       data += ' <span>' + l + '</span> ';
369     } else {
370       data += ' <a href="' + lang_url(l) + '">' + l + '</a> '
371     }
372   }
373
374   $(this.node).html(data);
375   widget.hideWhenNarrow(this);
376
377
378   // set or re-set "lang" URL parameter
379   function lang_url(lang) {
380     var query = location.search;
381     // no query parameters? done
382     if (!query) {
383       return "?lang=" + lang;
384     }
385
386     // parameter does not exist
387     if (!query.match(/[\?&]lang=/)) {
388       return query + "&lang=" + lang;
389     }
390
391     // replace existing parameter
392     query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
393     query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
394     return query;
395   }
396 });
397
398
399 mkws.registerWidgetType('MOTD', function() {
400   var container = this.team.widget('MOTDContainer');
401   if (container) {
402     // Move the MOTD from the provided element down into the container
403     $(this.node).appendTo(container.node);
404   }
405 });
406
407
408 // Some elements have mkws* classes that makes them appear as widgets
409 // -- for example, because we want to style them using CSS -- but have
410 // no actual functionality. We register these to prevent ignorable
411 // warnings when they occur.
412
413 mkws.registerWidgetType('Query', function() {});
414 mkws.registerWidgetType('MOTDContainer', function() {});
415 mkws.registerWidgetType('Button', function() {});
416 mkws.registerWidgetType('Popup', function() {});
417
418