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