');
acc.push('
' + data[i].name + ' '
+ '
' + data[i].freq + ' ');
@@ -345,12 +449,26 @@ function domReady ()
// when search button pressed
function onFormSubmitEventHandler()
{
- resetPage();
- loadSelect();
- triggerSearch();
+ newSearch(document.mkwsSearchForm.mkwsQuery.value);
+ return false;
+}
+
+function newSearch(query, sort, targets, windowid)
+{
+ debug("newSearch: " + query);
+
+ if (mkws_config.use_service_proxy && !mkws.authenticated) {
+ alert("searching before authentication");
+ return;
+ }
+
+ m_filters = []
+ redraw_navi(); // ### should use windowid
+ resetPage(); // ### the globals it resents should be indexed by windowid
+ loadSelect(); // ### should use windowid
+ triggerSearch(query, sort, targets, windowid);
mkws.switchView('records'); // In case it's configured to start off as hidden
submitted = true;
- return false;
}
function onSelectDdChange()
@@ -358,7 +476,7 @@ function onSelectDdChange()
if (!submitted) return false;
resetPage();
loadSelect();
- my_paz.show(0, recPerPage, curSort);
+ my_paz.show(0, recPerPage, m_sort);
return false;
}
@@ -368,16 +486,58 @@ function resetPage()
totalRec = 0;
}
-function triggerSearch ()
+function triggerSearch (query, sort, targets, windowid)
{
- my_paz.search(document.mkwsSearchForm.mkwsQuery.value, recPerPage, curSort, curFilter);
+ var pp2filter = "";
+ var pp2limit = "";
+
+ // Re-use previous query/sort if new ones are not specified
+ if (query) {
+ mkws.query = query;
+ }
+ if (sort) {
+ m_sort = sort;
+ }
+ if (targets) {
+ // ### should support multiple |-separated targets
+ m_filters.push({ id: targets, name: targets });
+ }
+
+ for (var i in m_filters) {
+ var filter = m_filters[i];
+ if (filter.id) {
+ if (pp2filter)
+ pp2filter += ",";
+ if (filter.id.match(/^[a-z:]+[=~]/)) {
+ debug("filter '" + filter.id + "' already begins with SETTING OP");
+ } else {
+ filter.id = 'pz:id=' + filter.id;
+ }
+ pp2filter += filter.id;
+ } else {
+ if (pp2limit)
+ pp2limit += ",";
+ pp2limit += filter.field + "=" + filter.value.replace(/[\\|,]/g, '\\$&');
+ }
+ }
+
+ var params = {};
+ if (pp2limit) {
+ params.limit = pp2limit;
+ }
+ if (windowid) {
+ params.windowid = windowid;
+ }
+ debug("triggerSearch(" + mkws.query + "): filters = " + $.toJSON(m_filters) + ", pp2filter = " + pp2filter + ", params = " + $.toJSON(params));
+
+ my_paz.search(mkws.query, recPerPage, m_sort, pp2filter, undefined, params);
}
function loadSelect ()
{
if (document.mkwsSelect) {
if (document.mkwsSelect.mkwsSort)
- curSort = document.mkwsSelect.mkwsSort.value;
+ m_sort = document.mkwsSelect.mkwsSort.value;
if (document.mkwsSelect.mkwsPerpage)
recPerPage = document.mkwsSelect.mkwsPerpage.value;
}
@@ -386,35 +546,100 @@ function loadSelect ()
// limit the query after clicking the facet
mkws.limitQuery = function (field, value)
{
- document.mkwsSearchForm.mkwsQuery.value += ' and ' + field + '="' + value + '"';
- onFormSubmitEventHandler();
+ debug("limitQuery(field=" + field + ", value=" + value + ")");
+ m_filters.push({ field: field, value: value });
+ redraw_navi();
+ resetPage();
+ loadSelect();
+ triggerSearch();
+ return false;
}
// limit by target functions
mkws.limitTarget = function (id, name)
{
- var navi = document.getElementById('mkwsNavi');
- navi.innerHTML =
- 'Source:
'
- + name + ' ';
- curFilter = 'pz:id=' + id;
+ debug("limitTarget(id=" + id + ", name=" + name + ")");
+ m_filters.push({ id: id, name: name });
+ redraw_navi();
resetPage();
loadSelect();
triggerSearch();
return false;
}
-mkws.delimitTarget = function ()
+mkws.delimitQuery = function (field, value)
{
- var navi = document.getElementById('mkwsNavi');
- navi.innerHTML = '';
- curFilter = null;
+ 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("delimitTarget() removing filter " + $.toJSON(filter));
+ } else {
+ debug("delimitTarget() keeping filter " + $.toJSON(filter));
+ newFilters.push(filter);
+ }
+ }
+ m_filters = newFilters;
+
+ redraw_navi();
+ resetPage();
+ loadSelect();
+ triggerSearch();
+ return false;
+}
+
+
+mkws.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;
+
+ redraw_navi();
resetPage();
loadSelect();
triggerSearch();
return false;
}
+
+function redraw_navi ()
+{
+ var navi = document.getElementById('mkwsNavi');
+ if (!navi) return;
+
+ var text = "";
+ for (var i in m_filters) {
+ if (text) {
+ text += " | ";
+ }
+ var filter = m_filters[i];
+ if (filter.id) {
+ text += 'Source:
' + filter.name + ' ';
+ } else {
+ text += filter.field + ':
' + filter.value + ' ';
+ }
+ }
+
+ navi.innerHTML = text;
+}
+
+
function drawPager (pagerDiv)
{
//client indexes pages from 1 but pz2 from 0
@@ -554,55 +779,106 @@ function replaceHtml(el, html) {
function renderDetails(data, marker)
{
- var details = '
';
- if (marker) details += ''+ marker + ' ';
-
- var locations = [];
- for (var i in data.location) {
- locations.push(data.location[i]['@name']);
+ if (mkws.templateRecord === undefined) {
+ loadTemplate("Record");
}
- details += renderField("Title", data["md-title"], data["md-title-remainder"], data["md-title-responsibility"]);
- details += renderField("Date", data["md-date"]);
- details += renderField("Author", data["md-author"]);
- details += renderField("URL", data["md-electronic-url"]);
- details += renderField("Subject", data["location"][0]["md-subject"]);
- if (locations.length == 0) {
- details += 'No locations for record! ';
- } else {
- details += renderField("Location" + (locations.length == 1 ? "" : "s"), locations);
- }
- details += '
';
-
- return details;
+ var template = mkws.templateRecord;
+ var details = template(data);
+ return '
' + details + '
';
}
-function renderField(caption, data, data2, data3) {
- if (data === undefined) {
- return "";
- }
-
- var res = "";
- for (var i = 0; i < data.length; i++) {
- var s = data[i];
- if (i > 0)
- res += ", ";
-
- if (caption == "URL")
- s = '
' + s + ' ';
- res += s
+function loadTemplate(name)
+{
+ var source = $("#mkwsTemplate" + name).html();
+ if (!source) {
+ source = defaultTemplate(name);
}
- if (data2 != undefined) {
- res = res + " (" + data2 + ")";
- }
+ var template = Handlebars.compile(source);
+ debug("compiled template '" + name + "'");
+ mkws['template' + name] = template;
+}
- if (data3 != undefined) {
- res = res + "
" + data3 + " ";
+
+function defaultTemplate(name)
+{
+ if (name === 'Record') {
+ return '\
+
\
+ \
+ {{translate "Title"}} \
+ \
+ {{md-title}}\
+ {{#if md-title-remainder}}\
+ ({{md-title-remainder}})\
+ {{/if}}\
+ {{#if md-title-responsibility}}\
+ {{md-title-responsibility}} \
+ {{/if}}\
+ \
+ \
+ {{#if md-date}}\
+ \
+ {{translate "Date"}} \
+ {{md-date}} \
+ \
+ {{/if}}\
+ {{#if md-author}}\
+ \
+ {{translate "Author"}} \
+ {{md-author}} \
+ \
+ {{/if}}\
+ {{#if md-electronic-url}}\
+ \
+ {{translate "URL"}} \
+ \
+ {{#each md-electronic-url}}\
+ {{this}} \
+ {{/each}}\
+ \
+ \
+ {{/if}}\
+ {{#if-any location having="md-subject"}}\
+ \
+ {{translate "Subject"}} \
+ \
+ {{#first location having="md-subject"}}\
+ {{#if md-subject}}\
+ {{md-subject}}\
+ {{/if}}\
+ {{/first}}\
+ \
+ \
+ {{/if-any}}\
+ \
+ {{translate "Locations"}} \
+ \
+ {{#commaList location}}\
+ {{attr "@name"}}{{/commaList}}\
+ \
+ \
+
\
+';
+ } else if (name === "Summary") {
+ return '\
+
\
+ {{md-title}} \
+ \
+ {{#if md-title-remainder}}\
+
{{md-title-remainder}} \
+ {{/if}}\
+ {{#if md-title-responsibility}}\
+
{{md-title-responsibility}} \
+ {{/if}}\
+';
}
- return '
' + M(caption) + ' ' + res + ' ';
+ var s = "There is no default '" + name +"' template!";
+ alert(s);
+ return s;
}
@@ -671,8 +947,14 @@ function mkws_html_all() {
mkws_html_switch();
- if (mkws_config.use_service_proxy)
- mkws_service_proxy_auth(mkws_config.service_proxy_auth);
+ if (mkws_config.use_service_proxy) {
+ mkws_service_proxy_auth(mkws_config.service_proxy_auth,
+ mkws_config.service_proxy_auth_domain,
+ mkws_config.pazpar2_url);
+ } else {
+ // raw pp2
+ run_auto_searches();
+ }
if (mkws_config.responsive_design_width) {
// Responsive web design - change layout on the fly based on
@@ -695,8 +977,48 @@ function mkws_html_all() {
}
}
+
+function run_auto_searches() {
+ debug("running auto searches");
+
+ $('[id^="mkwsRecords"]').each(function () {
+ var node = $(this);
+ var query = node.attr('autosearch');
+
+ if (query) {
+ var windowid = undefined;
+ var id = node.attr('id');
+ if (id.match(/^mkwsRecords_/, '')) {
+ windowid = id.replace(/^mkwsRecords_/, '');
+ }
+
+ var sort = node.attr('sort');
+ var targets = node.attr('targets');
+ var s = "running auto search: '" + query + "'";
+ if (windowid) s += " [windowid '" + windowid + "']";
+ if (sort) s += " sorted by '" + sort + "'";
+ if (targets) s += " in targets '" + targets + "'";
+ debug(s);
+ newSearch(query, sort, targets, windowid);
+ }
+ });
+}
+
+
+// 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;
+}
+
function mkws_set_lang() {
- var lang = $.parseQuerystring().lang || mkws_config.lang;
+ var lang = parseQuerystring().lang || mkws_config.lang;
if (!lang || !mkws.locale_lang[lang]) {
mkws_config.lang = ""
} else {
@@ -723,7 +1045,7 @@ function mkws_html_switch() {
}
function mkws_html_sort() {
- debug("HTML sort");
+ debug("HTML sort, m_sort = '" + m_sort + "'");
var sort_html = '
';
for(var i = 0; i < mkws_config.sort_options.length; i++) {
@@ -732,7 +1054,7 @@ function mkws_html_sort() {
var val = opt.length == 1 ? opt[0] : opt[1];
sort_html += '' + M(val) + ' ';
@@ -765,13 +1087,18 @@ function mkws_html_perpage() {
* The username/password is configured in the apache config file
* for the site.
*/
-function mkws_service_proxy_auth(auth_url) {
+function mkws_service_proxy_auth(auth_url, auth_domain, pp2_url) {
debug("Run service proxy auth URL: " + auth_url);
+ if (!auth_domain) {
+ auth_domain = pp2_url.replace(/^(https?:)?\/\/(.*?)\/.*/, '$2');
+ debug("guessed auth_domain '" + auth_domain + "' from pp2_url '" + pp2_url + "'");
+ }
+
var request = new pzHttpRequest(auth_url, function(err) {
- alert("HTTP call for authentication failed: " + err)
- return;
- });
+ alert("HTTP call for authentication failed: " + err)
+ return;
+ }, auth_domain);
request.get(null, function(data) {
if (!$.isXMLDoc(data)) {
@@ -785,7 +1112,8 @@ function mkws_service_proxy_auth(auth_url) {
}
debug("Service proxy auth successfully done");
- mkws.service_proxy_auth = true;
+ mkws.authenticated = true;
+ run_auto_searches();
});
}
@@ -868,25 +1196,79 @@ function M(word) {
return mkws.locale_lang[lang][word] || word;
}
+// main
+(function() {
+ try {
+ mkws_html_all()
+ }
+
+ catch (e) {
+ mkws_config.error = e.message;
+ // alert(e.message);
+ }
+})();
+
+ // done
+ mkws.init = true;
+};
+
+
/*
- * implement jQuery plugins
+ * implement jQuery plugin $.pazpar2({})
*/
-$.extend({
- // implement $.parseQuerystring() for parsing URL parameters
- parseQuerystring: function() {
- 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];
+function _mkws_jquery_plugin ($) {
+ var debug_level = 1;
+
+ function debug (string) {
+ if (!debug_level)
+ return;
+
+ if (typeof console === "undefined" || typeof console.log === "undefined")
+ return;
+
+ console.log("jquery.pazpar2: " + string);
+ }
+
+ function init_popup(obj) {
+ var config = obj ? obj : {};
+
+ var height = config.height || 760;
+ var width = config.width || 880;
+ var id_button = config.id_button || "input#mkwsButton";
+ var id_popup = config.id_popup || "#mkwsPopup";
+
+ debug("popup height: " + height + ", width: " + width);
+
+ // make sure that jquery-ui was loaded afte jQuery core lib, e.g.:
+ //
+ if (!$.ui) {
+ debug("Error: jquery-ui.js is missing, did you include it after jQuery core in the HTML file?");
+ return;
+ }
+
+ $(id_popup).dialog({
+ closeOnEscape: true,
+ autoOpen: false,
+ height: height,
+ width: width,
+ modal: true,
+ resizable: true,
+ buttons: {
+ Cancel: function() {
+ $(this).dialog("close");
+ }
+ },
+ close: function() { }
});
- return nvpair;
- },
- debug2: function(string) { // delayed debug, internal variables are set after dom ready
- setTimeout(function() { debug(string); }, 500);
- },
+ $(id_button)
+ .button()
+ .click(function() {
+ $(id_popup).dialog("open");
+ });
+ };
+
+ $.extend({
// service-proxy or pazpar2
pazpar2: function(config) {
@@ -958,71 +1340,27 @@ $.extend({
'
if (config && config.layout == 'div') {
- this.debug2("jquery plugin layout: div");
+ debug("jquery plugin layout: div");
document.write(div);
} else if (config && config.layout == 'popup') {
- this.debug2("jquery plugin layout: popup with id: " + id_popup);
+ debug("jquery plugin layout: popup with id: " + id_popup);
document.write(popup);
$(document).ready( function() { init_popup(config); } );
} else {
- this.debug2("jquery plugin layout: table");
+ debug("jquery plugin layout: table");
document.write(table);
}
}
});
-
-function init_popup(obj) {
- var config = obj ? obj : {};
-
- var height = config.height || 760;
- var width = config.width || 880;
- var id_button = config.id_button || "input#mkwsButton";
- var id_popup = config.id_popup || "#mkwsPopup";
-
- debug("popup height: " + height + ", width: " + width);
-
- // make sure that jquery-ui was loaded afte jQuery core lib, e.g.:
- //
- if (!$.ui) {
- debug("Error: jquery-ui.js is missing, did you included it after jquery core in the HTML file?");
- return;
- }
-
- $(id_popup).dialog({
- closeOnEscape: true,
- autoOpen: false,
- height: height,
- width: width,
- modal: true,
- resizable: true,
- buttons: {
- Cancel: function() {
- $(this).dialog("close");
- }
- },
- close: function() { }
- });
-
- $(id_button)
- .button()
- .click(function() {
- $(id_popup).dialog("open");
- });
};
+// wrapper to call _make_mkws_team() after page load
+(function (j) {
+ // enable before page load, so we could call it before mkws() runs
+ _mkws_jquery_plugin(j);
-
-
-/* magic */
-$(document).ready(function() {
- try {
- mkws_html_all()
- }
-
- catch (e) {
- mkws_config.error = e.message;
- // alert(e.message);
- }
-});
-
+ $(document).ready(function() {
+ // if (console && console.log) console.log("on load ready");
+ _make_mkws_team(j, null);
+ });
})(jQuery);