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() {
10 $(this.node).html('No information available yet.');
11 $(this.node).css("display", "none");
13 this.team.queue("targets").subscribe(function(data) {
14 var table ='<table><thead><tr>' +
15 '<td>' + M('Target ID') + '</td>' +
16 '<td>' + M('Hits') + '</td>' +
17 '<td>' + M('Diags') + '</td>' +
18 '<td>' + M('Records') + '</td>' +
19 '<td>' + M('State') + '</td>' +
20 '</tr></thead><tbody>';
22 for (var i = 0; i < data.length; i++) {
23 table += "<tr><td>" + data[i].id +
24 "</td><td>" + data[i].hits +
25 "</td><td>" + data[i].diagnostic +
26 "</td><td>" + data[i].records +
27 "</td><td>" + data[i].state + "</td></tr>";
30 table += '</tbody></table>';
31 $(that.node).html(table);
36 mkws.registerWidgetType('Stat', function() {
40 this.team.queue("stat").subscribe(function(data) {
41 if (that.node.length === 0) alert("huh?!");
43 $(that.node).html('<span class="head">' + M('Status info') + '</span>' +
45 '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
47 '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
52 mkws.registerWidgetType('Pager', function() {
56 this.team.queue("pager").subscribe(function(data) {
57 $(that.node).html(drawPager(data))
59 function drawPager(data) {
60 var teamName = that.team.name();
61 var s = '<div style="float: right">' + M('Displaying') + ': '
62 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
63 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
64 + data.total + ')</div>';
66 //client indexes pages from 1 but pz2 from 0
68 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
69 var currentPage = that.team.currentPage();
71 var firstClkbl = (currentPage - onsides > 0)
72 ? currentPage - onsides
75 var lastClkbl = firstClkbl + 2*onsides < pages
76 ? firstClkbl + 2*onsides
79 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span> | ';
81 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
82 +'<< ' + M('Prev') + '</a> | ';
85 for(var i = firstClkbl; i <= lastClkbl; i++) {
88 numLabel = '<span class="mkwsCurrentPage">' + i + '</span>';
90 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
94 var next = ' | <span class="mkwsNext">' + M('Next') + ' >></span>';
95 if (pages - currentPage > 0)
96 next = ' | <a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
97 + M('Next') + ' >></a>';
104 if (lastClkbl < pages)
107 s += '<div style="float: clear">'
108 + prev + predots + middle + postdots + next + '</div>';
116 mkws.registerWidgetType('Records', function() {
118 var team = this.team;
120 this.team.queue("records").subscribe(function(data) {
122 for (var i = 0; i < data.hits.length; i++) {
123 var hit = data.hits[i];
124 that.team.queue("record").publish(hit);
125 var divId = team.recordElementId(hit.recid[0]);
126 html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
127 // ### At some point, we may be able to move the
128 // m_currentRecordId and m_currentRecordData members
129 // from the team object into this widget.
130 if (hit.recid == team.currentRecordId()) {
131 if (team.currentRecordData())
132 html.push(team.renderDetails(team.currentRecordData()));
135 $(that.node).html(html.join(''));
137 function renderSummary(hit) {
138 var template = team.loadTemplate(that.config.template || "Summary");
139 hit._id = team.recordElementId(hit.recid[0]);
140 hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
141 return template(hit);
145 widget.autosearch(that);
149 mkws.registerWidgetType('Navi', function() {
151 var teamName = this.team.name();
154 this.team.queue("navi").subscribe(function() {
155 var filters = that.team.filters();
158 filters.visitTargets(function(id, name) {
159 if (text) text += " | ";
160 text += M('source') + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitTarget(\'' + teamName +
161 "', '" + id + "'" + ');return false;">' + name + '</a>';
164 filters.visitFields(function(field, value) {
165 if (text) text += " | ";
166 text += M(field) + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitQuery(\'' + teamName +
167 "', '" + field + "', '" + value + "'" +
168 ');return false;">' + value + '</a>';
171 $(that.node).html(text);
176 // It seems this and the Perpage widget doen't need to subscribe to
177 // anything, since they produce events rather than consuming them.
179 mkws.registerWidgetType('Sort', function() {
182 $(this.node).change(function() {
183 that.team.set_sortOrder($(that.node).val());
184 if (that.team.submitted()) {
192 mkws.registerWidgetType('Perpage', function() {
195 $(this.node).change(function() {
196 that.team.set_perpage($(that.node).val());
197 if (that.team.submitted()) {
205 mkws.registerWidgetType('Done', function() {
208 this.team.queue("complete").subscribe(function(n) {
209 $(that.node).html("Search complete: found " + n + " records");
214 mkws.registerWidgetType('Switch', function() {
215 var tname = this.team.name();
217 <a href="#" onclick="mkws.switchView(\'' + tname + '\', \'records\')">Records</a><span> \
219 </span><a href="#" onclick="mkws.switchView(\'' + tname + '\', \'targets\')">Targets</a>');
220 widget.hideWhenNarrow(this);
224 mkws.registerWidgetType('Search', function() {
225 var tname = this.team.name();
229 <form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
230 <input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + this.config.query_width + '" />\
231 <input class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
236 mkws.registerWidgetType('SearchForm', function() {
237 var team = this.team;
238 $(this.node).submit(function() {
239 var val = team.widget('Query').value();
246 mkws.registerWidgetType('Results', function() {
247 var tname = this.team.name();
250 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
252 <td class="mkwsTermlists-Container-wide mkwsTeam_' + tname + '" width="250" valign="top">\
253 <div class="mkwsTermlists mkwsTeam_' + tname + '"></div>\
255 <td class="mkwsMOTDContainer mkwsTeam_' + tname + '" valign="top">\
256 <div class="mkwsRanking mkwsTeam_' + tname + '"></div>\
257 <div class="mkwsPager mkwsTeam_' + tname + '"></div>\
258 <div class="mkwsNavi mkwsTeam_' + tname + '"></div>\
259 <div class="mkwsRecords mkwsTeam_' + tname + '"></div>\
264 <div class="mkwsTermlists-Container-narrow mkwsTeam_' + tname + '"></div>\
269 widget.autosearch(this);
273 mkws.registerWidgetType('Ranking', function() {
274 var tname = this.team.name();
279 if (this.config.show_sort) {
280 s += M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
282 if (this.config.show_perpage) {
283 s += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
287 $(this.node).html(s);
290 function mkwsHtmlSort() {
291 var order = that.team.sortOrder();
293 that.log("HTML sort, sortOrder = '" + order + "'");
294 var sort_html = '<select class="mkwsSort mkwsTeam_' + tname + '">';
296 for(var i = 0; i < that.config.sort_options.length; i++) {
297 var opt = that.config.sort_options[i];
299 var val = opt.length == 1 ? opt[0] : opt[1];
301 sort_html += '<option value="' + key + '"';
302 if (order == key || order == val) {
303 sort_html += ' selected="selected"';
305 sort_html += '>' + M(val) + '</option>';
307 sort_html += '</select>';
312 function mkwsHtmlPerpage() {
313 var perpage = that.team.perpage();
315 that.log("HTML perpage, perpage = " + perpage);
316 var perpage_html = '<select class="mkwsPerpage mkwsTeam_' + tname + '">';
318 for(var i = 0; i < that.config.perpage_options.length; i++) {
319 var key = that.config.perpage_options[i];
321 perpage_html += '<option value="' + key + '"';
322 if (key == perpage) {
323 perpage_html += ' selected="selected"';
325 perpage_html += '>' + key + '</option>';
327 perpage_html += '</select>';
334 mkws.registerWidgetType('Lang', function() {
335 // dynamic URL or static page? /path/foo?query=test
336 /* create locale language menu */
337 if (!this.config.show_lang) return;
339 var lang_default = "en";
340 var lang = this.config.lang || lang_default;
343 /* display a list of configured languages, or all */
344 var lang_options = this.config.lang_options || [];
345 var toBeIncluded = {};
346 for (var i = 0; i < lang_options.length; i++) {
347 toBeIncluded[lang_options[i]] = true;
350 for (var k in mkws.locale_lang) {
351 if (toBeIncluded[k] || lang_options.length == 0)
356 if (lang_options.length == 0 || toBeIncluded[lang_default])
357 list.push(lang_default);
359 this.log("Language menu for: " + list.join(", "));
363 for (var i = 0; i < list.length; i++) {
369 data += ' <span>' + l + '</span> ';
371 data += ' <a href="' + lang_url(l) + '">' + l + '</a> '
375 $(this.node).html(data);
376 widget.hideWhenNarrow(this);
379 // set or re-set "lang" URL parameter
380 function lang_url(lang) {
381 var query = location.search;
382 // no query parameters? done
384 return "?lang=" + lang;
387 // parameter does not exist
388 if (!query.match(/[\?&]lang=/)) {
389 return query + "&lang=" + lang;
392 // replace existing parameter
393 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
394 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
400 mkws.registerWidgetType('MOTD', function() {
401 var container = this.team.widget('MOTDContainer');
403 // Move the MOTD from the provided element down into the container
404 $(this.node).appendTo(container.node);
409 // Some elements have mkws* classes that makes them appear as widgets
410 // -- for example, because we want to style them using CSS -- but have
411 // no actual functionality. We register these to prevent ignorable
412 // warnings when they occur.
414 mkws.registerWidgetType('Query', function() {});
415 mkws.registerWidgetType('MOTDContainer', function() {});
416 mkws.registerWidgetType('Button', function() {});
417 mkws.registerWidgetType('Popup', function() {});