Rename all the templates MKWS-313
[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.id = data[i].id;
23       cur.hits = data[i].hits;
24       cur.diagnostic = data[i].diagnostic;
25       cur.records = data[i].records;
26       cur.state = data[i].state;
27       cleandata.push(cur);
28     }
29
30     var template = that.team.loadTemplate(that.config.template || "targets");
31     that.node.html(template({data: cleandata}));
32   });
33 });
34
35
36 mkws.registerWidgetType('stat', function() {
37   var that = this;
38   this.team.queue("stat").subscribe(function(data) {
39     var template = that.team.loadTemplate(that.config.template || "stat");
40     that.node.html(template(data));
41   });
42 });
43
44
45 mkws.registerWidgetType('pager', function() {
46   var that = this;
47   var M = mkws.M;
48
49   this.team.queue("pager").subscribe(function(data) {
50     var teamName = that.team.name();
51     var output = {};
52     output.first = data.start + 1;
53     output.last = data.start + data.num;
54     output.count = data.merged;
55     output.found = data.total;
56
57     //client indexes pages from 1 but pz2 from 0
58     var onsides = 6;
59     var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
60     var currentPage = that.team.currentPage();
61
62     var firstClkbl = (currentPage - onsides > 0)
63       ? currentPage - onsides
64       : 1;
65     var lastClkbl = firstClkbl + 2*onsides < pages
66       ? firstClkbl + 2*onsides
67       : pages;
68
69     if (firstClkbl > 1) output.morePrev = true;
70     if (lastClkbl < pages) output.moreNext = true;
71
72     if (currentPage > 1) output.prevClick = "mkws.pagerPrev(\'" + teamName + "\');";
73
74     output.pages = [];
75     for(var i = firstClkbl; i <= lastClkbl; i++) {
76       var o = {};
77       o.number = i;
78       if (i !== currentPage) {
79         o.click = "mkws.showPage(\'" + teamName + "\', " + i + ");";
80       }
81       output.pages.push(o);
82     }
83
84     if (pages - currentPage > 0) output.nextClick = "mkws.pagerNext(\'" + teamName + "\')";
85
86     var template = that.team.loadTemplate(that.config.template || "pager");
87     that.node.html(template(output));
88   });
89 });
90
91 mkws.registerWidgetType('details', function() {
92   var that = this;
93   var recid = that.node.attr("data-mkws-recid");
94   if (this.team.gotRecords()) { 
95     that.team.fetchDetails(recid);
96   } else {
97     this.team.queue("firstrecords").subscribe(function() {
98       that.team.fetchDetails(recid);
99     });
100   }
101   this.team.queue("record").subscribe(function(data) {
102     console.log(data);
103     if ($.inArray(recid, data.recid) > -1) {
104       var template = that.team.loadTemplate(that.config.template || "details");
105       that.node.html(template(data));
106     }
107   });
108   that.autosearch();
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       that.team.queue("record").publish(hit);
119       hit.detailLinkId = team.recordElementId(hit.recid[0]);
120       hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;";
121       hit.containerClass = "mkws-summary mkwsSummary mkws-team-" + team.name();
122       hit.containerClass += " " + hit.detailLinkId;
123       // ### At some point, we may be able to move the
124       // m_currentRecordId and m_currentRecordData members
125       // from the team object into this widget.
126       if (hit.recid == team.currentRecordId()) {
127         if (team.currentRecordData()) {
128           hit.renderedDetails = team.renderDetails(team.currentRecordData());
129         } 
130       }
131     }
132     var template = team.loadTemplate(that.config.template || "records");
133     var targs = $.extend({}, {"hits": data.hits}, that.config.template_vars);
134     that.node.html(template(targs));
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("navi").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   output.queryWidth = this.config.query_width;
225   var template = this.team.loadTemplate(this.config.template || "search");
226   this.node.html(template(output));
227 });
228
229
230 mkws.registerWidgetType('search-form', function() {
231   var team = this.team;
232   this.node.submit(function() {
233     var val = team.widget('query').value();
234     team.newSearch(val);
235     return false;
236   });
237 });
238
239
240 mkws.registerWidgetType('results', function() {
241   var template = this.team.loadTemplate(this.config.template || "results");
242   this.node.html(template({team: this.team.name()}));
243   this.autosearch();
244 });
245
246
247 mkws.registerWidgetType('ranking', function() {
248   var output = {};
249   output.perPage = [];
250   output.sort = [];
251   output.team = this.team.name();
252   output.showSort = this.config.show_sort;
253   output.showPerPage = this.config.show_perpage;
254
255   var order = this.team.sortOrder();
256   this.log("making sort, sortOrder = '" + order + "'");
257   for (var i = 0; i < this.config.sort_options.length; i++) {
258     var cur = {};
259     var opt = this.config.sort_options[i];
260     cur.key = opt[0];
261     cur.label = opt.length == 1 ? opt[0] : opt[1];
262     if (order == cur.key || order == cur.label) cur.selected = true;
263     output.sort.push(cur);
264   }
265
266   var perpage = this.team.perpage();
267   this.log("making perpage, perpage = " + perpage);
268   for(var i = 0; i < this.config.perpage_options.length; i++) {
269     var cur = {};
270     cur.perPage = this.config.perpage_options[i];
271     if (cur.perPage == perpage) cur.selected = true;
272     output.perPage.push(cur);
273   }
274
275   var template = this.team.loadTemplate(this.config.template || "ranking");
276   this.node.html(template(output));
277 });
278
279
280 mkws.registerWidgetType('lang', function() {
281   // dynamic URL or static page? /path/foo?query=test
282   /* create locale language menu */
283   if (!this.config.show_lang) return;
284
285   var lang_default = "en";
286   var lang = this.config.lang || lang_default;
287   var list = [];
288
289   /* display a list of configured languages, or all */
290   var lang_options = this.config.lang_options || [];
291   var toBeIncluded = {};
292   for (var i = 0; i < lang_options.length; i++) {
293     toBeIncluded[lang_options[i]] = true;
294   }
295
296   for (var k in mkws.locale_lang) {
297     if (toBeIncluded[k] || lang_options.length == 0) {
298       cur = {};
299       if (lang === k) cur.selected = true;
300       cur.code = k;
301       cur.url = lang_url(k);
302       list.push(cur);
303     }
304   }
305
306   // add english link
307   if (lang_options.length == 0 || toBeIncluded[lang_default]) {
308       cur = {};
309       if (lang === lang_default) cur.selected = true;
310       cur.code = lang_default;
311       cur.url = lang_url(lang_default);
312       list.push(cur);
313   }
314
315   this.log("language menu: " + list.join(", "));
316
317   var template = this.team.loadTemplate(this.config.template || "lang");
318   this.node.html(template({languages: list}));
319   this.hideWhenNarrow();
320
321   // set or re-set "lang" URL parameter
322   function lang_url(lang) {
323     var query = location.search;
324     // no query parameters? done
325     if (!query) {
326       return "?lang=" + lang;
327     }
328
329     // parameter does not exist
330     if (!query.match(/[\?&]lang=/)) {
331       return query + "&lang=" + lang;
332     }
333
334     // replace existing parameter
335     query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
336     query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
337     return query;
338   }
339 });
340
341
342 mkws.registerWidgetType('motd', function() {
343   var container = this.team.widget('motd-container');
344   if (container) {
345     // Move the MOTD from the provided element down into the container
346     this.node.appendTo(container.node);
347   }
348 });
349
350
351 // This widget has no functionality of its own, but its configuration
352 // is copied up into its team, allowing it to affect other widgets in
353 // the team.
354 //
355 mkws.registerWidgetType('config', function() {
356   var c = this.config;
357   for (var name in c) {
358     if (c.hasOwnProperty(name)) {
359       this.team.config[name] = c[name];
360       this.log(this + " copied property " + name + "='" + c[name] + "' up to team");
361     }
362   }
363 });
364
365
366 mkws.registerWidgetType('progress', function() {
367   var that = this;
368   this.node.hide();
369   this.team.queue("stat").subscribe(function(data) {
370     var template = that.team.loadTemplate(that.config.template || "progress");
371     that.node.html(template({
372       done: data.clients - data.activeclients,
373       waiting: data.activeclients
374     }));
375     that.node.show();
376   });
377 });
378
379
380 // Some elements have mkws* classes that makes them appear as widgets
381 // -- for example, because we want to style them using CSS -- but have
382 // no actual functionality. We register these to prevent ignorable
383 // warnings when they occur.
384
385 mkws.registerWidgetType('query', function() {});
386 mkws.registerWidgetType('motd-container', function() {});
387 mkws.registerWidgetType('button', function() {});
388
389
390 })(mkws.$); // jQuery wrapper