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 var m_needRedraw = false;
118 var m_frozen = false;
120 this.team.queue("searchtriggered").subscribe(function() {
121 var op = that.config.newsearch_opacity;
122 if (op !== undefined) { that.node.fadeTo(500, op); }
125 function refreshRecordData() {
126 that.node.css('opacity', 1);
129 for (var i = 0; i < m_data.hits.length; i++) {
130 var hit = m_data.hits[i];
131 hit.detailLinkId = team.recordElementId(hit.recid[0]);
132 hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;";
133 hit.containerClass = "mkws-summary mkwsSummary mkws-team-" + team.name();
134 hit.containerClass += " " + hit.detailLinkId;
135 // ### At some point, we may be able to move the
136 // m_currentRecordId and m_currentRecordData members
137 // from the team object into this widget.
138 if (hit.recid == team.currentRecordId()) {
139 if (team.currentRecordData()) {
140 hit.renderedDetails = team.renderDetails(team.currentRecordData());
144 var template = team.loadTemplate(that.config.template || "records");
145 var summaryPartial = team.loadTemplate(that.config['summary-template'] || "summary");
146 var tdata = $.extend({}, {"hits": m_data.hits}, that.config.template_vars);
147 that.node.html(template(tdata, {"partials":{"summary":summaryPartial}}));
150 m_needRedraw = false;
153 function setRecordData(data) {
161 this.team.queue("records").subscribe(setRecordData);
164 this.node.mousemove(function() {
165 that.info("freezing display records");
166 that.node.css('opacity', 0.5);
168 clearTimeout(m_timer);
169 m_timer = setTimeout(unfreezeRecordDisplay, 1000);
172 function unfreezeRecordDisplay() {
173 clearTimeout(m_timer);
174 that.info("refreshing records");
175 that.node.css('opacity', 1);
179 this.node.mouseleave(unfreezeRecordDisplay);
185 mkws.registerWidgetType('navi', function() {
187 var teamName = this.team.name();
189 this.team.queue("searchtriggered").subscribe(function() {
190 var filters = that.team.filters();
191 var output = {filters:[]};
193 filters.visitTargets(function(id, name) {
195 cur.facet = 'source';
197 cur.click = "mkws.delimitTarget('" + teamName + "', '" + id + "'); return false;";
198 output.filters.push(cur);
201 filters.visitFields(function(field, value) {
205 cur.click = "mkws.delimitQuery('" + teamName + "', '" + field + "', '" + value + "'" + ");return false;";
206 output.filters.push(cur);
209 var template = that.team.loadTemplate(that.config.template || "navi");
210 that.node.html(template(output));
215 // It seems this and the Perpage widget doen't need to subscribe to
216 // anything, since they produce events rather than consuming them.
218 mkws.registerWidgetType('sort', function() {
221 this.node.change(function() {
222 that.team.set_sortOrder(that.node.val());
223 if (that.team.submitted()) {
231 mkws.registerWidgetType('per-page', function() {
234 this.node.change(function() {
235 that.team.set_perpage(that.node.val());
236 if (that.team.submitted()) {
244 mkws.registerWidgetType('done', function() {
246 this.team.queue("complete").subscribe(function(n) {
247 var template = that.team.loadTemplate(that.config.template || "done");
248 that.node.html(template({count: n}));
253 mkws.registerWidgetType('switch', function() {
254 if (!this.config.show_switch) return;
255 var tname = this.team.name();
257 output.recordClick = "mkws.switchView(\'" + tname + "\', \'records\')";
258 output.targetClick = "mkws.switchView(\'" + tname + "\', \'targets\')";
259 var template = this.team.loadTemplate(this.config.template || "switch");
260 this.node.html(template(output));
261 this.hideWhenNarrow();
265 mkws.registerWidgetType('search', function() {
267 output.team = this.team.name();
268 var template = this.team.loadTemplate(this.config.template || "search");
269 this.node.html(template(output));
273 mkws.registerWidgetType('search-form', function() {
274 var team = this.team;
275 this.node.submit(function() {
276 var val = team.widget('query').value();
283 mkws.registerWidgetType('results', function() {
284 var template = this.team.loadTemplate(this.config.template || "results");
285 this.node.html(template({team: this.team.name()}));
290 mkws.registerWidgetType('ranking', function() {
294 output.team = this.team.name();
295 output.showSort = this.config.show_sort;
296 output.showPerPage = this.config.show_perpage;
298 var order = this.team.sortOrder();
299 this.info("making sort, sortOrder = '" + order + "'");
300 for (var i = 0; i < this.config.sort_options.length; i++) {
302 var opt = this.config.sort_options[i];
304 cur.label = opt.length == 1 ? opt[0] : mkws.M(opt[1]);
305 if (order == cur.key || order == cur.label) cur.selected = true;
306 output.sort.push(cur);
309 var perpage = this.team.perpage();
310 this.info("making perpage, perpage = " + perpage);
311 for(var i = 0; i < this.config.perpage_options.length; i++) {
313 cur.perPage = this.config.perpage_options[i];
314 if (cur.perPage == perpage) cur.selected = true;
315 output.perPage.push(cur);
318 var template = this.team.loadTemplate(this.config.template || "ranking");
319 this.node.html(template(output));
323 mkws.registerWidgetType('lang', function() {
324 // dynamic URL or static page? /path/foo?query=test
325 /* create locale language menu */
326 if (!this.config.show_lang) return;
328 var lang_default = "en";
329 var lang = this.config.lang || lang_default;
332 /* display a list of configured languages, or all */
333 var lang_options = this.config.lang_options;
334 var toBeIncluded = {};
335 for (var i = 0; i < lang_options.length; i++) {
336 toBeIncluded[lang_options[i]] = true;
339 for (var k in mkws.locale_lang) {
340 if (toBeIncluded[k] || lang_options.length == 0) {
342 if (lang === k) cur.selected = true;
344 cur.url = lang_url(k);
350 if (lang_options.length == 0 || toBeIncluded[lang_default]) {
352 if (lang === lang_default) cur.selected = true;
353 cur.code = lang_default;
354 cur.url = lang_url(lang_default);
358 this.info("language menu: " + list.join(", "));
360 var template = this.team.loadTemplate(this.config.template || "lang");
361 this.node.html(template({languages: list}));
362 this.hideWhenNarrow();
364 // set or re-set "lang" URL parameter
365 function lang_url(lang) {
366 var query = location.search;
367 // no query parameters? done
369 return "?lang=" + lang;
372 // parameter does not exist
373 if (!query.match(/[\?&]lang=/)) {
374 return query + "&lang=" + lang;
377 // replace existing parameter
378 query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
379 query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
385 mkws.registerWidgetType('motd', function() {
386 var container = this.team.widget('motd-container');
388 // Move the MOTD from the provided element down into the container
389 this.node.appendTo(container.node);
394 // This widget has no functionality of its own, but its configuration
395 // is copied up into its team, allowing it to affect other widgets in
398 mkws.registerWidgetType('config', function() {
400 for (var name in c) {
401 if (c.hasOwnProperty(name)) {
402 this.team.config[name] = c[name];
403 this.info(this + " copied property " + name + "='" + c[name] + "' up to team");
409 mkws.registerWidgetType('progress', function() {
412 this.team.queue("stat").subscribe(function(data) {
413 var template = that.team.loadTemplate(that.config.template || "progress");
414 that.node.html(template({
415 done: data.clients - data.activeclients,
416 waiting: data.activeclients
423 mkws.registerWidgetType('waiting', function() {
426 this.node.css("visibility", "hidden");
427 var template = that.team.loadTemplate(that.config.template || "waiting");
428 this.node.html(template({
429 src: this.config.src || "http://mkws.indexdata.com/progress.gif"
432 this.team.queue("searchtriggered").subscribe(function(data) {
433 that.node.css("visibility", "visible");
435 this.team.queue("complete").subscribe(function(n) {
436 that.node.css("visibility", "hidden");
441 // Some elements have mkws* classes that makes them appear as widgets
442 // -- for example, because we want to style them using CSS -- but have
443 // no actual functionality. We register these to prevent ignorable
444 // warnings when they occur.
446 mkws.registerWidgetType('query', function() {});
447 mkws.registerWidgetType('motd-container', function() {});
448 mkws.registerWidgetType('button', function() {});
451 })(mkws.$); // jQuery wrapper