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