1 // Functions follow for promoting the regular widget object into
2 // widgets of specific types. These could be moved into their own
6 mkws.registerWidgetType('Targets', function() {
7 if (!this.config.show_switch) return;
11 this.node.html('No information available yet.');
12 this.node.css("display", "none");
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>';
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>";
31 table += '</tbody></table>';
32 that.node.html(table);
37 mkws.registerWidgetType('Stat', function() {
41 this.team.queue("stat").subscribe(function(data) {
42 that.node.html(' -- ' +
43 '<span class="mkwsClientCount">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
45 M('Retrieved records') + ': ' + data.records + '/' + data.hits);
50 mkws.registerWidgetType('Pager', function() {
54 this.team.queue("pager").subscribe(function(data) {
55 that.node.html(drawPager(data))
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>';
64 //client indexes pages from 1 but pz2 from 0
66 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
67 var currentPage = that.team.currentPage();
69 var firstClkbl = (currentPage - onsides > 0)
70 ? currentPage - onsides
73 var lastClkbl = firstClkbl + 2*onsides < pages
74 ? firstClkbl + 2*onsides
77 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span> | ';
79 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
80 +'<< ' + M('Prev') + '</a> | ';
83 for(var i = firstClkbl; i <= lastClkbl; i++) {
86 numLabel = '<span class="mkwsCurrentPage">' + i + '</span>';
88 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
92 var next = ' | <span class="mkwsNext">' + M('Next') + ' >></span>';
93 if (pages - currentPage > 0)
94 next = ' | <a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
95 + M('Next') + ' >></a>';
102 if (lastClkbl < pages)
105 s += '<div style="float: clear">'
106 + prev + predots + middle + postdots + next + '</div>';
114 mkws.registerWidgetType('Records', function() {
116 var team = this.team;
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);
136 var template = team.loadTemplate(that.config.template || "Records");
137 that.node.html(template({"hits": data.hits}));
144 mkws.registerWidgetType('Navi', function() {
146 var teamName = this.team.name();
149 this.team.queue("navi").subscribe(function() {
150 var filters = that.team.filters();
153 filters.visitTargets(function(id, name) {
154 if (text) text += " | ";
155 text += M('source') + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitTarget(\'' + teamName +
156 "', '" + id + "'" + ');return false;">' + name + '</a>';
159 filters.visitFields(function(field, value) {
160 if (text) text += " | ";
161 text += M(field) + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitQuery(\'' + teamName +
162 "', '" + field + "', '" + value + "'" +
163 ');return false;">' + value + '</a>';
166 that.node.html(text);
171 // It seems this and the Perpage widget doen't need to subscribe to
172 // anything, since they produce events rather than consuming them.
174 mkws.registerWidgetType('Sort', function() {
177 this.node.change(function() {
178 that.team.set_sortOrder(that.node.val());
179 if (that.team.submitted()) {
187 mkws.registerWidgetType('Perpage', function() {
190 this.node.change(function() {
191 that.team.set_perpage(that.node.val());
192 if (that.team.submitted()) {
200 mkws.registerWidgetType('Done', function() {
203 this.team.queue("complete").subscribe(function(n) {
204 that.node.html("Search complete: found " + n + " records");
209 mkws.registerWidgetType('Switch', function() {
210 if (!this.config.show_switch) return;
211 var tname = this.team.name();
213 <a href="#" onclick="mkws.switchView(\'' + tname + '\', \'records\')">Records</a><span> \
215 </span><a href="#" onclick="mkws.switchView(\'' + tname + '\', \'targets\')">Targets</a>');
216 this.hideWhenNarrow();
220 mkws.registerWidgetType('Search', function() {
221 var tname = this.team.name();
225 <form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
226 <input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + this.config.query_width + '" />\
227 <input class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
232 mkws.registerWidgetType('SearchForm', function() {
233 var team = this.team;
234 this.node.submit(function() {
235 var val = team.widget('Query').value();
242 mkws.registerWidgetType('Results', function() {
243 var tname = this.team.name();
246 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
248 <td class="mkwsTermlists-Container-wide mkwsTeam_' + tname + '" width="250" valign="top">\
249 <div class="mkwsTermlists mkwsTeam_' + tname + '"></div>\
251 <td class="mkwsMOTDContainer mkwsTeam_' + tname + '" valign="top">\
252 <div class="mkwsRanking mkwsTeam_' + tname + '"></div>\
253 <div class="mkwsPager mkwsTeam_' + tname + '"></div>\
254 <div class="mkwsNavi mkwsTeam_' + tname + '"></div>\
255 <div class="mkwsRecords mkwsTeam_' + tname + '"></div>\
260 <div class="mkwsTermlists-Container-narrow mkwsTeam_' + tname + '"></div>\
269 mkws.registerWidgetType('Ranking', function() {
270 var tname = this.team.name();
275 if (this.config.show_sort) {
276 s += M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
278 if (this.config.show_perpage) {
279 s += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
286 function mkwsHtmlSort() {
287 var order = that.team.sortOrder();
289 that.log("making sort HTML, sortOrder = '" + order + "'");
290 var sort_html = '<select class="mkwsSort mkwsTeam_' + tname + '">';
292 for(var i = 0; i < that.config.sort_options.length; i++) {
293 var opt = that.config.sort_options[i];
295 var val = opt.length == 1 ? opt[0] : opt[1];
297 sort_html += '<option value="' + key + '"';
298 if (order == key || order == val) {
299 sort_html += ' selected="selected"';
301 sort_html += '>' + M(val) + '</option>';
303 sort_html += '</select>';
308 function mkwsHtmlPerpage() {
309 var perpage = that.team.perpage();
311 that.log("making perpage HTML, perpage = " + perpage);
312 var perpage_html = '<select class="mkwsPerpage mkwsTeam_' + tname + '">';
314 for(var i = 0; i < that.config.perpage_options.length; i++) {
315 var key = that.config.perpage_options[i];
317 perpage_html += '<option value="' + key + '"';
318 if (key == perpage) {
319 perpage_html += ' selected="selected"';
321 perpage_html += '>' + key + '</option>';
323 perpage_html += '</select>';
330 mkws.registerWidgetType('Lang', function() {
331 // dynamic URL or static page? /path/foo?query=test
332 /* create locale language menu */
333 if (!this.config.show_lang) return;
335 var lang_default = "en";
336 var lang = this.config.lang || lang_default;
339 /* display a list of configured languages, or all */
340 var lang_options = this.config.lang_options || [];
341 var toBeIncluded = {};
342 for (var i = 0; i < lang_options.length; i++) {
343 toBeIncluded[lang_options[i]] = true;
346 for (var k in mkws.locale_lang) {
347 if (toBeIncluded[k] || lang_options.length == 0)
352 if (lang_options.length == 0 || toBeIncluded[lang_default])
353 list.push(lang_default);
355 this.log("language menu: " + list.join(", "));
359 for (var i = 0; i < list.length; i++) {
365 data += ' <span>' + l + '</span> ';
367 data += ' <a href="' + lang_url(l) + '">' + l + '</a> '
371 this.node.html(data);
372 this.hideWhenNarrow();
375 // set or re-set "lang" URL parameter
376 function lang_url(lang) {
377 var query = location.search;
378 // no query parameters? done
380 return "?lang=" + lang;
383 // parameter does not exist
384 if (!query.match(/[\?&]lang=/)) {
385 return query + "&lang=" + lang;
388 // replace existing parameter
389 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
390 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
396 mkws.registerWidgetType('MOTD', function() {
397 var container = this.team.widget('MOTDContainer');
399 // Move the MOTD from the provided element down into the container
400 this.node.appendTo(container.node);
405 // This widget has no functionality of its own, but its configuration
406 // is copied up into its team, allowing it to affect other widgets in
409 mkws.registerWidgetType('Config', function() {
411 for (var name in c) {
412 if (c.hasOwnProperty(name)) {
413 this.team.config()[name] = c[name];
414 this.log(this + " copied property " + name + "='" + c[name] + "' up to team");
420 mkws.registerWidgetType('Progress', function() {
424 this.team.queue("stat").subscribe(function(data) {
425 var s = '<span class="mkwsDone">';
426 for (var i = 0; i < data.clients; i++) {
427 if (i == data.clients - data.activeclients) {
429 s += '<span class="mkwsWaiting">';
440 // Some elements have mkws* classes that makes them appear as widgets
441 // -- for example, because we want to style them using CSS -- but have
442 // no actual functionality. We register these to prevent ignorable
443 // warnings when they occur.
445 mkws.registerWidgetType('Query', function() {});
446 mkws.registerWidgetType('MOTDContainer', function() {});
447 mkws.registerWidgetType('Button', function() {});
448 mkws.registerWidgetType('Popup', function() {});