Switch and Lang widgets hide themselves when the window is narrow.
[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('Results', function() {
121     // Nothing to do apart from act as an autosearch trigger
122     // Contained elements do all the real work
123     widget.autosearch(this);
124 });
125
126
127 mkws.registerWidgetType('Records', function() {
128     var that = this;
129     var team = this.team;
130
131     this.team.queue("records").subscribe(function(data) {
132         var html = [];
133         for (var i = 0; i < data.hits.length; i++) {
134             var hit = data.hits[i];
135             that.team.queue("record").publish(hit);
136             var divId = team.recordElementId(hit.recid[0]);
137             html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
138             // ### At some point, we may be able to move the
139             // m_currentRecordId and m_currentRecordData members
140             // from the team object into this widget.
141             if (hit.recid == team.currentRecordId()) {
142                 if (team.currentRecordData())
143                     html.push(team.renderDetails(team.currentRecordData()));
144             }
145         }
146         $(that.node).html(html.join(''));
147
148         function renderSummary(hit) {
149             var template = team.loadTemplate(that.config.template || "Summary");
150             hit._id = team.recordElementId(hit.recid[0]);
151             hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
152             return template(hit);
153         }
154     });
155
156     widget.autosearch(that);
157 });
158
159
160 mkws.registerWidgetType('Navi', function() {
161     var that = this;
162     var teamName = this.team.name();
163     var M = mkws.M;
164
165     this.team.queue("navi").subscribe(function() {
166         var filters = that.team.filters();
167         var text = "";
168
169         filters.visitTargets(function(id, name) {
170             if (text) text += " | ";
171             text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
172                 "', '" + id + "'" + ');return false;">' + name + '</a>';
173         });
174
175         filters.visitFields(function(field, value) {
176             if (text) text += " | ";
177             text += M(field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
178                 "', '" + field + "', '" + value + "'" +
179                 ');return false;">' + value + '</a>';
180         });
181
182         $(that.node).html(text);
183     });
184 });
185
186
187 // It seems this and the Perpage widget doen't need to subscribe to
188 // anything, since they produce events rather than consuming them.
189 //
190 mkws.registerWidgetType('Sort', function() {
191     var that = this;
192
193     $(this.node).change(function() {
194         that.team.set_sortOrder($(that.node).val());
195         if (that.team.submitted()) {
196             that.team.reShow();
197         }
198         return false;
199     });
200 });
201
202
203 mkws.registerWidgetType('Perpage', function() {
204     var that = this;
205
206     $(this.node).change(function() {
207         that.team.set_perpage($(that.node).val());
208         if (that.team.submitted()) {
209             that.team.reShow();
210         }
211         return false;
212     });
213 });
214
215
216 mkws.registerWidgetType('Done', function() {
217     var that = this;
218
219     this.team.queue("complete").subscribe(function(n) {
220         $(that.node).html("Search complete: found " + n + " records");
221     });
222 });
223
224
225 mkws.registerWidgetType('Switch', function() {
226     var tname = this.team.name();
227     $(this.node).html('\
228 <a href="#" onclick="mkws.switchView(\'' + tname + '\', \'records\')">Records</a><span> \
229 | \
230 </span><a href="#" onclick="mkws.switchView(\'' + tname + '\', \'targets\')">Targets</a>');
231     widget.hideWhenNarrow(this);
232 });
233
234
235 mkws.registerWidgetType('Search', function() {
236     var tname = this.team.name();
237     var M = mkws.M;
238
239     $(this.node).html('\
240 <form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
241   <input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + this.config.query_width + '" />\
242   <input class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
243 </form>');
244 });
245
246
247 mkws.registerWidgetType('SearchForm', function() {
248     var team = this.team;    
249     $(this.node).submit(function() {
250         var val = team.widget('Query').value();
251         team.newSearch(val);
252         return false;
253     });
254 });
255
256
257 mkws.registerWidgetType('Results', function() {
258     var tname = this.team.name();
259
260     $(this.node).html('\
261 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
262   <tr>\
263     <td class="mkwsTermlists-Container-wide mkwsTeam_' + tname + '" width="250" valign="top">\
264       <div class="mkwsTermlists mkwsTeam_' + tname + '"></div>\
265     </td>\
266     <td class="mkwsMOTDContainer mkwsTeam_' + tname + '" valign="top">\
267       <div class="mkwsRanking mkwsTeam_' + tname + '"></div>\
268       <div class="mkwsPager mkwsTeam_' + tname + '"></div>\
269       <div class="mkwsNavi mkwsTeam_' + tname + '"></div>\
270       <div class="mkwsRecords mkwsTeam_' + tname + '"></div>\
271     </td>\
272   </tr>\
273   <tr>\
274     <td colspan="2">\
275       <div class="mkwsTermlists-Container-narrow mkwsTeam_' + tname + '"></div>\
276     </td>\
277   </tr>\
278 </table>');
279 });
280
281
282 mkws.registerWidgetType('Ranking', function() {
283     var tname = this.team.name();
284     var that = this;
285     var M = mkws.M;
286
287     var s = '<form name="mkwsSelect" class="mkwsSelect mkwsTeam_' + tname + '" action="" >';
288     if (this.config.show_sort) {
289         s +=  M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
290     }
291     if (this.config.show_perpage) {
292         s += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
293     }
294     s += '</form>';
295
296     $(this.node).html(s);
297
298
299     function mkwsHtmlSort() {
300         var order = that.team.sortOrder();
301
302         that.log("HTML sort, sortOrder = '" + order + "'");
303         var sort_html = '<select class="mkwsSort mkwsTeam_' + tname + '">';
304
305         for(var i = 0; i < that.config.sort_options.length; i++) {
306             var opt = that.config.sort_options[i];
307             var key = opt[0];
308             var val = opt.length == 1 ? opt[0] : opt[1];
309
310             sort_html += '<option value="' + key + '"';
311             if (order == key || order == val) {
312                 sort_html += ' selected="selected"';
313             }
314             sort_html += '>' + M(val) + '</option>';
315         }
316         sort_html += '</select>';
317
318         return sort_html;
319     }
320
321     function mkwsHtmlPerpage() {
322         var perpage = that.team.perpage();
323
324         that.log("HTML perpage, perpage = " + perpage);
325         var perpage_html = '<select class="mkwsPerpage mkwsTeam_' + tname + '">';
326
327         for(var i = 0; i < that.config.perpage_options.length; i++) {
328             var key = that.config.perpage_options[i];
329
330             perpage_html += '<option value="' + key + '"';
331             if (key == perpage) {
332                 perpage_html += ' selected="selected"';
333             }
334             perpage_html += '>' + key + '</option>';
335         }
336         perpage_html += '</select>';
337
338         return perpage_html;
339     }
340 });
341
342
343 mkws.registerWidgetType('Lang', function() {
344     // dynamic URL or static page? /path/foo?query=test
345     /* create locale language menu */
346     if (!this.config.show_lang) return;
347
348     var lang_default = "en";
349     var lang = this.config.lang || lang_default;
350     var list = [];
351
352     /* display a list of configured languages, or all */
353     var lang_options = this.config.lang_options || [];
354     var toBeIncluded = {};
355     for (var i = 0; i < lang_options.length; i++) {
356         toBeIncluded[lang_options[i]] = true;
357     }
358
359     for (var k in mkws.locale_lang) {
360         if (toBeIncluded[k] || lang_options.length == 0)
361             list.push(k);
362     }
363
364     // add english link
365     if (lang_options.length == 0 || toBeIncluded[lang_default])
366         list.push(lang_default);
367
368     this.log("Language menu for: " + list.join(", "));
369
370     /* the HTML part */
371     var data = "";
372     for (var i = 0; i < list.length; i++) {
373         var l = list[i];
374         if (data)
375             data += ' | ';
376
377         if (lang == l) {
378             data += ' <span>' + l + '</span> ';
379         } else {
380             data += ' <a href="' + lang_url(l) + '">' + l + '</a> '
381         }
382     }
383
384     $(this.node).html(data);
385     widget.hideWhenNarrow(this);
386
387
388     // set or re-set "lang" URL parameter
389     function lang_url(lang) {
390         var query = location.search;
391         // no query parameters? done
392         if (!query) {
393             return "?lang=" + lang;
394         }
395
396         // parameter does not exist
397         if (!query.match(/[\?&]lang=/)) {
398             return query + "&lang=" + lang;
399         }
400
401         // replace existing parameter
402         query = query.replace(/\?lang=([^&#;]*)/, "?lang=" + lang);
403         query = query.replace(/\&lang=([^&#;]*)/, "&lang=" + lang);
404         return query;
405     }
406 });
407
408
409 mkws.registerWidgetType('MOTD', function() {
410     var container = this.team.widget('MOTDContainer');
411     if (container) {
412         // Move the MOTD from the provided element down into the container
413         $(this.node).appendTo(container.node);
414     }
415 });
416
417
418 // Some elements have mkws* classes that makes them appear as widgets
419 // -- for example, because we want to style them using CSS -- but have
420 // no actual functionality. We register these to prevent ignorable
421 // warnings when they occur.
422
423 mkws.registerWidgetType('Query', function() {});
424 mkws.registerWidgetType('MOTDContainer', function() {});
425 mkws.registerWidgetType('Button', function() {});
426 mkws.registerWidgetType('Popup', function() {});
427
428 // Not sure whether the following should have functionality:
429 // Select               HTMLFormElement
430 // *-Container-wide     HTMLTableCellElement
431 // *-Container-narrow   HTMLDivElement
432 // Bytarget             HTMLDivElement