Replace team.config() getter with team.config object.
[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     var html = [];
120     for (var i = 0; i < data.hits.length; i++) {
121       var hit = data.hits[i];
122       that.team.queue("record").publish(hit);
123       var divId = team.recordElementId(hit.recid[0]);
124       html.push('<div class="mkwsSummary mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
125       // ### At some point, we may be able to move the
126       // m_currentRecordId and m_currentRecordData members
127       // from the team object into this widget.
128       if (hit.recid == team.currentRecordId()) {
129         if (team.currentRecordData())
130           html.push(team.renderDetails(team.currentRecordData()));
131       }
132     }
133     that.node.html(html.join(''));
134
135     function renderSummary(hit) {
136       var template = team.loadTemplate(that.config.template || "Summary");
137       hit._id = team.recordElementId(hit.recid[0]);
138       hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
139       return template(hit);
140     }
141   });
142
143   that.autosearch();
144 });
145
146
147 mkws.registerWidgetType('Navi', function() {
148   var that = this;
149   var teamName = this.team.name();
150   var M = mkws.M;
151
152   this.team.queue("navi").subscribe(function() {
153     var filters = that.team.filters();
154     var text = "";
155
156     filters.visitTargets(function(id, name) {
157       if (text) text += " | ";
158       text += M('source') + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitTarget(\'' + teamName +
159         "', '" + id + "'" + ');return false;">' + name + '</a>';
160     });
161
162     filters.visitFields(function(field, value) {
163       if (text) text += " | ";
164       text += M(field) + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitQuery(\'' + teamName +
165         "', '" + field + "', '" + value + "'" +
166         ');return false;">' + value + '</a>';
167     });
168
169     that.node.html(text);
170   });
171 });
172
173
174 // It seems this and the Perpage widget doen't need to subscribe to
175 // anything, since they produce events rather than consuming them.
176 //
177 mkws.registerWidgetType('Sort', function() {
178   var that = this;
179
180   this.node.change(function() {
181     that.team.set_sortOrder(that.node.val());
182     if (that.team.submitted()) {
183       that.team.reShow();
184     }
185     return false;
186   });
187 });
188
189
190 mkws.registerWidgetType('Perpage', function() {
191   var that = this;
192
193   this.node.change(function() {
194     that.team.set_perpage(that.node.val());
195     if (that.team.submitted()) {
196       that.team.reShow();
197     }
198     return false;
199   });
200 });
201
202
203 mkws.registerWidgetType('Done', function() {
204   var that = this;
205
206   this.team.queue("complete").subscribe(function(n) {
207     that.node.html("Search complete: found " + n + " records");
208   });
209 });
210
211
212 mkws.registerWidgetType('Switch', function() {
213   if (!this.config.show_switch) return;
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   this.hideWhenNarrow();
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   this.autosearch();
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("making sort HTML, 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("making perpage HTML, 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: " + 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   this.hideWhenNarrow();
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 // This widget has no functionality of its own, but its configuration
409 // is copied up into its team, allowing it to affect other widgets in
410 // the team.
411 //
412 mkws.registerWidgetType('Config', function() {
413   var c = this.config;
414   for (var name in c) {
415     if (c.hasOwnProperty(name)) {
416       this.team.config[name] = c[name];
417       this.log(this + " copied property " + name + "='" + c[name] + "' up to team");
418     }
419   }
420 });
421
422
423 mkws.registerWidgetType('Progress', function() {
424   var that = this;
425
426   this.node.hide();
427   this.team.queue("stat").subscribe(function(data) {
428     var s = '<span class="mkwsDone">';
429     for (var i = 0; i < data.clients; i++) {
430       if (i == data.clients - data.activeclients) {
431         s += '</span>';
432         s += '<span class="mkwsWaiting">';
433       }
434       s += '&#x2588;';
435     }
436     s += '</span>';
437     that.node.html(s);
438     that.node.show();
439   });
440 });
441
442
443 // Some elements have mkws* classes that makes them appear as widgets
444 // -- for example, because we want to style them using CSS -- but have
445 // no actual functionality. We register these to prevent ignorable
446 // warnings when they occur.
447
448 mkws.registerWidgetType('Query', function() {});
449 mkws.registerWidgetType('MOTDContainer', function() {});
450 mkws.registerWidgetType('Button', function() {});
451
452