update copyright year
[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.name = data[i].name;
23       cur.id = data[i].id;
24       cur.hits = data[i].hits;
25       cur.diagnostic = data[i].diagnostic;
26       cur.message = data[i].message;
27       cur.records = data[i].records;
28       cur.state = data[i].state.replace(/^Client_/, '');
29       cleandata.push(cur);
30     }
31
32     cleandata.sort(function(a,b) { return a.name.localeCompare(b.name) });
33
34     var template = that.team.loadTemplate(that.config.template || "targets");
35     that.node.html(template({data: cleandata}));
36   });
37 });
38
39
40 mkws.registerWidgetType('stat', function() {
41   var that = this;
42   this.team.queue("stat").subscribe(function(data) {
43     var template = that.team.loadTemplate(that.config.template || "stat");
44     that.node.html(template(data));
45   });
46 });
47
48
49 mkws.registerWidgetType('pager', function() {
50   var that = this;
51   var M = mkws.M;
52
53   this.team.queue("pager").subscribe(function(data) {
54     var teamName = that.team.name();
55     var output = {};
56     output.first = data.start + 1;
57     output.last = data.start + data.num;
58     output.count = data.merged;
59     output.found = data.total;
60
61     //client indexes pages from 1 but pz2 from 0
62     var onsides = 5;
63     var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
64     var currentPage = that.team.currentPage();
65
66     var firstClkbl = (currentPage - onsides > 0)
67       ? currentPage - onsides
68       : 1;
69     var lastClkbl = firstClkbl + 2*onsides < pages
70       ? firstClkbl + 2*onsides
71       : pages;
72
73     if (firstClkbl > 1) output.morePrev = true;
74     if (lastClkbl < pages) output.moreNext = true;
75
76     if (currentPage > 1) output.prevClick = "mkws.pagerPrev(\'" + teamName + "\');";
77
78     output.pages = [];
79     for(var i = firstClkbl; i <= lastClkbl; i++) {
80       var o = {};
81       o.number = i;
82       if (i !== currentPage) {
83         o.click = "mkws.showPage(\'" + teamName + "\', " + i + ");";
84       }
85       output.pages.push(o);
86     }
87
88     if (pages - currentPage > 0) output.nextClick = "mkws.pagerNext(\'" + teamName + "\')";
89
90     var template = that.team.loadTemplate(that.config.template || "pager");
91     that.node.html(template(output));
92   });
93 });
94
95 mkws.registerWidgetType('details', function() {
96   var that = this;
97   var recid = that.node.attr("data-mkws-recid");
98   if (this.team.gotRecords()) { 
99     that.team.fetchDetails(recid);
100   } else {
101     this.team.queue("firstrecords").subscribe(function() {
102       that.team.fetchDetails(recid);
103     });
104   }
105   this.team.queue("record").subscribe(function(data) {
106     if ($.inArray(recid, data.recid) > -1) {
107       var template = that.team.loadTemplate(that.config.template || "details");
108       that.node.html(template(data));
109     }
110   });
111 });
112
113 mkws.registerWidgetType('records', function() {
114   var that = this;
115   var team = this.team;
116
117   this.team.queue("searchtriggered").subscribe(function() {
118     var op = that.config.newsearch_opacity;
119     if (op !== undefined) { that.node.fadeTo(500, op); }
120   });
121
122   this.team.queue("records").subscribe(function(data) {
123     that.node.css('opacity', 1);
124     for (var i = 0; i < data.hits.length; i++) {
125       var hit = data.hits[i];
126       hit.detailLinkId = team.recordElementId(hit.recid[0]);
127       hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;";
128       hit.containerClass = "mkws-summary mkwsSummary mkws-team-" + team.name();
129       hit.containerClass += " " + hit.detailLinkId;
130       // ### At some point, we may be able to move the
131       // m_currentRecordId and m_currentRecordData members
132       // from the team object into this widget.
133       if (hit.recid == team.currentRecordId()) {
134         if (team.currentRecordData()) {
135           hit.renderedDetails = team.renderDetails(team.currentRecordData());
136         } 
137       }
138     }
139     var template = team.loadTemplate(that.config.template || "records");
140     var summaryPartial = team.loadTemplate(that.config['summary-template'] || "summary");
141     var tdata = $.extend({}, {"hits": data.hits}, that.config.template_vars);
142     that.node.html(template(tdata, {"partials":{"summary":summaryPartial}}));
143   });
144
145   that.autosearch();
146 });
147
148
149 mkws.registerWidgetType('navi', function() {
150   var that = this;
151   var teamName = this.team.name();
152
153   this.team.queue("searchtriggered").subscribe(function() {
154     var filters = that.team.filters();
155     var output = {filters:[]};
156
157     filters.visitTargets(function(id, name) {
158       var cur = {};
159       cur.facet = 'source';
160       cur.value = name;
161       cur.click = "mkws.delimitTarget('" + teamName + "', '" + id + "'); return false;";
162       output.filters.push(cur);
163     });
164
165     filters.visitFields(function(field, value) {
166       var cur = {};
167       cur.facet = field;
168       cur.value = value;
169       cur.click = "mkws.delimitQuery('" + teamName + "', '" + field + "', '" + value + "'" + ");return false;";
170       output.filters.push(cur);
171     });
172
173     var template = that.team.loadTemplate(that.config.template || "navi");
174     that.node.html(template(output));
175   });
176 });
177
178
179 // It seems this and the Perpage widget doen't need to subscribe to
180 // anything, since they produce events rather than consuming them.
181 //
182 mkws.registerWidgetType('sort', function() {
183   var that = this;
184
185   this.node.change(function() {
186     that.team.set_sortOrder(that.node.val());
187     if (that.team.submitted()) {
188       that.team.reShow();
189     }
190     return false;
191   });
192 });
193
194
195 mkws.registerWidgetType('per-page', function() {
196   var that = this;
197
198   this.node.change(function() {
199     that.team.set_perpage(that.node.val());
200     if (that.team.submitted()) {
201       that.team.reShow();
202     }
203     return false;
204   });
205 });
206
207
208 mkws.registerWidgetType('done', function() {
209   var that = this;
210   this.team.queue("complete").subscribe(function(n) {
211     var template = that.team.loadTemplate(that.config.template || "done");
212     that.node.html(template({count: n}));
213   });
214 });
215
216
217 mkws.registerWidgetType('switch', function() {
218   if (!this.config.show_switch) return;
219   var tname = this.team.name();
220   var output = {};
221   output.recordClick = "mkws.switchView(\'" + tname + "\', \'records\')";
222   output.targetClick = "mkws.switchView(\'" + tname + "\', \'targets\')";
223   var template = this.team.loadTemplate(this.config.template || "switch");
224   this.node.html(template(output));
225   this.hideWhenNarrow();
226 });
227
228
229 mkws.registerWidgetType('search', function() {
230   var output = {};
231   output.team = this.team.name();
232   var template = this.team.loadTemplate(this.config.template || "search");
233   this.node.html(template(output));
234 });
235
236
237 mkws.registerWidgetType('search-form', function() {
238   var team = this.team;
239   this.node.submit(function() {
240     var val = team.widget('query').value();
241     team.newSearch(val);
242     return false;
243   });
244 });
245
246
247 mkws.registerWidgetType('results', function() {
248   var template = this.team.loadTemplate(this.config.template || "results");
249   this.node.html(template({team: this.team.name()}));
250   this.autosearch();
251 });
252
253
254 mkws.registerWidgetType('ranking', function() {
255   var output = {};
256   output.perPage = [];
257   output.sort = [];
258   output.team = this.team.name();
259   output.showSort = this.config.show_sort;
260   output.showPerPage = this.config.show_perpage;
261
262   var order = this.team.sortOrder();
263   this.info("making sort, sortOrder = '" + order + "'");
264   for (var i = 0; i < this.config.sort_options.length; i++) {
265     var cur = {};
266     var opt = this.config.sort_options[i];
267     cur.key = opt[0];
268     cur.label = opt.length == 1 ? opt[0] : mkws.M(opt[1]);
269     if (order == cur.key || order == cur.label) cur.selected = true;
270     output.sort.push(cur);
271   }
272
273   var perpage = this.team.perpage();
274   this.info("making perpage, perpage = " + perpage);
275   for(var i = 0; i < this.config.perpage_options.length; i++) {
276     var cur = {};
277     cur.perPage = this.config.perpage_options[i];
278     if (cur.perPage == perpage) cur.selected = true;
279     output.perPage.push(cur);
280   }
281
282   var template = this.team.loadTemplate(this.config.template || "ranking");
283   this.node.html(template(output));
284 });
285
286
287 mkws.registerWidgetType('lang', function() {
288   // dynamic URL or static page? /path/foo?query=test
289   /* create locale language menu */
290   if (!this.config.show_lang) return;
291
292   var lang_default = "en";
293   var lang = this.config.lang || lang_default;
294   var list = [];
295
296   /* display a list of configured languages, or all */
297   var lang_options = this.config.lang_options;
298   var toBeIncluded = {};
299   for (var i = 0; i < lang_options.length; i++) {
300     toBeIncluded[lang_options[i]] = true;
301   }
302
303   for (var k in mkws.locale_lang) {
304     if (toBeIncluded[k] || lang_options.length == 0) {
305       cur = {};
306       if (lang === k) cur.selected = true;
307       cur.code = k;
308       cur.url = lang_url(k);
309       list.push(cur);
310     }
311   }
312
313   // add english link
314   if (lang_options.length == 0 || toBeIncluded[lang_default]) {
315       cur = {};
316       if (lang === lang_default) cur.selected = true;
317       cur.code = lang_default;
318       cur.url = lang_url(lang_default);
319       list.push(cur);
320   }
321
322   this.info("language menu: " + list.join(", "));
323
324   var template = this.team.loadTemplate(this.config.template || "lang");
325   this.node.html(template({languages: list}));
326   this.hideWhenNarrow();
327
328   // set or re-set "lang" URL parameter
329   function lang_url(lang) {
330     var query = location.search;
331     // no query parameters? done
332     if (!query) {
333       return "?lang=" + lang;
334     }
335
336     // parameter does not exist
337     if (!query.match(/[\?&]lang=/)) {
338       return query + "&lang=" + lang;
339     }
340
341     // replace existing parameter
342     query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
343     query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
344     return query;
345   }
346 });
347
348
349 mkws.registerWidgetType('motd', function() {
350   var container = this.team.widget('motd-container');
351   if (container) {
352     // Move the MOTD from the provided element down into the container
353     this.node.appendTo(container.node);
354   }
355 });
356
357
358 // This widget has no functionality of its own, but its configuration
359 // is copied up into its team, allowing it to affect other widgets in
360 // the team.
361 //
362 mkws.registerWidgetType('config', function() {
363   var c = this.config;
364   for (var name in c) {
365     if (c.hasOwnProperty(name)) {
366       this.team.config[name] = c[name];
367       this.info(this + " copied property " + name + "='" + c[name] + "' up to team");
368     }
369   }
370 });
371
372
373 mkws.registerWidgetType('progress', function() {
374   var that = this;
375   this.node.hide();
376   this.team.queue("stat").subscribe(function(data) {
377     var template = that.team.loadTemplate(that.config.template || "progress");
378     that.node.html(template({
379       done: data.clients - data.activeclients,
380       waiting: data.activeclients
381     }));
382     that.node.show();
383   });
384 });
385
386
387 mkws.registerWidgetType('waiting', function() {
388   var that = this;
389
390   this.node.css("visibility", "hidden");
391   var template = that.team.loadTemplate(that.config.template || "waiting");
392   this.node.html(template({
393     src: this.config.src || "http://mkws.indexdata.com/progress.gif"
394   }));
395
396   this.team.queue("searchtriggered").subscribe(function(data) {
397     that.node.css("visibility", "visible");
398   });
399   this.team.queue("complete").subscribe(function(n) {
400     that.node.css("visibility", "hidden");
401   });
402 });
403
404
405 // Some elements have mkws* classes that makes them appear as widgets
406 // -- for example, because we want to style them using CSS -- but have
407 // no actual functionality. We register these to prevent ignorable
408 // warnings when they occur.
409
410 mkws.registerWidgetType('query', function() {});
411 mkws.registerWidgetType('motd-container', function() {});
412 mkws.registerWidgetType('button', function() {});
413
414
415 })(mkws.$); // jQuery wrapper