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 + "'");
32 data = $.parseJSON(a.value);
33 for (var key in data) {
34 log(node + ": adding config element " + key + "='" + data[key] + "'");
35 that.config[key] = data[key];
38 alert("Can't parse " + node + " data-mkws-config as JSON: " + a.value);
40 } else if (a.name.match (/^data-mkws-/)) {
41 var name = a.name.replace(/^data-mkws-/, '')
42 that.config[name] = a.value;
43 log(node + ": set data-mkws attribute " + name + "='" + a.value + "'");
44 } else if (!ignoreAttrs[a.name]) {
45 that.config[a.name] = a.value;
46 log(node + ": set regular attribute " + a.name + "='" + a.value + "'");
50 var fn = mkws.promotionFunction(type);
53 log("made " + type + " widget(node=" + node + ")");
55 log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
62 // Functions follow for promoting the regular widget object into
63 // widgets of specific types. These could be moved into their own
67 mkws.registerWidgetType('Targets', function() {
71 this.team.queue("targets").subscribe(function(data) {
72 var table ='<table><thead><tr>' +
73 '<td>' + M('Target ID') + '</td>' +
74 '<td>' + M('Hits') + '</td>' +
75 '<td>' + M('Diags') + '</td>' +
76 '<td>' + M('Records') + '</td>' +
77 '<td>' + M('State') + '</td>' +
78 '</tr></thead><tbody>';
80 for (var i = 0; i < data.length; i++) {
81 table += "<tr><td>" + data[i].id +
82 "</td><td>" + data[i].hits +
83 "</td><td>" + data[i].diagnostic +
84 "</td><td>" + data[i].records +
85 "</td><td>" + data[i].state + "</td></tr>";
88 table += '</tbody></table>';
89 var subnode = $(that.node).children('.mkwsBytarget');
95 mkws.registerWidgetType('Stat', function() {
99 this.team.queue("stat").subscribe(function(data) {
100 if (that.node.length === 0) alert("huh?!");
102 $(that.node).html('<span class="head">' + M('Status info') + '</span>' +
104 '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
106 '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
111 mkws.registerWidgetType('Termlists', function() {
115 this.team.queue("termlists").subscribe(function(data) {
117 alert("termlists event when there are no termlists");
121 // no facets: this should never happen
122 var facets = that.config.facets;
123 if (!facets || facets.length == 0) {
124 alert("onTerm called even though we have no facets: " + $.toJSON(data));
129 // display if we first got results
133 acc.push('<div class="title">' + M('Termlists') + '</div>');
135 for (var i = 0; i < facets.length; i++) {
136 if (facets[i] == "xtargets") {
137 addSingleFacet(acc, "Sources", data.xtargets, 16, null);
138 } else if (facets[i] == "subject") {
139 addSingleFacet(acc, "Subjects", data.subject, 10, "subject");
140 } else if (facets[i] == "author") {
141 addSingleFacet(acc, "Authors", data.author, 10, "author");
143 alert("bad facet configuration: '" + facets[i] + "'");
147 $(that.node).html(acc.join(''));
149 function addSingleFacet(acc, caption, data, max, pzIndex) {
150 var teamName = that.team.name();
151 acc.push('<div class="facet mkwsFacet' + caption + ' mkwsTeam_' + teamName + '">');
152 acc.push('<div class="termtitle">' + M(caption) + '</div>');
153 for (var i = 0; i < data.length && i < max; i++) {
154 acc.push('<div class="term">');
155 acc.push('<a href="#" ');
158 // Special case: target selection
159 acc.push('target_id='+data[i].id+' ');
160 if (!that.team.targetFiltered(data[i].id)) {
161 action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
164 action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
166 acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
167 + ' <span>' + data[i].freq + '</span>');
176 mkws.registerWidgetType('Pager', function() {
180 this.team.queue("pager").subscribe(function(data) {
181 $(that.node).html(drawPager(data))
183 function drawPager(data) {
184 var teamName = that.team.name();
185 var s = '<div style="float: right">' + M('Displaying') + ': '
186 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
187 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
188 + data.total + ')</div>';
190 //client indexes pages from 1 but pz2 from 0
192 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
193 var currentPage = that.team.currentPage();
195 var firstClkbl = (currentPage - onsides > 0)
196 ? currentPage - onsides
199 var lastClkbl = firstClkbl + 2*onsides < pages
200 ? firstClkbl + 2*onsides
203 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span><b> | </b>';
205 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
206 +'<< ' + M('Prev') + '</a><b> | </b>';
209 for(var i = firstClkbl; i <= lastClkbl; i++) {
212 numLabel = '<b>' + i + '</b>';
214 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
215 + numLabel + ' </a>';
218 var next = '<b> | </b><span class="mkwsNext">' + M('Next') + ' >></span>';
219 if (pages - currentPage > 0)
220 next = '<b> | </b><a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
221 + M('Next') + ' >></a>';
228 if (lastClkbl < pages)
231 s += '<div style="float: clear">'
232 + prev + predots + middle + postdots + next + '</div>';
240 mkws.registerWidgetType('Records', function() {
242 var team = this.team;
244 this.team.queue("records").subscribe(function(data) {
246 for (var i = 0; i < data.hits.length; i++) {
247 var hit = data.hits[i];
248 var divId = team.recordElementId(hit.recid[0]);
249 html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
250 // ### At some point, we may be able to move the
251 // m_currentRecordId and m_currentRecordData members
252 // from the team object into this widget.
253 if (hit.recid == team.currentRecordId()) {
254 if (team.currentRecordData())
255 html.push(team.renderDetails(team.currentRecordData()));
258 $(that.node).html(html.join(''));
260 function renderSummary(hit)
262 var template = team.loadTemplate("Summary");
263 hit._id = team.recordElementId(hit.recid[0]);
264 hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
265 return template(hit);
271 mkws.registerWidgetType('Navi', function() {
273 var teamName = this.team.name();
276 this.team.queue("navi").subscribe(function() {
277 var filters = that.team.filters();
280 for (var i in filters) {
284 var filter = filters[i];
286 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
287 "', '" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
289 text += M(filter.field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
290 "', '" + filter.field + "', '" + filter.value + "'" +
291 ');return false;">' + filter.value + '</a>';
295 $(that.node).html(text);
300 // It seems this and the Perpage widget doen't need to subscribe to
301 // anything, since they produce events rather than consuming them.
303 mkws.registerWidgetType('Sort', function() {
306 $(this.node).change(function() {
307 that.team.set_sortOrder($(that.node).val());
308 if (that.team.submitted()) {
309 that.team.resetPage();
317 mkws.registerWidgetType('Perpage', function() {
320 $(this.node).change(function() {
321 that.team.set_perpage($(that.node).val());
322 if (that.team.submitted()) {
323 that.team.resetPage();