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