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() {
34 this.team.queue("stat").subscribe(function(data) {
35 var template = that.team.loadTemplate(that.config.template || "Stat");
36 that.node.html(template(data));
41 mkws.registerWidgetType('Pager', function() {
45 this.team.queue("pager").subscribe(function(data) {
46 that.node.html(drawPager(data))
48 function drawPager(data) {
49 var teamName = that.team.name();
50 var s = '<div style="float: right">' + M('Displaying') + ': '
51 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
52 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
53 + data.total + ')</div>';
55 //client indexes pages from 1 but pz2 from 0
57 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
58 var currentPage = that.team.currentPage();
60 var firstClkbl = (currentPage - onsides > 0)
61 ? currentPage - onsides
64 var lastClkbl = firstClkbl + 2*onsides < pages
65 ? firstClkbl + 2*onsides
68 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span> | ';
70 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
71 +'<< ' + M('Prev') + '</a> | ';
74 for(var i = firstClkbl; i <= lastClkbl; i++) {
77 numLabel = '<span class="mkwsCurrentPage">' + i + '</span>';
79 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
83 var next = ' | <span class="mkwsNext">' + M('Next') + ' >></span>';
84 if (pages - currentPage > 0)
85 next = ' | <a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
86 + M('Next') + ' >></a>';
93 if (lastClkbl < pages)
96 s += '<div style="float: clear">'
97 + prev + predots + middle + postdots + next + '</div>';
105 mkws.registerWidgetType('Records', function() {
107 var team = this.team;
109 this.team.queue("records").subscribe(function(data) {
110 for (var i = 0; i < data.hits.length; i++) {
111 var hit = data.hits[i];
112 that.team.queue("record").publish(hit);
113 hit.detailLinkId = team.recordElementId(hit.recid[0]);
114 hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
115 hit.containerClass = "mkwsSummary mkwsTeam_" + team.name();
116 hit.containerClass += " " + hit.detailLinkId;
117 // ### At some point, we may be able to move the
118 // m_currentRecordId and m_currentRecordData members
119 // from the team object into this widget.
120 if (hit.recid == team.currentRecordId()) {
121 if (team.currentRecordData()) {
122 hit.renderedDetails = team.renderDetails(team.currentRecordData());
123 console.log(hit.renderedDetails);
127 var template = team.loadTemplate(that.config.template || "Records");
128 var targs = $.extend({}, {"hits": data.hits}, that.config.template_vars);
129 that.node.html(template(targs));
136 mkws.registerWidgetType('Navi', function() {
138 var teamName = this.team.name();
141 this.team.queue("navi").subscribe(function() {
142 var filters = that.team.filters();
145 filters.visitTargets(function(id, name) {
146 if (text) text += " | ";
147 text += M('source') + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitTarget(\'' + teamName +
148 "', '" + id + "'" + ');return false;">' + name + '</a>';
151 filters.visitFields(function(field, value) {
152 if (text) text += " | ";
153 text += M(field) + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitQuery(\'' + teamName +
154 "', '" + field + "', '" + value + "'" +
155 ');return false;">' + value + '</a>';
158 that.node.html(text);
163 // It seems this and the Perpage widget doen't need to subscribe to
164 // anything, since they produce events rather than consuming them.
166 mkws.registerWidgetType('Sort', function() {
169 this.node.change(function() {
170 that.team.set_sortOrder(that.node.val());
171 if (that.team.submitted()) {
179 mkws.registerWidgetType('Perpage', function() {
182 this.node.change(function() {
183 that.team.set_perpage(that.node.val());
184 if (that.team.submitted()) {
192 mkws.registerWidgetType('Done', function() {
195 this.team.queue("complete").subscribe(function(n) {
196 that.node.html("Search complete: found " + n + " records");
201 mkws.registerWidgetType('Switch', function() {
202 if (!this.config.show_switch) return;
203 var tname = this.team.name();
205 <a href="#" onclick="mkws.switchView(\'' + tname + '\', \'records\')">Records</a><span> \
207 </span><a href="#" onclick="mkws.switchView(\'' + tname + '\', \'targets\')">Targets</a>');
208 this.hideWhenNarrow();
212 mkws.registerWidgetType('Search', function() {
213 var tname = this.team.name();
217 <form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
218 <input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + this.config.query_width + '" />\
219 <input class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
224 mkws.registerWidgetType('SearchForm', function() {
225 var team = this.team;
226 this.node.submit(function() {
227 var val = team.widget('Query').value();
234 mkws.registerWidgetType('Results', function() {
235 var tname = this.team.name();
238 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
240 <td class="mkwsTermlists-Container-wide mkwsTeam_' + tname + '" width="250" valign="top">\
241 <div class="mkwsTermlists mkwsTeam_' + tname + '"></div>\
243 <td class="mkwsMOTDContainer mkwsTeam_' + tname + '" valign="top">\
244 <div class="mkwsRanking mkwsTeam_' + tname + '"></div>\
245 <div class="mkwsPager mkwsTeam_' + tname + '"></div>\
246 <div class="mkwsNavi mkwsTeam_' + tname + '"></div>\
247 <div class="mkwsRecords mkwsTeam_' + tname + '"></div>\
252 <div class="mkwsTermlists-Container-narrow mkwsTeam_' + tname + '"></div>\
261 mkws.registerWidgetType('Ranking', function() {
262 var tname = this.team.name();
267 if (this.config.show_sort) {
268 s += M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
270 if (this.config.show_perpage) {
271 s += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
278 function mkwsHtmlSort() {
279 var order = that.team.sortOrder();
281 that.log("making sort HTML, sortOrder = '" + order + "'");
282 var sort_html = '<select class="mkwsSort mkwsTeam_' + tname + '">';
284 for(var i = 0; i < that.config.sort_options.length; i++) {
285 var opt = that.config.sort_options[i];
287 var val = opt.length == 1 ? opt[0] : opt[1];
289 sort_html += '<option value="' + key + '"';
290 if (order == key || order == val) {
291 sort_html += ' selected="selected"';
293 sort_html += '>' + M(val) + '</option>';
295 sort_html += '</select>';
300 function mkwsHtmlPerpage() {
301 var perpage = that.team.perpage();
303 that.log("making perpage HTML, perpage = " + perpage);
304 var perpage_html = '<select class="mkwsPerpage mkwsTeam_' + tname + '">';
306 for(var i = 0; i < that.config.perpage_options.length; i++) {
307 var key = that.config.perpage_options[i];
309 perpage_html += '<option value="' + key + '"';
310 if (key == perpage) {
311 perpage_html += ' selected="selected"';
313 perpage_html += '>' + key + '</option>';
315 perpage_html += '</select>';
322 mkws.registerWidgetType('Lang', function() {
323 // dynamic URL or static page? /path/foo?query=test
324 /* create locale language menu */
325 if (!this.config.show_lang) return;
327 var lang_default = "en";
328 var lang = this.config.lang || lang_default;
331 /* display a list of configured languages, or all */
332 var lang_options = this.config.lang_options || [];
333 var toBeIncluded = {};
334 for (var i = 0; i < lang_options.length; i++) {
335 toBeIncluded[lang_options[i]] = true;
338 for (var k in mkws.locale_lang) {
339 if (toBeIncluded[k] || lang_options.length == 0)
344 if (lang_options.length == 0 || toBeIncluded[lang_default])
345 list.push(lang_default);
347 this.log("language menu: " + list.join(", "));
351 for (var i = 0; i < list.length; i++) {
357 data += ' <span>' + l + '</span> ';
359 data += ' <a href="' + lang_url(l) + '">' + l + '</a> '
363 this.node.html(data);
364 this.hideWhenNarrow();
367 // set or re-set "lang" URL parameter
368 function lang_url(lang) {
369 var query = location.search;
370 // no query parameters? done
372 return "?lang=" + lang;
375 // parameter does not exist
376 if (!query.match(/[\?&]lang=/)) {
377 return query + "&lang=" + lang;
380 // replace existing parameter
381 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
382 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
388 mkws.registerWidgetType('MOTD', function() {
389 var container = this.team.widget('MOTDContainer');
391 // Move the MOTD from the provided element down into the container
392 this.node.appendTo(container.node);
397 // This widget has no functionality of its own, but its configuration
398 // is copied up into its team, allowing it to affect other widgets in
401 mkws.registerWidgetType('Config', function() {
403 for (var name in c) {
404 if (c.hasOwnProperty(name)) {
405 this.team.config[name] = c[name];
406 this.log(this + " copied property " + name + "='" + c[name] + "' up to team");
412 mkws.registerWidgetType('Progress', function() {
416 this.team.queue("stat").subscribe(function(data) {
417 var s = '<span class="mkwsDone">';
418 for (var i = 0; i < data.clients; i++) {
419 if (i == data.clients - data.activeclients) {
421 s += '<span class="mkwsWaiting">';
432 // Some elements have mkws* classes that makes them appear as widgets
433 // -- for example, because we want to style them using CSS -- but have
434 // no actual functionality. We register these to prevent ignorable
435 // warnings when they occur.
437 mkws.registerWidgetType('Query', function() {});
438 mkws.registerWidgetType('MOTDContainer', function() {});
439 mkws.registerWidgetType('Button', function() {});