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();
125 this.team.queue("navi").subscribe(function() {
126 var filters = that.team.filters();
127 var output = {filters:[]};
129 filters.visitTargets(function(id, name) {
131 cur.facet = 'source';
133 cur.click = "mkws.delimitTarget('" + teamName + "', '" + id + "'); return false;";
134 output.filters.push(cur);
137 filters.visitFields(function(field, value) {
141 cur.click = "mkws.delimitQuery('" + teamName + "', '" + field + "', '" + value + "'" + ");return false;";
142 output.filters.push(cur);
145 var template = that.team.loadTemplate(that.config.template || "Navi");
146 that.node.html(template(output));
151 // It seems this and the Perpage widget doen't need to subscribe to
152 // anything, since they produce events rather than consuming them.
154 mkws.registerWidgetType('Sort', function() {
157 this.node.change(function() {
158 that.team.set_sortOrder(that.node.val());
159 if (that.team.submitted()) {
167 mkws.registerWidgetType('Perpage', function() {
170 this.node.change(function() {
171 that.team.set_perpage(that.node.val());
172 if (that.team.submitted()) {
180 mkws.registerWidgetType('Done', function() {
182 this.team.queue("complete").subscribe(function(n) {
183 var template = that.team.loadTemplate(that.config.template || "Done");
184 that.node.html(template({count: n}));
189 mkws.registerWidgetType('Switch', function() {
190 if (!this.config.show_switch) return;
191 var tname = this.team.name();
193 output.recordClick = "mkws.switchView(\'" + tname + "\', \'records\')";
194 output.targetClick = "mkws.switchView(\'" + tname + "\', \'targets\')";
195 var template = this.team.loadTemplate(this.config.template || "Switch");
196 this.node.html(template(output));
197 this.hideWhenNarrow();
201 mkws.registerWidgetType('Search', function() {
203 output.team = this.team.name();
204 output.queryWidth = this.config.query_width;
205 var template = this.team.loadTemplate(this.config.template || "Search");
206 this.node.html(template(output));
210 mkws.registerWidgetType('SearchForm', function() {
211 var team = this.team;
212 this.node.submit(function() {
213 var val = team.widget('Query').value();
220 mkws.registerWidgetType('Results', function() {
221 var template = this.team.loadTemplate(this.config.template || "Results");
222 this.node.html(template({team: this.team.name()}));
227 mkws.registerWidgetType('Ranking', function() {
231 output.team = this.team.name();
232 output.showSort = this.config.show_sort;
233 output.showPerPage = this.config.show_perpage;
235 var order = this.team.sortOrder();
236 this.log("making sort, sortOrder = '" + order + "'");
237 for (var i = 0; i < this.config.sort_options.length; i++) {
239 var opt = this.config.sort_options[i];
241 cur.label = opt.length == 1 ? opt[0] : opt[1];
242 if (order == cur.key || order == cur.label) cur.selected = true;
243 output.sort.push(cur);
246 var perpage = this.team.perpage();
247 this.log("making perpage, perpage = " + perpage);
248 for(var i = 0; i < this.config.perpage_options.length; i++) {
250 cur.perPage = this.config.perpage_options[i];
251 if (cur.perPage == perpage) cur.selected = true;
252 output.perPage.push(cur);
255 var template = this.team.loadTemplate(this.config.template || "Ranking");
256 this.node.html(template(output));
260 mkws.registerWidgetType('Lang', function() {
261 // dynamic URL or static page? /path/foo?query=test
262 /* create locale language menu */
263 if (!this.config.show_lang) return;
265 var lang_default = "en";
266 var lang = this.config.lang || lang_default;
269 /* display a list of configured languages, or all */
270 var lang_options = this.config.lang_options || [];
271 var toBeIncluded = {};
272 for (var i = 0; i < lang_options.length; i++) {
273 toBeIncluded[lang_options[i]] = true;
276 for (var k in mkws.locale_lang) {
277 if (toBeIncluded[k] || lang_options.length == 0) {
279 if (lang === k) cur.selected = true;
281 cur.url = lang_url(k);
287 if (lang_options.length == 0 || toBeIncluded[lang_default]) {
289 if (lang === lang_default) cur.selected = true;
290 cur.code = lang_default;
291 cur.url = lang_url(lang_default);
295 this.log("language menu: " + list.join(", "));
297 var template = this.team.loadTemplate(this.config.template || "Lang");
298 this.node.html(template({languages: list}));
299 this.hideWhenNarrow();
301 // set or re-set "lang" URL parameter
302 function lang_url(lang) {
303 var query = location.search;
304 // no query parameters? done
306 return "?lang=" + lang;
309 // parameter does not exist
310 if (!query.match(/[\?&]lang=/)) {
311 return query + "&lang=" + lang;
314 // replace existing parameter
315 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
316 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
322 mkws.registerWidgetType('MOTD', function() {
323 var container = this.team.widget('MOTDContainer');
325 // Move the MOTD from the provided element down into the container
326 this.node.appendTo(container.node);
331 // This widget has no functionality of its own, but its configuration
332 // is copied up into its team, allowing it to affect other widgets in
335 mkws.registerWidgetType('Config', function() {
337 for (var name in c) {
338 if (c.hasOwnProperty(name)) {
339 this.team.config[name] = c[name];
340 this.log(this + " copied property " + name + "='" + c[name] + "' up to team");
346 mkws.registerWidgetType('Progress', function() {
349 this.team.queue("stat").subscribe(function(data) {
350 var template = that.team.loadTemplate(that.config.template || "Progress");
351 that.node.html(template({
352 done: data.clients - data.activeclients,
353 waiting: data.activeclients
360 // Some elements have mkws* classes that makes them appear as widgets
361 // -- for example, because we want to style them using CSS -- but have
362 // no actual functionality. We register these to prevent ignorable
363 // warnings when they occur.
365 mkws.registerWidgetType('Query', function() {});
366 mkws.registerWidgetType('MOTDContainer', function() {});
367 mkws.registerWidgetType('Button', function() {});