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