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: mkws.objectInheritingFrom(team.config())
21 that.toString = function() {
22 return '[Widget ' + team.name() + ':' + type + ']';
25 that.value = function() {
29 for (var i = 0; i < node.attributes.length; i++) {
30 var a = node.attributes[i];
31 if (a.name === 'data-mkws-config') {
32 // Treat as a JSON fragment configuring just this widget
33 log(node + ": parsing config fragment '" + a.value + "'");
36 data = $.parseJSON(a.value);
37 for (var key in data) {
38 log(node + ": adding config element " + key + "='" + data[key] + "'");
39 that.config[key] = data[key];
42 alert("Can't parse " + node + " data-mkws-config as JSON: " + a.value);
44 } else if (a.name.match (/^data-mkws-/)) {
45 var name = a.name.replace(/^data-mkws-/, '')
46 that.config[name] = a.value;
47 log(node + ": set data-mkws attribute " + name + "='" + a.value + "'");
48 } else if (!ignoreAttrs[a.name]) {
49 that.config[a.name] = a.value;
50 log(node + ": set regular attribute " + a.name + "='" + a.value + "'");
54 var fn = mkws.promotionFunction(type);
57 log("made " + type + " widget(node=" + node + ")");
59 log("made UNPROMOTED widget(type=" + type + ", node=" + node + ")");
66 // Utility function for use by all widgets that can invoke autosearch.
67 widget.autosearch = function(widget) {
68 widget.team.queue("ready").subscribe(function() {
69 var query = widget.config.autosearch;
71 if (query.match(/^!param!/)) {
72 var param = query.replace(/^!param!/, '');
73 query = mkws.getParameterByName(param);
74 widget.log("obtained query '" + query + "' from param '" + param + "'");
76 alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
78 } else if (query.match(/^!path!/)) {
79 var index = query.replace(/^!path!/, '');
80 var path = window.location.pathname.split('/');
81 query = path[path.length - index];
82 widget.log("obtained query '" + query + "' from path-component '" + index + "'");
84 alert("This page has a MasterKey widget that needs a query specified by the path-component " + index);
86 } else if (query.match(/^!var!/)) {
87 var name = query.replace(/^!var!/, '');
88 query = window[name]; // It's ridiculous that this works
89 widget.log("obtained query '" + query + "' from variable '" + name + "'");
91 alert("This page has a MasterKey widget that needs a query specified by the '" + name + "' variable");
95 var sortOrder = widget.config.sort;
96 var maxrecs = widget.config.maxrecs;
97 var perpage = widget.config.perpage;
98 var limit = widget.config.limit;
99 var targets = widget.config.targets;
100 var targetfilter = widget.config.targetfilter;
101 var target = widget.config.target;
102 if (target) targetfilter = 'udb=="' + target + '"';
104 var s = "running auto search: '" + query + "'";
105 if (sortOrder) s += " sorted by '" + sortOrder + "'";
106 if (maxrecs) s += " restricted to " + maxrecs + " records";
107 if (perpage) s += " with " + perpage + " per page";
108 if (limit) s += " limited by '" + limit + "'";
109 if (targets) s += " in targets '" + targets + "'";
110 if (targetfilter) s += " constrained by targetfilter '" + targetfilter + "'";
113 widget.team.newSearch(query, sortOrder, maxrecs, perpage, limit, targets, targetfilter);
119 // Functions follow for promoting the regular widget object into
120 // widgets of specific types. These could be moved into their own
124 mkws.registerWidgetType('Targets', function() {
129 <div class="mkwsBytarget mkwsTeam_' + this.team.name() + '">\
130 No information available yet.\
132 $(this.node).css("display", "none");
134 this.team.queue("targets").subscribe(function(data) {
135 var table ='<table><thead><tr>' +
136 '<td>' + M('Target ID') + '</td>' +
137 '<td>' + M('Hits') + '</td>' +
138 '<td>' + M('Diags') + '</td>' +
139 '<td>' + M('Records') + '</td>' +
140 '<td>' + M('State') + '</td>' +
141 '</tr></thead><tbody>';
143 for (var i = 0; i < data.length; i++) {
144 table += "<tr><td>" + data[i].id +
145 "</td><td>" + data[i].hits +
146 "</td><td>" + data[i].diagnostic +
147 "</td><td>" + data[i].records +
148 "</td><td>" + data[i].state + "</td></tr>";
151 table += '</tbody></table>';
152 var subnode = $(that.node).children('.mkwsBytarget');
158 mkws.registerWidgetType('Stat', function() {
162 this.team.queue("stat").subscribe(function(data) {
163 if (that.node.length === 0) alert("huh?!");
165 $(that.node).html('<span class="head">' + M('Status info') + '</span>' +
167 '<span class="clients">' + M('Active clients') + ': ' + data.activeclients + '/' + data.clients + '</span>' +
169 '<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
174 mkws.registerWidgetType('Pager', function() {
178 this.team.queue("pager").subscribe(function(data) {
179 $(that.node).html(drawPager(data))
181 function drawPager(data) {
182 var teamName = that.team.name();
183 var s = '<div style="float: right">' + M('Displaying') + ': '
184 + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
185 ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
186 + data.total + ')</div>';
188 //client indexes pages from 1 but pz2 from 0
190 var pages = Math.ceil(that.team.totalRecordCount() / that.team.perpage());
191 var currentPage = that.team.currentPage();
193 var firstClkbl = (currentPage - onsides > 0)
194 ? currentPage - onsides
197 var lastClkbl = firstClkbl + 2*onsides < pages
198 ? firstClkbl + 2*onsides
201 var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span> | ';
203 prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + teamName + '\');">'
204 +'<< ' + M('Prev') + '</a> | ';
207 for(var i = firstClkbl; i <= lastClkbl; i++) {
210 numLabel = '<span class="mkwsSelected">' + i + '</span>';
212 middle += '<a href="#" onclick="mkws.showPage(\'' + teamName + '\', ' + i + ')"> '
213 + numLabel + ' </a>';
216 var next = ' | <span class="mkwsNext">' + M('Next') + ' >></span>';
217 if (pages - currentPage > 0)
218 next = ' | <a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + teamName + '\')">'
219 + M('Next') + ' >></a>';
226 if (lastClkbl < pages)
229 s += '<div style="float: clear">'
230 + prev + predots + middle + postdots + next + '</div>';
238 mkws.registerWidgetType('Results', function() {
239 // Nothing to do apart from act as an autosearch trigger
240 // Contained elements do all the real work
241 widget.autosearch(this);
245 mkws.registerWidgetType('Records', function() {
247 var team = this.team;
249 this.team.queue("records").subscribe(function(data) {
251 for (var i = 0; i < data.hits.length; i++) {
252 var hit = data.hits[i];
253 that.team.queue("record").publish(hit);
254 var divId = team.recordElementId(hit.recid[0]);
255 html.push('<div class="record mkwsTeam_' + team.name() + ' ' + divId + '">', renderSummary(hit), '</div>');
256 // ### At some point, we may be able to move the
257 // m_currentRecordId and m_currentRecordData members
258 // from the team object into this widget.
259 if (hit.recid == team.currentRecordId()) {
260 if (team.currentRecordData())
261 html.push(team.renderDetails(team.currentRecordData()));
264 $(that.node).html(html.join(''));
266 function renderSummary(hit) {
267 var template = team.loadTemplate(that.config.template || "Summary");
268 hit._id = team.recordElementId(hit.recid[0]);
269 hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');return false;"
270 return template(hit);
274 widget.autosearch(that);
278 mkws.registerWidgetType('Navi', function() {
280 var teamName = this.team.name();
283 this.team.queue("navi").subscribe(function() {
284 var filters = that.team.filters();
287 filters.visitTargets(function(id, name) {
288 if (text) text += " | ";
289 text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + teamName +
290 "', '" + id + "'" + ');return false;">' + name + '</a>';
293 filters.visitFields(function(field, value) {
294 if (text) text += " | ";
295 text += M(field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + teamName +
296 "', '" + field + "', '" + value + "'" +
297 ');return false;">' + value + '</a>';
300 $(that.node).html(text);
305 // It seems this and the Perpage widget doen't need to subscribe to
306 // anything, since they produce events rather than consuming them.
308 mkws.registerWidgetType('Sort', function() {
311 $(this.node).change(function() {
312 that.team.set_sortOrder($(that.node).val());
313 if (that.team.submitted()) {
321 mkws.registerWidgetType('Perpage', function() {
324 $(this.node).change(function() {
325 that.team.set_perpage($(that.node).val());
326 if (that.team.submitted()) {
334 mkws.registerWidgetType('Done', function() {
337 this.team.queue("complete").subscribe(function(n) {
338 $(that.node).html("Search complete: found " + n + " records");
343 mkws.registerWidgetType('Switch', function() {
344 var tname = this.team.name();
346 <a href="#" onclick="mkws.switchView(\'' + tname + '\', \'records\')">Records</a><span> \
348 </span><a href="#" onclick="mkws.switchView(\'' + tname + '\', \'targets\')">Targets</a>');
352 mkws.registerWidgetType('Search', function() {
353 var tname = this.team.name();
357 <form name="mkwsSearchForm" class="mkwsSearchForm mkwsTeam_' + tname + '" action="" >\
358 <input class="mkwsQuery mkwsTeam_' + tname + '" type="text" size="' + this.config.query_width + '" />\
359 <input class="mkwsButton mkwsTeam_' + tname + '" type="submit" value="' + M('Search') + '" />\
364 mkws.registerWidgetType('SearchForm', function() {
365 var team = this.team;
366 $(this.node).submit(function() {
367 var val = team.widget('Query').value();
374 mkws.registerWidgetType('Results', function() {
375 var tname = this.team.name();
378 <table width="100%" border="0" cellpadding="6" cellspacing="0">\
380 <td class="mkwsTermlistContainer1 mkwsTeam_' + tname + '" width="250" valign="top">\
381 <div class="mkwsTermlists mkwsTeam_' + tname + '"></div>\
383 <td class="mkwsMOTDContainer mkwsTeam_' + tname + '" valign="top">\
384 <div class="mkwsRanking mkwsTeam_' + tname + '"></div>\
385 <div class="mkwsPager mkwsTeam_' + tname + '"></div>\
386 <div class="mkwsNavi mkwsTeam_' + tname + '"></div>\
387 <div class="mkwsRecords mkwsTeam_' + tname + '"></div>\
392 <div class="mkwsTermlistContainer2 mkwsTeam_' + tname + '"></div>\