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 UNPROMOTED 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 mkws.registerWidgetType('Targets', function() {
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');
63 mkws.registerWidgetType('Stat', function() {
67 this.team.queue("stat").subscribe(function(data) {
68 if (that.node.length === 0) alert("huh?!");
70 $(that.node).html('<span class="head">' + M('Status info') + '</span>' +
72 '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
74 '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
79 mkws.registerWidgetType('Termlists', function() {
83 this.team.queue("termlists").subscribe(function(data) {
85 alert("termlists event when there are no termlists");
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));
96 // display if we first got results
100 acc.push('<div class="title">' + M('Termlists') + '</div>');
101 var facets = mkws_config.facets;
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");
111 alert("bad facet configuration: '" + facets[i] + "'");
115 $(that.node).html(acc.join(''));
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="#" ');
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)';
132 action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
134 acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
135 + ' <span>' + data[i].freq + '</span>');
144 mkws.registerWidgetType('Pager', function() {
148 this.team.queue("pager").subscribe(function(data) {
149 $(that.node).html(drawPager(data))
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>';
158 //client indexes pages from 1 but pz2 from 0
160 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
161 var currentPage = that.team.currentPage();
163 var firstClkbl = (currentPage - onsides > 0)
164 ? currentPage - onsides
167 var lastClkbl = firstClkbl + 2*onsides < pages
168 ? firstClkbl + 2*onsides
171 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span><b> | </b>';
173 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
174 +'<< ' + M('Prev') + '</a><b> | </b>';
177 for(var i = firstClkbl; i <= lastClkbl; i++) {
180 numLabel = '<b>' + i + '</b>';
182 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
183 + numLabel + ' </a>';
186 var next = '<b> | </b><span class="mkwsNext">' + M('Next') + ' >></span>';
187 if (pages - currentPage > 0)
188 next = '<b> | </b><a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
189 + M('Next') + ' >></a>';
196 if (lastClkbl < pages)
199 s += '<div style="float: clear">'
200 + prev + predots + middle + postdots + next + '</div>';
208 mkws.registerWidgetType('Records', function() {
210 var team = this.team;
212 this.team.queue("records").subscribe(function(data) {
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()));
226 $(that.node).html(html.join(''));
228 function renderSummary(hit)
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);
239 mkws.registerWidgetType('Navi', function() {
241 var teamName = this.team.name();
244 this.team.queue("navi").subscribe(function() {
245 var filters = that.team.filters();
248 for (var i in filters) {
252 var filter = filters[i];
254 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
255 "', '" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
257 text += M(filter.field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
258 "', '" + filter.field + "', '" + filter.value + "'" +
259 ');return false;">' + filter.value + '</a>';
263 $(that.node).html(text);
268 // It seems this and the Perpage widget doen't need to subscribe to
269 // anything, since they produce events rather than consuming them.
271 mkws.registerWidgetType('Sort', function() {
274 $(this.node).change(function() {
275 that.team.set_sortOrder($(that.node).val());
276 if (that.team.submitted()) {
277 that.team.resetPage();
285 mkws.registerWidgetType('Perpage', function() {
288 $(this.node).change(function() {
289 that.team.set_perpage($(that.node).val());
290 if (that.team.submitted()) {
291 that.team.resetPage();