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('Results', function() {
121 // Nothing to do apart from act as an autosearch trigger
122 // Contained elements do all the real work
123 widget.autosearch(this);
127 mkws.registerWidgetType('Records', function() {
129 var team = this.team;
131 this.team.queue("records").subscribe(function(data) {
133 for (var i = 0; i < data.hits.length; i++) {
134 var hit = data.hits[i];
135 that.team.queue("record").publish(hit);
136 var divId = team.recordElementId(hit.recid[0]);
137 html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
138 // ### At some point, we may be able to move the
139 // m_currentRecordId and m_currentRecordData members
140 // from the team object into this widget.
141 if (hit.recid == team.currentRecordId()) {
142 if (team.currentRecordData())
143 html.push(team.renderDetails(team.currentRecordData()));
146 $(that.node).html(html.join(''));
148 function renderSummary(hit) {
149 var template = team.loadTemplate(that.config.template || "Summary");
150 hit._id = team.recordElementId(hit.recid[0]);
151 hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
152 return template(hit);
156 widget.autosearch(that);
160 mkws.registerWidgetType('Navi', function() {
162 var teamName = this.team.name();
165 this.team.queue("navi").subscribe(function() {
166 var filters = that.team.filters();
169 filters.visitTargets(function(id, name) {
170 if (text) text += " | ";
171 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
172 "', '" + id + "'" + ');return false;">' + name + '</a>';
175 filters.visitFields(function(field, value) {
176 if (text) text += " | ";
177 text += M(field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
178 "', '" + field + "', '" + value + "'" +
179 ');return false;">' + value + '</a>';
182 $(that.node).html(text);
187 // It seems this and the Perpage widget doen't need to subscribe to
188 // anything, since they produce events rather than consuming them.
190 mkws.registerWidgetType('Sort', function() {
193 $(this.node).change(function() {
194 that.team.set_sortOrder($(that.node).val());
195 if (that.team.submitted()) {
203 mkws.registerWidgetType('Perpage', function() {
206 $(this.node).change(function() {
207 that.team.set_perpage($(that.node).val());
208 if (that.team.submitted()) {
216 mkws.registerWidgetType('Done', function() {
219 this.team.queue("complete").subscribe(function(n) {
220 $(that.node).html("Search complete: found " + n + " records");
225 mkws.registerWidgetType('Switch', function() {
226 var tname = this.team.name();
228 <a href="#" onclick="mkws.switchView(\'' + tname + '\', \'records\')">Records</a><span> \
230 </span><a href="#" onclick="mkws.switchView(\'' + tname + '\', \'targets\')">Targets</a>');
234 mkws.registerWidgetType('Search', function() {
235 var tname = this.team.name();
239 <form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
240 <input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + this.config.query_width + '" />\
241 <input class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
246 mkws.registerWidgetType('SearchForm', function() {
247 var team = this.team;
248 $(this.node).submit(function() {
249 var val = team.widget('Query').value();
256 mkws.registerWidgetType('Results', function() {
257 var tname = this.team.name();
260 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
262 <td class="mkwsTermlistContainer1 mkwsTeam_' + tname + '" width="250" valign="top">\
263 <div class="mkwsTermlists mkwsTeam_' + tname + '"></div>\
265 <td class="mkwsMOTDContainer mkwsTeam_' + tname + '" valign="top">\
266 <div class="mkwsRanking mkwsTeam_' + tname + '"></div>\
267 <div class="mkwsPager mkwsTeam_' + tname + '"></div>\
268 <div class="mkwsNavi mkwsTeam_' + tname + '"></div>\
269 <div class="mkwsRecords mkwsTeam_' + tname + '"></div>\
274 <div class="mkwsTermlistContainer2 mkwsTeam_' + tname + '"></div>\
281 mkws.registerWidgetType('Ranking', function() {
282 var tname = this.team.name();
286 var s = '<form name="mkwsSelect" class="mkwsSelect mkwsTeam_' + tname + '" action="" >';
287 if (this.config.show_sort) {
288 s += M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
290 if (this.config.show_perpage) {
291 s += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
295 $(this.node).html(s);
298 function mkwsHtmlSort() {
299 var order = that.team.sortOrder();
301 that.log("HTML sort, sortOrder = '" + order + "'");
302 var sort_html = '<select class="mkwsSort mkwsTeam_' + tname + '">';
304 for(var i = 0; i < that.config.sort_options.length; i++) {
305 var opt = that.config.sort_options[i];
307 var val = opt.length == 1 ? opt[0] : opt[1];
309 sort_html += '<option value="' + key + '"';
310 if (order == key || order == val) {
311 sort_html += ' selected="selected"';
313 sort_html += '>' + M(val) + '</option>';
315 sort_html += '</select>';
320 function mkwsHtmlPerpage() {
321 var perpage = that.team.perpage();
323 that.log("HTML perpage, perpage = " + perpage);
324 var perpage_html = '<select class="mkwsPerpage mkwsTeam_' + tname + '">';
326 for(var i = 0; i < that.config.perpage_options.length; i++) {
327 var key = that.config.perpage_options[i];
329 perpage_html += '<option value="' + key + '"';
330 if (key == perpage) {
331 perpage_html += ' selected="selected"';
333 perpage_html += '>' + key + '</option>';
335 perpage_html += '</select>';
342 mkws.registerWidgetType('Lang', function() {
343 // dynamic URL or static page? /path/foo?query=test
344 /* create locale language menu */
345 if (!this.config.show_lang) return;
347 var lang_default = "en";
348 var lang = this.config.lang || lang_default;
351 /* display a list of configured languages, or all */
352 var lang_options = this.config.lang_options || [];
353 var toBeIncluded = {};
354 for (var i = 0; i < lang_options.length; i++) {
355 toBeIncluded[lang_options[i]] = true;
358 for (var k in mkws.locale_lang) {
359 if (toBeIncluded[k] || lang_options.length == 0)
364 if (lang_options.length == 0 || toBeIncluded[lang_default])
365 list.push(lang_default);
367 this.log("Language menu for: " + list.join(", "));
371 for (var i = 0; i < list.length; i++) {
377 data += ' <span>' + l + '</span> ';
379 data += ' <a href="' + lang_url(l) + '">' + l + '</a> '
383 $(this.node).html(data);
386 // set or re-set "lang" URL parameter
387 function lang_url(lang) {
388 var query = location.search;
389 // no query parameters? done
391 return "?lang=" + lang;
394 // parameter does not exist
395 if (!query.match(/[\?&]lang=/)) {
396 return query + "&lang=" + lang;
399 // replace existing parameter
400 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
401 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
407 mkws.registerWidgetType('MOTD', function() {
408 var container = this.team.widget('MOTDContainer');
410 // Move the MOTD from the provided element down into the container
411 $(this.node).appendTo(container.node);
416 // Some elements have mkws* classes that makes them appear as widgets
417 // -- for example, because we want to style them using CSS -- but have
418 // no actual functionality. We register these to prevent ignorable
419 // warnings when they occur.
421 mkws.registerWidgetType('Query', function() {});
422 mkws.registerWidgetType('MOTDContainer', function() {});
423 mkws.registerWidgetType('Button', function() {});
424 mkws.registerWidgetType('Popup', function() {});
426 // Not sure whether the following should have functionality:
427 // Select HTMLFormElement
428 // TermlistContainer1 HTMLTableCellElement
429 // TermlistContainer2 HTMLDivElement
430 // Bytarget HTMLDivElement