var M = mkws.M;
- if (type === 'Targets') {
- promoteTargets();
- } else if (type === 'Stat') {
- promoteStat();
- } else if (type === 'Termlists') {
- promoteTermlists();
+ 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>' +
}
});
}
+
+
+ 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];
+ html.push('<div class="record" id="mkwsRecdiv_' + team.name() + '_' + hit.recid + '" >',
+ 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(onSortChange);
+
+ function onSortChange()
+ {
+ team.set_sortOrder($(node).val());
+ if (!team.submitted()) return false;
+ team.resetPage();
+ team.reShow();
+ return false;
+ }
+ }
+
+
+ function promotePerpage() {
+ $(node).change(onPerpageChange);
+
+ function onPerpageChange()
+ {
+ team.set_perpage($(node).val());
+ if (!team.submitted()) return false;
+ 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_sortOrder; // will be set below
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();
mkws.debug(m_teamName + ": " + timestamp + s);
}
+ that.debug = debug;
debug("start running MKWS");
function onShow(data, teamName) {
debug("show");
m_totalRecordCount = 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_currentRecordId) {
- if (m_currentRecordData)
- html.push(renderDetails(m_currentRecordData));
- }
- }
- results.html(html.join(''));
+ queue("pager").publish(data);
+ queue("records").publish(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(m_totalRecordCount / m_perpage);
-
- var firstClkbl = (m_currentPage - onsides > 0)
- ? m_currentPage - onsides
- : 1;
-
- var lastClkbl = firstClkbl + 2*onsides < pages
- ? firstClkbl + 2*onsides
- : pages;
-
- var prev = '<span class="mkwsPrev"><< ' + M('Prev') + '</span><b> | </b>';
- if (m_currentPage > 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_currentPage)
- 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_currentPage > 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;
- }
-
-
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
}
m_filters = []
- redrawNavi();
- resetPage();
- loadSelect();
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_sortOrder);
+ 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_currentPage = 1;
- m_totalRecordCount = 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_sortOrder) {
- debug("changing m_sortOrder from " + m_sortOrder + " to " + node.val());
- m_sortOrder = 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, sortOrder, targets)
{
+ resetPage();
+ queue("navi").publish();
+
var pp2filter = "";
var pp2limit = "";
}
- // 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;
- }
-
-
- // 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.reShow = function() {
+ m_paz.show(0, m_perpage, m_sortOrder);
}
- 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)
{
alert("Unknown view '" + view + "'");
}
}
-
-
that.switchView = switchView;
// .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 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;
// 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 instead mkwsResults.
+ // mkwsTermlists inside mkwsResults.
$('[class^="mkws"],[class*=" mkws"]').each(function () {
mkws.handleNodeWithTeam(this, function(tname, type) {
var myTeam = mkws.teams[tname];