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;
10 this.node.html('No information available yet.');
11 this.node.css("display", "none");
13 this.team.queue("targets").subscribe(function(data) {
14 // There is a bug in pz2.js
16 for (var i = 0; i < data.length; i++) {
19 cur.hits = data[i].hits;
20 cur.diagnostic = data[i].diagnostic;
21 cur.records = data[i].records;
22 cur.state = data[i].state;
26 var template = that.team.loadTemplate(that.config.template || "Targets");
27 that.node.html(template({data: cleandata}));
32 mkws.registerWidgetType('Stat', function() {
36 this.team.queue("stat").subscribe(function(data) {
37 that.node.html(' -- ' +
38 '<span class="mkwsClientCount">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
40 M('Retrieved records') + ': ' + data.records + '/' + data.hits);
45 mkws.registerWidgetType('Pager', function() {
49 this.team.queue("pager").subscribe(function(data) {
50 that.node.html(drawPager(data))
52 function drawPager(data) {
53 var teamName = that.team.name();
54 var s = '<div style="float: right">' + M('Displaying') + ': '
55 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
56 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
57 + data.total + ')</div>';
59 //client indexes pages from 1 but pz2 from 0
61 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
62 var currentPage = that.team.currentPage();
64 var firstClkbl = (currentPage - onsides > 0)
65 ? currentPage - onsides
68 var lastClkbl = firstClkbl + 2*onsides < pages
69 ? firstClkbl + 2*onsides
72 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span> | ';
74 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
75 +'<< ' + M('Prev') + '</a> | ';
78 for(var i = firstClkbl; i <= lastClkbl; i++) {
81 numLabel = '<span class="mkwsCurrentPage">' + i + '</span>';
83 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
87 var next = ' | <span class="mkwsNext">' + M('Next') + ' >></span>';
88 if (pages - currentPage > 0)
89 next = ' | <a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
90 + M('Next') + ' >></a>';
97 if (lastClkbl < pages)
100 s += '<div style="float: clear">'
101 + prev + predots + middle + postdots + next + '</div>';
109 mkws.registerWidgetType('Records', function() {
111 var team = this.team;
113 this.team.queue("records").subscribe(function(data) {
114 for (var i = 0; i < data.hits.length; i++) {
115 var hit = data.hits[i];
116 that.team.queue("record").publish(hit);
117 hit.detailLinkId = team.recordElementId(hit.recid[0]);
118 hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
119 hit.containerClass = "mkwsSummary mkwsTeam_" + team.name();
120 hit.containerClass += " " + hit.detailLinkId;
121 // ### At some point, we may be able to move the
122 // m_currentRecordId and m_currentRecordData members
123 // from the team object into this widget.
124 if (hit.recid == team.currentRecordId()) {
125 if (team.currentRecordData()) {
126 hit.renderedDetails = team.renderDetails(team.currentRecordData());
127 console.log(hit.renderedDetails);
131 var template = team.loadTemplate(that.config.template || "Records");
132 var targs = $.extend({}, {"hits": data.hits}, that.config.template_vars);
133 that.node.html(template(targs));
140 mkws.registerWidgetType('Navi', function() {
142 var teamName = this.team.name();
145 this.team.queue("navi").subscribe(function() {
146 var filters = that.team.filters();
149 filters.visitTargets(function(id, name) {
150 if (text) text += " | ";
151 text += M('source') + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitTarget(\'' + teamName +
152 "', '" + id + "'" + ');return false;">' + name + '</a>';
155 filters.visitFields(function(field, value) {
156 if (text) text += " | ";
157 text += M(field) + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitQuery(\'' + teamName +
158 "', '" + field + "', '" + value + "'" +
159 ');return false;">' + value + '</a>';
162 that.node.html(text);
167 // It seems this and the Perpage widget doen't need to subscribe to
168 // anything, since they produce events rather than consuming them.
170 mkws.registerWidgetType('Sort', function() {
173 this.node.change(function() {
174 that.team.set_sortOrder(that.node.val());
175 if (that.team.submitted()) {
183 mkws.registerWidgetType('Perpage', function() {
186 this.node.change(function() {
187 that.team.set_perpage(that.node.val());
188 if (that.team.submitted()) {
196 mkws.registerWidgetType('Done', function() {
199 this.team.queue("complete").subscribe(function(n) {
200 that.node.html("Search complete: found " + n + " records");
205 mkws.registerWidgetType('Switch', function() {
206 if (!this.config.show_switch) return;
207 var tname = this.team.name();
209 <a href="#" onclick="mkws.switchView(\'' + tname + '\', \'records\')">Records</a><span> \
211 </span><a href="#" onclick="mkws.switchView(\'' + tname + '\', \'targets\')">Targets</a>');
212 this.hideWhenNarrow();
216 mkws.registerWidgetType('Search', function() {
217 var tname = this.team.name();
221 <form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
222 <input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + this.config.query_width + '" />\
223 <input class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
228 mkws.registerWidgetType('SearchForm', function() {
229 var team = this.team;
230 this.node.submit(function() {
231 var val = team.widget('Query').value();
238 mkws.registerWidgetType('Results', function() {
239 var tname = this.team.name();
242 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
244 <td class="mkwsTermlists-Container-wide mkwsTeam_' + tname + '" width="250" valign="top">\
245 <div class="mkwsTermlists mkwsTeam_' + tname + '"></div>\
247 <td class="mkwsMOTDContainer mkwsTeam_' + tname + '" valign="top">\
248 <div class="mkwsRanking mkwsTeam_' + tname + '"></div>\
249 <div class="mkwsPager mkwsTeam_' + tname + '"></div>\
250 <div class="mkwsNavi mkwsTeam_' + tname + '"></div>\
251 <div class="mkwsRecords mkwsTeam_' + tname + '"></div>\
256 <div class="mkwsTermlists-Container-narrow mkwsTeam_' + tname + '"></div>\
265 mkws.registerWidgetType('Ranking', function() {
266 var tname = this.team.name();
271 if (this.config.show_sort) {
272 s += M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
274 if (this.config.show_perpage) {
275 s += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
282 function mkwsHtmlSort() {
283 var order = that.team.sortOrder();
285 that.log("making sort HTML, sortOrder = '" + order + "'");
286 var sort_html = '<select class="mkwsSort mkwsTeam_' + tname + '">';
288 for(var i = 0; i < that.config.sort_options.length; i++) {
289 var opt = that.config.sort_options[i];
291 var val = opt.length == 1 ? opt[0] : opt[1];
293 sort_html += '<option value="' + key + '"';
294 if (order == key || order == val) {
295 sort_html += ' selected="selected"';
297 sort_html += '>' + M(val) + '</option>';
299 sort_html += '</select>';
304 function mkwsHtmlPerpage() {
305 var perpage = that.team.perpage();
307 that.log("making perpage HTML, perpage = " + perpage);
308 var perpage_html = '<select class="mkwsPerpage mkwsTeam_' + tname + '">';
310 for(var i = 0; i < that.config.perpage_options.length; i++) {
311 var key = that.config.perpage_options[i];
313 perpage_html += '<option value="' + key + '"';
314 if (key == perpage) {
315 perpage_html += ' selected="selected"';
317 perpage_html += '>' + key + '</option>';
319 perpage_html += '</select>';
326 mkws.registerWidgetType('Lang', function() {
327 // dynamic URL or static page? /path/foo?query=test
328 /* create locale language menu */
329 if (!this.config.show_lang) return;
331 var lang_default = "en";
332 var lang = this.config.lang || lang_default;
335 /* display a list of configured languages, or all */
336 var lang_options = this.config.lang_options || [];
337 var toBeIncluded = {};
338 for (var i = 0; i < lang_options.length; i++) {
339 toBeIncluded[lang_options[i]] = true;
342 for (var k in mkws.locale_lang) {
343 if (toBeIncluded[k] || lang_options.length == 0)
348 if (lang_options.length == 0 || toBeIncluded[lang_default])
349 list.push(lang_default);
351 this.log("language menu: " + list.join(", "));
355 for (var i = 0; i < list.length; i++) {
361 data += ' <span>' + l + '</span> ';
363 data += ' <a href="' + lang_url(l) + '">' + l + '</a> '
367 this.node.html(data);
368 this.hideWhenNarrow();
371 // set or re-set "lang" URL parameter
372 function lang_url(lang) {
373 var query = location.search;
374 // no query parameters? done
376 return "?lang=" + lang;
379 // parameter does not exist
380 if (!query.match(/[\?&]lang=/)) {
381 return query + "&lang=" + lang;
384 // replace existing parameter
385 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
386 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
392 mkws.registerWidgetType('MOTD', function() {
393 var container = this.team.widget('MOTDContainer');
395 // Move the MOTD from the provided element down into the container
396 this.node.appendTo(container.node);
401 // This widget has no functionality of its own, but its configuration
402 // is copied up into its team, allowing it to affect other widgets in
405 mkws.registerWidgetType('Config', function() {
407 for (var name in c) {
408 if (c.hasOwnProperty(name)) {
409 this.team.config[name] = c[name];
410 this.log(this + " copied property " + name + "='" + c[name] + "' up to team");
416 mkws.registerWidgetType('Progress', function() {
420 this.team.queue("stat").subscribe(function(data) {
421 var s = '<span class="mkwsDone">';
422 for (var i = 0; i < data.clients; i++) {
423 if (i == data.clients - data.activeclients) {
425 s += '<span class="mkwsWaiting">';
436 // Some elements have mkws* classes that makes them appear as widgets
437 // -- for example, because we want to style them using CSS -- but have
438 // no actual functionality. We register these to prevent ignorable
439 // warnings when they occur.
441 mkws.registerWidgetType('Query', function() {});
442 mkws.registerWidgetType('MOTDContainer', function() {});
443 mkws.registerWidgetType('Button', function() {});