// Set up namespace and some state.
var mkdru = {
// Settings to pass to pz2.js
- usesessions: true,
- showResponseType: '',
+ useSessions: Drupal.settings.mkdru.use_sessions === '1',
// Variables
- curPage: 1,
- recPerPage: 20,
- totalRec: 0,
- curSort: 'relevance',
- curFilter: null,
- submitted: false,
- sourceMax: 16, // facets
- subjectMax: 10,
- authorMax: 10,
+ active: false,
pz2: null,
- pazpar2Path: '/pazpar2/search.pz2',
+ totalRec: 0,
+ pagerRange: 6,
+ pazpar2Path: Drupal.settings.mkdru.pz2_path,
+ facetContainerSelector: Drupal.settings.mkdru.facetContainerSelector,
+ // Facets
+ facets: {
+ source: {
+ displayName: Drupal.settings.mkdru.source_dname || "Source",
+ orderWeight: Drupal.settings.mkdru.source_weight || 1,
+ pz2Name: 'xtargets',
+ max: Drupal.settings.mkdru.source_max || 10
+ },
+ subject: {
+ displayName: Drupal.settings.mkdru.subject_dname || "Subject",
+ orderWeight: Drupal.settings.mkdru.source_weight || 2,
+ pz2Name: 'subject',
+ max: Drupal.settings.mkdru.subject_max || 10,
+ multiLimit: true,
+ limiter: 'su'
+ },
+ author: {
+ displayName: Drupal.settings.mkdru.author_dname || "Author",
+ orderWeight: Drupal.settings.mkdru.source_weight || 3,
+ pz2Name: 'author',
+ max: Drupal.settings.mkdru.author_max || 10,
+ multiLimit: false,
+ limiter: 'au'
+ }
+ },
+ // State; Keys limit_{facet key} will also be initialised
+ defaultState: {
+ page: 1,
+ perpage: 20,
+ sort: 'relevance',
+ query:'',
+ recid:null
+ },
+ state: {},
+ realm: ''
};
+// Wrapper for jQuery
+(function ($) {
+
+// So we can use jQuery BBQ with Drupal 6 and its 1.2.6 jQuery
+if (!$.isArray) $.isArray = function(obj) {
+ return Object.prototype.toString.call(obj) === "[object Array]";
+};
+
+// BBQ has no handy way to remove params without changing the hash.
+// This takes an object to add and an array of keys to delete.
+mkdru.hashAddDelMany = function (add, del) {
+ var newHash = $.deparam.fragment();
+ if (typeof(add) === 'object')
+ $.extend(newHash, add);
+ if ($.isArray(del))
+ for (var i=0; i < del.length; i++)
+ if (newHash[del[i]] !== 'undefined')
+ delete newHash[del[i]];
+ return $.param.fragment("#", newHash);
+}
+
+// It's sometimes cumbersome that object literals can't take variable keys.
+mkdru.hashAddDelOne = function (key, value, del) {
+ var toAdd;
+ var toDel;
+ if (key && value) {
+ var toAdd = {};
+ toAdd[key] = value;
+ }
+ if (del) {
+ var toDel = [];
+ toDel.push(del);
+ }
+ return mkdru.hashAddDelMany(toAdd, toDel);
+}
+
+
+
// pz2.js event handlers:
mkdru.pz2Init = function () {
+ if (mkdru.state.query) {
+ mkdru.search();
+ }
mkdru.pz2.stat();
- mkdru.pz2.bytarget();
+ //mkdru.pz2.bytarget();
};
mkdru.pz2Show = function (data) {
mkdru.totalRec = data.merged;
- $('#mkdru-pager').html(Drupal.theme('mkdruPager', data, mkdru.curPage,
- Math.ceil(mkdru.totalRec / mkdru.recPerPage)));
- $('.mkdru-next').bind('click', mkdru.nextPage);
- $('.mkdru-prev').bind('click', mkdru.prevPage);
-
+ $('.mkdru-pager').html(mkdru.generatePager());
+ $('.mkdru-counts').html(Drupal.theme('mkdruCounts', data.start + 1,
+ data.num, data.merged, data.total));
var html = "";
for (var i = 0; i < data.hits.length; i++) {
html += Drupal.theme('mkdruResult', data.hits[i],
- i + 1 + mkdru.recPerPage *
- (mkdru.curPage - 1));
+ i + 1 + mkdru.state.perpage * (mkdru.state.page - 1),
+ "#" + $.param.fragment($.param.fragment(
+ window.location.href, {recid: data.hits[i].recid})) + "\n"
+ );
+ }
+ $('.mkdru-result-list').html(html);
+ if (mkdru.state.recid) {
+ mkdru.pz2.record(mkdru.state.recid);
+ }
+ else {
+ $('.mkdru-results').show();
}
- $('#mkdru-results').html(html);
};
mkdru.pz2Status = function (data) {
+ $('.mkdru-status').html(Drupal.theme('mkdruStatus', data.activeclients, data.clients));
};
-mkdru.pz2Term = function (data) {
- var html = "";
- for (var i = 0; i < data.xtargets.length && i < mkdru.sourceMax; i++ ) {
- html += Drupal.theme('mkdruTerm', data.xtargets[i].name, data.xtargets[i].freq,
- 'mkdru-facet-link-source', data.xtargets[i].id);
+mkdru.contains = function (hash, key, value) {
+ if (hash[key]) {
+ if (hash[key].length) {
+ for (var i=0; i<hash[key].length; i++) {
+ if (hash[key][i] === value) {
+ return true;
+ }
+ }
+ } else {
+ return hash[key] === value;
}
- $('#mkdru-sources').html(html);
+ }
+ return false;
+}
- html = "";
- for (var i = 0; i < data.subject.length && i < mkdru.subjectMax; i++ ) {
- html += Drupal.theme('mkdruTerm', data.subject[i].name, data.subject[i].freq,
- 'mkdru-facet-link-subject', data.subject[i].id);
+mkdru.pz2Term = function (data) {
+ // if signaled, prepare container client-side
+ if (mkdru.facetContainerSelector) {
+ $(mkdru.facetContainerSelector).html(Drupal.theme('mkdruFacetContainer',
+ data, mkdru.facets));
+ }
+ // map all facets against selected, for simple rendering
+ var hash = $.deparam.fragment();
+ for (var key in hash) {
+ if (key.indexOf('limit') == 0) //always wrap in array
+ hash[key.substr(6)] = hash[key].split(';');
+ delete hash[key];
+ }
+ for (var facet in mkdru.facets) {
+ var terms = data[mkdru.facets[facet].pz2Name];
+ for (var i=0; i<terms.length; i++) {
+ var term = terms[i];
+ var value = facet == "source" ? term.id : term.name;
+ if (mkdru.contains(hash, facet, value)) { //enabled
+ term.hashQuery = mkdru.removeLimit(facet, value);
+ term.selected = true;
+ } else { //disabled
+ term.hashQuery = mkdru.addLimit(facet, value);
+ term.selected = false;
+ }
}
- $('#mkdru-subjects').html(html);
+ $('.mkdru-facet-' + facet).html(
+ Drupal.theme('mkdruFacet', terms, facet, mkdru.facets[facet].max,
+ hash[facet]));
+ }
+};
+
+mkdru.pz2ByTarget = function (data) {
+
+};
+
+mkdru.pz2Record = function (data) {
+ clearTimeout(mkdru.pz2.showTimer);
+ $('.mkdru-results').hide();
+ $('.mkdru-detail').html(Drupal.theme('mkdruDetail', data,
+ mkdru.hashAddDelOne(null, null, 'recid')));
+ $('.mkdru-detail').show();
+ clearTimeout(mkdru.pz2.recordTimer);
+};
- html = "";
- for (var i = 0; i < data.author.length && i < mkdru.authorMax; i++ ) {
- html += Drupal.theme('mkdruTerm', data.author[i].name, data.author[i].freq,
- 'mkdru-facet-link-author', data.author[i].id);
- }
- $('#mkdru-authors').html(html);
- $('.mkdru-facet-link-source').bind('click', function (e) {
- mkdru.limitTarget(this.getAttribute('target_id'), this.firstChild.nodeValue);
- return false;
- });
- $('.mkdru-facet-link-subject').bind('click', function (e) {
- mkdru.limitQuery('su', this.firstChild.nodeValue);
- return false;
- });
+// State and URL handling
- $('.mkdru-facet-link-author').bind('click', function (e) {
- mkdru.limitQuery('au', this.firstChild.nodeValue);
- return false;
- });
+// populate state from an object and fill in the blanks with defaults
+mkdru.stateFromObject = function (obj) {
+ mkdru.state = $.extend({}, mkdru.defaultState);
+ for (var key in mkdru.defaultState)
+ if (typeof(obj[key]) != "undefined")
+ mkdru.state[key] = obj[key];
};
-mkdru.pz2ByTarget = function (data) {
-
+// populate state from current window's hash string
+mkdru.stateFromHash = function () {
+ mkdru.stateFromObject($.deparam.fragment());
};
+// set current window's hash string from state
+mkdru.hashFromState = function () {
+ // only include non-default settings in the URL
+ var alteredState = {};
+ for (var key in mkdru.defaultState) {
+ if (mkdru.state[key] != mkdru.defaultState[key]) {
+ alteredState[key] = mkdru.state[key];
+ }
+ }
+ $.bbq.pushState(alteredState, 2);
+};
+// update mkdru_form theme's ui to match state
+mkdru.uiFromState = function () {
+ for (var key in mkdru.state) {
+ switch(key) {
+ case 'query':
+ $('.mkdru-search input:text').attr('value', mkdru.state[key]);
+ break;
+ case 'perpage':
+ $('.mkdru-perpage').attr('value', mkdru.state[key]);
+ break;
+ case 'sort':
+ $('.mkdru-sort').attr('value', mkdru.state[key]);
+ break;
+ }
+ }
+};
-// UI functions:
-mkdru.submitQuery = function () {
- mkdru.submitted = true;
-// mkdru.resetPage();
-// mkdru.pollDropDowns();
- mkdru.search();
- return false;
+mkdru.hashChange = function () {
+ // do we need to restart the search?
+ var searchTrigger = false;
+ // shallow copy of state so we can see what changed.
+ var oldState = $.extend({}, mkdru.state);
+ mkdru.stateFromHash();
+ // only have to compare values since all keys are initialised
+ for (key in mkdru.state) {
+ var changed = (mkdru.state[key] != oldState[key]);
+ if (key.substring(0,5) === 'limit' && changed)
+ searchTrigger = true;
+ if (key === 'page' && changed)
+ mkdru.pz2.showPage(mkdru.state.page-1);
+ if (key === 'query' && changed)
+ searchTrigger = true;
+ }
+ if (searchTrigger)
+ mkdru.search();
+ // request for record detail
+ if (mkdru.state.recid && (mkdru.state.recid != oldState.recid)) {
+ mkdru.pz2.record(mkdru.state.recid);
+ }
+ else {
+ $('.mkdru-detail').hide();
+ $('.mkdru-results').show();
+ }
};
-mkdru.search = function () {
- mkdru.pz2.search($('.mkdru-search input:text').attr('value'),
- mkdru.recPerPage, mkdru.curSort, mkdru.curFilter);
+// return link to limit facet
+mkdru.addLimit = function (facet, limit) {
+ var newHash = $.deparam.fragment();
+ delete newHash['page'];
+ if ((typeof(newHash['limit_' + facet]) === 'undefined')
+ || !mkdru.facets[facet].multiLimit) {
+ newHash['limit_' + facet] = limit;
+ }
+ else {
+ newHash['limit_' + facet] += ';' + limit;
+ }
+ return $.param.fragment("#", newHash);
};
-mkdru.pollDropDowns = function () {
- mkdru.recPerPage = $('#mkdru-perpage').value;
- mkdru.curSort = $('#mkdru-sort').value;
- if (!mkdru.submitted) return false;
- mkdru.resetPage();
- mkdru.pz2.show(0, mkdru.recPerPage, mkdru.curSort);
-};
-
-mkdru.limitQuery = function (field, value) {
- $('.mkdru-search input:text').attr('value', function () {
- return this.value += ' and ' + field + '="' + value + '"';
- });
- mkdru.submitQuery();
-};
-
-mkdru.limitTarget = function (id, name) {
- var navi = document.getElementById('mkdru-navi');
- navi.innerHTML =
- 'Source: <a class="crossout" href="#" onclick="delimitTarget();return false;">'
- + name + '</a>';
- navi.innerHTML += '<hr/>';
- mkdru.curFilter = 'pz:id=' + id;
- mkdru.resetPage();
+// return link to remove limit from facet
+mkdru.removeLimit = function (facet, limit) {
+ var newHash = $.deparam.fragment();
+ delete newHash['page'];
+ if (!newHash['limit_' + facet].indexOf(';')
+ || !mkdru.facets[facet].multiLimit) {
+ delete newHash['limit_' + facet];
+ }
+ else {
+ var limits = newHash['limit_' + facet].split(';');
+ for (var i = 0; i < limits.length; i++) {
+ if (limits[i] == limit) {
+ limits.splice(i, 1);
+ if (limits.length < 1)
+ delete newHash['limit_' + facet];
+ else
+ newHash['limit_' + facet] = limits.join(';');
+ break;
+ }
+ }
+ }
+ return $.param.fragment("#", newHash);
+};
+
+
+
+// form submit handler
+mkdru.submitQuery = function () {
+ // new query, back to defaults (shallow copy)
+ mkdru.state = $.extend({}, mkdru.defaultState);
+ mkdru.state.query = $('.mkdru-search input:text').attr('value');
mkdru.pollDropDowns();
+ mkdru.hashFromState();
mkdru.search();
+ mkdru.active = true;
return false;
};
-mkdru.resetPage = function () {
- mkdru.curPage = 1;
- mkdru.totalRec = 0;
+// criteria drop-down (perpage, sort) handler
+mkdru.submitCriteria = function () {
+ mkdru.pollDropDowns();
+ //search is not ON, do nothing
+ if (!mkdru.active) return false;
+ // pages mean different things now
+ mkdru.state.page = 1;
+ mkdru.hashFromState();
+ mkdru.pz2.show(0, mkdru.state.perpage, mkdru.state.sort);
+ return false;
+}
+
+mkdru.search = function () {
+ var filter = null;
+ var query = mkdru.state.query;
+
+ // facet limit implementation
+ for (var facet in mkdru.facets) {
+ // facet is limited
+ if (mkdru.state['limit_' + facet]) {
+ if (facet == "source") {
+ filter = 'pz:id=' + mkdru.state.limit_source;
+ }
+ else {
+ var limits = mkdru.state['limit_' + facet].split(';');
+ for (var i = 0; i < limits.length; i++) {
+ // ex. query + and au="{limit_author}"
+ query += ' and ' + mkdru.facets[facet]['limiter'] + '="'
+ + mkdru.state['limit_' + facet] + '"';
+ }
+ }
+ }
+ }
+
+ mkdru.pz2.search(query, mkdru.state.perpage, mkdru.state.sort, filter);
+ mkdru.active = true;
};
-mkdru.showPage = function (pageNum) {
- mkdru.curPage = pageNum;
- mkdru.pz2.showPage(pageNum-1);
+mkdru.pollDropDowns = function () {
+ mkdru.state.perpage = $('.mkdru-perpage').attr('value');
+ mkdru.state.sort = $('.mkdru-sort').attr('value');
};
-mkdru.nextPage = function () {
- if (mkdru.totalRec - mkdru.recPerPage * mkdru.curPage > 0) {
- mkdru.pz2.showNext();
- mkdru.curPage++;
+mkdru.generatePager = function () {
+ // cast page parameter to numeric so we can add to it
+ if (typeof mkdru.state.page == "string") {
+ mkdru.state.page = Number(mkdru.state.page);
}
-};
+ var total = Math.ceil(mkdru.totalRec / mkdru.state.perpage);
+ var first = (mkdru.state.page - mkdru.pagerRange > 0)
+ ? mkdru.state.page - mkdru.pagerRange : 1;
+ var last = first + 2 * mkdru.pagerRange < total
+ ? first + 2 * mkdru.pagerRange : total;
+ var prev = null;
+ var next = null;
+ var pages = [];
-mkdru.prevPage = function () {
- if (mkdru.pz2.showPrev() != false) {
- mkdru.curPage--;
+ if ((mkdru.state.page - 1) >= first) {
+ prev = "#" + $.param.fragment($.param.fragment(
+ window.location.href, {page: mkdru.state.page - 1}))
+ }
+ if ((mkdru.state.page + 1) <= total) {
+ next = "#" + $.param.fragment($.param.fragment(
+ window.location.href, {page: mkdru.state.page + 1}))
}
+
+ for (var i = first; i <= last; i++) {
+ pages.push("#" + $.param.fragment($.param.fragment(
+ window.location.href, {page: i})));
+ }
+
+ return Drupal.theme('mkdruPager', pages, first, mkdru.state.page,
+ total, prev, next);
};
+
// wait until the DOM is ready, bind events
// and instantiate pz2 library
$(document).ready(function () {
+ $(window).bind( 'hashchange', mkdru.hashChange);
$('.mkdru-search').bind('submit', mkdru.submitQuery);
$('.mkdru-search input:text').attr('value', '');
- $('#mkdru-perpage').bind('change', function () { mkdru.pollDropDowns() });
- $('#mkdru-sort').bind('change', function () { mkdru.pollDropDowns() });
+ $('.mkdru-perpage').bind('change', mkdru.submitCriteria);
+ $('.mkdru-sort').bind('change', mkdru.submitCriteria);
+
+ // generate termlist for pz2.js and populate facet limit state
+ var termlist = [];
+ for (var key in mkdru.facets) {
+ termlist.push(mkdru.facets[key].pz2Name);
+ mkdru.defaultState['limit_' + key] = null;
+ }
+
mkdru.pz2 = new pz2( { "onshow": mkdru.pz2Show,
"showtime": 500, //each timer (show, stat, term, bytarget) can be specified this way
- "pazpar2path": mkdru.pazpar2path,
+ "pazpar2path": mkdru.pazpar2Path,
"oninit": mkdru.pz2Init,
"onstat": mkdru.pz2Status,
"onterm": mkdru.pz2Term,
- "termlist": "xtargets,subject,author",
+ "termlist": termlist.join(','),
"onbytarget": mkdru.pz2ByTarget,
- "usesessions" : mkdru.usesessions,
+ "usesessions" : mkdru.useSessions,
"showResponseType": mkdru.showResponseType,
- "onrecord": mkdru.pz2Record } );
-});
\ No newline at end of file
+ "onrecord": mkdru.pz2Record,
+ "autoInit": false } );
+
+ // initialise state to hash string or defaults
+ mkdru.stateFromHash();
+ // and update UI to match
+ mkdru.uiFromState();
+
+ // ting thing
+ if (typeof(Drupal.settings.mkdru.query) !== "undefined") {
+ mkdru.state.query = Drupal.settings.mkdru.query
+ //force the hash to contain query
+ mkdru.hashFromState();
+ }
+
+ //not running against SP? init, otherwise authenticate
+ if (mkdru.useSessions) {
+ mkdru.pz2.init();
+ } else {
+ //runnin against SP
+ var user = Drupal.settings.mkdru.sp_user;
+ var pass = Drupal.settings.mkdru.sp_pass;
+ var params = {};
+ params['command'] = 'auth';
+ if (user && pass) {
+ params['action'] = 'login';
+ params['username'] = user;
+ params['password'] = pass;
+ } else {
+ params['action'] = 'ipauth';
+ }
+ var authReq = new pzHttpRequest(mkdru.pazpar2Path,
+ function (err) {
+ alert("Authentication against metasearch gateway failed: " +err);
+ }
+ );
+ authReq.get(params,
+ function (data) {
+ var s = data.getElementsByTagName('status');
+ if (s.length && Element_getTextContent(s[0]) == "OK") {
+ mkdru.realm = data.getElementsByTagName('realm');
+ mkdru.pz2Init();
+ } else {
+ alert("Malformed response when authenticating against the metasearch"
+ + " gateway");
+ }
+ }
+ );
+ }
+});
+})(jQuery);