Progress of MKWS-58.
[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   var m_timer;
164   this.node.mousemove(function() {
165     that.info("freezing display records");
166     that.node.css('opacity', 0.5);
167     m_frozen = true;
168     clearTimeout(m_timer);
169     m_timer = setTimeout(unfreezeRecordDisplay, 1000);
170   });
171
172   function unfreezeRecordDisplay() {
173     clearTimeout(m_timer);
174     that.info("refreshing records");
175     that.node.css('opacity', 1);
176     m_frozen = false;
177     refreshRecordData();
178   }
179   this.node.mouseleave(unfreezeRecordDisplay);
180
181 /*
182   var m_busy = false;
183   this.node.mousemove(function() {
184     if (!m_busy) {
185       m_busy = true;
186       that.info("making semi-transparent in 0.001 s");
187       that.node.fadeTo(1, 0.5, function() {
188         that.info("making opaque in 2 s");
189         that.node.fadeTo(2000, 1, function() {
190           that.info("done making opaque");
191           m_busy = false;
192         });
193       });
194     }
195   });
196 */
197
198   that.autosearch();
199 });
200
201
202 mkws.registerWidgetType('navi', function() {
203   var that = this;
204   var teamName = this.team.name();
205
206   this.team.queue("searchtriggered").subscribe(function() {
207     var filters = that.team.filters();
208     var output = {filters:[]};
209
210     filters.visitTargets(function(id, name) {
211       var cur = {};
212       cur.facet = 'source';
213       cur.value = name;
214       cur.click = "mkws.delimitTarget('" + teamName + "', '" + id + "'); return false;";
215       output.filters.push(cur);
216     });
217
218     filters.visitFields(function(field, value) {
219       var cur = {};
220       cur.facet = field;
221       cur.value = value;
222       cur.click = "mkws.delimitQuery('" + teamName + "', '" + field + "', '" + value + "'" + ");return false;";
223       output.filters.push(cur);
224     });
225
226     var template = that.team.loadTemplate(that.config.template || "navi");
227     that.node.html(template(output));
228   });
229 });
230
231
232 // It seems this and the Perpage widget doen't need to subscribe to
233 // anything, since they produce events rather than consuming them.
234 //
235 mkws.registerWidgetType('sort', function() {
236   var that = this;
237
238   this.node.change(function() {
239     that.team.set_sortOrder(that.node.val());
240     if (that.team.submitted()) {
241       that.team.reShow();
242     }
243     return false;
244   });
245 });
246
247
248 mkws.registerWidgetType('per-page', function() {
249   var that = this;
250
251   this.node.change(function() {
252     that.team.set_perpage(that.node.val());
253     if (that.team.submitted()) {
254       that.team.reShow();
255     }
256     return false;
257   });
258 });
259
260
261 mkws.registerWidgetType('done', function() {
262   var that = this;
263   this.team.queue("complete").subscribe(function(n) {
264     var template = that.team.loadTemplate(that.config.template || "done");
265     that.node.html(template({count: n}));
266   });
267 });
268
269
270 mkws.registerWidgetType('switch', function() {
271   if (!this.config.show_switch) return;
272   var tname = this.team.name();
273   var output = {};
274   output.recordClick = "mkws.switchView(\'" + tname + "\', \'records\')";
275   output.targetClick = "mkws.switchView(\'" + tname + "\', \'targets\')";
276   var template = this.team.loadTemplate(this.config.template || "switch");
277   this.node.html(template(output));
278   this.hideWhenNarrow();
279 });
280
281
282 mkws.registerWidgetType('search', function() {
283   var output = {};
284   output.team = this.team.name();
285   var template = this.team.loadTemplate(this.config.template || "search");
286   this.node.html(template(output));
287 });
288
289
290 mkws.registerWidgetType('search-form', function() {
291   var team = this.team;
292   this.node.submit(function() {
293     var val = team.widget('query').value();
294     team.newSearch(val);
295     return false;
296   });
297 });
298
299
300 mkws.registerWidgetType('results', function() {
301   var template = this.team.loadTemplate(this.config.template || "results");
302   this.node.html(template({team: this.team.name()}));
303   this.autosearch();
304 });
305
306
307 mkws.registerWidgetType('ranking', function() {
308   var output = {};
309   output.perPage = [];
310   output.sort = [];
311   output.team = this.team.name();
312   output.showSort = this.config.show_sort;
313   output.showPerPage = this.config.show_perpage;
314
315   var order = this.team.sortOrder();
316   this.info("making sort, sortOrder = '" + order + "'");
317   for (var i = 0; i < this.config.sort_options.length; i++) {
318     var cur = {};
319     var opt = this.config.sort_options[i];
320     cur.key = opt[0];
321     cur.label = opt.length == 1 ? opt[0] : mkws.M(opt[1]);
322     if (order == cur.key || order == cur.label) cur.selected = true;
323     output.sort.push(cur);
324   }
325
326   var perpage = this.team.perpage();
327   this.info("making perpage, perpage = " + perpage);
328   for(var i = 0; i < this.config.perpage_options.length; i++) {
329     var cur = {};
330     cur.perPage = this.config.perpage_options[i];
331     if (cur.perPage == perpage) cur.selected = true;
332     output.perPage.push(cur);
333   }
334
335   var template = this.team.loadTemplate(this.config.template || "ranking");
336   this.node.html(template(output));
337 });
338
339
340 mkws.registerWidgetType('lang', function() {
341   // dynamic URL or static page? /path/foo?query=test
342   /* create locale language menu */
343   if (!this.config.show_lang) return;
344
345   var lang_default = "en";
346   var lang = this.config.lang || lang_default;
347   var list = [];
348
349   /* display a list of configured languages, or all */
350   var lang_options = this.config.lang_options;
351   var toBeIncluded = {};
352   for (var i = 0; i < lang_options.length; i++) {
353     toBeIncluded[lang_options[i]] = true;
354   }
355
356   for (var k in mkws.locale_lang) {
357     if (toBeIncluded[k] || lang_options.length == 0) {
358       cur = {};
359       if (lang === k) cur.selected = true;
360       cur.code = k;
361       cur.url = lang_url(k);
362       list.push(cur);
363     }
364   }
365
366   // add english link
367   if (lang_options.length == 0 || toBeIncluded[lang_default]) {
368       cur = {};
369       if (lang === lang_default) cur.selected = true;
370       cur.code = lang_default;
371       cur.url = lang_url(lang_default);
372       list.push(cur);
373   }
374
375   this.info("language menu: " + list.join(", "));
376
377   var template = this.team.loadTemplate(this.config.template || "lang");
378   this.node.html(template({languages: list}));
379   this.hideWhenNarrow();
380
381   // set or re-set "lang" URL parameter
382   function lang_url(lang) {
383     var query = location.search;
384     // no query parameters? done
385     if (!query) {
386       return "?lang=" + lang;
387     }
388
389     // parameter does not exist
390     if (!query.match(/[\?&]lang=/)) {
391       return query + "&lang=" + lang;
392     }
393
394     // replace existing parameter
395     query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
396     query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
397     return query;
398   }
399 });
400
401
402 mkws.registerWidgetType('motd', function() {
403   var container = this.team.widget('motd-container');
404   if (container) {
405     // Move the MOTD from the provided element down into the container
406     this.node.appendTo(container.node);
407   }
408 });
409
410
411 // This widget has no functionality of its own, but its configuration
412 // is copied up into its team, allowing it to affect other widgets in
413 // the team.
414 //
415 mkws.registerWidgetType('config', function() {
416   var c = this.config;
417   for (var name in c) {
418     if (c.hasOwnProperty(name)) {
419       this.team.config[name] = c[name];
420       this.info(this + " copied property " + name + "='" + c[name] + "' up to team");
421     }
422   }
423 });
424
425
426 mkws.registerWidgetType('progress', function() {
427   var that = this;
428   this.node.hide();
429   this.team.queue("stat").subscribe(function(data) {
430     var template = that.team.loadTemplate(that.config.template || "progress");
431     that.node.html(template({
432       done: data.clients - data.activeclients,
433       waiting: data.activeclients
434     }));
435     that.node.show();
436   });
437 });
438
439
440 mkws.registerWidgetType('waiting', function() {
441   var that = this;
442
443   this.node.css("visibility", "hidden");
444   var template = that.team.loadTemplate(that.config.template || "waiting");
445   this.node.html(template({
446     src: this.config.src || "http://mkws.indexdata.com/progress.gif"
447   }));
448
449   this.team.queue("searchtriggered").subscribe(function(data) {
450     that.node.css("visibility", "visible");
451   });
452   this.team.queue("complete").subscribe(function(n) {
453     that.node.css("visibility", "hidden");
454   });
455 });
456
457
458 // Some elements have mkws* classes that makes them appear as widgets
459 // -- for example, because we want to style them using CSS -- but have
460 // no actual functionality. We register these to prevent ignorable
461 // warnings when they occur.
462
463 mkws.registerWidgetType('query', function() {});
464 mkws.registerWidgetType('motd-container', function() {});
465 mkws.registerWidgetType('button', function() {});
466
467
468 })(mkws.$); // jQuery wrapper