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 wherein it makes each data object an array but
15 // simply assigns properties to it.
16 // TODO: remove this when PAZ-946 is addressed.
18 for (var i = 0; i < data.length; i++) {
21 cur.hits = data[i].hits;
22 cur.diagnostic = data[i].diagnostic;
23 cur.records = data[i].records;
24 cur.state = data[i].state;
28 var template = that.team.loadTemplate(that.config.template || "Targets");
29 that.node.html(template({data: cleandata}));
34 mkws.registerWidgetType('Stat', function() {
36 this.team.queue("stat").subscribe(function(data) {
37 var template = that.team.loadTemplate(that.config.template || "Stat");
38 that.node.html(template(data));
43 mkws.registerWidgetType('Pager', function() {
47 this.team.queue("pager").subscribe(function(data) {
48 var teamName = that.team.name();
50 output.first = data.start + 1;
51 output.last = data.start + data.num;
52 output.count = data.merged;
53 output.found = data.total;
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
63 var lastClkbl = firstClkbl + 2*onsides < pages
64 ? firstClkbl + 2*onsides
67 if (firstClkbl > 1) output.morePrev = true;
68 if (lastClkbl < pages) output.moreNext = true;
70 if (currentPage > 1) output.prevClick = "mkws.pagerPrev(\'" + teamName + "\');";
73 for(var i = firstClkbl; i <= lastClkbl; i++) {
76 if (i !== currentPage) {
77 o.click = "mkws.showPage(\'" + teamName + "\', " + i + ");";
82 if (pages - currentPage > 0) output.nextClick = "mkws.pagerNext(\'" + teamName + "\')";
84 var template = that.team.loadTemplate(that.config.template || "Pager");
85 that.node.html(template(output));
90 mkws.registerWidgetType('Records', function() {
94 this.team.queue("records").subscribe(function(data) {
95 for (var i = 0; i < data.hits.length; i++) {
96 var hit = data.hits[i];
97 that.team.queue("record").publish(hit);
98 hit.detailLinkId = team.recordElementId(hit.recid[0]);
99 hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
100 hit.containerClass = "mkwsSummary mkwsTeam_" + team.name();
101 hit.containerClass += " " + hit.detailLinkId;
102 // ### At some point, we may be able to move the
103 // m_currentRecordId and m_currentRecordData members
104 // from the team object into this widget.
105 if (hit.recid == team.currentRecordId()) {
106 if (team.currentRecordData()) {
107 hit.renderedDetails = team.renderDetails(team.currentRecordData());
108 console.log(hit.renderedDetails);
112 var template = team.loadTemplate(that.config.template || "Records");
113 var targs = $.extend({}, {"hits": data.hits}, that.config.template_vars);
114 that.node.html(template(targs));
121 mkws.registerWidgetType('Navi', function() {
123 var teamName = this.team.name();
126 this.team.queue("navi").subscribe(function() {
127 var filters = that.team.filters();
130 filters.visitTargets(function(id, name) {
131 if (text) text += " | ";
132 text += M('source') + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitTarget(\'' + teamName +
133 "', '" + id + "'" + ');return false;">' + name + '</a>';
136 filters.visitFields(function(field, value) {
137 if (text) text += " | ";
138 text += M(field) + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitQuery(\'' + teamName +
139 "', '" + field + "', '" + value + "'" +
140 ');return false;">' + value + '</a>';
143 that.node.html(text);
148 // It seems this and the Perpage widget doen't need to subscribe to
149 // anything, since they produce events rather than consuming them.
151 mkws.registerWidgetType('Sort', function() {
154 this.node.change(function() {
155 that.team.set_sortOrder(that.node.val());
156 if (that.team.submitted()) {
164 mkws.registerWidgetType('Perpage', function() {
167 this.node.change(function() {
168 that.team.set_perpage(that.node.val());
169 if (that.team.submitted()) {
177 mkws.registerWidgetType('Done', function() {
179 this.team.queue("complete").subscribe(function(n) {
180 var template = that.team.loadTemplate(that.config.template || "Done");
181 that.node.html(template({count: n}));
186 mkws.registerWidgetType('Switch', function() {
187 if (!this.config.show_switch) return;
188 var tname = this.team.name();
190 output.recordClick = "mkws.switchView(\'" + tname + "\', \'records\')";
191 output.targetClick = "mkws.switchView(\'" + tname + "\', \'targets\')";
192 var template = this.team.loadTemplate(this.config.template || "Switch");
193 this.node.html(template(output));
194 this.hideWhenNarrow();
198 mkws.registerWidgetType('Search', function() {
200 output.team = this.team.name();
201 output.queryWidth = this.config.query_width;
202 var template = this.team.loadTemplate(this.config.template || "Search");
203 this.node.html(template(output));
207 mkws.registerWidgetType('SearchForm', function() {
208 var team = this.team;
209 this.node.submit(function() {
210 var val = team.widget('Query').value();
217 mkws.registerWidgetType('Results', function() {
218 var template = this.team.loadTemplate(this.config.template || "Results");
219 this.node.html(template({team: this.team.name()}));
224 mkws.registerWidgetType('Ranking', function() {
228 output.team = this.team.name();
229 output.showSort = this.config.show_sort;
230 output.showPerPage = this.config.show_perpage;
232 var order = this.team.sortOrder();
233 this.log("making sort, sortOrder = '" + order + "'");
234 for (var i = 0; i < this.config.sort_options.length; i++) {
236 var opt = this.config.sort_options[i];
238 cur.label = opt.length == 1 ? opt[0] : opt[1];
239 if (order == cur.key || order == cur.label) cur.selected = true;
240 output.sort.push(cur);
243 var perpage = this.team.perpage();
244 this.log("making perpage, perpage = " + perpage);
245 for(var i = 0; i < this.config.perpage_options.length; i++) {
247 cur.perPage = this.config.perpage_options[i];
248 if (cur.perPage == perpage) cur.selected = true;
249 output.perPage.push(cur);
252 var template = this.team.loadTemplate(this.config.template || "Ranking");
253 this.node.html(template(output));
257 mkws.registerWidgetType('Lang', function() {
258 // dynamic URL or static page? /path/foo?query=test
259 /* create locale language menu */
260 if (!this.config.show_lang) return;
262 var lang_default = "en";
263 var lang = this.config.lang || lang_default;
266 /* display a list of configured languages, or all */
267 var lang_options = this.config.lang_options || [];
268 var toBeIncluded = {};
269 for (var i = 0; i < lang_options.length; i++) {
270 toBeIncluded[lang_options[i]] = true;
273 for (var k in mkws.locale_lang) {
274 if (toBeIncluded[k] || lang_options.length == 0) {
276 if (lang === k) cur.selected = true;
278 cur.url = lang_url(k);
284 if (lang_options.length == 0 || toBeIncluded[lang_default]) {
286 if (lang === lang_default) cur.selected = true;
287 cur.code = lang_default;
288 cur.url = lang_url(lang_default);
292 this.log("language menu: " + list.join(", "));
294 var template = this.team.loadTemplate(this.config.template || "Lang");
295 this.node.html(template({languages: list}));
296 this.hideWhenNarrow();
298 // set or re-set "lang" URL parameter
299 function lang_url(lang) {
300 var query = location.search;
301 // no query parameters? done
303 return "?lang=" + lang;
306 // parameter does not exist
307 if (!query.match(/[\?&]lang=/)) {
308 return query + "&lang=" + lang;
311 // replace existing parameter
312 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
313 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
319 mkws.registerWidgetType('MOTD', function() {
320 var container = this.team.widget('MOTDContainer');
322 // Move the MOTD from the provided element down into the container
323 this.node.appendTo(container.node);
328 // This widget has no functionality of its own, but its configuration
329 // is copied up into its team, allowing it to affect other widgets in
332 mkws.registerWidgetType('Config', function() {
334 for (var name in c) {
335 if (c.hasOwnProperty(name)) {
336 this.team.config[name] = c[name];
337 this.log(this + " copied property " + name + "='" + c[name] + "' up to team");
343 mkws.registerWidgetType('Progress', function() {
347 this.team.queue("stat").subscribe(function(data) {
348 var s = '<span class="mkwsDone">';
349 for (var i = 0; i < data.clients; i++) {
350 if (i == data.clients - data.activeclients) {
352 s += '<span class="mkwsWaiting">';
363 // Some elements have mkws* classes that makes them appear as widgets
364 // -- for example, because we want to style them using CSS -- but have
365 // no actual functionality. We register these to prevent ignorable
366 // warnings when they occur.
368 mkws.registerWidgetType('Query', function() {});
369 mkws.registerWidgetType('MOTDContainer', function() {});
370 mkws.registerWidgetType('Button', function() {});