1 // Factory function for widget objects.
2 function widget($, team, type, node) {
3 // Static register of attributes that do not contribute to config
5 id:1, class:1, style:1, name:1, action:1, type:1, size:1,
6 value:1, width:1, valign:1
13 config: Object.create(team.config())
21 that.toString = function() {
22 return '[Widget ' + team.name() + ':' + type + ']';
25 for (var i = 0; i < node.attributes.length; i++) {
26 var a = node.attributes[i];
27 if (a.name === 'data-mkws-config') {
28 // Treat as a JSON fragment configuring just this widget
29 log(node + ": parsing config fragment '" + a.value + "'");
30 var data = $.parseJSON(a.value);
31 for (var key in data) {
32 log(node + ": adding config element " + key + "='" + data[key] + "'");
33 that.config[key] = data[key];
35 } else if (a.name.match (/^data-mkws-/)) {
36 var name = a.name.replace(/^data-mkws-/, '')
37 that.config[name] = a.value;
38 log(node + ": set data-mkws attribute " + name + "='" + a.value + "'");
39 } else if (!ignoreAttrs[a.name]) {
40 that.config[a.name] = a.value;
41 log(node + ": set regular attribute " + a.name + "='" + a.value + "'");
45 var fn = mkws.promotionFunction(type);
48 log("made " + type + " widget(node=" + node + ")");
50 log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
57 // Functions follow for promoting the regular widget object into
58 // widgets of specific types. These could be moved into their own
62 mkws.registerWidgetType('Targets', function() {
66 this.team.queue("targets").subscribe(function(data) {
67 var table ='<table><thead><tr>' +
68 '<td>' + M('Target ID') + '</td>' +
69 '<td>' + M('Hits') + '</td>' +
70 '<td>' + M('Diags') + '</td>' +
71 '<td>' + M('Records') + '</td>' +
72 '<td>' + M('State') + '</td>' +
73 '</tr></thead><tbody>';
75 for (var i = 0; i < data.length; i++) {
76 table += "<tr><td>" + data[i].id +
77 "</td><td>" + data[i].hits +
78 "</td><td>" + data[i].diagnostic +
79 "</td><td>" + data[i].records +
80 "</td><td>" + data[i].state + "</td></tr>";
83 table += '</tbody></table>';
84 var subnode = $(that.node).children('.mkwsBytarget');
90 mkws.registerWidgetType('Stat', function() {
94 this.team.queue("stat").subscribe(function(data) {
95 if (that.node.length === 0) alert("huh?!");
97 $(that.node).html('<span class="head">' + M('Status info') + '</span>' +
99 '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
101 '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
106 mkws.registerWidgetType('Termlists', function() {
110 this.team.queue("termlists").subscribe(function(data) {
112 alert("termlists event when there are no termlists");
116 // no facets: this should never happen
117 var facets = that.config.facets;
118 if (!facets || facets.length == 0) {
119 alert("onTerm called even though we have no facets: " + $.toJSON(data));
124 // display if we first got results
128 acc.push('<div class="title">' + M('Termlists') + '</div>');
130 for (var i = 0; i < facets.length; i++) {
131 if (facets[i] == "xtargets") {
132 addSingleFacet(acc, "Sources", data.xtargets, 16, null);
133 } else if (facets[i] == "subject") {
134 addSingleFacet(acc, "Subjects", data.subject, 10, "subject");
135 } else if (facets[i] == "author") {
136 addSingleFacet(acc, "Authors", data.author, 10, "author");
138 alert("bad facet configuration: '" + facets[i] + "'");
142 $(that.node).html(acc.join(''));
144 function addSingleFacet(acc, caption, data, max, pzIndex) {
145 var teamName = that.team.name();
146 acc.push('<div class="facet mkwsFacet' + caption + ' mkwsTeam_' + teamName + '">');
147 acc.push('<div class="termtitle">' + M(caption) + '</div>');
148 for (var i = 0; i < data.length && i < max; i++) {
149 acc.push('<div class="term">');
150 acc.push('<a href="#" ');
153 // Special case: target selection
154 acc.push('target_id='+data[i].id+' ');
155 if (!that.team.targetFiltered(data[i].id)) {
156 action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
159 action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
161 acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
162 + ' <span>' + data[i].freq + '</span>');
171 mkws.registerWidgetType('Pager', function() {
175 this.team.queue("pager").subscribe(function(data) {
176 $(that.node).html(drawPager(data))
178 function drawPager(data) {
179 var teamName = that.team.name();
180 var s = '<div style="float: right">' + M('Displaying') + ': '
181 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
182 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
183 + data.total + ')</div>';
185 //client indexes pages from 1 but pz2 from 0
187 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
188 var currentPage = that.team.currentPage();
190 var firstClkbl = (currentPage - onsides > 0)
191 ? currentPage - onsides
194 var lastClkbl = firstClkbl + 2*onsides < pages
195 ? firstClkbl + 2*onsides
198 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span><b> | </b>';
200 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
201 +'<< ' + M('Prev') + '</a><b> | </b>';
204 for(var i = firstClkbl; i <= lastClkbl; i++) {
207 numLabel = '<b>' + i + '</b>';
209 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
210 + numLabel + ' </a>';
213 var next = '<b> | </b><span class="mkwsNext">' + M('Next') + ' >></span>';
214 if (pages - currentPage > 0)
215 next = '<b> | </b><a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
216 + M('Next') + ' >></a>';
223 if (lastClkbl < pages)
226 s += '<div style="float: clear">'
227 + prev + predots + middle + postdots + next + '</div>';
235 mkws.registerWidgetType('Records', function() {
237 var team = this.team;
239 this.team.queue("records").subscribe(function(data) {
241 for (var i = 0; i < data.hits.length; i++) {
242 var hit = data.hits[i];
243 var divId = team.recordElementId(hit.recid[0]);
244 html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
245 // ### At some point, we may be able to move the
246 // m_currentRecordId and m_currentRecordData members
247 // from the team object into this widget.
248 if (hit.recid == team.currentRecordId()) {
249 if (team.currentRecordData())
250 html.push(team.renderDetails(team.currentRecordData()));
253 $(that.node).html(html.join(''));
255 function renderSummary(hit)
257 var template = team.loadTemplate("Summary");
258 hit._id = team.recordElementId(hit.recid[0]);
259 hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
260 return template(hit);
266 mkws.registerWidgetType('Navi', function() {
268 var teamName = this.team.name();
271 this.team.queue("navi").subscribe(function() {
272 var filters = that.team.filters();
275 for (var i in filters) {
279 var filter = filters[i];
281 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
282 "', '" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
284 text += M(filter.field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
285 "', '" + filter.field + "', '" + filter.value + "'" +
286 ');return false;">' + filter.value + '</a>';
290 $(that.node).html(text);
295 // It seems this and the Perpage widget doen't need to subscribe to
296 // anything, since they produce events rather than consuming them.
298 mkws.registerWidgetType('Sort', function() {
301 $(this.node).change(function() {
302 that.team.set_sortOrder($(that.node).val());
303 if (that.team.submitted()) {
304 that.team.resetPage();
312 mkws.registerWidgetType('Perpage', function() {
315 $(this.node).change(function() {
316 that.team.set_perpage($(that.node).val());
317 if (that.team.submitted()) {
318 that.team.resetPage();