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