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