Wrap mkws-widget-main.js to allow jQuery use.
[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 || "Record");
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 = "mkwsSummary mkwsTeam_" + 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           console.log(hit.renderedDetails); 
130         } 
131       }
132     }
133     var template = team.loadTemplate(that.config.template || "Records");
134     var targs = $.extend({}, {"hits": data.hits}, that.config.template_vars);
135     that.node.html(template(targs));
136   });
137
138   that.autosearch();
139 });
140
141
142 mkws.registerWidgetType('Navi', function() {
143   var that = this;
144   var teamName = this.team.name();
145
146   this.team.queue("navi").subscribe(function() {
147     var filters = that.team.filters();
148     var output = {filters:[]};
149
150     filters.visitTargets(function(id, name) {
151       var cur = {};
152       cur.facet = 'source';
153       cur.value = name;
154       cur.click = "mkws.delimitTarget('" + teamName + "', '" + id + "'); return false;";
155       output.filters.push(cur);
156     });
157
158     filters.visitFields(function(field, value) {
159       var cur = {};
160       cur.facet = field;
161       cur.value = value;
162       cur.click = "mkws.delimitQuery('" + teamName + "', '" + field + "', '" + value + "'" + ");return false;";
163       output.filters.push(cur);
164     });
165
166     var template = that.team.loadTemplate(that.config.template || "Navi");
167     that.node.html(template(output));
168   });
169 });
170
171
172 // It seems this and the Perpage widget doen't need to subscribe to
173 // anything, since they produce events rather than consuming them.
174 //
175 mkws.registerWidgetType('Sort', function() {
176   var that = this;
177
178   this.node.change(function() {
179     that.team.set_sortOrder(that.node.val());
180     if (that.team.submitted()) {
181       that.team.reShow();
182     }
183     return false;
184   });
185 });
186
187
188 mkws.registerWidgetType('Perpage', function() {
189   var that = this;
190
191   this.node.change(function() {
192     that.team.set_perpage(that.node.val());
193     if (that.team.submitted()) {
194       that.team.reShow();
195     }
196     return false;
197   });
198 });
199
200
201 mkws.registerWidgetType('Done', function() {
202   var that = this;
203   this.team.queue("complete").subscribe(function(n) {
204     var template = that.team.loadTemplate(that.config.template || "Done");
205     that.node.html(template({count: n}));
206   });
207 });
208
209
210 mkws.registerWidgetType('Switch', function() {
211   if (!this.config.show_switch) return;
212   var tname = this.team.name();
213   var output = {};
214   output.recordClick = "mkws.switchView(\'" + tname + "\', \'records\')";
215   output.targetClick = "mkws.switchView(\'" + tname + "\', \'targets\')";
216   var template = this.team.loadTemplate(this.config.template || "Switch");
217   this.node.html(template(output));
218   this.hideWhenNarrow();
219 });
220
221
222 mkws.registerWidgetType('Search', function() {
223   var output = {};
224   output.team = this.team.name();
225   output.queryWidth = this.config.query_width;
226   var template = this.team.loadTemplate(this.config.template || "Search");
227   this.node.html(template(output));
228 });
229
230
231 mkws.registerWidgetType('SearchForm', function() {
232   var team = this.team;
233   this.node.submit(function() {
234     var val = team.widget('Query').value();
235     team.newSearch(val);
236     return false;
237   });
238 });
239
240
241 mkws.registerWidgetType('Results', function() {
242   var template = this.team.loadTemplate(this.config.template || "Results");
243   this.node.html(template({team: this.team.name()}));
244   this.autosearch();
245 });
246
247
248 mkws.registerWidgetType('Ranking', function() {
249   var output = {};
250   output.perPage = [];
251   output.sort = [];
252   output.team = this.team.name();
253   output.showSort = this.config.show_sort;
254   output.showPerPage = this.config.show_perpage;
255
256   var order = this.team.sortOrder();
257   this.log("making sort, sortOrder = '" + order + "'");
258   for (var i = 0; i < this.config.sort_options.length; i++) {
259     var cur = {};
260     var opt = this.config.sort_options[i];
261     cur.key = opt[0];
262     cur.label = opt.length == 1 ? opt[0] : opt[1];
263     if (order == cur.key || order == cur.label) cur.selected = true;
264     output.sort.push(cur);
265   }
266
267   var perpage = this.team.perpage();
268   this.log("making perpage, perpage = " + perpage);
269   for(var i = 0; i < this.config.perpage_options.length; i++) {
270     var cur = {};
271     cur.perPage = this.config.perpage_options[i];
272     if (cur.perPage == perpage) cur.selected = true;
273     output.perPage.push(cur);
274   }
275
276   var template = this.team.loadTemplate(this.config.template || "Ranking");
277   this.node.html(template(output));
278 });
279
280
281 mkws.registerWidgetType('Lang', function() {
282   // dynamic URL or static page? /path/foo?query=test
283   /* create locale language menu */
284   if (!this.config.show_lang) return;
285
286   var lang_default = "en";
287   var lang = this.config.lang || lang_default;
288   var list = [];
289
290   /* display a list of configured languages, or all */
291   var lang_options = this.config.lang_options || [];
292   var toBeIncluded = {};
293   for (var i = 0; i < lang_options.length; i++) {
294     toBeIncluded[lang_options[i]] = true;
295   }
296
297   for (var k in mkws.locale_lang) {
298     if (toBeIncluded[k] || lang_options.length == 0) {
299       cur = {};
300       if (lang === k) cur.selected = true;
301       cur.code = k;
302       cur.url = lang_url(k);
303       list.push(cur);
304     }
305   }
306
307   // add english link
308   if (lang_options.length == 0 || toBeIncluded[lang_default]) {
309       cur = {};
310       if (lang === lang_default) cur.selected = true;
311       cur.code = lang_default;
312       cur.url = lang_url(lang_default);
313       list.push(cur);
314   }
315
316   this.log("language menu: " + list.join(", "));
317
318   var template = this.team.loadTemplate(this.config.template || "Lang");
319   this.node.html(template({languages: list}));
320   this.hideWhenNarrow();
321
322   // set or re-set "lang" URL parameter
323   function lang_url(lang) {
324     var query = location.search;
325     // no query parameters? done
326     if (!query) {
327       return "?lang=" + lang;
328     }
329
330     // parameter does not exist
331     if (!query.match(/[\?&]lang=/)) {
332       return query + "&lang=" + lang;
333     }
334
335     // replace existing parameter
336     query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
337     query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
338     return query;
339   }
340 });
341
342
343 mkws.registerWidgetType('MOTD', function() {
344   var container = this.team.widget('MOTDContainer');
345   if (container) {
346     // Move the MOTD from the provided element down into the container
347     this.node.appendTo(container.node);
348   }
349 });
350
351
352 // This widget has no functionality of its own, but its configuration
353 // is copied up into its team, allowing it to affect other widgets in
354 // the team.
355 //
356 mkws.registerWidgetType('Config', function() {
357   var c = this.config;
358   for (var name in c) {
359     if (c.hasOwnProperty(name)) {
360       this.team.config[name] = c[name];
361       this.log(this + " copied property " + name + "='" + c[name] + "' up to team");
362     }
363   }
364 });
365
366
367 mkws.registerWidgetType('Progress', function() {
368   var that = this;
369   this.node.hide();
370   this.team.queue("stat").subscribe(function(data) {
371     var template = that.team.loadTemplate(that.config.template || "Progress");
372     that.node.html(template({
373       done: data.clients - data.activeclients,
374       waiting: data.activeclients
375     }));
376     that.node.show();
377   });
378 });
379
380
381 // Some elements have mkws* classes that makes them appear as widgets
382 // -- for example, because we want to style them using CSS -- but have
383 // no actual functionality. We register these to prevent ignorable
384 // warnings when they occur.
385
386 mkws.registerWidgetType('Query', function() {});
387 mkws.registerWidgetType('MOTDContainer', function() {});
388 mkws.registerWidgetType('Button', function() {});
389
390
391 })(mkws.$); // jQuery wrapper