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 // Utility function for use by all widgets that can invoke autosearch.
63 widget.maybeAutosearch = function(widget) {
64 var query = widget.config.autosearch;
66 if (query.match(/^!param!/)) {
67 var param = query.replace(/^!param!/, '');
68 query = mkws.getParameterByName(param);
69 widget.log("obtained query '" + query + "' from param '" + param + "'");
71 alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
73 } else if (query.match(/^!path!/)) {
74 var index = query.replace(/^!path!/, '');
75 var path = window.location.pathname.split('/');
76 query = path[path.length - index];
77 widget.log("obtained query '" + query + "' from path-component '" + index + "'");
79 alert("This page has a MasterKey widget that needs a query specified by the path-component " + index);
83 widget.team.queue("ready").subscribe(function() {
84 var sortOrder = widget.config.sort;
85 var perpage = widget.config.perpage;
86 var limit = widget.config.limit;
87 var targets = widget.config.targets;
88 var targetfilter = widget.config.targetfilter;
89 var s = "running auto search: '" + query + "'";
90 if (sortOrder) s += " sorted by '" + sortOrder + "'";
91 if (perpage) s += " with " + perpage + " per page";
92 if (limit) s += " limited by '" + limit + "'";
93 if (targets) s += " in targets '" + targets + "'";
94 if (targetfilter) s += " constrained by targetfilter '" + targetfilter + "'";
97 widget.team.newSearch(query, sortOrder, perpage, limit, targets, targetfilter);
103 // Functions follow for promoting the regular widget object into
104 // widgets of specific types. These could be moved into their own
108 mkws.registerWidgetType('Targets', function() {
112 this.team.queue("targets").subscribe(function(data) {
113 var table ='<table><thead><tr>' +
114 '<td>' + M('Target ID') + '</td>' +
115 '<td>' + M('Hits') + '</td>' +
116 '<td>' + M('Diags') + '</td>' +
117 '<td>' + M('Records') + '</td>' +
118 '<td>' + M('State') + '</td>' +
119 '</tr></thead><tbody>';
121 for (var i = 0; i < data.length; i++) {
122 table += "<tr><td>" + data[i].id +
123 "</td><td>" + data[i].hits +
124 "</td><td>" + data[i].diagnostic +
125 "</td><td>" + data[i].records +
126 "</td><td>" + data[i].state + "</td></tr>";
129 table += '</tbody></table>';
130 var subnode = $(that.node).children('.mkwsBytarget');
136 mkws.registerWidgetType('Stat', function() {
140 this.team.queue("stat").subscribe(function(data) {
141 if (that.node.length === 0) alert("huh?!");
143 $(that.node).html('<span class="head">' + M('Status info') + '</span>' +
145 '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
147 '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
152 mkws.registerWidgetType('Termlists', function() {
156 this.team.queue("termlists").subscribe(function(data) {
158 alert("termlists event when there are no termlists");
162 // no facets: this should never happen
163 var facets = that.config.facets;
164 if (!facets || facets.length == 0) {
165 alert("onTerm called even though we have no facets: " + $.toJSON(data));
170 // display if we first got results
174 acc.push('<div class="title">' + M('Termlists') + '</div>');
176 for (var i = 0; i < facets.length; i++) {
177 if (facets[i] == "xtargets") {
178 addSingleFacet(acc, "Sources", data.xtargets, 16, null);
179 } else if (facets[i] == "subject") {
180 addSingleFacet(acc, "Subjects", data.subject, 10, "subject");
181 } else if (facets[i] == "author") {
182 addSingleFacet(acc, "Authors", data.author, 10, "author");
184 alert("bad facet configuration: '" + facets[i] + "'");
188 $(that.node).html(acc.join(''));
190 function addSingleFacet(acc, caption, data, max, pzIndex) {
191 var teamName = that.team.name();
192 acc.push('<div class="facet mkwsFacet' + caption + ' mkwsTeam_' + teamName + '">');
193 acc.push('<div class="termtitle">' + M(caption) + '</div>');
194 for (var i = 0; i < data.length && i < max; i++) {
195 acc.push('<div class="term">');
196 acc.push('<a href="#" ');
199 // Special case: target selection
200 acc.push('target_id='+data[i].id+' ');
201 if (!that.team.targetFiltered(data[i].id)) {
202 action = 'mkws.limitTarget(\'' + teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
205 action = 'mkws.limitQuery(\'' + teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
207 acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
208 + ' <span>' + data[i].freq + '</span>');
217 mkws.registerWidgetType('Pager', function() {
221 this.team.queue("pager").subscribe(function(data) {
222 $(that.node).html(drawPager(data))
224 function drawPager(data) {
225 var teamName = that.team.name();
226 var s = '<div style="float: right">' + M('Displaying') + ': '
227 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
228 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
229 + data.total + ')</div>';
231 //client indexes pages from 1 but pz2 from 0
233 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
234 var currentPage = that.team.currentPage();
236 var firstClkbl = (currentPage - onsides > 0)
237 ? currentPage - onsides
240 var lastClkbl = firstClkbl + 2*onsides < pages
241 ? firstClkbl + 2*onsides
244 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span><b> | </b>';
246 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
247 +'<< ' + M('Prev') + '</a><b> | </b>';
250 for(var i = firstClkbl; i <= lastClkbl; i++) {
253 numLabel = '<b>' + i + '</b>';
255 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
256 + numLabel + ' </a>';
259 var next = '<b> | </b><span class="mkwsNext">' + M('Next') + ' >></span>';
260 if (pages - currentPage > 0)
261 next = '<b> | </b><a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
262 + M('Next') + ' >></a>';
269 if (lastClkbl < pages)
272 s += '<div style="float: clear">'
273 + prev + predots + middle + postdots + next + '</div>';
281 mkws.registerWidgetType('Records', function() {
283 var team = this.team;
285 this.team.queue("records").subscribe(function(data) {
287 for (var i = 0; i < data.hits.length; i++) {
288 var hit = data.hits[i];
289 var divId = team.recordElementId(hit.recid[0]);
290 html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
291 // ### At some point, we may be able to move the
292 // m_currentRecordId and m_currentRecordData members
293 // from the team object into this widget.
294 if (hit.recid == team.currentRecordId()) {
295 if (team.currentRecordData())
296 html.push(team.renderDetails(team.currentRecordData()));
299 $(that.node).html(html.join(''));
301 function renderSummary(hit) {
302 var template = team.loadTemplate("Summary");
303 hit._id = team.recordElementId(hit.recid[0]);
304 hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
305 return template(hit);
309 widget.maybeAutosearch(that);
313 mkws.registerWidgetType('Navi', function() {
315 var teamName = this.team.name();
318 this.team.queue("navi").subscribe(function() {
319 var filters = that.team.filters();
322 for (var i in filters) {
326 var filter = filters[i];
328 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
329 "', '" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
331 text += M(filter.field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
332 "', '" + filter.field + "', '" + filter.value + "'" +
333 ');return false;">' + filter.value + '</a>';
337 $(that.node).html(text);
342 // It seems this and the Perpage widget doen't need to subscribe to
343 // anything, since they produce events rather than consuming them.
345 mkws.registerWidgetType('Sort', function() {
348 $(this.node).change(function() {
349 that.team.set_sortOrder($(that.node).val());
350 if (that.team.submitted()) {
351 that.team.resetPage();
359 mkws.registerWidgetType('Perpage', function() {
362 $(this.node).change(function() {
363 that.team.set_perpage($(that.node).val());
364 if (that.team.submitted()) {
365 that.team.resetPage();