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