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 var targs = $.extend({}, {"hits": data.hits}, that.config.template_vars);
138 that.node.html(template(targs));
145 mkws.registerWidgetType('Navi', function() {
147 var teamName = this.team.name();
150 this.team.queue("navi").subscribe(function() {
151 var filters = that.team.filters();
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>';
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>';
167 that.node.html(text);
172 // It seems this and the Perpage widget doen't need to subscribe to
173 // anything, since they produce events rather than consuming them.
175 mkws.registerWidgetType('Sort', function() {
178 this.node.change(function() {
179 that.team.set_sortOrder(that.node.val());
180 if (that.team.submitted()) {
188 mkws.registerWidgetType('Perpage', function() {
191 this.node.change(function() {
192 that.team.set_perpage(that.node.val());
193 if (that.team.submitted()) {
201 mkws.registerWidgetType('Done', function() {
204 this.team.queue("complete").subscribe(function(n) {
205 that.node.html("Search complete: found " + n + " records");
210 mkws.registerWidgetType('Switch', function() {
211 if (!this.config.show_switch) return;
212 var tname = this.team.name();
214 <a href="#" onclick="mkws.switchView(\'' + tname + '\', \'records\')">Records</a><span> \
216 </span><a href="#" onclick="mkws.switchView(\'' + tname + '\', \'targets\')">Targets</a>');
217 this.hideWhenNarrow();
221 mkws.registerWidgetType('Search', function() {
222 var tname = this.team.name();
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') + '" />\
233 mkws.registerWidgetType('SearchForm', function() {
234 var team = this.team;
235 this.node.submit(function() {
236 var val = team.widget('Query').value();
243 mkws.registerWidgetType('Results', function() {
244 var tname = this.team.name();
247 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
249 <td class="mkwsTermlists-Container-wide mkwsTeam_' + tname + '" width="250" valign="top">\
250 <div class="mkwsTermlists mkwsTeam_' + tname + '"></div>\
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>\
261 <div class="mkwsTermlists-Container-narrow mkwsTeam_' + tname + '"></div>\
270 mkws.registerWidgetType('Ranking', function() {
271 var tname = this.team.name();
276 if (this.config.show_sort) {
277 s += M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
279 if (this.config.show_perpage) {
280 s += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
287 function mkwsHtmlSort() {
288 var order = that.team.sortOrder();
290 that.log("making sort HTML, sortOrder = '" + order + "'");
291 var sort_html = '<select class="mkwsSort mkwsTeam_' + tname + '">';
293 for(var i = 0; i < that.config.sort_options.length; i++) {
294 var opt = that.config.sort_options[i];
296 var val = opt.length == 1 ? opt[0] : opt[1];
298 sort_html += '<option value="' + key + '"';
299 if (order == key || order == val) {
300 sort_html += ' selected="selected"';
302 sort_html += '>' + M(val) + '</option>';
304 sort_html += '</select>';
309 function mkwsHtmlPerpage() {
310 var perpage = that.team.perpage();
312 that.log("making perpage HTML, perpage = " + perpage);
313 var perpage_html = '<select class="mkwsPerpage mkwsTeam_' + tname + '">';
315 for(var i = 0; i < that.config.perpage_options.length; i++) {
316 var key = that.config.perpage_options[i];
318 perpage_html += '<option value="' + key + '"';
319 if (key == perpage) {
320 perpage_html += ' selected="selected"';
322 perpage_html += '>' + key + '</option>';
324 perpage_html += '</select>';
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;
336 var lang_default = "en";
337 var lang = this.config.lang || lang_default;
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;
347 for (var k in mkws.locale_lang) {
348 if (toBeIncluded[k] || lang_options.length == 0)
353 if (lang_options.length == 0 || toBeIncluded[lang_default])
354 list.push(lang_default);
356 this.log("language menu: " + list.join(", "));
360 for (var i = 0; i < list.length; i++) {
366 data += ' <span>' + l + '</span> ';
368 data += ' <a href="' + lang_url(l) + '">' + l + '</a> '
372 this.node.html(data);
373 this.hideWhenNarrow();
376 // set or re-set "lang" URL parameter
377 function lang_url(lang) {
378 var query = location.search;
379 // no query parameters? done
381 return "?lang=" + lang;
384 // parameter does not exist
385 if (!query.match(/[\?&]lang=/)) {
386 return query + "&lang=" + lang;
389 // replace existing parameter
390 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
391 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
397 mkws.registerWidgetType('MOTD', function() {
398 var container = this.team.widget('MOTDContainer');
400 // Move the MOTD from the provided element down into the container
401 this.node.appendTo(container.node);
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
410 mkws.registerWidgetType('Config', function() {
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");
421 mkws.registerWidgetType('Progress', function() {
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) {
430 s += '<span class="mkwsWaiting">';
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.
446 mkws.registerWidgetType('Query', function() {});
447 mkws.registerWidgetType('MOTDContainer', function() {});
448 mkws.registerWidgetType('Button', function() {});