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++) {
22 cur.name = data[i].name;
24 cur.hits = data[i].hits;
25 cur.diagnostic = data[i].diagnostic;
26 cur.message = data[i].message;
27 cur.records = data[i].records;
28 cur.state = data[i].state.replace(/^Client_/, '');
32 cleandata.sort(function(a,b) { return a.name.localeCompare(b.name) });
34 var template = that.team.loadTemplate(that.config.template || "targets");
35 that.node.html(template({data: cleandata}));
40 mkws.registerWidgetType('stat', function() {
42 this.team.queue("stat").subscribe(function(data) {
43 var template = that.team.loadTemplate(that.config.template || "stat");
44 that.node.html(template(data));
49 mkws.registerWidgetType('pager', function() {
53 this.team.queue("pager").subscribe(function(data) {
54 var teamName = that.team.name();
56 output.first = data.start + 1;
57 output.last = data.start + data.num;
58 output.count = data.merged;
59 output.found = data.total;
61 //client indexes pages from 1 but pz2 from 0
63 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
64 var currentPage = that.team.currentPage();
66 var firstClkbl = (currentPage - onsides > 0)
67 ? currentPage - onsides
69 var lastClkbl = firstClkbl + 2*onsides < pages
70 ? firstClkbl + 2*onsides
73 if (firstClkbl > 1) output.morePrev = true;
74 if (lastClkbl < pages) output.moreNext = true;
76 if (currentPage > 1) output.prevClick = "mkws.pagerPrev(\'" + teamName + "\');";
79 for(var i = firstClkbl; i <= lastClkbl; i++) {
82 if (i !== currentPage) {
83 o.click = "mkws.showPage(\'" + teamName + "\', " + i + ");";
88 if (pages - currentPage > 0) output.nextClick = "mkws.pagerNext(\'" + teamName + "\')";
90 var template = that.team.loadTemplate(that.config.template || "pager");
91 that.node.html(template(output));
95 mkws.registerWidgetType('details', function() {
97 var recid = that.node.attr("data-mkws-recid");
98 if (this.team.gotRecords()) {
99 that.team.fetchDetails(recid);
101 this.team.queue("firstrecords").subscribe(function() {
102 that.team.fetchDetails(recid);
105 this.team.queue("record").subscribe(function(data) {
106 if ($.inArray(recid, data.recid) > -1) {
107 var template = that.team.loadTemplate(that.config.template || "details");
108 that.node.html(template(data));
113 mkws.registerWidgetType('records', function() {
115 var team = this.team;
117 this.team.queue("records").subscribe(function(data) {
118 for (var i = 0; i < data.hits.length; i++) {
119 var hit = data.hits[i];
120 hit.detailLinkId = team.recordElementId(hit.recid[0]);
121 hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;";
122 hit.containerClass = "mkws-summary mkwsSummary mkws-team-" + team.name();
123 hit.containerClass += " " + hit.detailLinkId;
124 // ### At some point, we may be able to move the
125 // m_currentRecordId and m_currentRecordData members
126 // from the team object into this widget.
127 if (hit.recid == team.currentRecordId()) {
128 if (team.currentRecordData()) {
129 hit.renderedDetails = team.renderDetails(team.currentRecordData());
133 var template = team.loadTemplate(that.config.template || "records");
134 var summaryPartial = team.loadTemplate(that.config['summary-template'] || "summary");
135 var tdata = $.extend({}, {"hits": data.hits}, that.config.template_vars);
136 that.node.html(template(tdata, {"partials":{"summary":summaryPartial}}));
143 mkws.registerWidgetType('navi', function() {
145 var teamName = this.team.name();
147 this.team.queue("searchtriggered").subscribe(function() {
148 var filters = that.team.filters();
149 var output = {filters:[]};
151 filters.visitTargets(function(id, name) {
153 cur.facet = 'source';
155 cur.click = "mkws.delimitTarget('" + teamName + "', '" + id + "'); return false;";
156 output.filters.push(cur);
159 filters.visitFields(function(field, value) {
163 cur.click = "mkws.delimitQuery('" + teamName + "', '" + field + "', '" + value + "'" + ");return false;";
164 output.filters.push(cur);
167 var template = that.team.loadTemplate(that.config.template || "navi");
168 that.node.html(template(output));
173 // It seems this and the Perpage widget doen't need to subscribe to
174 // anything, since they produce events rather than consuming them.
176 mkws.registerWidgetType('sort', function() {
179 this.node.change(function() {
180 that.team.set_sortOrder(that.node.val());
181 if (that.team.submitted()) {
189 mkws.registerWidgetType('per-page', function() {
192 this.node.change(function() {
193 that.team.set_perpage(that.node.val());
194 if (that.team.submitted()) {
202 mkws.registerWidgetType('done', function() {
204 this.team.queue("complete").subscribe(function(n) {
205 var template = that.team.loadTemplate(that.config.template || "done");
206 that.node.html(template({count: n}));
211 mkws.registerWidgetType('switch', function() {
212 if (!this.config.show_switch) return;
213 var tname = this.team.name();
215 output.recordClick = "mkws.switchView(\'" + tname + "\', \'records\')";
216 output.targetClick = "mkws.switchView(\'" + tname + "\', \'targets\')";
217 var template = this.team.loadTemplate(this.config.template || "switch");
218 this.node.html(template(output));
219 this.hideWhenNarrow();
223 mkws.registerWidgetType('search', function() {
225 output.team = this.team.name();
226 var template = this.team.loadTemplate(this.config.template || "search");
227 this.node.html(template(output));
231 mkws.registerWidgetType('search-form', function() {
232 var team = this.team;
233 this.node.submit(function() {
234 var val = team.widget('query').value();
241 mkws.registerWidgetType('results', function() {
242 var template = this.team.loadTemplate(this.config.template || "results");
243 this.node.html(template({team: this.team.name()}));
248 mkws.registerWidgetType('ranking', function() {
252 output.team = this.team.name();
253 output.showSort = this.config.show_sort;
254 output.showPerPage = this.config.show_perpage;
256 var order = this.team.sortOrder();
257 this.info("making sort, sortOrder = '" + order + "'");
258 for (var i = 0; i < this.config.sort_options.length; i++) {
260 var opt = this.config.sort_options[i];
262 cur.label = opt.length == 1 ? opt[0] : mkws.M(opt[1]);
263 if (order == cur.key || order == cur.label) cur.selected = true;
264 output.sort.push(cur);
267 var perpage = this.team.perpage();
268 this.info("making perpage, perpage = " + perpage);
269 for(var i = 0; i < this.config.perpage_options.length; i++) {
271 cur.perPage = this.config.perpage_options[i];
272 if (cur.perPage == perpage) cur.selected = true;
273 output.perPage.push(cur);
276 var template = this.team.loadTemplate(this.config.template || "ranking");
277 this.node.html(template(output));
281 mkws.registerWidgetType('lang', function() {
282 // dynamic URL or static page? /path/foo?query=test
283 /* create locale language menu */
284 if (!this.config.show_lang) return;
286 var lang_default = "en";
287 var lang = this.config.lang || lang_default;
290 /* display a list of configured languages, or all */
291 var lang_options = this.config.lang_options;
292 var toBeIncluded = {};
293 for (var i = 0; i < lang_options.length; i++) {
294 toBeIncluded[lang_options[i]] = true;
297 for (var k in mkws.locale_lang) {
298 if (toBeIncluded[k] || lang_options.length == 0) {
300 if (lang === k) cur.selected = true;
302 cur.url = lang_url(k);
308 if (lang_options.length == 0 || toBeIncluded[lang_default]) {
310 if (lang === lang_default) cur.selected = true;
311 cur.code = lang_default;
312 cur.url = lang_url(lang_default);
316 this.info("language menu: " + list.join(", "));
318 var template = this.team.loadTemplate(this.config.template || "lang");
319 this.node.html(template({languages: list}));
320 this.hideWhenNarrow();
322 // set or re-set "lang" URL parameter
323 function lang_url(lang) {
324 var query = location.search;
325 // no query parameters? done
327 return "?lang=" + lang;
330 // parameter does not exist
331 if (!query.match(/[\?&]lang=/)) {
332 return query + "&lang=" + lang;
335 // replace existing parameter
336 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
337 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
343 mkws.registerWidgetType('motd', function() {
344 var container = this.team.widget('motd-container');
346 // Move the MOTD from the provided element down into the container
347 this.node.appendTo(container.node);
352 // This widget has no functionality of its own, but its configuration
353 // is copied up into its team, allowing it to affect other widgets in
356 mkws.registerWidgetType('config', function() {
358 for (var name in c) {
359 if (c.hasOwnProperty(name)) {
360 this.team.config[name] = c[name];
361 this.info(this + " copied property " + name + "='" + c[name] + "' up to team");
367 mkws.registerWidgetType('progress', function() {
370 this.team.queue("stat").subscribe(function(data) {
371 var template = that.team.loadTemplate(that.config.template || "progress");
372 that.node.html(template({
373 done: data.clients - data.activeclients,
374 waiting: data.activeclients
381 // Some elements have mkws* classes that makes them appear as widgets
382 // -- for example, because we want to style them using CSS -- but have
383 // no actual functionality. We register these to prevent ignorable
384 // warnings when they occur.
386 mkws.registerWidgetType('query', function() {});
387 mkws.registerWidgetType('motd-container', function() {});
388 mkws.registerWidgetType('button', function() {});
391 })(mkws.$); // jQuery wrapper