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) {
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()));
133 that.node.html(html.join(''));
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);
147 mkws.registerWidgetType('Navi', function() {
149 var teamName = this.team.name();
152 this.team.queue("navi").subscribe(function() {
153 var filters = that.team.filters();
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>';
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>';
169 that.node.html(text);
174 // It seems this and the Perpage widget doen't need to subscribe to
175 // anything, since they produce events rather than consuming them.
177 mkws.registerWidgetType('Sort', function() {
180 this.node.change(function() {
181 that.team.set_sortOrder(that.node.val());
182 if (that.team.submitted()) {
190 mkws.registerWidgetType('Perpage', function() {
193 this.node.change(function() {
194 that.team.set_perpage(that.node.val());
195 if (that.team.submitted()) {
203 mkws.registerWidgetType('Done', function() {
206 this.team.queue("complete").subscribe(function(n) {
207 that.node.html("Search complete: found " + n + " records");
212 mkws.registerWidgetType('Switch', function() {
213 if (!this.config.show_switch) return;
214 var tname = this.team.name();
216 <a href="#" onclick="mkws.switchView(\'' + tname + '\', \'records\')">Records</a><span> \
218 </span><a href="#" onclick="mkws.switchView(\'' + tname + '\', \'targets\')">Targets</a>');
219 this.hideWhenNarrow();
223 mkws.registerWidgetType('Search', function() {
224 var tname = this.team.name();
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') + '" />\
235 mkws.registerWidgetType('SearchForm', function() {
236 var team = this.team;
237 this.node.submit(function() {
238 var val = team.widget('Query').value();
245 mkws.registerWidgetType('Results', function() {
246 var tname = this.team.name();
249 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
251 <td class="mkwsTermlists-Container-wide mkwsTeam_' + tname + '" width="250" valign="top">\
252 <div class="mkwsTermlists mkwsTeam_' + tname + '"></div>\
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>\
263 <div class="mkwsTermlists-Container-narrow mkwsTeam_' + tname + '"></div>\
272 mkws.registerWidgetType('Ranking', function() {
273 var tname = this.team.name();
278 if (this.config.show_sort) {
279 s += M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
281 if (this.config.show_perpage) {
282 s += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
289 function mkwsHtmlSort() {
290 var order = that.team.sortOrder();
292 that.log("making sort HTML, sortOrder = '" + order + "'");
293 var sort_html = '<select class="mkwsSort mkwsTeam_' + tname + '">';
295 for(var i = 0; i < that.config.sort_options.length; i++) {
296 var opt = that.config.sort_options[i];
298 var val = opt.length == 1 ? opt[0] : opt[1];
300 sort_html += '<option value="' + key + '"';
301 if (order == key || order == val) {
302 sort_html += ' selected="selected"';
304 sort_html += '>' + M(val) + '</option>';
306 sort_html += '</select>';
311 function mkwsHtmlPerpage() {
312 var perpage = that.team.perpage();
314 that.log("making perpage HTML, perpage = " + perpage);
315 var perpage_html = '<select class="mkwsPerpage mkwsTeam_' + tname + '">';
317 for(var i = 0; i < that.config.perpage_options.length; i++) {
318 var key = that.config.perpage_options[i];
320 perpage_html += '<option value="' + key + '"';
321 if (key == perpage) {
322 perpage_html += ' selected="selected"';
324 perpage_html += '>' + key + '</option>';
326 perpage_html += '</select>';
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;
338 var lang_default = "en";
339 var lang = this.config.lang || lang_default;
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;
349 for (var k in mkws.locale_lang) {
350 if (toBeIncluded[k] || lang_options.length == 0)
355 if (lang_options.length == 0 || toBeIncluded[lang_default])
356 list.push(lang_default);
358 this.log("language menu: " + list.join(", "));
362 for (var i = 0; i < list.length; i++) {
368 data += ' <span>' + l + '</span> ';
370 data += ' <a href="' + lang_url(l) + '">' + l + '</a> '
374 this.node.html(data);
375 this.hideWhenNarrow();
378 // set or re-set "lang" URL parameter
379 function lang_url(lang) {
380 var query = location.search;
381 // no query parameters? done
383 return "?lang=" + lang;
386 // parameter does not exist
387 if (!query.match(/[\?&]lang=/)) {
388 return query + "&lang=" + lang;
391 // replace existing parameter
392 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
393 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
399 mkws.registerWidgetType('MOTD', function() {
400 var container = this.team.widget('MOTDContainer');
402 // Move the MOTD from the provided element down into the container
403 this.node.appendTo(container.node);
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
412 mkws.registerWidgetType('Config', function() {
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");
423 mkws.registerWidgetType('Progress', function() {
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) {
432 s += '<span class="mkwsWaiting">';
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.
448 mkws.registerWidgetType('Query', function() {});
449 mkws.registerWidgetType('MOTDContainer', function() {});
450 mkws.registerWidgetType('Button', function() {});
451 mkws.registerWidgetType('Popup', function() {});