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