X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;ds=sidebyside;f=tools%2Fhtdocs%2Fmkws.js;h=d03dcd50f365352535a70978f317b901f7394c7a;hb=697da818a4a182e4ae50c13b6937226fa3ba921f;hp=04dca89fa97f87c725f74f4eabc7a61cf5554b91;hpb=61b3773a2c551e9a7114a2057b2e2fb601db7e4d;p=mkws-moved-to-github.git
diff --git a/tools/htdocs/mkws.js b/tools/htdocs/mkws.js
index 04dca89..d03dcd5 100644
--- a/tools/htdocs/mkws.js
+++ b/tools/htdocs/mkws.js
@@ -3,7 +3,7 @@
* See the file LICENSE for details
*/
-"use strict"; // HTML5: disable for debug_level >= 2
+"use strict"; // HTML5: disable for log_level >= 2
// Handlebars helpers
@@ -76,8 +76,8 @@ Handlebars.registerHelper('index1', function(obj) {
//
var mkws = {
authenticated: false,
- debug_level: 1, // Will be overridden from mkws_config, but
- // initial value allows jQuery popup to use logging.
+ log_level: 1, // Will be overridden from mkws_config, but
+ // initial value allows jQuery popup to use logging.
paz: undefined, // will be set up during initialisation
teams: {},
locale_lang: {
@@ -158,30 +158,38 @@ function widget($, team, type, node) {
var M = mkws.M;
- if (type === 'Targets') {
- promoteTargets();
- } else if (type === 'Stat') {
- promoteStat();
- } else if (type === 'Termlists') {
- promoteTermlists();
- } else if (type === 'Pager') {
- promotePager();
+ 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();
+ log("made " + type + " widget(node=" + node + ")");
} else {
- // ### Handle other types here
+ log("made UNENCAPSULATED widget(type=" + type + ", node=" + node + ")");
}
- mkws.debug("made widget(team=" + team + ", type=" + type + ", node=" + node);
return that;
+ function log(s) {
+ team.log(s);
+ }
+
// Functions follow for promoting the regular widget object into
// widgets of specific types. These could be moved outside of the
// widget object, or even into their own source files.
function promoteTargets() {
team.queue("targets").subscribe(function(data) {
- if (node.length === 0) alert("huh?!");
-
var table ='
' +
'
' + M('Target ID') + '
' +
'
' + M('Hits') + '
' +
@@ -220,7 +228,6 @@ function widget($, team, type, node) {
function promoteTermlists() {
team.queue("termlists").subscribe(function(data) {
- mkws.debug("in termlist consumer");
if (!node) {
alert("termlists event when there are no termlists");
return;
@@ -282,9 +289,7 @@ function widget($, team, type, node) {
function promotePager() {
team.queue("pager").subscribe(function(data) {
- if (node) {
- $(node).html(drawPager(data))
- }
+ $(node).html(drawPager(data))
function drawPager(data) {
var s = '
' + M('Displaying') + ': '
@@ -340,6 +345,86 @@ function widget($, team, type, node) {
}
});
}
+
+
+ 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('
', renderSummary(hit), '
');
+ // ### 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 = team.recordElementId(hit.recid[0]);
+ hit._onclick = "mkws.showDetails('" + team.name() + "', '" + hit.recid[0] + "');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') + ': ' + filter.name + '';
+ } else {
+ text += M(filter.field) + ': ' + filter.value + '';
+ }
+ }
+
+ $(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;
+ });
+ }
}
@@ -363,7 +448,7 @@ function team($, teamName) {
var m_currentPage = 1;
var m_currentRecordId = '';
var m_currentRecordData = null;
- var m_debugTime = {
+ var m_logTime = {
// Timestamps for logging
"start": $.now(),
"last": $.now()
@@ -372,24 +457,33 @@ function team($, teamName) {
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 log = function (s) {
var now = $.now();
- var timestamp = ((now - m_debugTime.start)/1000).toFixed(3) + " (+" + ((now - m_debugTime.last)/1000).toFixed(3) + ") "
- m_debugTime.last = now;
+ var timestamp = ((now - m_logTime.start)/1000).toFixed(3) + " (+" + ((now - m_logTime.last)/1000).toFixed(3) + ") "
+ m_logTime.last = now;
- mkws.debug(m_teamName + ": " + timestamp + s);
+ mkws.log(m_teamName + ": " + timestamp + s);
}
+ that.log = log;
- debug("start running MKWS");
+ log("start running MKWS");
m_sortOrder = mkws_config.sort_default;
m_perpage = mkws_config.perpage_default;
- debug("Create main pz2 object");
+ log("Create main pz2 object");
// create a parameters array and pass it to the pz2's constructor
// then register the form submit event with the pz2.search function
// autoInit is set to true on default
@@ -411,70 +505,68 @@ function team($, teamName) {
// pz2.js event handlers:
//
function onInit() {
- debug("init");
+ log("init");
m_paz.stat();
m_paz.bytarget();
}
function onBytarget(data) {
- debug("target");
+ log("target");
queue("targets").publish(data);
}
function onStat(data) {
- debug("stat");
+ log("stat");
queue("stat").publish(data);
}
function onTerm(data) {
- debug("term");
+ log("term");
queue("termlists").publish(data);
}
function onShow(data, teamName) {
- debug("show");
+ log("show");
m_totalRecordCount = data.merged;
-
queue("pager").publish(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('
',
- renderSummary(hit),
- '
');
- if (hit.recid == m_currentRecordId) {
- if (m_currentRecordData)
- html.push(renderDetails(m_currentRecordData));
- }
- }
- results.html(html.join(''));
+ queue("records").publish(data);
}
function onRecord(data, args, teamName) {
- debug("record");
+ log("record");
// FIXME: record is async!!
clearTimeout(m_paz.recordTimer);
- // in case on_show was faster to redraw element
- var detRecordDiv = document.getElementById('mkwsDet_' + teamName + '_' + data.recid);
- if (detRecordDiv) return;
+ // ##### restrict to current team
+ var detRecordDiv = document.getElementById(recordDetailsId(data.recid[0]));
+ if (detRecordDiv) {
+ // in case on_show was faster to redraw element
+ return;
+ }
m_currentRecordData = data;
- // Can't use jQuery's $('#x') syntax to find this ID, because it contains spaces.
- var recordDiv = document.getElementById('mkwsRecdiv_' + teamName + '_' + m_currentRecordData.recid);
+ var recordDiv = findnode('.' + recordElementId(m_currentRecordData.recid[0]));
var html = renderDetails(m_currentRecordData);
$(recordDiv).append(html);
}
+ // Used by promoteRecords() and onRecord()
+ function recordElementId(s) {
+ return 'mkwsRec_' + s.replace(/[^a-z0-9]/ig, '_');
+ }
+ that.recordElementId = recordElementId;
+
+ // Used by onRecord(), showDetails() and renderDetails()
+ function recordDetailsId(s) {
+ return 'mkwsDet_' + s.replace(/[^a-z0-9]/ig, '_');
+ }
+ that.recordElementId = recordElementId;
+
+
that.targetFiltered = function(id) {
for (var i = 0; i < m_filters.length; i++) {
if (m_filters[i].id === id ||
@@ -501,7 +593,7 @@ function team($, teamName) {
function newSearch(query, sortOrder, targets)
{
- debug("newSearch: " + query);
+ log("newSearch: " + query);
if (mkws_config.use_service_proxy && !mkws.authenticated) {
alert("searching before authentication");
@@ -509,74 +601,87 @@ function team($, teamName) {
}
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);
+ log("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;
+ log("limitQuery(field=" + field + ", value=" + value + ")");
+ m_filters.push({ field: field, value: value });
+ triggerSearch();
+ return false;
+ }
+
- var text = "";
+ that.delimitTarget = function (id)
+ {
+ log("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') + ': ' + filter.name + '';
+ log("delimitTarget() removing filter " + $.toJSON(filter));
} else {
- text += M(filter.field) + ': ' + filter.value + '';
+ log("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;
+ log("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) {
+ log("delimitQuery() removing filter " + $.toJSON(filter));
+ } else {
+ log("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 = "";
@@ -597,7 +702,7 @@ function team($, teamName) {
if (pp2filter)
pp2filter += ",";
if (filter.id.match(/^[a-z:]+[=~]/)) {
- debug("filter '" + filter.id + "' already begins with SETTING OP");
+ log("filter '" + filter.id + "' already begins with SETTING OP");
} else {
filter.id = 'pz:id=' + filter.id;
}
@@ -614,7 +719,7 @@ function team($, teamName) {
params.limit = pp2limit;
}
- debug("triggerSearch(" + m_query + "): filters = " + $.toJSON(m_filters) + ", pp2filter = " + pp2filter + ", params = " + $.toJSON(params));
+ log("triggerSearch(" + m_query + "): filters = " + $.toJSON(m_filters) + ", pp2filter = " + pp2filter + ", params = " + $.toJSON(params));
// We can use: params.torusquery = "udb=NAME"
// Note: that won't work when running against raw pazpar2
@@ -622,79 +727,11 @@ function team($, teamName) {
}
- // 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)
{
@@ -749,19 +786,17 @@ function team($, teamName) {
alert("Unknown view '" + view + "'");
}
}
-
-
that.switchView = switchView;
// detailed record drawing
- that.showDetails = function (prefixRecId) {
- var recId = prefixRecId.replace('mkwsRec_', '');
+ that.showDetails = function (recId) {
var oldRecordId = m_currentRecordId;
m_currentRecordId = recId;
// remove current detailed view if any
- var detRecordDiv = document.getElementById('mkwsDet_' + m_teamName + '_' + oldRecordId);
+ // ##### restrict to current team
+ var detRecordDiv = document.getElementById(recordDetailsId(oldRecordId));
// lovin DOM!
if (detRecordDiv)
detRecordDiv.parentNode.removeChild(detRecordDiv);
@@ -773,7 +808,7 @@ function team($, teamName) {
return;
}
// request the record
- debug("showDetails() requesting record '" + recId + "'");
+ log("showDetails() requesting record '" + recId + "'");
m_paz.record(recId);
}
@@ -787,7 +822,7 @@ function team($, teamName) {
if (mkws_config.show_lang)
mkwsHtmlLang();
- debug("HTML search form");
+ log("HTML search form");
mkws.handleNodeWithTeam(findnode('.mkwsSearch'), function(tname) {
this.html('\
');
});
- debug("HTML records");
+ log("HTML records");
// If the team has a .mkwsResults, populate it in the usual
// way. If not, assume that it's a smarter application that
// defines its own subcomponents, some or all of the
@@ -806,8 +841,7 @@ function team($, teamName) {
// .mkwsPager
// .mkwsNavi
// .mkwsRecords
- if (findnode(".mkwsResults").length) {
- findnode(".mkwsResults").html('\
+ findnode(".mkwsResults").html('\
\
\
\
@@ -826,43 +860,27 @@ function team($, teamName) {
\
\
');
- }
- var node = findnode(".mkwsRanking");
- if (node.length) {
- var ranking_data = '';
- ranking_data += '';
-
- node.html(ranking_data);
+ var ranking_data = '';
+ 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);
}
}
@@ -875,7 +893,7 @@ function team($, teamName) {
mkws_config.lang = lang;
}
- debug("Locale language: " + (mkws_config.lang ? mkws_config.lang : "none"));
+ log("Locale language: " + (mkws_config.lang ? mkws_config.lang : "none"));
return mkws_config.lang;
}
@@ -902,7 +920,7 @@ function team($, teamName) {
if (lang_options.length == 0 || toBeIncluded[lang_default])
list.push(lang_default);
- debug("Language menu for: " + list.join(", "));
+ log("Language menu for: " + list.join(", "));
/* the HTML part */
var data = "";
@@ -924,7 +942,7 @@ function team($, teamName) {
function mkwsHtmlSort() {
- debug("HTML sort, m_sortOrder = '" + m_sortOrder + "'");
+ log("HTML sort, m_sortOrder = '" + m_sortOrder + "'");
var sort_html = '