1 // Factory function for widget objects.
2 function widget($, team, type, node) {
14 that.toString = function() {
15 return '[Widget ' + team.name() + ':' + type + ']';
18 var promote = 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 outside of the
32 // widget object, or even into their own source files.
34 function promoteTargets() {
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>';
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>";
55 table += '</tbody></table>';
56 var subnode = $(that.node).children('.mkwsBytarget');
62 function promoteStat() {
66 this.team.queue("stat").subscribe(function(data) {
67 if (that.node.length === 0) alert("huh?!");
69 $(that.node).html('<span class="head">' + M('Status info') + '</span>' +
71 '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
73 '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
78 function promoteTermlists() {
82 this.team.queue("termlists").subscribe(function(data) {
84 alert("termlists event when there are no termlists");
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));
95 // display if we first got results
99 acc.push('<div class="title">' + M('Termlists') + '</div>');
100 var facets = mkws_config.facets;
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");
110 alert("bad facet configuration: '" + facets[i] + "'");
114 $(that.node).html(acc.join(''));
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="#" ');
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)';
131 action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
133 acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
134 + ' <span>' + data[i].freq + '</span>');
143 function promotePager() {
147 this.team.queue("pager").subscribe(function(data) {
148 $(that.node).html(drawPager(data))
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>';
157 //client indexes pages from 1 but pz2 from 0
159 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
160 var currentPage = that.team.currentPage();
162 var firstClkbl = (currentPage - onsides > 0)
163 ? currentPage - onsides
166 var lastClkbl = firstClkbl + 2*onsides < pages
167 ? firstClkbl + 2*onsides
170 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span><b> | </b>';
172 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
173 +'<< ' + M('Prev') + '</a><b> | </b>';
176 for(var i = firstClkbl; i <= lastClkbl; i++) {
179 numLabel = '<b>' + i + '</b>';
181 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
182 + numLabel + ' </a>';
185 var next = '<b> | </b><span class="mkwsNext">' + M('Next') + ' >></span>';
186 if (pages - currentPage > 0)
187 next = '<b> | </b><a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
188 + M('Next') + ' >></a>';
195 if (lastClkbl < pages)
198 s += '<div style="float: clear">'
199 + prev + predots + middle + postdots + next + '</div>';
207 function promoteRecords() {
209 var team = this.team;
211 this.team.queue("records").subscribe(function(data) {
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()));
225 $(that.node).html(html.join(''));
227 function renderSummary(hit)
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);
238 function promoteNavi() {
240 var teamName = this.team.name();
243 this.team.queue("navi").subscribe(function() {
244 var filters = that.team.filters();
247 for (var i in filters) {
251 var filter = filters[i];
253 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
254 "', '" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
256 text += M(filter.field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
257 "', '" + filter.field + "', '" + filter.value + "'" +
258 ');return false;">' + filter.value + '</a>';
262 $(that.node).html(text);
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();
282 function promotePerpage() {
283 $(this.node).change(function() {
284 this.team.set_perpage($(node).val());
285 if (this.team.submitted()) {
286 this.team.resetPage();
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);