Merge branch 'master' into templateallthemarkup
[mkws-moved-to-github.git] / src / mkws-widget-main.js
1 // Functions follow for promoting the regular widget object into
2 // widgets of specific types. These could be moved into their own
3 // source files.
4
5
6 mkws.registerWidgetType('Targets', function() {
7   if (!this.config.show_switch) return;
8   var that = this;
9
10   this.node.html('No information available yet.');
11   this.node.css("display", "none");
12
13   this.team.queue("targets").subscribe(function(data) {
14     // There is a bug in pz2.js wherein it makes each data object an array but
15     // simply assigns properties to it.
16     // TODO: remove this when PAZ-946 is addressed.
17     var cleandata = [];
18     for (var i = 0; i < data.length; i++) {
19       var cur = {};
20       cur.id = data[i].id;
21       cur.hits = data[i].hits;
22       cur.diagnostic = data[i].diagnostic;
23       cur.records = data[i].records;
24       cur.state = data[i].state;
25       cleandata.push(cur);
26     }
27
28     var template = that.team.loadTemplate(that.config.template || "Targets");
29     that.node.html(template({data: cleandata}));
30   });
31 });
32
33
34 mkws.registerWidgetType('Stat', function() {
35   var that = this;
36   this.team.queue("stat").subscribe(function(data) {
37     var template = that.team.loadTemplate(that.config.template || "Stat");
38     that.node.html(template(data));
39   });
40 });
41
42
43 mkws.registerWidgetType('Pager', function() {
44   var that = this;
45   var M = mkws.M;
46
47   this.team.queue("pager").subscribe(function(data) {
48     var teamName = that.team.name();
49     var output = {};
50     output.first = data.start + 1;
51     output.last = data.start + data.num;
52     output.count = data.merged;
53     output.found = data.total;
54
55     //client indexes pages from 1 but pz2 from 0
56     var onsides = 6;
57     var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
58     var currentPage = that.team.currentPage();
59
60     var firstClkbl = (currentPage - onsides > 0)
61       ? currentPage - onsides
62       : 1;
63     var lastClkbl = firstClkbl + 2*onsides < pages
64       ? firstClkbl + 2*onsides
65       : pages;
66
67     if (firstClkbl > 1) output.morePrev = true;
68     if (lastClkbl < pages) output.moreNext = true;
69
70     if (currentPage > 1) output.prevClick = "mkws.pagerPrev(\'" + teamName + "\');";
71
72     output.pages = [];
73     for(var i = firstClkbl; i <= lastClkbl; i++) {
74       var o = {};
75       o.number = i;
76       if (i !== currentPage) {
77         o.click = "mkws.showPage(\'" + teamName + "\', " + i + ");";
78       }
79       output.pages.push(o);
80     }
81
82     if (pages - currentPage > 0) output.nextClick = "mkws.pagerNext(\'" + teamName + "\')";
83
84     var template = that.team.loadTemplate(that.config.template || "Pager");
85     that.node.html(template(output));
86   });
87 });
88
89 mkws.registerWidgetType('Details', function() {
90   var that = this;
91   var recid = that.node.attr("data-mkws-recid");
92   if (this.team.gotRecords()) { 
93     that.team.fetchDetails(recid);
94   } else {
95     this.team.queue("firstrecords").subscribe(function() {
96       that.team.fetchDetails(recid);
97     });
98   }
99   this.team.queue("record").subscribe(function(data) {
100     console.log(data);
101     if ($.inArray(recid, data.recid) > -1) {
102       var template = that.team.loadTemplate(that.config.template || "Record");
103       that.node.html(template(data));
104     }
105   });
106   that.autosearch();
107 });
108
109 mkws.registerWidgetType('Records', function() {
110   var that = this;
111   var team = this.team;
112
113   this.team.queue("records").subscribe(function(data) {
114     for (var i = 0; i < data.hits.length; i++) {
115       var hit = data.hits[i];
116       that.team.queue("record").publish(hit);
117       hit.detailLinkId = team.recordElementId(hit.recid[0]);
118       hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;";
119       hit.containerClass = "mkwsSummary mkwsTeam_" + team.name();
120       hit.containerClass += " " + hit.detailLinkId;
121       // ### At some point, we may be able to move the
122       // m_currentRecordId and m_currentRecordData members
123       // from the team object into this widget.
124       if (hit.recid == team.currentRecordId()) {
125         if (team.currentRecordData()) {
126           hit.renderedDetails = team.renderDetails(team.currentRecordData());
127           console.log(hit.renderedDetails); 
128         } 
129       }
130     }
131     var template = team.loadTemplate(that.config.template || "Records");
132     var targs = $.extend({}, {"hits": data.hits}, that.config.template_vars);
133     that.node.html(template(targs));
134   });
135
136   that.autosearch();
137 });
138
139
140 mkws.registerWidgetType('Navi', function() {
141   var that = this;
142   var teamName = this.team.name();
143
144   this.team.queue("navi").subscribe(function() {
145     var filters = that.team.filters();
146     var output = {filters:[]};
147
148     filters.visitTargets(function(id, name) {
149       var cur = {};
150       cur.facet = 'source';
151       cur.value = name;
152       cur.click = "mkws.delimitTarget('" + teamName + "', '" + id + "'); return false;";
153       output.filters.push(cur);
154     });
155
156     filters.visitFields(function(field, value) {
157       var cur = {};
158       cur.facet = field;
159       cur.value = value;
160       cur.click = "mkws.delimitQuery('" + teamName + "', '" + field + "', '" + value + "'" + ");return false;";
161       output.filters.push(cur);
162     });
163
164     var template = that.team.loadTemplate(that.config.template || "Navi");
165     that.node.html(template(output));
166   });
167 });
168
169
170 // It seems this and the Perpage widget doen't need to subscribe to
171 // anything, since they produce events rather than consuming them.
172 //
173 mkws.registerWidgetType('Sort', function() {
174   var that = this;
175
176   this.node.change(function() {
177     that.team.set_sortOrder(that.node.val());
178     if (that.team.submitted()) {
179       that.team.reShow();
180     }
181     return false;
182   });
183 });
184
185
186 mkws.registerWidgetType('Perpage', function() {
187   var that = this;
188
189   this.node.change(function() {
190     that.team.set_perpage(that.node.val());
191     if (that.team.submitted()) {
192       that.team.reShow();
193     }
194     return false;
195   });
196 });
197
198
199 mkws.registerWidgetType('Done', function() {
200   var that = this;
201   this.team.queue("complete").subscribe(function(n) {
202     var template = that.team.loadTemplate(that.config.template || "Done");
203     that.node.html(template({count: n}));
204   });
205 });
206
207
208 mkws.registerWidgetType('Switch', function() {
209   if (!this.config.show_switch) return;
210   var tname = this.team.name();
211   var output = {};
212   output.recordClick = "mkws.switchView(\'" + tname + "\', \'records\')";
213   output.targetClick = "mkws.switchView(\'" + tname + "\', \'targets\')";
214   var template = this.team.loadTemplate(this.config.template || "Switch");
215   this.node.html(template(output));
216   this.hideWhenNarrow();
217 });
218
219
220 mkws.registerWidgetType('Search', function() {
221   var output = {};
222   output.team = this.team.name();
223   output.queryWidth = this.config.query_width;
224   var template = this.team.loadTemplate(this.config.template || "Search");
225   this.node.html(template(output));
226 });
227
228
229 mkws.registerWidgetType('SearchForm', 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.log("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.log("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.log("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('MOTDContainer');
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.log(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('MOTDContainer', function() {});
386 mkws.registerWidgetType('Button', function() {});
387
388