Towards a proper fix for MKWS-352.
[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 = that.team.targetName(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;
29       cleandata.push(cur);
30     }
31
32     var template = that.team.loadTemplate(that.config.template || "targets");
33     that.node.html(template({data: cleandata}));
34   });
35 });
36
37
38 mkws.registerWidgetType('stat', function() {
39   var that = this;
40   this.team.queue("stat").subscribe(function(data) {
41     var template = that.team.loadTemplate(that.config.template || "stat");
42     that.node.html(template(data));
43   });
44 });
45
46
47 mkws.registerWidgetType('pager', function() {
48   var that = this;
49   var M = mkws.M;
50
51   this.team.queue("pager").subscribe(function(data) {
52     var teamName = that.team.name();
53     var output = {};
54     output.first = data.start + 1;
55     output.last = data.start + data.num;
56     output.count = data.merged;
57     output.found = data.total;
58
59     //client indexes pages from 1 but pz2 from 0
60     var onsides = 5;
61     var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
62     var currentPage = that.team.currentPage();
63
64     var firstClkbl = (currentPage - onsides > 0)
65       ? currentPage - onsides
66       : 1;
67     var lastClkbl = firstClkbl + 2*onsides < pages
68       ? firstClkbl + 2*onsides
69       : pages;
70
71     if (firstClkbl > 1) output.morePrev = true;
72     if (lastClkbl < pages) output.moreNext = true;
73
74     if (currentPage > 1) output.prevClick = "mkws.pagerPrev(\'" + teamName + "\');";
75
76     output.pages = [];
77     for(var i = firstClkbl; i <= lastClkbl; i++) {
78       var o = {};
79       o.number = i;
80       if (i !== currentPage) {
81         o.click = "mkws.showPage(\'" + teamName + "\', " + i + ");";
82       }
83       output.pages.push(o);
84     }
85
86     if (pages - currentPage > 0) output.nextClick = "mkws.pagerNext(\'" + teamName + "\')";
87
88     var template = that.team.loadTemplate(that.config.template || "pager");
89     that.node.html(template(output));
90   });
91 });
92
93 mkws.registerWidgetType('details', function() {
94   var that = this;
95   var recid = that.node.attr("data-mkws-recid");
96   if (this.team.gotRecords()) { 
97     that.team.fetchDetails(recid);
98   } else {
99     this.team.queue("firstrecords").subscribe(function() {
100       that.team.fetchDetails(recid);
101     });
102   }
103   this.team.queue("record").subscribe(function(data) {
104     if ($.inArray(recid, data.recid) > -1) {
105       var template = that.team.loadTemplate(that.config.template || "details");
106       that.node.html(template(data));
107     }
108   });
109 });
110
111 mkws.registerWidgetType('records', function() {
112   var that = this;
113   var team = this.team;
114
115   this.team.queue("records").subscribe(function(data) {
116     for (var i = 0; i < data.hits.length; i++) {
117       var hit = data.hits[i];
118       hit.detailLinkId = team.recordElementId(hit.recid[0]);
119       hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;";
120       hit.containerClass = "mkws-summary mkwsSummary mkws-team-" + team.name();
121       hit.containerClass += " " + hit.detailLinkId;
122       // ### At some point, we may be able to move the
123       // m_currentRecordId and m_currentRecordData members
124       // from the team object into this widget.
125       if (hit.recid == team.currentRecordId()) {
126         if (team.currentRecordData()) {
127           hit.renderedDetails = team.renderDetails(team.currentRecordData());
128         } 
129       }
130     }
131     var template = team.loadTemplate(that.config.template || "records");
132     var summaryPartial = team.loadTemplate(that.config['summary-template'] || "summary");
133     var tdata = $.extend({}, {"hits": data.hits}, that.config.template_vars);
134     that.node.html(template(tdata, {"partials":{"summary":summaryPartial}}));
135   });
136
137   that.autosearch();
138 });
139
140
141 mkws.registerWidgetType('navi', function() {
142   var that = this;
143   var teamName = this.team.name();
144
145   this.team.queue("searchtriggered").subscribe(function() {
146     var filters = that.team.filters();
147     var output = {filters:[]};
148
149     filters.visitTargets(function(id, name) {
150       var cur = {};
151       cur.facet = 'source';
152       cur.value = name;
153       cur.click = "mkws.delimitTarget('" + teamName + "', '" + id + "'); return false;";
154       output.filters.push(cur);
155     });
156
157     filters.visitFields(function(field, value) {
158       var cur = {};
159       cur.facet = field;
160       cur.value = value;
161       cur.click = "mkws.delimitQuery('" + teamName + "', '" + field + "', '" + value + "'" + ");return false;";
162       output.filters.push(cur);
163     });
164
165     var template = that.team.loadTemplate(that.config.template || "navi");
166     that.node.html(template(output));
167   });
168 });
169
170
171 // It seems this and the Perpage widget doen't need to subscribe to
172 // anything, since they produce events rather than consuming them.
173 //
174 mkws.registerWidgetType('sort', function() {
175   var that = this;
176
177   this.node.change(function() {
178     that.team.set_sortOrder(that.node.val());
179     if (that.team.submitted()) {
180       that.team.reShow();
181     }
182     return false;
183   });
184 });
185
186
187 mkws.registerWidgetType('per-page', function() {
188   var that = this;
189
190   this.node.change(function() {
191     that.team.set_perpage(that.node.val());
192     if (that.team.submitted()) {
193       that.team.reShow();
194     }
195     return false;
196   });
197 });
198
199
200 mkws.registerWidgetType('done', function() {
201   var that = this;
202   this.team.queue("complete").subscribe(function(n) {
203     var template = that.team.loadTemplate(that.config.template || "done");
204     that.node.html(template({count: n}));
205   });
206 });
207
208
209 mkws.registerWidgetType('switch', function() {
210   if (!this.config.show_switch) return;
211   var tname = this.team.name();
212   var output = {};
213   output.recordClick = "mkws.switchView(\'" + tname + "\', \'records\')";
214   output.targetClick = "mkws.switchView(\'" + tname + "\', \'targets\')";
215   var template = this.team.loadTemplate(this.config.template || "switch");
216   this.node.html(template(output));
217   this.hideWhenNarrow();
218 });
219
220
221 mkws.registerWidgetType('search', function() {
222   var output = {};
223   output.team = this.team.name();
224   var template = this.team.loadTemplate(this.config.template || "search");
225   this.node.html(template(output));
226 });
227
228
229 mkws.registerWidgetType('search-form', function() {
230   var team = this.team;
231   this.node.submit(function() {
232     var val = team.widget('query').value();
233     team.newSearch(val);
234     return false;
235   });
236 });
237
238
239 mkws.registerWidgetType('results', function() {
240   var template = this.team.loadTemplate(this.config.template || "results");
241   this.node.html(template({team: this.team.name()}));
242   this.autosearch();
243 });
244
245
246 mkws.registerWidgetType('ranking', function() {
247   var output = {};
248   output.perPage = [];
249   output.sort = [];
250   output.team = this.team.name();
251   output.showSort = this.config.show_sort;
252   output.showPerPage = this.config.show_perpage;
253
254   var order = this.team.sortOrder();
255   this.info("making sort, sortOrder = '" + order + "'");
256   for (var i = 0; i < this.config.sort_options.length; i++) {
257     var cur = {};
258     var opt = this.config.sort_options[i];
259     cur.key = opt[0];
260     cur.label = opt.length == 1 ? opt[0] : opt[1];
261     if (order == cur.key || order == cur.label) cur.selected = true;
262     output.sort.push(cur);
263   }
264
265   var perpage = this.team.perpage();
266   this.info("making perpage, perpage = " + perpage);
267   for(var i = 0; i < this.config.perpage_options.length; i++) {
268     var cur = {};
269     cur.perPage = this.config.perpage_options[i];
270     if (cur.perPage == perpage) cur.selected = true;
271     output.perPage.push(cur);
272   }
273
274   var template = this.team.loadTemplate(this.config.template || "ranking");
275   this.node.html(template(output));
276 });
277
278
279 mkws.registerWidgetType('lang', function() {
280   // dynamic URL or static page? /path/foo?query=test
281   /* create locale language menu */
282   if (!this.config.show_lang) return;
283
284   var lang_default = "en";
285   var lang = this.config.lang || lang_default;
286   var list = [];
287
288   /* display a list of configured languages, or all */
289   var lang_options = this.config.lang_options;
290   var toBeIncluded = {};
291   for (var i = 0; i < lang_options.length; i++) {
292     toBeIncluded[lang_options[i]] = true;
293   }
294
295   for (var k in mkws.locale_lang) {
296     if (toBeIncluded[k] || lang_options.length == 0) {
297       cur = {};
298       if (lang === k) cur.selected = true;
299       cur.code = k;
300       cur.url = lang_url(k);
301       list.push(cur);
302     }
303   }
304
305   // add english link
306   if (lang_options.length == 0 || toBeIncluded[lang_default]) {
307       cur = {};
308       if (lang === lang_default) cur.selected = true;
309       cur.code = lang_default;
310       cur.url = lang_url(lang_default);
311       list.push(cur);
312   }
313
314   this.info("language menu: " + list.join(", "));
315
316   var template = this.team.loadTemplate(this.config.template || "lang");
317   this.node.html(template({languages: list}));
318   this.hideWhenNarrow();
319
320   // set or re-set "lang" URL parameter
321   function lang_url(lang) {
322     var query = location.search;
323     // no query parameters? done
324     if (!query) {
325       return "?lang=" + lang;
326     }
327
328     // parameter does not exist
329     if (!query.match(/[\?&]lang=/)) {
330       return query + "&lang=" + lang;
331     }
332
333     // replace existing parameter
334     query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
335     query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
336     return query;
337   }
338 });
339
340
341 mkws.registerWidgetType('motd', function() {
342   var container = this.team.widget('motd-container');
343   if (container) {
344     // Move the MOTD from the provided element down into the container
345     this.node.appendTo(container.node);
346   }
347 });
348
349
350 // This widget has no functionality of its own, but its configuration
351 // is copied up into its team, allowing it to affect other widgets in
352 // the team.
353 //
354 mkws.registerWidgetType('config', function() {
355   var c = this.config;
356   for (var name in c) {
357     if (c.hasOwnProperty(name)) {
358       this.team.config[name] = c[name];
359       this.info(this + " copied property " + name + "='" + c[name] + "' up to team");
360     }
361   }
362 });
363
364
365 mkws.registerWidgetType('progress', function() {
366   var that = this;
367   this.node.hide();
368   this.team.queue("stat").subscribe(function(data) {
369     var template = that.team.loadTemplate(that.config.template || "progress");
370     that.node.html(template({
371       done: data.clients - data.activeclients,
372       waiting: data.activeclients
373     }));
374     that.node.show();
375   });
376 });
377
378
379 // Some elements have mkws* classes that makes them appear as widgets
380 // -- for example, because we want to style them using CSS -- but have
381 // no actual functionality. We register these to prevent ignorable
382 // warnings when they occur.
383
384 mkws.registerWidgetType('query', function() {});
385 mkws.registerWidgetType('motd-container', function() {});
386 mkws.registerWidgetType('button', function() {});
387
388
389 })(mkws.$); // jQuery wrapper