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