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(' -- ' +
44 '<span class="mkwsClientCount">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
46 '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
51 mkws.registerWidgetType('Pager', function() {
55 this.team.queue("pager").subscribe(function(data) {
56 $(that.node).html(drawPager(data))
58 function drawPager(data) {
59 var teamName = that.team.name();
60 var s = '<div style="float: right">' + M('Displaying') + ': '
61 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
62 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
63 + data.total + ')</div>';
65 //client indexes pages from 1 but pz2 from 0
67 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
68 var currentPage = that.team.currentPage();
70 var firstClkbl = (currentPage - onsides > 0)
71 ? currentPage - onsides
74 var lastClkbl = firstClkbl + 2*onsides < pages
75 ? firstClkbl + 2*onsides
78 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span> | ';
80 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
81 +'<< ' + M('Prev') + '</a> | ';
84 for(var i = firstClkbl; i <= lastClkbl; i++) {
87 numLabel = '<span class="mkwsCurrentPage">' + i + '</span>';
89 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
93 var next = ' | <span class="mkwsNext">' + M('Next') + ' >></span>';
94 if (pages - currentPage > 0)
95 next = ' | <a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
96 + M('Next') + ' >></a>';
103 if (lastClkbl < pages)
106 s += '<div style="float: clear">'
107 + prev + predots + middle + postdots + next + '</div>';
115 mkws.registerWidgetType('Records', function() {
117 var team = this.team;
119 this.team.queue("records").subscribe(function(data) {
121 for (var i = 0; i < data.hits.length; i++) {
122 var hit = data.hits[i];
123 that.team.queue("record").publish(hit);
124 var divId = team.recordElementId(hit.recid[0]);
125 html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
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 html.push(team.renderDetails(team.currentRecordData()));
134 $(that.node).html(html.join(''));
136 function renderSummary(hit) {
137 var template = team.loadTemplate(that.config.template || "Summary");
138 hit._id = team.recordElementId(hit.recid[0]);
139 hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
140 return template(hit);
144 widget.autosearch(that);
148 mkws.registerWidgetType('Navi', function() {
150 var teamName = this.team.name();
153 this.team.queue("navi").subscribe(function() {
154 var filters = that.team.filters();
157 filters.visitTargets(function(id, name) {
158 if (text) text += " | ";
159 text += M('source') + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitTarget(\'' + teamName +
160 "', '" + id + "'" + ');return false;">' + name + '</a>';
163 filters.visitFields(function(field, value) {
164 if (text) text += " | ";
165 text += M(field) + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitQuery(\'' + teamName +
166 "', '" + field + "', '" + value + "'" +
167 ');return false;">' + value + '</a>';
170 $(that.node).html(text);
175 // It seems this and the Perpage widget doen't need to subscribe to
176 // anything, since they produce events rather than consuming them.
178 mkws.registerWidgetType('Sort', function() {
181 $(this.node).change(function() {
182 that.team.set_sortOrder($(that.node).val());
183 if (that.team.submitted()) {
191 mkws.registerWidgetType('Perpage', function() {
194 $(this.node).change(function() {
195 that.team.set_perpage($(that.node).val());
196 if (that.team.submitted()) {
204 mkws.registerWidgetType('Done', function() {
207 this.team.queue("complete").subscribe(function(n) {
208 $(that.node).html("Search complete: found " + n + " records");
213 mkws.registerWidgetType('Switch', function() {
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 widget.hideWhenNarrow(this);
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>\
268 widget.autosearch(this);
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') + '.';
286 $(this.node).html(s);
289 function mkwsHtmlSort() {
290 var order = that.team.sortOrder();
292 that.log("HTML sort, 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("HTML perpage, 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 for: " + 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 widget.hideWhenNarrow(this);
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 // Some elements have mkws* classes that makes them appear as widgets
409 // -- for example, because we want to style them using CSS -- but have
410 // no actual functionality. We register these to prevent ignorable
411 // warnings when they occur.
413 mkws.registerWidgetType('Query', function() {});
414 mkws.registerWidgetType('MOTDContainer', function() {});
415 mkws.registerWidgetType('Button', function() {});
416 mkws.registerWidgetType('Popup', function() {});