1 (function($) { // jQuery wrapper
3 // Functions follow for promoting the regular widget object into
4 // widgets of specific types. These could be moved into their own
8 mkws.registerWidgetType('targets', function() {
9 if (!this.config.show_switch) return;
12 this.node.html('No information available yet.');
13 this.node.css("display", "none");
15 this.team.queue("targets").subscribe(function(data) {
16 // There is a bug in pz2.js wherein it makes each data object an array but
17 // simply assigns properties to it.
18 // TODO: remove this when PAZ-946 is addressed.
20 for (var i = 0; i < data.length; i++) {
23 cur.hits = data[i].hits;
24 cur.diagnostic = data[i].diagnostic;
25 cur.message = data[i].message;
26 cur.records = data[i].records;
27 cur.state = data[i].state;
31 var template = that.team.loadTemplate(that.config.template || "targets");
32 that.node.html(template({data: cleandata}));
37 mkws.registerWidgetType('stat', function() {
39 this.team.queue("stat").subscribe(function(data) {
40 var template = that.team.loadTemplate(that.config.template || "stat");
41 that.node.html(template(data));
46 mkws.registerWidgetType('pager', function() {
50 this.team.queue("pager").subscribe(function(data) {
51 var teamName = that.team.name();
53 output.first = data.start + 1;
54 output.last = data.start + data.num;
55 output.count = data.merged;
56 output.found = data.total;
58 //client indexes pages from 1 but pz2 from 0
60 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
61 var currentPage = that.team.currentPage();
63 var firstClkbl = (currentPage - onsides > 0)
64 ? currentPage - onsides
66 var lastClkbl = firstClkbl + 2*onsides < pages
67 ? firstClkbl + 2*onsides
70 if (firstClkbl > 1) output.morePrev = true;
71 if (lastClkbl < pages) output.moreNext = true;
73 if (currentPage > 1) output.prevClick = "mkws.pagerPrev(\'" + teamName + "\');";
76 for(var i = firstClkbl; i <= lastClkbl; i++) {
79 if (i !== currentPage) {
80 o.click = "mkws.showPage(\'" + teamName + "\', " + i + ");";
85 if (pages - currentPage > 0) output.nextClick = "mkws.pagerNext(\'" + teamName + "\')";
87 var template = that.team.loadTemplate(that.config.template || "pager");
88 that.node.html(template(output));
92 mkws.registerWidgetType('details', function() {
94 var recid = that.node.attr("data-mkws-recid");
95 if (this.team.gotRecords()) {
96 that.team.fetchDetails(recid);
98 this.team.queue("firstrecords").subscribe(function() {
99 that.team.fetchDetails(recid);
102 this.team.queue("record").subscribe(function(data) {
103 if ($.inArray(recid, data.recid) > -1) {
104 var template = that.team.loadTemplate(that.config.template || "details");
105 that.node.html(template(data));
110 mkws.registerWidgetType('records', function() {
112 var team = this.team;
114 this.team.queue("records").subscribe(function(data) {
115 for (var i = 0; i < data.hits.length; i++) {
116 var hit = data.hits[i];
117 hit.detailLinkId = team.recordElementId(hit.recid[0]);
118 hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;";
119 hit.containerClass = "mkws-summary mkwsSummary mkws-team-" + 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());
130 var template = team.loadTemplate(that.config.template || "records");
131 var summaryPartial = team.loadTemplate("summary");
132 var tdata = $.extend({}, {"hits": data.hits}, that.config.template_vars);
133 that.node.html(template(tdata, {"partials":{"summary":summaryPartial}}));
140 mkws.registerWidgetType('navi', function() {
142 var teamName = this.team.name();
144 this.team.queue("navi").subscribe(function() {
145 var filters = that.team.filters();
146 var output = {filters:[]};
148 filters.visitTargets(function(id, name) {
150 cur.facet = 'source';
152 cur.click = "mkws.delimitTarget('" + teamName + "', '" + id + "'); return false;";
153 output.filters.push(cur);
156 filters.visitFields(function(field, value) {
160 cur.click = "mkws.delimitQuery('" + teamName + "', '" + field + "', '" + value + "'" + ");return false;";
161 output.filters.push(cur);
164 var template = that.team.loadTemplate(that.config.template || "navi");
165 that.node.html(template(output));
170 // It seems this and the Perpage widget doen't need to subscribe to
171 // anything, since they produce events rather than consuming them.
173 mkws.registerWidgetType('sort', function() {
176 this.node.change(function() {
177 that.team.set_sortOrder(that.node.val());
178 if (that.team.submitted()) {
186 mkws.registerWidgetType('per-page', function() {
189 this.node.change(function() {
190 that.team.set_perpage(that.node.val());
191 if (that.team.submitted()) {
199 mkws.registerWidgetType('done', function() {
201 this.team.queue("complete").subscribe(function(n) {
202 var template = that.team.loadTemplate(that.config.template || "done");
203 that.node.html(template({count: n}));
208 mkws.registerWidgetType('switch', function() {
209 if (!this.config.show_switch) return;
210 var tname = this.team.name();
212 output.recordClick = "mkws.switchView(\'" + tname + "\', \'records\')";
213 output.targetClick = "mkws.switchView(\'" + tname + "\', \'targets\')";
214 var template = this.team.loadTemplate(this.config.template || "switch");
215 this.node.html(template(output));
216 this.hideWhenNarrow();
220 mkws.registerWidgetType('search', function() {
222 output.team = this.team.name();
223 output.queryWidth = this.config.query_width;
224 var template = this.team.loadTemplate(this.config.template || "search");
225 this.node.html(template(output));
229 mkws.registerWidgetType('search-form', function() {
230 var team = this.team;
231 this.node.submit(function() {
232 var val = team.widget('query').value();
239 mkws.registerWidgetType('results', function() {
240 var template = this.team.loadTemplate(this.config.template || "results");
241 this.node.html(template({team: this.team.name()}));
246 mkws.registerWidgetType('ranking', function() {
250 output.team = this.team.name();
251 output.showSort = this.config.show_sort;
252 output.showPerPage = this.config.show_perpage;
254 var order = this.team.sortOrder();
255 this.log("making sort, sortOrder = '" + order + "'");
256 for (var i = 0; i < this.config.sort_options.length; i++) {
258 var opt = this.config.sort_options[i];
260 cur.label = opt.length == 1 ? opt[0] : opt[1];
261 if (order == cur.key || order == cur.label) cur.selected = true;
262 output.sort.push(cur);
265 var perpage = this.team.perpage();
266 this.log("making perpage, perpage = " + perpage);
267 for(var i = 0; i < this.config.perpage_options.length; i++) {
269 cur.perPage = this.config.perpage_options[i];
270 if (cur.perPage == perpage) cur.selected = true;
271 output.perPage.push(cur);
274 var template = this.team.loadTemplate(this.config.template || "ranking");
275 this.node.html(template(output));
279 mkws.registerWidgetType('lang', function() {
280 // dynamic URL or static page? /path/foo?query=test
281 /* create locale language menu */
282 if (!this.config.show_lang) return;
284 var lang_default = "en";
285 var lang = this.config.lang || lang_default;
288 /* display a list of configured languages, or all */
289 var lang_options = this.config.lang_options;
290 var toBeIncluded = {};
291 for (var i = 0; i < lang_options.length; i++) {
292 toBeIncluded[lang_options[i]] = true;
295 for (var k in mkws.locale_lang) {
296 if (toBeIncluded[k] || lang_options.length == 0) {
298 if (lang === k) cur.selected = true;
300 cur.url = lang_url(k);
306 if (lang_options.length == 0 || toBeIncluded[lang_default]) {
308 if (lang === lang_default) cur.selected = true;
309 cur.code = lang_default;
310 cur.url = lang_url(lang_default);
314 this.log("language menu: " + list.join(", "));
316 var template = this.team.loadTemplate(this.config.template || "lang");
317 this.node.html(template({languages: list}));
318 this.hideWhenNarrow();
320 // set or re-set "lang" URL parameter
321 function lang_url(lang) {
322 var query = location.search;
323 // no query parameters? done
325 return "?lang=" + lang;
328 // parameter does not exist
329 if (!query.match(/[\?&]lang=/)) {
330 return query + "&lang=" + lang;
333 // replace existing parameter
334 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
335 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
341 mkws.registerWidgetType('motd', function() {
342 var container = this.team.widget('motd-container');
344 // Move the MOTD from the provided element down into the container
345 this.node.appendTo(container.node);
350 // This widget has no functionality of its own, but its configuration
351 // is copied up into its team, allowing it to affect other widgets in
354 mkws.registerWidgetType('config', function() {
356 for (var name in c) {
357 if (c.hasOwnProperty(name)) {
358 this.team.config[name] = c[name];
359 this.log(this + " copied property " + name + "='" + c[name] + "' up to team");
365 mkws.registerWidgetType('progress', function() {
368 this.team.queue("stat").subscribe(function(data) {
369 var template = that.team.loadTemplate(that.config.template || "progress");
370 that.node.html(template({
371 done: data.clients - data.activeclients,
372 waiting: data.activeclients
379 // Some elements have mkws* classes that makes them appear as widgets
380 // -- for example, because we want to style them using CSS -- but have
381 // no actual functionality. We register these to prevent ignorable
382 // warnings when they occur.
384 mkws.registerWidgetType('query', function() {});
385 mkws.registerWidgetType('motd-container', function() {});
386 mkws.registerWidgetType('button', function() {});
389 })(mkws.$); // jQuery wrapper