var M = mkws.M;
- if (type === 'Targets') {
- promoteTargets();
- } else if (type === 'Stat') {
- promoteStat();
+ var type2fn = {
+ Targets: promoteTargets,
+ Stat: promoteStat,
+ Termlists: promoteTermlists,
+ Pager: promotePager,
+ Records: promoteRecords,
+ Navi: promoteNavi,
+ Sort: promoteSort,
+ Perpage: promotePerpage
+ };
+
+ var promote = type2fn[type];
+ if (promote) {
+ promote();
+ team.debug("made " + type + " widget(node=" + node + ")");
} else {
- // ### Handle other types here
+ team.debug("made UNENCAPSULATED widget(type=" + type + ", node=" + node + ")");
}
- mkws.debug("made widget(team=" + team + ", type=" + type + ", node=" + node);
return that;
function promoteTargets() {
team.queue("targets").subscribe(function(data) {
- if (node.length === 0) alert("huh?!");
-
var table ='<table><thead><tr>' +
'<td>' + M('Target ID') + '</td>' +
'<td>' + M('Hits') + '</td>' +
'<span class="records">' + M('Retrieved records') + ': ' + data.records + '/' + data.hits + '</span>');
});
}
+
+
+ function promoteTermlists() {
+ team.queue("termlists").subscribe(function(data) {
+ if (!node) {
+ alert("termlists event when there are no termlists");
+ return;
+ }
+
+ // no facets: this should never happen
+ if (!mkws_config.facets || mkws_config.facets.length == 0) {
+ alert("onTerm called even though we have no facets: " + $.toJSON(data));
+ $(node).hide();
+ return;
+ }
+
+ // display if we first got results
+ $(node).show();
+
+ var acc = [];
+ acc.push('<div class="title">' + M('Termlists') + '</div>');
+ var facets = mkws_config.facets;
+
+ for (var i = 0; i < facets.length; i++) {
+ if (facets[i] == "xtargets") {
+ addSingleFacet(acc, "Sources", data.xtargets, 16, null);
+ } else if (facets[i] == "subject") {
+ addSingleFacet(acc, "Subjects", data.subject, 10, "subject");
+ } else if (facets[i] == "author") {
+ addSingleFacet(acc, "Authors", data.author, 10, "author");
+ } else {
+ alert("bad facet configuration: '" + facets[i] + "'");
+ }
+ }
+
+ $(node).html(acc.join(''));
+
+ function addSingleFacet(acc, caption, data, max, pzIndex) {
+ acc.push('<div class="facet mkwsFacet' + caption + ' mkwsTeam_' + team.name() + '">');
+ acc.push('<div class="termtitle">' + M(caption) + '</div>');
+ for (var i = 0; i < data.length && i < max; i++) {
+ acc.push('<div class="term">');
+ acc.push('<a href="#" ');
+ var action = '';
+ if (!pzIndex) {
+ // Special case: target selection
+ acc.push('target_id='+data[i].id+' ');
+ if (!team.targetFiltered(data[i].id)) {
+ action = 'mkws.limitTarget(\'' + team.name() + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
+ }
+ } else {
+ action = 'mkws.limitQuery(\'' + team.name() + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
+ }
+ acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
+ + ' <span>' + data[i].freq + '</span>');
+ acc.push('</div>');
+ }
+ acc.push('</div>');
+ }
+ });
+ }
+
+
+ function promotePager() {
+ team.queue("pager").subscribe(function(data) {
+ $(node).html(drawPager(data))
+
+ function drawPager(data) {
+ var s = '<div style="float: right">' + M('Displaying') + ': '
+ + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
+ ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
+ + data.total + ')</div>';
+
+ //client indexes pages from 1 but pz2 from 0
+ var onsides = 6;
+ var pages = Math.ceil(team.totalRecordCount() / team.perpage());
+ var currentPage = team.currentPage();
+
+ var firstClkbl = (currentPage - onsides > 0)
+ ? currentPage - onsides
+ : 1;
+
+ var lastClkbl = firstClkbl + 2*onsides < pages
+ ? firstClkbl + 2*onsides
+ : pages;
+
+ var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span><b> | </b>';
+ if (currentPage > 1)
+ prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + team.name() + '\');">'
+ +'<< ' + M('Prev') + '</a><b> | </b>';
+
+ var middle = '';
+ for(var i = firstClkbl; i <= lastClkbl; i++) {
+ var numLabel = i;
+ if(i == currentPage)
+ numLabel = '<b>' + i + '</b>';
+
+ middle += '<a href="#" onclick="mkws.showPage(\'' + team.name() + '\', ' + i + ')"> '
+ + numLabel + ' </a>';
+ }
+
+ var next = '<b> | </b><span class="mkwsNext">' + M('Next') + ' >></span>';
+ if (pages - currentPage > 0)
+ next = '<b> | </b><a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + team.name() + '\')">'
+ + M('Next') + ' >></a>';
+
+ var predots = '';
+ if (firstClkbl > 1)
+ predots = '...';
+
+ var postdots = '';
+ if (lastClkbl < pages)
+ postdots = '...';
+
+ s += '<div style="float: clear">'
+ + prev + predots + middle + postdots + next + '</div>';
+
+ return s;
+ }
+ });
+ }
+
+
+ function promoteRecords() {
+ team.queue("records").subscribe(function(data) {
+ var html = [];
+ for (var i = 0; i < data.hits.length; i++) {
+ var hit = data.hits[i];
+ var divId = team.recordElementId(hit.recid[0]);
+ html.push('<div class="record mkwsTeam_' + team.name() + '" id="' + divId + '">', renderSummary(hit), '</div>');
+ // ### At some point, we may be able to move the
+ // m_currentRecordId and m_currentRecordData members
+ // from the team object into this widget.
+ if (hit.recid == team.currentRecordId()) {
+ if (team.currentRecordData())
+ html.push(team.renderDetails(team.currentRecordData()));
+ }
+ }
+ $(node).html(html.join(''));
+
+ function renderSummary(hit)
+ {
+ var template = team.loadTemplate("Summary");
+ hit._id = "mkwsRec_" + hit.recid;
+ hit._onclick = "mkws.showDetails('" + team.name() + "', this.id);return false;"
+ return template(hit);
+ }
+ });
+ }
+
+
+ function promoteNavi() {
+ team.queue("navi").subscribe(function() {
+ var filters = team.filters();
+ var text = "";
+
+ for (var i in filters) {
+ if (text) {
+ text += " | ";
+ }
+ var filter = filters[i];
+ if (filter.id) {
+ text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + team.name() +
+ "', '" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
+ } else {
+ text += M(filter.field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + team.name() +
+ "', '" + filter.field + "', '" + filter.value + "'" +
+ ');return false;">' + filter.value + '</a>';
+ }
+ }
+
+ $(node).html(text);
+ });
+ }
+
+
+ function promoteSort() {
+ // It seems this and the Perpage widget doen't need to
+ // subscribe to anything, since they produce events rather
+ // than consuming them.
+ $(node).change(function () {
+ team.set_sortOrder($(node).val());
+ if (team.submitted()) {
+ team.resetPage();
+ team.reShow();
+ }
+ return false;
+ });
+ }
+
+
+ function promotePerpage() {
+ $(node).change(function() {
+ team.set_perpage($(node).val());
+ if (team.submitted()) {
+ team.resetPage();
+ team.reShow();
+ }
+ return false;
+ });
+ }
}
function team($, teamName) {
var that = {};
var m_teamName = teamName;
- that.name = function() { return m_teamName; }
var m_submitted = false;
var m_query; // initially undefined
- var m_sort; // will be set below
+ var m_sortOrder; // will be set below
var m_perpage; // will be set below
var m_filters = [];
- var m_totalRec = 0;
- var m_curPage = 1;
- var m_curDetRecId = '';
- var m_curDetRecData = null;
- var m_debug_time = {
+ var m_totalRecordCount = 0;
+ var m_currentPage = 1;
+ var m_currentRecordId = '';
+ var m_currentRecordData = null;
+ var m_debugTime = {
// Timestamps for logging
"start": $.now(),
"last": $.now()
var m_paz; // will be initialised below
var m_template = {};
+ that.name = function() { return m_teamName; }
+ that.submitted = function() { return m_submitted; }
+ that.perpage = function() { return m_perpage; }
+ that.totalRecordCount = function() { return m_totalRecordCount; }
+ that.currentPage = function() { return m_currentPage; }
+ that.currentRecordId = function() { return m_currentRecordId; }
+ that.currentRecordData = function() { return m_currentRecordData; }
+ that.filters = function() { return m_filters; }
+
+ that.set_sortOrder = function(val) { m_sortOrder = val };
+ that.set_perpage = function(val) { m_perpage = val };
+
var debug = function (s) {
var now = $.now();
- var timestamp = ((now - m_debug_time.start)/1000).toFixed(3) + " (+" + ((now - m_debug_time.last)/1000).toFixed(3) + ") "
- m_debug_time.last = now;
+ var timestamp = ((now - m_debugTime.start)/1000).toFixed(3) + " (+" + ((now - m_debugTime.last)/1000).toFixed(3) + ") "
+ m_debugTime.last = now;
mkws.debug(m_teamName + ": " + timestamp + s);
}
+ that.debug = debug;
debug("start running MKWS");
- m_sort = mkws_config.sort_default;
+ m_sortOrder = mkws_config.sort_default;
m_perpage = mkws_config.perpage_default;
debug("Create main pz2 object");
function onTerm(data) {
debug("term");
- var node = findnode(".mkwsTermlists");
- if (node.length == 0) return;
-
- // no facets: this should never happen
- if (!mkws_config.facets || mkws_config.facets.length == 0) {
- alert("onTerm called even though we have no facets: " + $.toJSON(data));
- node.hide();
- return;
- }
-
- // display if we first got results
- node.show();
-
- var acc = [];
- acc.push('<div class="title">' + M('Termlists') + '</div>');
- var facets = mkws_config.facets;
-
- for(var i = 0; i < facets.length; i++) {
- if (facets[i] == "xtargets") {
- addSingleFacet(acc, "Sources", data.xtargets, 16, null);
- } else if (facets[i] == "subject") {
- addSingleFacet(acc, "Subjects", data.subject, 10, "subject");
- } else if (facets[i] == "author") {
- addSingleFacet(acc, "Authors", data.author, 10, "author");
- } else {
- alert("bad facet configuration: '" + facets[i] + "'");
- }
- }
-
- node.html(acc.join(''));
+ queue("termlists").publish(data);
}
function onShow(data, teamName) {
debug("show");
- m_totalRec = data.merged;
-
- var pager = findnode(".mkwsPager");
- if (pager.length) {
- pager.html(drawPager(data))
- }
-
- var results = findnode(".mkwsRecords");
- if (!results.length)
- return;
-
- var html = [];
- for (var i = 0; i < data.hits.length; i++) {
- var hit = data.hits[i];
- html.push('<div class="record" id="mkwsRecdiv_' + teamName + '_' + hit.recid + '" >',
- renderSummary(hit),
- '</div>');
- if (hit.recid == m_curDetRecId) {
- if (m_curDetRecData)
- html.push(renderDetails(m_curDetRecData));
- }
- }
- results.html(html.join(''));
+ m_totalRecordCount = data.merged;
+ queue("pager").publish(data);
+ queue("records").publish(data);
}
// in case on_show was faster to redraw element
var detRecordDiv = document.getElementById('mkwsDet_' + teamName + '_' + data.recid);
if (detRecordDiv) return;
- m_curDetRecData = data;
- // Can't use jQuery's $('#x') syntax to find this ID, because it contains spaces.
- var recordDiv = document.getElementById('mkwsRecdiv_' + teamName + '_' + m_curDetRecData.recid);
- var html = renderDetails(m_curDetRecData);
+ m_currentRecordData = data;
+ var recordDiv = findnode(recordElementId(m_currentRecordData.recid[0]));
+ var html = renderDetails(m_currentRecordData);
$(recordDiv).append(html);
}
- function addSingleFacet(acc, caption, data, max, pzIndex) {
- acc.push('<div class="facet mkwsFacet' + caption + ' mkwsTeam_' + m_teamName + '">');
- acc.push('<div class="termtitle">' + M(caption) + '</div>');
- for (var i = 0; i < data.length && i < max; i++) {
- acc.push('<div class="term">');
- acc.push('<a href="#" ');
- var action = '';
- if (!pzIndex) {
- // Special case: target selection
- acc.push('target_id='+data[i].id+' ');
- if (!targetFiltered(data[i].id)) {
- action = 'mkws.limitTarget(\'' + m_teamName + '\', this.getAttribute(\'target_id\'),this.firstChild.nodeValue)';
- }
- } else {
- action = 'mkws.limitQuery(\'' + m_teamName + '\', \'' + pzIndex + '\', this.firstChild.nodeValue)';
- }
- acc.push('onclick="' + action + ';return false;">' + data[i].name + '</a>'
- + ' <span>' + data[i].freq + '</span>');
- acc.push('</div>');
- }
- acc.push('</div>');
+ // Used by promoteRecords() and onRecord()
+ function recordElementId(s) {
+ return 'mkwsRec_' + s.replace(/[^a-z0-9]/ig, '_');
}
+ that.recordElementId = recordElementId;
- function targetFiltered(id) {
+ that.targetFiltered = function(id) {
for (var i = 0; i < m_filters.length; i++) {
if (m_filters[i].id === id ||
m_filters[i].id === 'pz:id=' + id) {
}
- function drawPager (data)
- {
- var s = '<div style="float: right">' + M('Displaying') + ': '
- + (data.start + 1) + ' ' + M('to') + ' ' + (data.start + data.num) +
- ' ' + M('of') + ' ' + data.merged + ' (' + M('found') + ': '
- + data.total + ')</div>';
-
- //client indexes pages from 1 but pz2 from 0
- var onsides = 6;
- var pages = Math.ceil(m_totalRec / m_perpage);
-
- var firstClkbl = (m_curPage - onsides > 0)
- ? m_curPage - onsides
- : 1;
-
- var lastClkbl = firstClkbl + 2*onsides < pages
- ? firstClkbl + 2*onsides
- : pages;
-
- var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span><b> | </b>';
- if (m_curPage > 1)
- prev = '<a href="#" class="mkwsPrev" onclick="mkws.pagerPrev(\'' + m_teamName + '\');">'
- +'<< ' + M('Prev') + '</a><b> | </b>';
-
- var middle = '';
- for(var i = firstClkbl; i <= lastClkbl; i++) {
- var numLabel = i;
- if(i == m_curPage)
- numLabel = '<b>' + i + '</b>';
-
- middle += '<a href="#" onclick="mkws.showPage(\'' + m_teamName + '\', ' + i + ')"> '
- + numLabel + ' </a>';
- }
-
- var next = '<b> | </b><span class="mkwsNext">' + M('Next') + ' >></span>';
- if (pages - m_curPage > 0)
- next = '<b> | </b><a href="#" class="mkwsNext" onclick="mkws.pagerNext(\'' + m_teamName + '\')">'
- + M('Next') + ' >></a>';
-
- var predots = '';
- if (firstClkbl > 1)
- predots = '...';
-
- var postdots = '';
- if (lastClkbl < pages)
- postdots = '...';
-
- s += '<div style="float: clear">'
- + prev + predots + middle + postdots + next + '</div>';
-
- return s;
- }
-
-
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
}
- function newSearch(query, sort, targets)
+ function newSearch(query, sortOrder, targets)
{
debug("newSearch: " + query);
}
m_filters = []
- redrawNavi();
- resetPage();
- loadSelect();
- triggerSearch(query, sort, targets);
+ triggerSearch(query, sortOrder, targets);
switchView('records'); // In case it's configured to start off as hidden
m_submitted = true;
}
- function onSelectDdChange()
+ // limit by target functions
+ that.limitTarget = function (id, name)
{
- if (!m_submitted) return false;
- resetPage();
- loadSelect();
- m_paz.show(0, m_perpage, m_sort);
+ debug("limitTarget(id=" + id + ", name=" + name + ")");
+ m_filters.push({ id: id, name: name });
+ triggerSearch();
return false;
}
- function redrawNavi ()
+ // limit the query after clicking the facet
+ that.limitQuery = function (field, value)
{
- var navi = findnode('.mkwsNavi');
- if (!navi) return;
+ debug("limitQuery(field=" + field + ", value=" + value + ")");
+ m_filters.push({ field: field, value: value });
+ triggerSearch();
+ return false;
+ }
+
- var text = "";
+ that.delimitTarget = function (id)
+ {
+ debug("delimitTarget(id=" + id + ")");
+ var newFilters = [];
for (var i in m_filters) {
- if (text) {
- text += " | ";
- }
var filter = m_filters[i];
if (filter.id) {
- text += M('source') + ': <a class="crossout" href="#" onclick="mkws.delimitTarget(\'' + m_teamName +
- "', '" + filter.id + "'" + ');return false;">' + filter.name + '</a>';
+ debug("delimitTarget() removing filter " + $.toJSON(filter));
} else {
- text += M(filter.field) + ': <a class="crossout" href="#" onclick="mkws.delimitQuery(\'' + m_teamName +
- "', '" + filter.field + "', '" + filter.value + "'" +
- ');return false;">' + filter.value + '</a>';
+ debug("delimitTarget() keeping filter " + $.toJSON(filter));
+ newFilters.push(filter);
}
}
+ m_filters = newFilters;
- navi.html(text);
+ triggerSearch();
+ return false;
}
- function resetPage()
+ that.delimitQuery = function (field, value)
{
- m_curPage = 1;
- m_totalRec = 0;
+ debug("delimitQuery(field=" + field + ", value=" + value + ")");
+ var newFilters = [];
+ for (var i in m_filters) {
+ var filter = m_filters[i];
+ if (filter.field &&
+ field == filter.field &&
+ value == filter.value) {
+ debug("delimitQuery() removing filter " + $.toJSON(filter));
+ } else {
+ debug("delimitQuery() keeping filter " + $.toJSON(filter));
+ newFilters.push(filter);
+ }
+ }
+ m_filters = newFilters;
+
+ triggerSearch();
+ return false;
}
- function loadSelect ()
+ function resetPage()
{
- var node = findnode('.mkwsSort');
- if (node.length && node.val() != m_sort) {
- debug("changing m_sort from " + m_sort + " to " + node.val());
- m_sort = node.val();
- }
- node = findnode('.mkwsPerpage');
- if (node.length && node.val() != m_perpage) {
- debug("changing m_perpage from " + m_perpage + " to " + node.val());
- m_perpage = node.val();
- }
+ m_currentPage = 1;
+ m_totalRecordCount = 0;
}
+ that.resetPage = resetPage;
- function triggerSearch (query, sort, targets)
+ function triggerSearch (query, sortOrder, targets)
{
+ resetPage();
+ queue("navi").publish();
+
var pp2filter = "";
var pp2limit = "";
- // Re-use previous query/sort if new ones are not specified
+ // Continue to use previous query/sort-order unless new ones are specified
if (query) {
m_query = query;
}
- if (sort) {
- m_sort = sort;
+ if (sortOrder) {
+ m_sortOrder = sortOrder;
}
if (targets) {
m_filters.push({ id: targets, name: targets });
// We can use: params.torusquery = "udb=NAME"
// Note: that won't work when running against raw pazpar2
- m_paz.search(m_query, m_perpage, m_sort, pp2filter, undefined, params);
+ m_paz.search(m_query, m_perpage, m_sortOrder, pp2filter, undefined, params);
}
- // limit by target functions
- that.limitTarget = function (id, name)
- {
- debug("limitTarget(id=" + id + ", name=" + name + ")");
- m_filters.push({ id: id, name: name });
- redrawNavi();
- resetPage();
- loadSelect();
- triggerSearch();
- return false;
+ that.reShow = function() {
+ m_paz.show(0, m_perpage, m_sortOrder);
}
- // limit the query after clicking the facet
- that.limitQuery = function (field, value)
- {
- debug("limitQuery(field=" + field + ", value=" + value + ")");
- m_filters.push({ field: field, value: value });
- redrawNavi();
- resetPage();
- loadSelect();
- triggerSearch();
- return false;
- }
-
-
- that.delimitTarget = function (id)
- {
- debug("delimitTarget(id=" + id + ")");
- var newFilters = [];
- for (var i in m_filters) {
- var filter = m_filters[i];
- if (filter.id) {
- debug("delimitTarget() removing filter " + $.toJSON(filter));
- } else {
- debug("delimitTarget() keeping filter " + $.toJSON(filter));
- newFilters.push(filter);
- }
- }
- m_filters = newFilters;
-
- redrawNavi();
- resetPage();
- loadSelect();
- triggerSearch();
- return false;
- }
-
-
- that.delimitQuery = function (field, value)
- {
- debug("delimitQuery(field=" + field + ", value=" + value + ")");
- var newFilters = [];
- for (var i in m_filters) {
- var filter = m_filters[i];
- if (filter.field &&
- field == filter.field &&
- value == filter.value) {
- debug("delimitQuery() removing filter " + $.toJSON(filter));
- } else {
- debug("delimitQuery() keeping filter " + $.toJSON(filter));
- newFilters.push(filter);
- }
- }
- m_filters = newFilters;
-
- redrawNavi();
- resetPage();
- loadSelect();
- triggerSearch();
- return false;
- }
-
that.showPage = function (pageNum)
{
- m_curPage = pageNum;
- m_paz.showPage(m_curPage - 1);
+ m_currentPage = pageNum;
+ m_paz.showPage(m_currentPage - 1);
}
// simple paging functions
that.pagerNext = function () {
- if (m_totalRec - m_perpage*m_curPage > 0) {
+ if (m_totalRecordCount - m_perpage*m_currentPage > 0) {
m_paz.showNext();
- m_curPage++;
+ m_currentPage++;
}
}
that.pagerPrev = function () {
if (m_paz.showPrev() != false)
- m_curPage--;
+ m_currentPage--;
}
alert("Unknown view '" + view + "'");
}
}
-
-
that.switchView = switchView;
// detailed record drawing
that.showDetails = function (prefixRecId) {
var recId = prefixRecId.replace('mkwsRec_', '');
- var oldRecId = m_curDetRecId;
- m_curDetRecId = recId;
+ var oldRecordId = m_currentRecordId;
+ m_currentRecordId = recId;
// remove current detailed view if any
- var detRecordDiv = document.getElementById('mkwsDet_' + m_teamName + '_' + oldRecId);
+ var detRecordDiv = document.getElementById('mkwsDet_' + m_teamName + '_' + oldRecordId);
// lovin DOM!
if (detRecordDiv)
detRecordDiv.parentNode.removeChild(detRecordDiv);
// if the same clicked, just hide
- if (recId == oldRecId) {
- m_curDetRecId = '';
- m_curDetRecData = null;
+ if (recId == oldRecordId) {
+ m_currentRecordId = '';
+ m_currentRecordData = null;
return;
}
// request the record
// .mkwsPager
// .mkwsNavi
// .mkwsRecords
- if (findnode(".mkwsResults").length) {
- findnode(".mkwsResults").html('\
+ findnode(".mkwsResults").html('\
<table width="100%" border="0" cellpadding="6" cellspacing="0">\
<tr>\
<td class="mkwsTermlistContainer1 mkwsTeam_' + m_teamName + '" width="250" valign="top">\
</td>\
</tr>\
</table>');
- }
- var node = findnode(".mkwsRanking");
- if (node.length) {
- var ranking_data = '';
- ranking_data += '<form name="mkwsSelect" class="mkwsSelect mkwsTeam_' + m_teamName + '" action="" >';
- if (mkws_config.show_sort) {
- ranking_data += M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
- }
- if (mkws_config.show_perpage) {
- ranking_data += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
- }
- ranking_data += '</form>';
-
- node.html(ranking_data);
+ var ranking_data = '<form name="mkwsSelect" class="mkwsSelect mkwsTeam_' + m_teamName + '" action="" >';
+ if (mkws_config.show_sort) {
+ ranking_data += M('Sort by') + ' ' + mkwsHtmlSort() + ' ';
+ }
+ if (mkws_config.show_perpage) {
+ ranking_data += M('and show') + ' ' + mkwsHtmlPerpage() + ' ' + M('per page') + '.';
}
+ ranking_data += '</form>';
+ findnode(".mkwsRanking").html(ranking_data);
mkwsHtmlSwitch();
- var node;
- node = findnode('.mkwsSearchForm');
- if (node.length)
- node.submit(onFormSubmitEventHandler);
- node = findnode('.mkwsSort');
- if (node.length)
- node.change(onSelectDdChange);
- node = findnode('.mkwsPerpage');
- if (node.length)
- node.change(onSelectDdChange);
+ findnode('.mkwsSearchForm').submit(onFormSubmitEventHandler);
// on first page, hide the termlist
$(document).ready(function() { findnode(".mkwsTermlists").hide(); });
- var motd = findnode(".mkwsMOTD");
var container = findnode(".mkwsMOTDContainer");
- if (motd.length && container.length) {
+ if (container.length) {
// Move the MOTD from the provided element down into the container
- motd.appendTo(container);
+ findnode(".mkwsMOTD").appendTo(container);
}
}
function mkwsSetLang() {
- var lang = parseQuerystring().lang || mkws_config.lang;
+ var lang = mkws.getParameterByName("lang") || mkws_config.lang;
if (!lang || !mkws.locale_lang[lang]) {
mkws_config.lang = ""
} else {
function mkwsHtmlSort() {
- debug("HTML sort, m_sort = '" + m_sort + "'");
+ debug("HTML sort, m_sortOrder = '" + m_sortOrder + "'");
var sort_html = '<select class="mkwsSort mkwsTeam_' + m_teamName + '">';
for(var i = 0; i < mkws_config.sort_options.length; i++) {
var val = opt.length == 1 ? opt[0] : opt[1];
sort_html += '<option value="' + key + '"';
- if (m_sort == key || m_sort == val) {
+ if (m_sortOrder == key || m_sortOrder == val) {
sort_html += ' selected="selected"';
}
sort_html += '>' + M(val) + '</option>';
that.runAutoSearch = function() {
- // ### should check mkwsTermlist as well, for facet-only teams
- var node = findnode('.mkwsRecords');
+ var node = findnode('.mkwsRecords,.mkwsTermlists');
var query = node.attr('autosearch');
if (!query)
return;
if (query.match(/^!param!/)) {
var param = query.replace(/^!param!/, '');
- query = getParameterByName(param);
+ query = mkws.getParameterByName(param);
debug("obtained query '" + query + "' from param '" + param + "'");
if (!query) {
alert("This page has a MasterKey widget that needs a query specified by the '" + param + "' parameter");
debug("node=" + node + ", class='" + node.className + "', query=" + query);
- var sort = node.attr('sort');
+ var sortOrder = node.attr('sort');
var targets = node.attr('targets');
var s = "running auto search: '" + query + "'";
- if (sort) s += " sorted by '" + sort + "'";
+ if (sortOrder) s += " sorted by '" + sortOrder + "'";
if (targets) s += " in targets '" + targets + "'";
debug(s);
- newSearch(query, sort, targets);
- }
-
-
- // implement $.parseQuerystring() for parsing URL parameters
- function parseQuerystring() {
- var nvpair = {};
- var qs = window.location.search.replace('?', '');
- var pairs = qs.split('&');
- $.each(pairs, function(i, v){
- var pair = v.split('=');
- nvpair[pair[0]] = pair[1];
- });
- return nvpair;
- }
-
-
- // This function is taken from a StackOverflow answer
- // http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/901144#901144
- // ### should we unify this and parseQuerystring()?
- function getParameterByName(name) {
- name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
- var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
- results = regex.exec(location.search);
- return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
+ newSearch(query, sortOrder, targets);
}
- /* locale */
+ // Translation function. At present, this is properly a
+ // global-level function (hence the assignment to mkws.M) but we
+ // want to make it per-team so different teams can operate in
+ // different languages.
+ //
function M(word) {
var lang = mkws_config.lang;
function findnode(selector, teamName) {
teamName = teamName || m_teamName;
- selector = selector.split(',').map(function(s) {
+ selector = $.map(selector.split(','), function(s, i) {
return s + '.mkwsTeam_' + teamName;
}).join(',');
}
- function renderSummary(hit)
- {
- var template = loadTemplate("Summary");
- hit._id = "mkwsRec_" + hit.recid;
- hit._onclick = "mkws.showDetails('" + m_teamName + "', this.id);return false;"
- return template(hit);
- }
-
-
function renderDetails(data, marker)
{
var template = loadTemplate("Record");
var details = template(data);
return '<div class="details" id="mkwsDet_' + m_teamName + '_' + data.recid + '">' + details + '</div>';
}
+ that.renderDetails = renderDetails;
function loadTemplate(name)
return template;
}
+ that.loadTemplate = loadTemplate;
// The following PubSub code is modified from the jQuery manual:
}
return queues[id];
}
-
that.queue = queue;
}
+ // This function is taken from a StackOverflow answer
+ // http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript/901144#901144
+ mkws.getParameterByName = function(name) {
+ name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
+ var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
+ results = regex.exec(location.search);
+ return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
+ }
+
+
mkws.defaultTemplate = function(name)
{
if (name === 'Record') {
}
});
- // Find all nodes with an class, and determine their team from
+ // Find all nodes with an MKWS class, and determine their team from
// the mkwsTeam_* class. Make all team objects.
var then = $.now();
$('[class^="mkws"],[class*=" mkws"]').each(function () {
mkws.teams[tname] = team(j, tname);
debug("Made MKWS team '" + tname + "'");
}
+ });
+ });
+ // Second pass: make the individual widget objects. This has
+ // to be done separately, and after the team-creation, since
+ // that sometimes makes new widget nodes (e.g. creating
+ // mkwsTermlists inside mkwsResults.
+ $('[class^="mkws"],[class*=" mkws"]').each(function () {
+ mkws.handleNodeWithTeam(this, function(tname, type) {
var myTeam = mkws.teams[tname];
var myWidget = widget(j, myTeam, type, this);
});