Copy mkws_config into mkws.config, and thereafter use that
[mkws-moved-to-github.git] / src / mkws-widgets.js
1 // Factory function for widget objects.
2 function widget($, team, type, node) {
3     var that = {
4         team: team,
5         type: type,
6         node: node
7     };
8
9     function log(s) {
10         team.log(s);
11     }
12     that.log = log;
13
14     that.toString = function() {
15         return '[Widget ' + team.name() + ':' + type + ']';
16     };
17
18     var fn = mkws.promotionFunction(type);
19     if (fn) {
20         fn.call(that);
21         log("made " + type + " widget(node=" + node + ")");
22     } else {
23         log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
24     }
25
26     return that;
27 }
28
29
30 // Functions follow for promoting the regular widget object into
31 // widgets of specific types. These could be moved into their own
32 // source files.
33
34
35 mkws.registerWidgetType('Targets', function() {
36     var that = this;
37     var M = mkws.M;
38
39     this.team.queue("targets").subscribe(function(data) {
40         var table ='<table><thead><tr>' +
41             '<td>' + M('Target ID') + '</td>' +
42             '<td>' + M('Hits') + '</td>' +
43             '<td>' + M('Diags') + '</td>' +
44             '<td>' + M('Records') + '</td>' +
45             '<td>' + M('State') + '</td>' +
46             '</tr></thead><tbody>';
47
48         for (var i = 0; i < data.length; i++) {
49             table += "<tr><td>" + data[i].id +
50                 "</td><td>" + data[i].hits +
51                 "</td><td>" + data[i].diagnostic +
52                 "</td><td>" + data[i].records +
53                 "</td><td>" + data[i].state + "</td></tr>";
54         }
55
56         table += '</tbody></table>';
57         var subnode = $(that.node).children('.mkwsBytarget');
58         subnode.html(table);
59     });
60 });
61
62
63 mkws.registerWidgetType('Stat', function() {
64     var that = this;
65     var M = mkws.M;
66
67     this.team.queue("stat").subscribe(function(data) {
68         if (that.node.length === 0)  alert("huh?!");
69
70         $(that.node).html('<span class="head">' + M('Status info') + '</span>' +
71             ' -- ' +
72             '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
73             ' -- ' +
74             '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
75     });
76 });
77
78
79 mkws.registerWidgetType('Termlists', function() {
80     var that = this;
81     var M = mkws.M;
82
83     this.team.queue("termlists").subscribe(function(data) {
84         if (!that.node) {
85             alert("termlists event when there are no termlists");
86             return;
87         }
88
89         // no facets: this should never happen
90         if (!mkws.config.facets || mkws.config.facets.length == 0) {
91             alert("onTerm called even though we have no facets: " + $.toJSON(data));
92             $(that.node).hide();
93             return;
94         }
95
96         // display if we first got results
97         $(that.node).show();
98
99         var acc = [];
100         acc.push('<div class="title">' + M('Termlists') + '</div>');
101         var facets = mkws.config.facets;
102
103         for (var i = 0; i < facets.length; i++) {
104             if (facets[i] == "xtargets") {
105                 addSingleFacet(acc, "Sources",  data.xtargets, 16, null);
106             } else if (facets[i] == "subject") {
107                 addSingleFacet(acc, "Subjects", data.subject,  10, "subject");
108             } else if (facets[i] == "author") {
109                 addSingleFacet(acc, "Authors",  data.author,   10, "author");
110             } else {
111                 alert("bad facet configuration: '" + facets[i] + "'");
112             }
113         }
114
115         $(that.node).html(acc.join(''));
116
117         function addSingleFacet(acc, caption, data, max, pzIndex) {
118             var teamName = that.team.name();
119             acc.push('<div class="facet mkwsFacet' + caption + ' mkwsTeam_' + teamName + '">');
120             acc.push('<div class="termtitle">' + M(caption) + '</div>');
121             for (var i = 0; i < data.length && i < max; i++) {
122                 acc.push('<div class="term">');
123                 acc.push('<a href="#" ');
124                 var action = '';
125                 if (!pzIndex) {
126                     // Special case: target selection
127                     acc.push('target_id='+data[i].id+' ');
128                     if (!that.team.targetFiltered(data[i].id)) {
129                         action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
130                     }
131                 } else {
132                     action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
133                 }
134                 acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
135                          + ' <span>' + data[i].freq + '</span>');
136                 acc.push('</div>');
137             }
138             acc.push('</div>');
139         }
140     });
141 });
142
143
144 mkws.registerWidgetType('Pager', function() {
145     var that = this;
146     var M = mkws.M;
147
148     this.team.queue("pager").subscribe(function(data) {
149         $(that.node).html(drawPager(data))
150
151         function drawPager(data) {
152             var teamName = that.team.name();
153             var s = '<div style="float: right">' + M('Displaying') + ': '
154                 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
155                 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
156                 + data.total + ')</div>';
157
158             //client indexes pages from 1 but pz2 from 0
159             var onsides = 6;
160             var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
161             var currentPage = that.team.currentPage();
162
163             var firstClkbl = (currentPage - onsides > 0)
164                 ? currentPage - onsides
165                 : 1;
166
167             var lastClkbl = firstClkbl + 2*onsides < pages
168                 ? firstClkbl + 2*onsides
169                 : pages;
170
171             var prev = '<span class="mkwsPrev">&#60;&#60; ' + M('Prev') + '</span><b> | </b>';
172             if (currentPage > 1)
173                 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
174                 +'&#60;&#60; ' + M('Prev') + '</a><b> | </b>';
175
176             var middle = '';
177             for(var i = firstClkbl; i <= lastClkbl; i++) {
178                 var numLabel = i;
179                 if(i == currentPage)
180                     numLabel = '<b>' + i + '</b>';
181
182                 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
183                     + numLabel + ' </a>';
184             }
185
186             var next = '<b> | </b><span class="mkwsNext">' + M('Next') + ' &#62;&#62;</span>';
187             if (pages - currentPage > 0)
188                 next = '<b> | </b><a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
189                 + M('Next') + ' &#62;&#62;</a>';
190
191             var predots = '';
192             if (firstClkbl > 1)
193                 predots = '...';
194
195             var postdots = '';
196             if (lastClkbl < pages)
197                 postdots = '...';
198
199             s += '<div style="float: clear">'
200                 + prev + predots + middle + postdots + next + '</div>';
201
202             return s;
203         }
204     });
205 });
206
207
208 mkws.registerWidgetType('Records', function() {
209     var that = this;
210     var team = this.team;
211
212     this.team.queue("records").subscribe(function(data) {
213         var html = [];
214         for (var i = 0; i < data.hits.length; i++) {
215             var hit = data.hits[i];
216             var divId = team.recordElementId(hit.recid[0]);
217             html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
218             // ### At some point, we may be able to move the
219             // m_currentRecordId and m_currentRecordData members
220             // from the team object into this widget.
221             if (hit.recid == team.currentRecordId()) {
222                 if (team.currentRecordData())
223                     html.push(team.renderDetails(team.currentRecordData()));
224             }
225         }
226         $(that.node).html(html.join(''));
227
228         function renderSummary(hit)
229         {
230             var template = team.loadTemplate("Summary");
231             hit._id = team.recordElementId(hit.recid[0]);
232             hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
233             return template(hit);
234         }
235     });
236 });
237
238
239 mkws.registerWidgetType('Navi', function() {
240     var that = this;
241     var teamName = this.team.name();
242     var M = mkws.M;
243
244     this.team.queue("navi").subscribe(function() {
245         var filters = that.team.filters();
246         var text = "";
247
248         for (var i in filters) {
249             if (text) {
250                 text += " | ";
251             }
252             var filter = filters[i];
253             if (filter.id) {
254                 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
255                     "', '" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
256             } else {
257                 text += M(filter.field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
258                     "', '" + filter.field + "', '" + filter.value + "'" +
259                     ');return false;">' + filter.value + '</a>';
260             }
261         }
262
263         $(that.node).html(text);
264     });
265 });
266
267
268 // It seems this and the Perpage widget doen't need to subscribe to
269 // anything, since they produce events rather than consuming them.
270 //
271 mkws.registerWidgetType('Sort', function() {
272     var that = this;
273
274     $(this.node).change(function() {
275         that.team.set_sortOrder($(that.node).val());
276         if (that.team.submitted()) {
277             that.team.resetPage();
278             that.team.reShow();
279         }
280         return false;
281     });
282 });
283
284
285 mkws.registerWidgetType('Perpage', function() {
286     var that = this;
287
288     $(this.node).change(function() {
289         that.team.set_perpage($(that.node).val());
290         if (that.team.submitted()) {
291             that.team.resetPage();
292             that.team.reShow();
293         }
294         return false;
295     });
296 });