1 // Factory function for widget objects.
2 function widget($, team, type, node) {
14 that.toString = function() {
15 return '[Widget ' + team.name() + ':' + type + ']';
18 var fn = mkws.promotionFunction(type);
21 log("made " + type + " widget(node=" + node + ")");
23 log("made UNENCAPSULATED widget(type=" + type + ", node=" + node + ")");
30 // Functions follow for promoting the regular widget object into
31 // widgets of specific types. These could be moved into their own
35 function promoteToTargets() {
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>';
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>";
56 table += '</tbody></table>';
57 var subnode = $(that.node).children('.mkwsBytarget');
61 mkws.registerWidgetType('Targets', promoteToTargets);
64 function promoteToStat() {
68 this.team.queue("stat").subscribe(function(data) {
69 if (that.node.length === 0) alert("huh?!");
71 $(that.node).html('<span class="head">' + M('Status info') + '</span>' +
73 '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
75 '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
78 mkws.registerWidgetType('Stat', promoteToStat);
81 function promoteToTermlists() {
85 this.team.queue("termlists").subscribe(function(data) {
87 alert("termlists event when there are no termlists");
91 // no facets: this should never happen
92 if (!mkws_config.facets || mkws_config.facets.length == 0) {
93 alert("onTerm called even though we have no facets: " + $.toJSON(data));
98 // display if we first got results
102 acc.push('<div class="title">' + M('Termlists') + '</div>');
103 var facets = mkws_config.facets;
105 for (var i = 0; i < facets.length; i++) {
106 if (facets[i] == "xtargets") {
107 addSingleFacet(acc, "Sources", data.xtargets, 16, null);
108 } else if (facets[i] == "subject") {
109 addSingleFacet(acc, "Subjects", data.subject, 10, "subject");
110 } else if (facets[i] == "author") {
111 addSingleFacet(acc, "Authors", data.author, 10, "author");
113 alert("bad facet configuration: '" + facets[i] + "'");
117 $(that.node).html(acc.join(''));
119 function addSingleFacet(acc, caption, data, max, pzIndex) {
120 var teamName = that.team.name();
121 acc.push('<div class="facet mkwsFacet' + caption + ' mkwsTeam_' + teamName + '">');
122 acc.push('<div class="termtitle">' + M(caption) + '</div>');
123 for (var i = 0; i < data.length && i < max; i++) {
124 acc.push('<div class="term">');
125 acc.push('<a href="#" ');
128 // Special case: target selection
129 acc.push('target_id='+data[i].id+' ');
130 if (!that.team.targetFiltered(data[i].id)) {
131 action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
134 action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
136 acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
137 + ' <span>' + data[i].freq + '</span>');
144 mkws.registerWidgetType('Termlists', promoteToTermlists);
147 function promoteToPager() {
151 this.team.queue("pager").subscribe(function(data) {
152 $(that.node).html(drawPager(data))
154 function drawPager(data) {
155 var teamName = that.team.name();
156 var s = '<div style="float: right">' + M('Displaying') + ': '
157 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
158 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
159 + data.total + ')</div>';
161 //client indexes pages from 1 but pz2 from 0
163 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
164 var currentPage = that.team.currentPage();
166 var firstClkbl = (currentPage - onsides > 0)
167 ? currentPage - onsides
170 var lastClkbl = firstClkbl + 2*onsides < pages
171 ? firstClkbl + 2*onsides
174 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span><b> | </b>';
176 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
177 +'<< ' + M('Prev') + '</a><b> | </b>';
180 for(var i = firstClkbl; i <= lastClkbl; i++) {
183 numLabel = '<b>' + i + '</b>';
185 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
186 + numLabel + ' </a>';
189 var next = '<b> | </b><span class="mkwsNext">' + M('Next') + ' >></span>';
190 if (pages - currentPage > 0)
191 next = '<b> | </b><a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
192 + M('Next') + ' >></a>';
199 if (lastClkbl < pages)
202 s += '<div style="float: clear">'
203 + prev + predots + middle + postdots + next + '</div>';
209 mkws.registerWidgetType('Pager', promoteToPager);
212 function promoteToRecords() {
214 var team = this.team;
216 this.team.queue("records").subscribe(function(data) {
218 for (var i = 0; i < data.hits.length; i++) {
219 var hit = data.hits[i];
220 var divId = team.recordElementId(hit.recid[0]);
221 html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
222 // ### At some point, we may be able to move the
223 // m_currentRecordId and m_currentRecordData members
224 // from the team object into this widget.
225 if (hit.recid == team.currentRecordId()) {
226 if (team.currentRecordData())
227 html.push(team.renderDetails(team.currentRecordData()));
230 $(that.node).html(html.join(''));
232 function renderSummary(hit)
234 var template = team.loadTemplate("Summary");
235 hit._id = team.recordElementId(hit.recid[0]);
236 hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
237 return template(hit);
241 mkws.registerWidgetType('Records', promoteToRecords);
244 function promoteToNavi() {
246 var teamName = this.team.name();
249 this.team.queue("navi").subscribe(function() {
250 var filters = that.team.filters();
253 for (var i in filters) {
257 var filter = filters[i];
259 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
260 "', '" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
262 text += M(filter.field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
263 "', '" + filter.field + "', '" + filter.value + "'" +
264 ');return false;">' + filter.value + '</a>';
268 $(that.node).html(text);
271 mkws.registerWidgetType('Navi', promoteToNavi);
274 // It seems this and the Perpage widget doen't need to subscribe to
275 // anything, since they produce events rather than consuming them.
277 function promoteToSort() {
280 $(this.node).change(function () {
281 that.team.set_sortOrder($(that.node).val());
282 if (that.team.submitted()) {
283 that.team.resetPage();
289 mkws.registerWidgetType('Sort', promoteToSort);
292 function promoteToPerpage() {
295 $(this.node).change(function() {
296 that.team.set_perpage($(that.node).val());
297 if (that.team.submitted()) {
298 that.team.resetPage();
304 mkws.registerWidgetType('Perpage', promoteToPerpage);