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() {
11 <div class="mkwsBytarget mkwsTeam_' + this.team.name() + '">\
12 No information available yet.\
14 $(this.node).css("display", "none");
16 this.team.queue("targets").subscribe(function(data) {
17 var table ='<table><thead><tr>' +
18 '<td>' + M('Target ID') + '</td>' +
19 '<td>' + M('Hits') + '</td>' +
20 '<td>' + M('Diags') + '</td>' +
21 '<td>' + M('Records') + '</td>' +
22 '<td>' + M('State') + '</td>' +
23 '</tr></thead><tbody>';
25 for (var i = 0; i < data.length; i++) {
26 table += "<tr><td>" + data[i].id +
27 "</td><td>" + data[i].hits +
28 "</td><td>" + data[i].diagnostic +
29 "</td><td>" + data[i].records +
30 "</td><td>" + data[i].state + "</td></tr>";
33 table += '</tbody></table>';
34 var subnode = $(that.node).children('.mkwsBytarget');
40 mkws.registerWidgetType('Stat', function() {
44 this.team.queue("stat").subscribe(function(data) {
45 if (that.node.length === 0) alert("huh?!");
47 $(that.node).html('<span class="head">' + M('Status info') + '</span>' +
49 '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
51 '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
56 mkws.registerWidgetType('Pager', function() {
60 this.team.queue("pager").subscribe(function(data) {
61 $(that.node).html(drawPager(data))
63 function drawPager(data) {
64 var teamName = that.team.name();
65 var s = '<div style="float: right">' + M('Displaying') + ': '
66 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
67 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
68 + data.total + ')</div>';
70 //client indexes pages from 1 but pz2 from 0
72 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
73 var currentPage = that.team.currentPage();
75 var firstClkbl = (currentPage - onsides > 0)
76 ? currentPage - onsides
79 var lastClkbl = firstClkbl + 2*onsides < pages
80 ? firstClkbl + 2*onsides
83 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span> | ';
85 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
86 +'<< ' + M('Prev') + '</a> | ';
89 for(var i = firstClkbl; i <= lastClkbl; i++) {
92 numLabel = '<span class="mkwsSelected">' + i + '</span>';
94 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
98 var next = ' | <span class="mkwsNext">' + M('Next') + ' >></span>';
99 if (pages - currentPage > 0)
100 next = ' | <a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
101 + M('Next') + ' >></a>';
108 if (lastClkbl < pages)
111 s += '<div style="float: clear">'
112 + prev + predots + middle + postdots + next + '</div>';
120 mkws.registerWidgetType('Records', function() {
122 var team = this.team;
124 this.team.queue("records").subscribe(function(data) {
126 for (var i = 0; i < data.hits.length; i++) {
127 var hit = data.hits[i];
128 that.team.queue("record").publish(hit);
129 var divId = team.recordElementId(hit.recid[0]);
130 html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
131 // ### At some point, we may be able to move the
132 // m_currentRecordId and m_currentRecordData members
133 // from the team object into this widget.
134 if (hit.recid == team.currentRecordId()) {
135 if (team.currentRecordData())
136 html.push(team.renderDetails(team.currentRecordData()));
139 $(that.node).html(html.join(''));
141 function renderSummary(hit) {
142 var template = team.loadTemplate(that.config.template || "Summary");
143 hit._id = team.recordElementId(hit.recid[0]);
144 hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
145 return template(hit);
149 widget.autosearch(that);
153 mkws.registerWidgetType('Navi', function() {
155 var teamName = this.team.name();
158 this.team.queue("navi").subscribe(function() {
159 var filters = that.team.filters();
162 filters.visitTargets(function(id, name) {
163 if (text) text += " | ";
164 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
165 "', '" + id + "'" + ');return false;">' + name + '</a>';
168 filters.visitFields(function(field, value) {
169 if (text) text += " | ";
170 text += M(field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
171 "', '" + field + "', '" + value + "'" +
172 ');return false;">' + value + '</a>';
175 $(that.node).html(text);
180 // It seems this and the Perpage widget doen't need to subscribe to
181 // anything, since they produce events rather than consuming them.
183 mkws.registerWidgetType('Sort', function() {
186 $(this.node).change(function() {
187 that.team.set_sortOrder($(that.node).val());
188 if (that.team.submitted()) {
196 mkws.registerWidgetType('Perpage', function() {
199 $(this.node).change(function() {
200 that.team.set_perpage($(that.node).val());
201 if (that.team.submitted()) {
209 mkws.registerWidgetType('Done', function() {
212 this.team.queue("complete").subscribe(function(n) {
213 $(that.node).html("Search complete: found " + n + " records");
218 mkws.registerWidgetType('Switch', function() {
219 var tname = this.team.name();
221 <a href="#" onclick="mkws.switchView(\'' + tname + '\', \'records\')">Records</a><span> \
223 </span><a href="#" onclick="mkws.switchView(\'' + tname + '\', \'targets\')">Targets</a>');
224 widget.hideWhenNarrow(this);
228 mkws.registerWidgetType('Search', function() {
229 var tname = this.team.name();
233 <form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
234 <input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + this.config.query_width + '" />\
235 <input class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
240 mkws.registerWidgetType('SearchForm', function() {
241 var team = this.team;
242 $(this.node).submit(function() {
243 var val = team.widget('Query').value();
250 mkws.registerWidgetType('Results', function() {
251 var tname = this.team.name();
254 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
256 <td class="mkwsTermlists-Container-wide mkwsTeam_' + tname + '" width="250" valign="top">\
257 <div class="mkwsTermlists mkwsTeam_' + tname + '"></div>\
259 <td class="mkwsMOTDContainer mkwsTeam_' + tname + '" valign="top">\
260 <div class="mkwsRanking mkwsTeam_' + tname + '"></div>\
261 <div class="mkwsPager mkwsTeam_' + tname + '"></div>\
262 <div class="mkwsNavi mkwsTeam_' + tname + '"></div>\
263 <div class="mkwsRecords mkwsTeam_' + tname + '"></div>\
268 <div class="mkwsTermlists-Container-narrow mkwsTeam_' + tname + '"></div>\
273 widget.autosearch(this);
277 mkws.registerWidgetType('Ranking', function() {
278 var tname = this.team.name();
282 var s = '<form name="mkwsSelect" class="mkwsSelect mkwsTeam_' + tname + '" action="" >';
283 if (this.config.show_sort) {
284 s += M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
286 if (this.config.show_perpage) {
287 s += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
291 $(this.node).html(s);
294 function mkwsHtmlSort() {
295 var order = that.team.sortOrder();
297 that.log("HTML sort, sortOrder = '" + order + "'");
298 var sort_html = '<select class="mkwsSort mkwsTeam_' + tname + '">';
300 for(var i = 0; i < that.config.sort_options.length; i++) {
301 var opt = that.config.sort_options[i];
303 var val = opt.length == 1 ? opt[0] : opt[1];
305 sort_html += '<option value="' + key + '"';
306 if (order == key || order == val) {
307 sort_html += ' selected="selected"';
309 sort_html += '>' + M(val) + '</option>';
311 sort_html += '</select>';
316 function mkwsHtmlPerpage() {
317 var perpage = that.team.perpage();
319 that.log("HTML perpage, perpage = " + perpage);
320 var perpage_html = '<select class="mkwsPerpage mkwsTeam_' + tname + '">';
322 for(var i = 0; i < that.config.perpage_options.length; i++) {
323 var key = that.config.perpage_options[i];
325 perpage_html += '<option value="' + key + '"';
326 if (key == perpage) {
327 perpage_html += ' selected="selected"';
329 perpage_html += '>' + key + '</option>';
331 perpage_html += '</select>';
338 mkws.registerWidgetType('Lang', function() {
339 // dynamic URL or static page? /path/foo?query=test
340 /* create locale language menu */
341 if (!this.config.show_lang) return;
343 var lang_default = "en";
344 var lang = this.config.lang || lang_default;
347 /* display a list of configured languages, or all */
348 var lang_options = this.config.lang_options || [];
349 var toBeIncluded = {};
350 for (var i = 0; i < lang_options.length; i++) {
351 toBeIncluded[lang_options[i]] = true;
354 for (var k in mkws.locale_lang) {
355 if (toBeIncluded[k] || lang_options.length == 0)
360 if (lang_options.length == 0 || toBeIncluded[lang_default])
361 list.push(lang_default);
363 this.log("Language menu for: " + list.join(", "));
367 for (var i = 0; i < list.length; i++) {
373 data += ' <span>' + l + '</span> ';
375 data += ' <a href="' + lang_url(l) + '">' + l + '</a> '
379 $(this.node).html(data);
380 widget.hideWhenNarrow(this);
383 // set or re-set "lang" URL parameter
384 function lang_url(lang) {
385 var query = location.search;
386 // no query parameters? done
388 return "?lang=" + lang;
391 // parameter does not exist
392 if (!query.match(/[\?&]lang=/)) {
393 return query + "&lang=" + lang;
396 // replace existing parameter
397 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
398 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
404 mkws.registerWidgetType('MOTD', function() {
405 var container = this.team.widget('MOTDContainer');
407 // Move the MOTD from the provided element down into the container
408 $(this.node).appendTo(container.node);
413 // Some elements have mkws* classes that makes them appear as widgets
414 // -- for example, because we want to style them using CSS -- but have
415 // no actual functionality. We register these to prevent ignorable
416 // warnings when they occur.
418 mkws.registerWidgetType('Query', function() {});
419 mkws.registerWidgetType('MOTDContainer', function() {});
420 mkws.registerWidgetType('Button', function() {});
421 mkws.registerWidgetType('Popup', function() {});