2 mkws.registerWidget = function(name, fn) {
3 mkws.type2fn[name] = fn;
6 // Factory function for widget objects.
7 function widget($, team, type, node) {
14 mkws.registerWidget('Targets', promoteTargets);
15 mkws.registerWidget('Stat', promoteStat);
16 mkws.registerWidget('Termlists', promoteTermlists);
17 mkws.registerWidget('Pager', promotePager);
18 mkws.registerWidget('Records', promoteRecords);
19 mkws.registerWidget('Navi', promoteNavi);
20 mkws.registerWidget('Sort', promoteSort);
21 mkws.registerWidget('Perpage', promotePerpage);
25 var promote = mkws.type2fn[type];
28 log("made " + type + " widget(node=" + node + ")");
30 log("made UNENCAPSULATED widget(type=" + type + ", node=" + node + ")");
40 // Functions follow for promoting the regular widget object into
41 // widgets of specific types. These could be moved outside of the
42 // widget object, or even into their own source files.
44 function promoteTargets() {
45 team.queue("targets").subscribe(function(data) {
46 var table ='<table><thead><tr>' +
47 '<td>' + M('Target ID') + '</td>' +
48 '<td>' + M('Hits') + '</td>' +
49 '<td>' + M('Diags') + '</td>' +
50 '<td>' + M('Records') + '</td>' +
51 '<td>' + M('State') + '</td>' +
52 '</tr></thead><tbody>';
54 for (var i = 0; i < data.length; i++) {
55 table += "<tr><td>" + data[i].id +
56 "</td><td>" + data[i].hits +
57 "</td><td>" + data[i].diagnostic +
58 "</td><td>" + data[i].records +
59 "</td><td>" + data[i].state + "</td></tr>";
62 table += '</tbody></table>';
63 var subnode = $(node).children('.mkwsBytarget');
69 function promoteStat() {
70 team.queue("stat").subscribe(function(data) {
71 if (node.length === 0) alert("huh?!");
73 $(node).html('<span class="head">' + M('Status info') + '</span>' +
75 '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
77 '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
82 function promoteTermlists() {
83 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 $(node).html(acc.join(''));
117 function addSingleFacet(acc, caption, data, max, pzIndex) {
118 acc.push('<div class="facet mkwsFacet' + caption + ' mkwsTeam_' + team.name() + '">');
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 (!team.targetFiltered(data[i].id)) {
128 action = 'mkws.limitTarget(\'' + team.name() + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
131 action = 'mkws.limitQuery(\'' + team.name() + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
133 acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
134 + ' <span>' + data[i].freq + '</span>');
143 function promotePager() {
144 team.queue("pager").subscribe(function(data) {
145 $(node).html(drawPager(data))
147 function drawPager(data) {
148 var s = '<div style="float: right">' + M('Displaying') + ': '
149 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
150 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
151 + data.total + ')</div>';
153 //client indexes pages from 1 but pz2 from 0
155 var pages = Math.ceil(team.totalRecordCount() / team.perpage());
156 var currentPage = team.currentPage();
158 var firstClkbl = (currentPage - onsides > 0)
159 ? currentPage - onsides
162 var lastClkbl = firstClkbl + 2*onsides < pages
163 ? firstClkbl + 2*onsides
166 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span><b> | </b>';
168 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + team.name() + '\');">'
169 +'<< ' + M('Prev') + '</a><b> | </b>';
172 for(var i = firstClkbl; i <= lastClkbl; i++) {
175 numLabel = '<b>' + i + '</b>';
177 middle += '<a href="#" onclick="mkws.showPage(\'' + team.name() + '\', ' + i + ')"> '
178 + numLabel + ' </a>';
181 var next = '<b> | </b><span class="mkwsNext">' + M('Next') + ' >></span>';
182 if (pages - currentPage > 0)
183 next = '<b> | </b><a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + team.name() + '\')">'
184 + M('Next') + ' >></a>';
191 if (lastClkbl < pages)
194 s += '<div style="float: clear">'
195 + prev + predots + middle + postdots + next + '</div>';
203 function promoteRecords() {
204 team.queue("records").subscribe(function(data) {
206 for (var i = 0; i < data.hits.length; i++) {
207 var hit = data.hits[i];
208 var divId = team.recordElementId(hit.recid[0]);
209 html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
210 // ### At some point, we may be able to move the
211 // m_currentRecordId and m_currentRecordData members
212 // from the team object into this widget.
213 if (hit.recid == team.currentRecordId()) {
214 if (team.currentRecordData())
215 html.push(team.renderDetails(team.currentRecordData()));
218 $(node).html(html.join(''));
220 function renderSummary(hit)
222 var template = team.loadTemplate("Summary");
223 hit._id = team.recordElementId(hit.recid[0]);
224 hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
225 return template(hit);
231 function promoteNavi() {
232 team.queue("navi").subscribe(function() {
233 var filters = team.filters();
236 for (var i in filters) {
240 var filter = filters[i];
242 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + team.name() +
243 "', '" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
245 text += M(filter.field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + team.name() +
246 "', '" + filter.field + "', '" + filter.value + "'" +
247 ');return false;">' + filter.value + '</a>';
256 function promoteSort() {
257 // It seems this and the Perpage widget doen't need to
258 // subscribe to anything, since they produce events rather
259 // than consuming them.
260 $(node).change(function () {
261 team.set_sortOrder($(node).val());
262 if (team.submitted()) {
271 function promotePerpage() {
272 $(node).change(function() {
273 team.set_perpage($(node).val());
274 if (team.submitted()) {