446fe75c463a490f08e3333f34cad28ee24b3483
[mkws-moved-to-github.git] / src / mkws-widget-main.js
1 (function($) { // jQuery wrapper
2
3 // Functions follow for promoting the regular widget object into
4 // widgets of specific types. These could be moved into their own
5 // source files.
6
7
8 mkws.registerWidgetType('targets', function() {
9   if (!this.config.show_switch) return;
10   var that = this;
11
12   this.node.html('No information available yet.');
13   this.node.css("display", "none");
14
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.
19     var cleandata = [];
20     for (var i = 0; i < data.length; i++) {
21       var cur = {};
22       cur.name = data[i].name;
23       cur.id = data[i].id;
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_/, '');
29       cleandata.push(cur);
30     }
31
32     cleandata.sort(function(a,b) { return a.name.localeCompare(b.name) });
33
34     var template = that.team.loadTemplate(that.config.template || "targets");
35     that.node.html(template({data: cleandata}));
36   });
37 });
38
39
40 mkws.registerWidgetType('stat', function() {
41   var that = this;
42   this.team.queue("stat").subscribe(function(data) {
43     var template = that.team.loadTemplate(that.config.template || "stat");
44     that.node.html(template(data));
45   });
46 });
47
48
49 mkws.registerWidgetType('pager', function() {
50   var that = this;
51   var M = mkws.M;
52
53   this.team.queue("pager").subscribe(function(data) {
54     var teamName = that.team.name();
55     var output = {};
56     output.first = data.start + 1;
57     output.last = data.start + data.num;
58     output.count = data.merged;
59     output.found = data.total;
60
61     //client indexes pages from 1 but pz2 from 0
62     var onsides = 5;
63     var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
64     var currentPage = that.team.currentPage();
65
66     var firstClkbl = (currentPage - onsides > 0)
67       ? currentPage - onsides
68       : 1;
69     var lastClkbl = firstClkbl + 2*onsides < pages
70       ? firstClkbl + 2*onsides
71       : pages;
72
73     if (firstClkbl > 1) output.morePrev = true;
74     if (lastClkbl < pages) output.moreNext = true;
75
76     if (currentPage > 1) output.prevClick = "mkws.pagerPrev(\'" + teamName + "\');";
77
78     output.pages = [];
79     for(var i = firstClkbl; i <= lastClkbl; i++) {
80       var o = {};
81       o.number = i;
82       if (i !== currentPage) {
83         o.click = "mkws.showPage(\'" + teamName + "\', " + i + ");";
84       }
85       output.pages.push(o);
86     }
87
88     if (pages - currentPage > 0) output.nextClick = "mkws.pagerNext(\'" + teamName + "\')";
89
90     var template = that.team.loadTemplate(that.config.template || "pager");
91     that.node.html(template(output));
92   });
93 });
94
95 mkws.registerWidgetType('details', function() {
96   var that = this;
97   var recid = that.node.attr("data-mkws-recid");
98   if (this.team.gotRecords()) { 
99     that.team.fetchDetails(recid);
100   } else {
101     this.team.queue("firstrecords").subscribe(function() {
102       that.team.fetchDetails(recid);
103     });
104   }
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));
109     }
110   });
111 });
112
113 mkws.registerWidgetType('records', function() {
114   var that = this;
115   var team = this.team;
116   var m_data;
117   var m_needRedraw = false;
118   var m_frozen = false;
119
120   this.team.queue("searchtriggered").subscribe(function() {
121     var op = that.config.newsearch_opacity;
122     if (op !== undefined) { that.node.fadeTo(500, op); }
123   });
124
125   function refreshRecordData() {
126     that.node.css('opacity', 1);
127
128     if (m_needRedraw) {
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());
141           } 
142         }
143       }
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}}));
148     }
149
150     m_needRedraw = false;
151   }
152
153   function setRecordData(data) {
154     m_data = data;
155     m_needRedraw = true;
156     if (!m_frozen) {
157       refreshRecordData();
158     }
159   }
160
161   this.team.queue("records").subscribe(setRecordData);
162
163   this.node.mousemove(function() {
164     that.info("freezing display records");
165     that.node.css('opacity', 0.5);
166     m_frozen = true;
167   });
168
169   this.node.mouseleave(function() {
170     that.info("refreshing records");
171     that.node.css('opacity', 1);
172     m_frozen = false;
173     refreshRecordData();
174   });
175
176 /*
177   var m_busy = false;
178   this.node.mousemove(function() {
179     if (!m_busy) {
180       m_busy = true;
181       that.info("making semi-transparent in 0.001 s");
182       that.node.fadeTo(1, 0.5, function() {
183         that.info("making opaque in 2 s");
184         that.node.fadeTo(2000, 1, function() {
185           that.info("done making opaque");
186           m_busy = false;
187         });
188       });
189     }
190   });
191 */
192
193   that.autosearch();
194 });
195
196
197 mkws.registerWidgetType('navi', function() {
198   var that = this;
199   var teamName = this.team.name();
200
201   this.team.queue("searchtriggered").subscribe(function() {
202     var filters = that.team.filters();
203     var output = {filters:[]};
204
205     filters.visitTargets(function(id, name) {
206       var cur = {};
207       cur.facet = 'source';
208       cur.value = name;
209       cur.click = "mkws.delimitTarget('" + teamName + "', '" + id + "'); return false;";
210       output.filters.push(cur);
211     });
212
213     filters.visitFields(function(field, value) {
214       var cur = {};
215       cur.facet = field;
216       cur.value = value;
217       cur.click = "mkws.delimitQuery('" + teamName + "', '" + field + "', '" + value + "'" + ");return false;";
218       output.filters.push(cur);
219     });
220
221     var template = that.team.loadTemplate(that.config.template || "navi");
222     that.node.html(template(output));
223   });
224 });
225
226
227 // It seems this and the Perpage widget doen't need to subscribe to
228 // anything, since they produce events rather than consuming them.
229 //
230 mkws.registerWidgetType('sort', function() {
231   var that = this;
232
233   this.node.change(function() {
234     that.team.set_sortOrder(that.node.val());
235     if (that.team.submitted()) {
236       that.team.reShow();
237     }
238     return false;
239   });
240 });
241
242
243 mkws.registerWidgetType('per-page', function() {
244   var that = this;
245
246   this.node.change(function() {
247     that.team.set_perpage(that.node.val());
248     if (that.team.submitted()) {
249       that.team.reShow();
250     }
251     return false;
252   });
253 });
254
255
256 mkws.registerWidgetType('done', function() {
257   var that = this;
258   this.team.queue("complete").subscribe(function(n) {
259     var template = that.team.loadTemplate(that.config.template || "done");
260     that.node.html(template({count: n}));
261   });
262 });
263
264
265 mkws.registerWidgetType('switch', function() {
266   if (!this.config.show_switch) return;
267   var tname = this.team.name();
268   var output = {};
269   output.recordClick = "mkws.switchView(\'" + tname + "\', \'records\')";
270   output.targetClick = "mkws.switchView(\'" + tname + "\', \'targets\')";
271   var template = this.team.loadTemplate(this.config.template || "switch");
272   this.node.html(template(output));
273   this.hideWhenNarrow();
274 });
275
276
277 mkws.registerWidgetType('search', function() {
278   var output = {};
279   output.team = this.team.name();
280   var template = this.team.loadTemplate(this.config.template || "search");
281   this.node.html(template(output));
282 });
283
284
285 mkws.registerWidgetType('search-form', function() {
286   var team = this.team;
287   this.node.submit(function() {
288     var val = team.widget('query').value();
289     team.newSearch(val);
290     return false;
291   });
292 });
293
294
295 mkws.registerWidgetType('results', function() {
296   var template = this.team.loadTemplate(this.config.template || "results");
297   this.node.html(template({team: this.team.name()}));
298   this.autosearch();
299 });
300
301
302 mkws.registerWidgetType('ranking', function() {
303   var output = {};
304   output.perPage = [];
305   output.sort = [];
306   output.team = this.team.name();
307   output.showSort = this.config.show_sort;
308   output.showPerPage = this.config.show_perpage;
309
310   var order = this.team.sortOrder();
311   this.info("making sort, sortOrder = '" + order + "'");
312   for (var i = 0; i < this.config.sort_options.length; i++) {
313     var cur = {};
314     var opt = this.config.sort_options[i];
315     cur.key = opt[0];
316     cur.label = opt.length == 1 ? opt[0] : mkws.M(opt[1]);
317     if (order == cur.key || order == cur.label) cur.selected = true;
318     output.sort.push(cur);
319   }
320
321   var perpage = this.team.perpage();
322   this.info("making perpage, perpage = " + perpage);
323   for(var i = 0; i < this.config.perpage_options.length; i++) {
324     var cur = {};
325     cur.perPage = this.config.perpage_options[i];
326     if (cur.perPage == perpage) cur.selected = true;
327     output.perPage.push(cur);
328   }
329
330   var template = this.team.loadTemplate(this.config.template || "ranking");
331   this.node.html(template(output));
332 });
333
334
335 mkws.registerWidgetType('lang', function() {
336   // dynamic URL or static page? /path/foo?query=test
337   /* create locale language menu */
338   if (!this.config.show_lang) return;
339
340   var lang_default = "en";
341   var lang = this.config.lang || lang_default;
342   var list = [];
343
344   /* display a list of configured languages, or all */
345   var lang_options = this.config.lang_options;
346   var toBeIncluded = {};
347   for (var i = 0; i < lang_options.length; i++) {
348     toBeIncluded[lang_options[i]] = true;
349   }
350
351   for (var k in mkws.locale_lang) {
352     if (toBeIncluded[k] || lang_options.length == 0) {
353       cur = {};
354       if (lang === k) cur.selected = true;
355       cur.code = k;
356       cur.url = lang_url(k);
357       list.push(cur);
358     }
359   }
360
361   // add english link
362   if (lang_options.length == 0 || toBeIncluded[lang_default]) {
363       cur = {};
364       if (lang === lang_default) cur.selected = true;
365       cur.code = lang_default;
366       cur.url = lang_url(lang_default);
367       list.push(cur);
368   }
369
370   this.info("language menu: " + list.join(", "));
371
372   var template = this.team.loadTemplate(this.config.template || "lang");
373   this.node.html(template({languages: list}));
374   this.hideWhenNarrow();
375
376   // set or re-set "lang" URL parameter
377   function lang_url(lang) {
378     var query = location.search;
379     // no query parameters? done
380     if (!query) {
381       return "?lang=" + lang;
382     }
383
384     // parameter does not exist
385     if (!query.match(/[\?&]lang=/)) {
386       return query + "&lang=" + lang;
387     }
388
389     // replace existing parameter
390     query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
391     query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
392     return query;
393   }
394 });
395
396
397 mkws.registerWidgetType('motd', function() {
398   var container = this.team.widget('motd-container');
399   if (container) {
400     // Move the MOTD from the provided element down into the container
401     this.node.appendTo(container.node);
402   }
403 });
404
405
406 // This widget has no functionality of its own, but its configuration
407 // is copied up into its team, allowing it to affect other widgets in
408 // the team.
409 //
410 mkws.registerWidgetType('config', function() {
411   var c = this.config;
412   for (var name in c) {
413     if (c.hasOwnProperty(name)) {
414       this.team.config[name] = c[name];
415       this.info(this + " copied property " + name + "='" + c[name] + "' up to team");
416     }
417   }
418 });
419
420
421 mkws.registerWidgetType('progress', function() {
422   var that = this;
423   this.node.hide();
424   this.team.queue("stat").subscribe(function(data) {
425     var template = that.team.loadTemplate(that.config.template || "progress");
426     that.node.html(template({
427       done: data.clients - data.activeclients,
428       waiting: data.activeclients
429     }));
430     that.node.show();
431   });
432 });
433
434
435 mkws.registerWidgetType('waiting', function() {
436   var that = this;
437
438   this.node.css("visibility", "hidden");
439   var template = that.team.loadTemplate(that.config.template || "waiting");
440   this.node.html(template({
441     src: this.config.src || "http://mkws.indexdata.com/progress.gif"
442   }));
443
444   this.team.queue("searchtriggered").subscribe(function(data) {
445     that.node.css("visibility", "visible");
446   });
447   this.team.queue("complete").subscribe(function(n) {
448     that.node.css("visibility", "hidden");
449   });
450 });
451
452
453 // Some elements have mkws* classes that makes them appear as widgets
454 // -- for example, because we want to style them using CSS -- but have
455 // no actual functionality. We register these to prevent ignorable
456 // warnings when they occur.
457
458 mkws.registerWidgetType('query', function() {});
459 mkws.registerWidgetType('motd-container', function() {});
460 mkws.registerWidgetType('button', function() {});
461
462
463 })(mkws.$); // jQuery wrapper