1 // Factory function for widget objects.
2 function widget($, team, type, node) {
7 config: Object.create(team.config())
15 that.toString = function() {
16 return '[Widget ' + team.name() + ':' + type + ']';
19 var fn = mkws.promotionFunction(type);
22 log("made " + type + " widget(node=" + node + ")");
24 log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
31 // Functions follow for promoting the regular widget object into
32 // widgets of specific types. These could be moved into their own
36 mkws.registerWidgetType('Targets', function() {
40 this.team.queue("targets").subscribe(function(data) {
41 var table ='<table><thead><tr>' +
42 '<td>' + M('Target ID') + '</td>' +
43 '<td>' + M('Hits') + '</td>' +
44 '<td>' + M('Diags') + '</td>' +
45 '<td>' + M('Records') + '</td>' +
46 '<td>' + M('State') + '</td>' +
47 '</tr></thead><tbody>';
49 for (var i = 0; i < data.length; i++) {
50 table += "<tr><td>" + data[i].id +
51 "</td><td>" + data[i].hits +
52 "</td><td>" + data[i].diagnostic +
53 "</td><td>" + data[i].records +
54 "</td><td>" + data[i].state + "</td></tr>";
57 table += '</tbody></table>';
58 var subnode = $(that.node).children('.mkwsBytarget');
64 mkws.registerWidgetType('Stat', function() {
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>');
80 mkws.registerWidgetType('Termlists', function() {
84 this.team.queue("termlists").subscribe(function(data) {
86 alert("termlists event when there are no termlists");
90 // no facets: this should never happen
91 var facets = that.config.facets;
92 if (!facets || 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>');
104 for (var i = 0; i < facets.length; i++) {
105 if (facets[i] == "xtargets") {
106 addSingleFacet(acc, "Sources", data.xtargets, 16, null);
107 } else if (facets[i] == "subject") {
108 addSingleFacet(acc, "Subjects", data.subject, 10, "subject");
109 } else if (facets[i] == "author") {
110 addSingleFacet(acc, "Authors", data.author, 10, "author");
112 alert("bad facet configuration: '" + facets[i] + "'");
116 $(that.node).html(acc.join(''));
118 function addSingleFacet(acc, caption, data, max, pzIndex) {
119 var teamName = that.team.name();
120 acc.push('<div class="facet mkwsFacet' + caption + ' mkwsTeam_' + teamName + '">');
121 acc.push('<div class="termtitle">' + M(caption) + '</div>');
122 for (var i = 0; i < data.length && i < max; i++) {
123 acc.push('<div class="term">');
124 acc.push('<a href="#" ');
127 // Special case: target selection
128 acc.push('target_id='+data[i].id+' ');
129 if (!that.team.targetFiltered(data[i].id)) {
130 action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
133 action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
135 acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
136 + ' <span>' + data[i].freq + '</span>');
145 mkws.registerWidgetType('Pager', function() {
149 this.team.queue("pager").subscribe(function(data) {
150 $(that.node).html(drawPager(data))
152 function drawPager(data) {
153 var teamName = that.team.name();
154 var s = '<div style="float: right">' + M('Displaying') + ': '
155 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
156 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
157 + data.total + ')</div>';
159 //client indexes pages from 1 but pz2 from 0
161 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
162 var currentPage = that.team.currentPage();
164 var firstClkbl = (currentPage - onsides > 0)
165 ? currentPage - onsides
168 var lastClkbl = firstClkbl + 2*onsides < pages
169 ? firstClkbl + 2*onsides
172 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span><b> | </b>';
174 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
175 +'<< ' + M('Prev') + '</a><b> | </b>';
178 for(var i = firstClkbl; i <= lastClkbl; i++) {
181 numLabel = '<b>' + i + '</b>';
183 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
184 + numLabel + ' </a>';
187 var next = '<b> | </b><span class="mkwsNext">' + M('Next') + ' >></span>';
188 if (pages - currentPage > 0)
189 next = '<b> | </b><a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
190 + M('Next') + ' >></a>';
197 if (lastClkbl < pages)
200 s += '<div style="float: clear">'
201 + prev + predots + middle + postdots + next + '</div>';
209 mkws.registerWidgetType('Records', function() {
211 var team = this.team;
213 this.team.queue("records").subscribe(function(data) {
215 for (var i = 0; i < data.hits.length; i++) {
216 var hit = data.hits[i];
217 var divId = team.recordElementId(hit.recid[0]);
218 html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
219 // ### At some point, we may be able to move the
220 // m_currentRecordId and m_currentRecordData members
221 // from the team object into this widget.
222 if (hit.recid == team.currentRecordId()) {
223 if (team.currentRecordData())
224 html.push(team.renderDetails(team.currentRecordData()));
227 $(that.node).html(html.join(''));
229 function renderSummary(hit)
231 var template = team.loadTemplate("Summary");
232 hit._id = team.recordElementId(hit.recid[0]);
233 hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
234 return template(hit);
240 mkws.registerWidgetType('Navi', function() {
242 var teamName = this.team.name();
245 this.team.queue("navi").subscribe(function() {
246 var filters = that.team.filters();
249 for (var i in filters) {
253 var filter = filters[i];
255 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
256 "', '" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
258 text += M(filter.field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
259 "', '" + filter.field + "', '" + filter.value + "'" +
260 ');return false;">' + filter.value + '</a>';
264 $(that.node).html(text);
269 // It seems this and the Perpage widget doen't need to subscribe to
270 // anything, since they produce events rather than consuming them.
272 mkws.registerWidgetType('Sort', function() {
275 $(this.node).change(function() {
276 that.team.set_sortOrder($(that.node).val());
277 if (that.team.submitted()) {
278 that.team.resetPage();
286 mkws.registerWidgetType('Perpage', function() {
289 $(this.node).change(function() {
290 that.team.set_perpage($(that.node).val());
291 if (that.team.submitted()) {
292 that.team.resetPage();