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("searchtriggered").subscribe(function() {
118 var op = that.config.newsearch_opacity;
119 if (op !== undefined) { that.node.fadeTo(500, op); }
122 var m_dataToRedraw = null;
123 function refreshRecordData() {
124 that.node.css('opacity', 1);
126 if (m_dataToRedraw) {
127 for (var i = 0; i < m_dataToRedraw.hits.length; i++) {
128 var hit = m_dataToRedraw.hits[i];
129 hit.detailLinkId = team.recordElementId(hit.recid[0]);
130 hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;";
131 hit.containerClass = "mkws-summary mkwsSummary mkws-team-" + team.name();
132 hit.containerClass += " " + hit.detailLinkId;
133 // ### At some point, we may be able to move the
134 // m_currentRecordId and m_currentRecordData members
135 // from the team object into this widget.
136 if (hit.recid == team.currentRecordId()) {
137 if (team.currentRecordData()) {
138 hit.renderedDetails = team.renderDetails(team.currentRecordData());
142 var urls = hit['md-electronic-url'];
146 for (var j = 0; j < urls.length; j++) {
148 if (!url.match(/^(https?:)?\/\//)) {
149 that.warn("link '" + url + "' is not a valid URL");
150 } else if (!bestLink) {
153 otherLinks.push(url);
156 hit.bestLink = bestLink;
157 hit.otherLinks = otherLinks;
161 var template = team.loadTemplate(that.config.template || "records");
162 var summaryPartial = team.loadTemplate(that.config['summary-template'] || "summary");
163 var tdata = $.extend({}, {"hits": m_dataToRedraw.hits}, that.config.template_vars);
164 that.node.html(template(tdata, {"partials":{"summary":summaryPartial}}));
167 m_dataToRedraw = null;
170 var m_frozen = false;
171 this.team.queue("records").subscribe(function(data) {
172 m_dataToRedraw = data;
179 this.node.mousemove(function() {
180 var op = that.config.freeze_opacity;
181 if (op !== undefined) { that.node.css('opacity', op); }
183 clearTimeout(m_timer);
184 m_timer = setTimeout(unfreezeRecordDisplay, 1000);
187 function unfreezeRecordDisplay() {
188 clearTimeout(m_timer);
189 that.node.css('opacity', 1);
193 this.node.mouseleave(unfreezeRecordDisplay);
199 mkws.registerWidgetType('navi', function() {
201 var teamName = this.team.name();
203 this.team.queue("searchtriggered").subscribe(function() {
204 var filters = that.team.filters();
205 var output = {filters:[]};
207 filters.visitTargets(function(id, name) {
209 cur.facet = 'source';
211 cur.click = "mkws.delimitTarget('" + teamName + "', '" + id + "'); return false;";
212 output.filters.push(cur);
215 filters.visitFields(function(field, value) {
219 cur.click = "mkws.delimitQuery('" + teamName + "', '" + field + "', '" + value + "'" + ");return false;";
220 output.filters.push(cur);
223 var template = that.team.loadTemplate(that.config.template || "navi");
224 that.node.html(template(output));
229 // It seems this and the Perpage widget doen't need to subscribe to
230 // anything, since they produce events rather than consuming them.
232 mkws.registerWidgetType('sort', function() {
235 this.node.change(function() {
236 that.team.set_sortOrder(that.node.val());
237 if (that.team.submitted()) {
245 mkws.registerWidgetType('per-page', function() {
248 this.node.change(function() {
249 that.team.set_perpage(that.node.val());
250 if (that.team.submitted()) {
258 mkws.registerWidgetType('done', function() {
260 this.team.queue("complete").subscribe(function(n) {
261 var template = that.team.loadTemplate(that.config.template || "done");
262 that.node.html(template({count: n}));
267 mkws.registerWidgetType('switch', function() {
268 if (!this.config.show_switch) return;
269 var tname = this.team.name();
271 output.recordClick = "mkws.switchView(\'" + tname + "\', \'records\')";
272 output.targetClick = "mkws.switchView(\'" + tname + "\', \'targets\')";
273 var template = this.team.loadTemplate(this.config.template || "switch");
274 this.node.html(template(output));
275 this.hideWhenNarrow();
279 mkws.registerWidgetType('search', function() {
281 output.team = this.team.name();
282 var template = this.team.loadTemplate(this.config.template || "search");
283 this.node.html(template(output));
287 mkws.registerWidgetType('search-form', function() {
288 var team = this.team;
290 this.node.submit(function() {
291 var val = team.widget('query').value();
292 team.newSearch(that, val);
298 mkws.registerWidgetType('results', function() {
299 var template = this.team.loadTemplate(this.config.template || "results");
300 this.node.html(template({team: this.team.name()}));
305 mkws.registerWidgetType('ranking', function() {
309 output.team = this.team.name();
310 output.showSort = this.config.show_sort;
311 output.showPerPage = this.config.show_perpage;
313 var order = this.team.sortOrder();
314 this.info("making sort, sortOrder = '" + order + "'");
315 for (var i = 0; i < this.config.sort_options.length; i++) {
317 var opt = this.config.sort_options[i];
319 cur.label = opt.length == 1 ? opt[0] : mkws.M(opt[1]);
320 if (order == cur.key || order == cur.label) cur.selected = true;
321 output.sort.push(cur);
324 var perpage = this.team.perpage();
325 this.info("making perpage, perpage = " + perpage);
326 for(var i = 0; i < this.config.perpage_options.length; i++) {
328 cur.perPage = this.config.perpage_options[i];
329 if (cur.perPage == perpage) cur.selected = true;
330 output.perPage.push(cur);
333 var template = this.team.loadTemplate(this.config.template || "ranking");
334 this.node.html(template(output));
338 mkws.registerWidgetType('lang', function() {
339 // dynamic URL or static page? /path/foo?query=test
340 /* create locale language menu */
341 if (!this.config.show_lang) return;
343 var lang_default = "en";
344 var lang = this.config.lang || lang_default;
347 /* display a list of configured languages, or all */
348 var lang_options = this.config.lang_options;
349 var toBeIncluded = {};
350 for (var i = 0; i < lang_options.length; i++) {
351 toBeIncluded[lang_options[i]] = true;
354 for (var k in mkws.locale_lang) {
355 if (toBeIncluded[k] || lang_options.length == 0) {
357 if (lang === k) cur.selected = true;
359 cur.url = lang_url(k);
365 if (lang_options.length == 0 || toBeIncluded[lang_default]) {
367 if (lang === lang_default) cur.selected = true;
368 cur.code = lang_default;
369 cur.url = lang_url(lang_default);
373 this.info("language menu: " + list.join(", "));
375 var template = this.team.loadTemplate(this.config.template || "lang");
376 this.node.html(template({languages: list}));
377 this.hideWhenNarrow();
379 // set or re-set "lang" URL parameter
380 function lang_url(lang) {
381 var query = location.search;
382 // no query parameters? done
384 return "?lang=" + lang;
387 // parameter does not exist
388 if (!query.match(/[\?&]lang=/)) {
389 return query + "&lang=" + lang;
392 // replace existing parameter
393 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
394 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
400 mkws.registerWidgetType('motd', function() {
401 var container = this.team.widget('motd-container');
403 // Move the MOTD from the provided element down into the container
404 this.node.appendTo(container.node);
409 // This widget has no functionality of its own, but its configuration
410 // is copied up into its team, allowing it to affect other widgets in
413 mkws.registerWidgetType('config', function() {
415 for (var name in c) {
416 if (c.hasOwnProperty(name)) {
417 this.team.config[name] = c[name];
418 this.info(this + " copied property " + name + "='" + c[name] + "' up to team");
424 mkws.registerWidgetType('progress', function() {
427 this.team.queue("stat").subscribe(function(data) {
428 var template = that.team.loadTemplate(that.config.template || "progress");
429 that.node.html(template({
430 done: data.clients - data.activeclients,
431 waiting: data.activeclients
438 mkws.registerWidgetType('waiting', function() {
441 this.node.css("visibility", "hidden");
442 var template = that.team.loadTemplate(that.config.template || "waiting");
443 this.node.html(template({
444 src: this.config.src || "http://mkws.indexdata.com/progress.gif"
447 this.team.queue("searchtriggered").subscribe(function(data) {
448 that.node.css("visibility", "visible");
450 this.team.queue("complete").subscribe(function(n) {
451 that.node.css("visibility", "hidden");
456 // Some elements have mkws* classes that makes them appear as widgets
457 // -- for example, because we want to style them using CSS -- but have
458 // no actual functionality. We register these to prevent ignorable
459 // warnings when they occur.
461 mkws.registerWidgetType('query', function() {});
462 mkws.registerWidgetType('motd-container', function() {});
463 mkws.registerWidgetType('button', function() {});
466 })(mkws.$); // jQuery wrapper