224595ef52b4d66a83305c4fc84181820bb3234d
[mkws-moved-to-github.git] / src / mkws-widget-main.js
1 // Functions follow for promoting the regular widget object into
2 // widgets of specific types. These could be moved into their own
3 // source files.
4
5
6 mkws.registerWidgetType('Targets', function() {
7   if (!this.config.show_switch) return;
8   var that = this;
9
10   this.node.html('No information available yet.');
11   this.node.css("display", "none");
12
13   this.team.queue("targets").subscribe(function(data) {
14     // There is a bug in pz2.js
15     var cleandata = [];
16     for (var i = 0; i < data.length; i++) {
17       var cur = {};
18       cur.id = data[i].id;
19       cur.hits = data[i].hits;
20       cur.diagnostic = data[i].diagnostic;
21       cur.records = data[i].records;
22       cur.state = data[i].state;
23       cleandata.push(cur);
24     }
25
26     var template = that.team.loadTemplate(that.config.template || "Targets");
27     that.node.html(template({data: cleandata}));
28   });
29 });
30
31
32 mkws.registerWidgetType('Stat', function() {
33   var that = this;
34   var M = mkws.M;
35
36   this.team.queue("stat").subscribe(function(data) {
37     that.node.html(' -- ' +
38                       '<span class="mkwsClientCount">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
39                       ' -- ' +
40                       M('Retrieved records') + ': ' + data.records + '/' + data.hits);
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     that.node.html(drawPager(data))
51
52     function drawPager(data) {
53       var teamName = that.team.name();
54       var s = '<div style="float: right">' + M('Displaying') + ': '
55         + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
56         ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
57         + data.total + ')</div>';
58
59       //client indexes pages from 1 but pz2 from 0
60       var onsides = 6;
61       var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
62       var currentPage = that.team.currentPage();
63
64       var firstClkbl = (currentPage - onsides > 0)
65         ? currentPage - onsides
66         : 1;
67
68       var lastClkbl = firstClkbl + 2*onsides < pages
69         ? firstClkbl + 2*onsides
70         : pages;
71
72       var prev = '<span class="mkwsPrev">&#60;&#60; ' + M('Prev') + '</span> | ';
73       if (currentPage > 1)
74         prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
75         +'&#60;&#60; ' + M('Prev') + '</a> | ';
76
77       var middle = '';
78       for(var i = firstClkbl; i <= lastClkbl; i++) {
79         var numLabel = i;
80         if(i == currentPage)
81           numLabel = '<span class="mkwsCurrentPage">' + i + '</span>';
82
83         middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
84           + numLabel + ' </a>';
85       }
86
87       var next = ' | <span class="mkwsNext">' + M('Next') + ' &#62;&#62;</span>';
88       if (pages - currentPage > 0)
89         next = ' | <a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
90         + M('Next') + ' &#62;&#62;</a>';
91
92       var predots = '';
93       if (firstClkbl > 1)
94         predots = '...';
95
96       var postdots = '';
97       if (lastClkbl < pages)
98         postdots = '...';
99
100       s += '<div style="float: clear">'
101         + prev + predots + middle + postdots + next + '</div>';
102
103       return s;
104     }
105   });
106 });
107
108
109 mkws.registerWidgetType('Records', function() {
110   var that = this;
111   var team = this.team;
112
113   this.team.queue("records").subscribe(function(data) {
114     for (var i = 0; i < data.hits.length; i++) {
115       var hit = data.hits[i];
116       that.team.queue("record").publish(hit);
117       hit.detailLinkId = team.recordElementId(hit.recid[0]);
118       hit.detailClick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
119       hit.containerClass = "mkwsSummary mkwsTeam_" + 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           console.log(hit.renderedDetails); 
128         } 
129       }
130     }
131     var template = team.loadTemplate(that.config.template || "Records");
132     var targs = $.extend({}, {"hits": data.hits}, that.config.template_vars);
133     that.node.html(template(targs));
134   });
135
136   that.autosearch();
137 });
138
139
140 mkws.registerWidgetType('Navi', function() {
141   var that = this;
142   var teamName = this.team.name();
143   var M = mkws.M;
144
145   this.team.queue("navi").subscribe(function() {
146     var filters = that.team.filters();
147     var text = "";
148
149     filters.visitTargets(function(id, name) {
150       if (text) text += " | ";
151       text += M('source') + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitTarget(\'' + teamName +
152         "', '" + id + "'" + ');return false;">' + name + '</a>';
153     });
154
155     filters.visitFields(function(field, value) {
156       if (text) text += " | ";
157       text += M(field) + ': <a class="mkwsRemovable" href="#" onclick="mkws.delimitQuery(\'' + teamName +
158         "', '" + field + "', '" + value + "'" +
159         ');return false;">' + value + '</a>';
160     });
161
162     that.node.html(text);
163   });
164 });
165
166
167 // It seems this and the Perpage widget doen't need to subscribe to
168 // anything, since they produce events rather than consuming them.
169 //
170 mkws.registerWidgetType('Sort', function() {
171   var that = this;
172
173   this.node.change(function() {
174     that.team.set_sortOrder(that.node.val());
175     if (that.team.submitted()) {
176       that.team.reShow();
177     }
178     return false;
179   });
180 });
181
182
183 mkws.registerWidgetType('Perpage', function() {
184   var that = this;
185
186   this.node.change(function() {
187     that.team.set_perpage(that.node.val());
188     if (that.team.submitted()) {
189       that.team.reShow();
190     }
191     return false;
192   });
193 });
194
195
196 mkws.registerWidgetType('Done', function() {
197   var that = this;
198
199   this.team.queue("complete").subscribe(function(n) {
200     that.node.html("Search complete: found " + n + " records");
201   });
202 });
203
204
205 mkws.registerWidgetType('Switch', function() {
206   if (!this.config.show_switch) return;
207   var tname = this.team.name();
208   this.node.html('\
209 <a href="#" onclick="mkws.switchView(\'' + tname + '\', \'records\')">Records</a><span> \
210 | \
211 </span><a href="#" onclick="mkws.switchView(\'' + tname + '\', \'targets\')">Targets</a>');
212   this.hideWhenNarrow();
213 });
214
215
216 mkws.registerWidgetType('Search', function() {
217   var tname = this.team.name();
218   var M = mkws.M;
219
220   this.node.html('\
221 <form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
222   <input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + this.config.query_width + '" />\
223   <input class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
224 </form>');
225 });
226
227
228 mkws.registerWidgetType('SearchForm', 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 tname = this.team.name();
240
241   this.node.html('\
242 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
243   <tr>\
244     <td class="mkwsTermlists-Container-wide mkwsTeam_' + tname + '" width="250" valign="top">\
245       <div class="mkwsTermlists mkwsTeam_' + tname + '"></div>\
246     </td>\
247     <td class="mkwsMOTDContainer mkwsTeam_' + tname + '" valign="top">\
248       <div class="mkwsRanking mkwsTeam_' + tname + '"></div>\
249       <div class="mkwsPager mkwsTeam_' + tname + '"></div>\
250       <div class="mkwsNavi mkwsTeam_' + tname + '"></div>\
251       <div class="mkwsRecords mkwsTeam_' + tname + '"></div>\
252     </td>\
253   </tr>\
254   <tr>\
255     <td colspan="2">\
256       <div class="mkwsTermlists-Container-narrow mkwsTeam_' + tname + '"></div>\
257     </td>\
258   </tr>\
259 </table>');
260
261   this.autosearch();
262 });
263
264
265 mkws.registerWidgetType('Ranking', function() {
266   var tname = this.team.name();
267   var that = this;
268   var M = mkws.M;
269
270   var s = '<form>';
271   if (this.config.show_sort) {
272     s +=  M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
273   }
274   if (this.config.show_perpage) {
275     s += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
276   }
277   s += '</form>';
278
279   this.node.html(s);
280
281
282   function mkwsHtmlSort() {
283     var order = that.team.sortOrder();
284
285     that.log("making sort HTML, sortOrder = '" + order + "'");
286     var sort_html = '<select class="mkwsSort mkwsTeam_' + tname + '">';
287
288     for(var i = 0; i < that.config.sort_options.length; i++) {
289       var opt = that.config.sort_options[i];
290       var key = opt[0];
291       var val = opt.length == 1 ? opt[0] : opt[1];
292
293       sort_html += '<option value="' + key + '"';
294       if (order == key || order == val) {
295         sort_html += ' selected="selected"';
296       }
297       sort_html += '>' + M(val) + '</option>';
298     }
299     sort_html += '</select>';
300
301     return sort_html;
302   }
303
304   function mkwsHtmlPerpage() {
305     var perpage = that.team.perpage();
306
307     that.log("making perpage HTML, perpage = " + perpage);
308     var perpage_html = '<select class="mkwsPerpage mkwsTeam_' + tname + '">';
309
310     for(var i = 0; i < that.config.perpage_options.length; i++) {
311       var key = that.config.perpage_options[i];
312
313       perpage_html += '<option value="' + key + '"';
314       if (key == perpage) {
315         perpage_html += ' selected="selected"';
316       }
317       perpage_html += '>' + key + '</option>';
318     }
319     perpage_html += '</select>';
320
321     return perpage_html;
322   }
323 });
324
325
326 mkws.registerWidgetType('Lang', function() {
327   // dynamic URL or static page? /path/foo?query=test
328   /* create locale language menu */
329   if (!this.config.show_lang) return;
330
331   var lang_default = "en";
332   var lang = this.config.lang || lang_default;
333   var list = [];
334
335   /* display a list of configured languages, or all */
336   var lang_options = this.config.lang_options || [];
337   var toBeIncluded = {};
338   for (var i = 0; i < lang_options.length; i++) {
339     toBeIncluded[lang_options[i]] = true;
340   }
341
342   for (var k in mkws.locale_lang) {
343     if (toBeIncluded[k] || lang_options.length == 0)
344       list.push(k);
345   }
346
347   // add english link
348   if (lang_options.length == 0 || toBeIncluded[lang_default])
349     list.push(lang_default);
350
351   this.log("language menu: " + list.join(", "));
352
353   /* the HTML part */
354   var data = "";
355   for (var i = 0; i < list.length; i++) {
356     var l = list[i];
357     if (data)
358       data += ' | ';
359
360     if (lang == l) {
361       data += ' <span>' + l + '</span> ';
362     } else {
363       data += ' <a href="' + lang_url(l) + '">' + l + '</a> '
364     }
365   }
366
367   this.node.html(data);
368   this.hideWhenNarrow();
369
370
371   // set or re-set "lang" URL parameter
372   function lang_url(lang) {
373     var query = location.search;
374     // no query parameters? done
375     if (!query) {
376       return "?lang=" + lang;
377     }
378
379     // parameter does not exist
380     if (!query.match(/[\?&]lang=/)) {
381       return query + "&lang=" + lang;
382     }
383
384     // replace existing parameter
385     query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
386     query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
387     return query;
388   }
389 });
390
391
392 mkws.registerWidgetType('MOTD', function() {
393   var container = this.team.widget('MOTDContainer');
394   if (container) {
395     // Move the MOTD from the provided element down into the container
396     this.node.appendTo(container.node);
397   }
398 });
399
400
401 // This widget has no functionality of its own, but its configuration
402 // is copied up into its team, allowing it to affect other widgets in
403 // the team.
404 //
405 mkws.registerWidgetType('Config', function() {
406   var c = this.config;
407   for (var name in c) {
408     if (c.hasOwnProperty(name)) {
409       this.team.config[name] = c[name];
410       this.log(this + " copied property " + name + "='" + c[name] + "' up to team");
411     }
412   }
413 });
414
415
416 mkws.registerWidgetType('Progress', function() {
417   var that = this;
418
419   this.node.hide();
420   this.team.queue("stat").subscribe(function(data) {
421     var s = '<span class="mkwsDone">';
422     for (var i = 0; i < data.clients; i++) {
423       if (i == data.clients - data.activeclients) {
424         s += '</span>';
425         s += '<span class="mkwsWaiting">';
426       }
427       s += '&#x2588;';
428     }
429     s += '</span>';
430     that.node.html(s);
431     that.node.show();
432   });
433 });
434
435
436 // Some elements have mkws* classes that makes them appear as widgets
437 // -- for example, because we want to style them using CSS -- but have
438 // no actual functionality. We register these to prevent ignorable
439 // warnings when they occur.
440
441 mkws.registerWidgetType('Query', function() {});
442 mkws.registerWidgetType('MOTDContainer', function() {});
443 mkws.registerWidgetType('Button', function() {});
444
445